From 55bf37feed1bf27d97f1670990b5f704a3ca71bb Mon Sep 17 00:00:00 2001 From: ms-temp <150784508+ms-temp@users.noreply.github.com> Date: Tue, 21 Nov 2023 17:23:18 +0000 Subject: [PATCH] minor changes --- src/parser.c | 21 +++++++++++---------- src/types.c | 3 ++- src/types.h | 34 +++++++++++++++++----------------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/parser.c b/src/parser.c index ee6db40..7d58ebc 100644 --- a/src/parser.c +++ b/src/parser.c @@ -247,12 +247,13 @@ Value *parseAssignmentExpression(Value *t) { prev(); Value *n = parseIdentifier(t); if (check(n)) return freeValue(t), freeValue(n); - Value *q = fmatch(EQ, "expected ="); - if (check(q)) return freeValue(t), freeValue(n), freeValue(q); + // Value *q = fmatch(EQ, "expected ="); + next(); // consume operator + // if (check(q)) return freeValue(t), freeValue(n), freeValue(q); Value *r = parseExpression(); - if (check(r)) return freeValue(t), freeValue(n), freeValue(q), freeValue(r); + if (check(r)) return freeValue(t), freeValue(n), freeValue(r); match(DELMT); - return toValue(initAssignmentExpression(n->value, r->value)); + return toValue(initAssignmentExpression(n->value, r->value, 0)); } Value *parseIdentifier(Value *t) { @@ -270,14 +271,14 @@ Value *parseStringLiteral() { Value *parseLiteral() { debug_log("lit", parser.code, peekNext()); if (match(STR)) return parseStringLiteral(); - if (!matchAny(6, TRUE, FALSE, DEC, HEX, OCTAL, BIN)) return NULL; - if (matchAny(1, WHILE)) { + if (matchAny(14, FOR, CLASS, FUNC, WHILE, IF, DO, ELSE, FROM, IMPORT, NEW, AWAIT, AS, ASYNC, RETURN)) { debug_log("---lit", parser.code, peekNext()); Value *v = malloc(sizeof(Value)); v->error = true; - v->value = error("Parse", parser.file, "unexpected identifier", peekNext()); + v->value = error("Parse", parser.file, "unexpected keyword", current()); return v; } + if (!matchAny(6, TRUE, FALSE, DEC, HEX, OCTAL, BIN)) return NULL; return toValue(initLiteral(getTokenContent(parser.code, peekNext()))); } Value *varDecl(Value *t) { @@ -288,7 +289,7 @@ Value *varDecl(Value *t) { Value *r = parseExpression(); if (check(r)) return freeValue(t), freeValue(n), freeValue(q), freeValue(r); match(DELMT); - return toValue(initAssignmentExpression(n->value, r->value)); + return toValue(initAssignmentExpression(n->value, r->value, ASGN_EQ)); } //* working as expected @@ -379,12 +380,12 @@ Value *parseExpression() { else if (nextIs(LPAREN)) return parseCallExpression(t); // else if (nextIs(DOT)) return chainExpr(); - else if (matchAny(1, EQ)) { + else if (matchAny(3, EQ, ADD_EQ, SUB_EQ)) { prev(); return parseAssignmentExpression(t); } else - return prev(), parseIdentifier(t); + return parseIdentifier(t); } else if (matchAny(4, DEC, HEX, OCTAL, BIN)) { Value *l = toValue(current()); if (matchAny(13, ADD, SUB, MUL, DIV, LAND, LOR, BAND, BOR, BXOR, REM, diff --git a/src/types.c b/src/types.c index 1c74934..e41bb72 100644 --- a/src/types.c +++ b/src/types.c @@ -60,11 +60,12 @@ ClassDeclaration *initClassDeclaration(Identifier *n, Array *b) { return decl; } -AssignmentExpression *initAssignmentExpression(Typed *l, Typed *r) { +AssignmentExpression *initAssignmentExpression(Typed *l, Typed *r, AssignmentOperator oper) { AssignmentExpression *asgn = malloc(sizeof(AssignmentExpression)); asgn->type = ASSIGNMENT_EXPRESSION; asgn->left = l; asgn->right = r; + asgn->oper = oper; return asgn; } diff --git a/src/types.h b/src/types.h index 14aa080..621889c 100644 --- a/src/types.h +++ b/src/types.h @@ -206,25 +206,25 @@ typedef struct ReturnStatement { Typed *expr; } ReturnStatement; -typedef enum BinaryAssignmentOperators { - BIN_ASGN_EQ = 1, - BIN_ASGN_ADD_EQ, - BIN_ASGN_SUB_EQ, - BIN_ASGN_MUL_EQ, - BIN_ASGN_DIV_EQ, - BIN_ASGN_LAND_EQ, - BIN_ASGN_LOR_EQ, - BIN_ASGN_BAND_EQ, - BIN_ASGN_BOR_EQ, - BIN_ASGN_BXOR_EQ, - BIN_ASGN_REM_EQ, - BIN_ASGN_RSHIFT_EQ, - BIN_ASGN_LSHIFT_EQ -} BinaryAssignmentOperator; +typedef enum AssignmentOperators { + ASGN_EQ = 1, + ASGN_ADD_EQ, + ASGN_SUB_EQ, + ASGN_MUL_EQ, + ASGN_DIV_EQ, + ASGN_LAND_EQ, + ASGN_LOR_EQ, + ASGN_BAND_EQ, + ASGN_BOR_EQ, + ASGN_BXOR_EQ, + ASGN_REM_EQ, + ASGN_RSHIFT_EQ, + ASGN_LSHIFT_EQ +} AssignmentOperator; typedef struct AssignmentExpression { AST_Type type; - BinaryAssignmentOperator oper; + AssignmentOperator oper; Typed *left; Typed *right; } AssignmentExpression; @@ -269,7 +269,7 @@ extern ImportSpecifier *initImportSpecifier( extern ImportDeclaration *initImportDeclaration( StringLiteral *, Array *import_specifier_array); extern AssignmentExpression *initAssignmentExpression( - Typed *left, Typed *right); + Typed *left, Typed *right, AssignmentOperator oper); extern Literal *initLiteral(char *value); extern StringLiteral *initStringLiteral(char *value); extern Identifier *initIdentifier(char *name);