Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-temp committed Nov 21, 2023
1 parent 3ac567d commit 55bf37f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
21 changes: 11 additions & 10 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
34 changes: 17 additions & 17 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 55bf37f

Please sign in to comment.