From 4d9368f569fda585817b1393257c99f32629e17d Mon Sep 17 00:00:00 2001
From: Thomas Bruce <tdb@tdbio.me>
Date: Wed, 19 Feb 2025 17:05:42 -0500
Subject: [PATCH] fix parse assignment

---
 .gitignore |  1 +
 parser.c   | 17 ++++++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 .gitignore

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) {
-- 
2.39.5