From: Thomas Bruce <tdb@tdbio.me>
Date: Wed, 19 Feb 2025 22:05:42 +0000 (-0500)
Subject: fix parse assignment
X-Git-Url: https://git.deglebe.com/p/w/static/non-work/r/index.html?a=commitdiff_plain;h=4d9368f569fda585817b1393257c99f32629e17d;p=barec%2Fbarec.git

fix parse assignment
---

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..74e79bb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+next-to-add.c
diff --git a/parser.c b/parser.c
index 1446792..a02250e 100644
--- a/parser.c
+++ b/parser.c
@@ -99,6 +99,7 @@ static ASTNode* parseAdditive(void);
 static ASTNode* parseTerm(void);
 static ASTNode* parseFactor(void);
 static ASTNode* parseDeclaration(void);
+static ASTNode* parseAssignment(void);
 static ASTNode* parseStatement(void);
 static ASTNode* parseCompoundStatement(void);
 static ASTNode* parseFunctionDefinition(void);
@@ -208,7 +209,7 @@ static ASTNode* parseStatement(void) {
 
 /* expression := equality */
 static ASTNode* parseExpression(void) {
-	return parseEquality();
+	return parseAssignment();
 }
 
 /* equality := additive ( "==" additive )* */
@@ -302,6 +303,20 @@ static ASTNode* parseDeclaration(void) {
 	return decl;
 }
 
+static ASTNode* parseAssignment(void) {
+	ASTNode * node = parseEquality();
+	if (g_currentToken.kind == TK_ASSIGN) {
+		if (node->kind != AST_IDENT) {
+			fprintf(stderr, "Parse err: left side is not ident.");
+			exit(1);
+		}
+		nextToken();
+		ASTNode* rhs = parseAssignment();
+		node = newBinaryNode(node, rhs, "=");
+	}
+	return node;
+}
+
 
 /* demonstration: ast printing*/
 static void printIndent(int indent) {