Skip to content

Commit d614f9a

Browse files
committed
fix: correctly processing tertary and unary operators
1 parent d8f36c2 commit d614f9a

File tree

7 files changed

+1195
-204
lines changed

7 files changed

+1195
-204
lines changed

src/molang/syntax/tokens.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,38 @@ function checkUnaryOperators(item: Token, index: number, items: Token[]) {
237237
item.type = TokenType.UnaryOperator;
238238
return;
239239
}
240-
if (item.type !== TokenType.Operator && item.value !== "-") return;
240+
if (item.type !== TokenType.Operator) return;
241+
if (item.value !== "-" && item.value !== "+") return;
241242

242243
if (index === 0) {
243244
item.type = TokenType.UnaryOperator;
244245
return;
245246
}
246247
const previous = items[index - 1];
247248
switch (previous.type) {
249+
case TokenType.CloseBrace:
250+
case TokenType.CloseBracket:
251+
case TokenType.CloseParen:
252+
case TokenType.Identifier:
253+
case TokenType.NamespacedIdentifier:
254+
case TokenType.Number:
255+
case TokenType.StringLiteral:
256+
break;
257+
case TokenType.ArrayAccess:
258+
case TokenType.Arrow:
259+
case TokenType.Assignment:
260+
case TokenType.Colon:
248261
case TokenType.Comma:
249-
case TokenType.Operator:
262+
case TokenType.Dot:
263+
case TokenType.EOF:
264+
case TokenType.NullishCoalescing:
250265
case TokenType.OpenBrace:
251266
case TokenType.OpenBracket:
252267
case TokenType.OpenParen:
268+
case TokenType.Operator:
269+
case TokenType.QuestionMark:
270+
case TokenType.Semicolon:
271+
case TokenType.UnaryOperator:
253272
item.type = TokenType.UnaryOperator;
254273
break;
255274
default:

test/data/dataset-valid.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const valid_syntaxes = [
2323
"variable.foo1 = 0; variable.foo2 = 0; v.foo3 = 0;",
2424
"variable.foo3 = 0;",
2525
"( variable.use_item_interval_progress > 0.0 ) || ( variable.use_item_startup_progress > 0.0 )",
26+
"variable.eat_anim = math.clamp(variable.eat_anim + (query.is_grazing ? ((1.0 - variable.eat_anim) * 0.4 + 0.05) : -variable.eat_anim * 0.4 - 0.05) * query.delta_time * 20.0, 0.0, 1.0);",
2627
// Returns statement:
2728
"variable.is_blinking = 0; variable.return_from_blink = query.life_time + math.random(0, 0.2); return query.life_time > (variable.last_blink_time + math.random(3, 40));",
2829

0 commit comments

Comments
 (0)