git » unicorn-sparkle-basic.git » commit 9afba23

AST construction starting with strings

author Alan Dipert
2024-01-05 05:56:23 UTC
committer Alan Dipert
2024-01-05 05:56:23 UTC
parent f74798c9ceed91ba766623b7897a338c3188c320

AST construction starting with strings

Makefile +5 -2
ast.c +8 -0
ast.h +25 -0
bbasic.l +3 -3
bbasic.y +15 -12

diff --git a/Makefile b/Makefile
index e0077a0..e29ad37 100644
--- a/Makefile
+++ b/Makefile
@@ -6,8 +6,11 @@ YFLAGS=-d
 
 all: bbasic
 
-bbasic: lex.yy.o bbasic.tab.o main.o
-	$(CC) $(CFLAGS) -o bbasic lex.yy.o bbasic.tab.o main.o -lfl
+bbasic: lex.yy.o bbasic.tab.o ast.o main.o
+	$(CC) $(CFLAGS) -o bbasic lex.yy.o bbasic.tab.o ast.o main.o -lfl
+
+ast.o: ast.c ast.h
+	$(CC) $(CFLAGS) -c ast.c
 
 main.o: main.c
 	$(CC) $(CFLAGS) -c main.c
diff --git a/ast.c b/ast.c
new file mode 100644
index 0000000..334237a
--- /dev/null
+++ b/ast.c
@@ -0,0 +1,8 @@
+#include "ast.h"
+
+struct type_node* ast_make_string(char *s) {
+  struct type_node *n = malloc(sizeof(struct type_node));
+  n->type = NODE_STRING;
+  n->val = s;
+  return n;
+}
diff --git a/ast.h b/ast.h
new file mode 100644
index 0000000..fd24d0f
--- /dev/null
+++ b/ast.h
@@ -0,0 +1,25 @@
+#include <stdlib.h>
+
+enum TYPE_NODE {
+  NODE_LINE,
+  NODE_PRINT,
+  NODE_IF,
+  NODE_ADD,
+  NODE_SUBTRACT,
+  NODE_MULTIPLY,
+  NODE_DIVIDE,
+  NODE_NUMBER_INTEGER,
+  NODE_STRING,
+  NODE_ID
+};
+
+struct type_node {
+  enum TYPE_NODE type;
+  char *name;
+  void *val;
+  /* Applicable operands */
+  void *node0;
+  void *node1;
+};
+
+struct type_node* ast_make_string(char *s);
diff --git a/bbasic.l b/bbasic.l
index 8cfca66..3423c5f 100644
--- a/bbasic.l
+++ b/bbasic.l
@@ -7,14 +7,14 @@
 %%
 
 [ \t]+              ; /* Ignore whitespaces */
-\n                  return EOL;
+\n                  { return 0; }
 "print"             return PRINT;
 "if"                return IF;
 "then"              return THEN;
 "goto"              return GOTO;
 "="                 return EQUALS;
-[0-9]+              { yylval.num = atoi(yytext); return NUMBER; }
-[a-zA-Z][a-zA-Z0-9]* { yylval.str = strdup(yytext); return IDENTIFIER; }
+[0-9]+              { yylval.num_int = atoi(yytext); return NUMBER_INTEGER; }
+[a-zA-Z][a-zA-Z0-9]* { yylval.id = strdup(yytext); return IDENTIFIER; }
 \"([^\\\"]|\\.)*\"   { yylval.str = strdup(yytext); return STRING; }
 .                   return *yytext;
 
diff --git a/bbasic.y b/bbasic.y
index 07e8e48..7c104a8 100644
--- a/bbasic.y
+++ b/bbasic.y
@@ -1,6 +1,7 @@
 %{
 #include <stdio.h>
 #include <stdlib.h>
+#include "ast.h"
 extern int yylex();
 extern int yyparse();
 extern FILE *yyin;
@@ -11,14 +12,18 @@ void yyerror(const char *s) {
 %}
 
 %union {
-    int num;
-    char *str;
+  int num_int;
+  char *id;
+  char *str;
+  struct type_node *node;
 }
 
-%token <num> NUMBER
-%token <str> IDENTIFIER
+%token <num_int> NUMBER_INTEGER
+%token <id> IDENTIFIER
 %token <str> STRING
-%token EOL PRINT IF THEN GOTO EQUALS
+%token PRINT IF THEN GOTO EQUALS
+
+%type <node> expression
 
 %define parse.error verbose
 
@@ -26,12 +31,8 @@ void yyerror(const char *s) {
 %left '*' '/'
 
 %%
-program:
-    | program line
-    ;
-
 line:
-    NUMBER statement EOL
+    NUMBER_INTEGER statement
     ;
 
 statement:
@@ -41,8 +42,10 @@ statement:
 
 expression:
     IDENTIFIER
-    | STRING
-    | NUMBER
+    | STRING {
+      $$ = ast_make_string($1);
+    }
+    | NUMBER_INTEGER
     | expression '+' expression
     | expression '-' expression
     | expression '*' expression