git » unicorn-sparkle-basic.git » commit 1a4f4e2

Numbers an linums are now both double

author Alan Dipert
2024-01-20 05:04:16 UTC
committer Alan Dipert
2024-01-20 05:04:16 UTC
parent 39f29b265e736c37827f4222bc573d85ca7f1633

Numbers an linums are now both double

main.c +1 -1
parse.c +3 -3
parse.h +6 -6
usbasic.l +1 -2
usbasic.y +5 -5

diff --git a/main.c b/main.c
index b90c9c1..a534220 100644
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@ void read_loop() {
   while (1) {
     if ((line = read_line_stdin("🦄 "))) {
       printf("It parsed 👍\n");
-      printf("linum: %d\n", line->data.line.linum);
+      printf("linum: %g\n", line->data.line.linum);
     }
   }
 }
diff --git a/parse.c b/parse.c
index 4ceb2bf..ce54cc6 100644
--- a/parse.c
+++ b/parse.c
@@ -19,7 +19,7 @@ struct node_tag *ast_make_string(char *s) {
   return new_node;
 }
 
-struct node_tag *ast_make_numbered_line(int line, struct node_tag *stmt) {
+struct node_tag *ast_make_numbered_line(double line, struct node_tag *stmt) {
   struct node_tag *new_node = ast_alloc(NODE_NUMBERED_LINE);
   new_node->data.line.linum = line;
   new_node->data.line.stmt = stmt;
@@ -46,9 +46,9 @@ struct node_tag *ast_make_id(char *name) {
   return new_node;
 }
 
-struct node_tag *ast_make_number_integer(int val) {
+struct node_tag *ast_make_number(double val) {
   struct node_tag *new_node = ast_alloc(NODE_NUMBER_INTEGER);
-  new_node->data.number_integer.val = val;
+  new_node->data.number.val = val;
   return new_node;
 }
 
diff --git a/parse.h b/parse.h
index f9b06bc..6813b6f 100644
--- a/parse.h
+++ b/parse.h
@@ -23,7 +23,7 @@ enum NODE_TYPE {
 };
 
 struct node_line_data {
-  int linum;
+  double linum;
   struct node_tag *stmt;
 };
 
@@ -44,8 +44,8 @@ struct node_id_data {
   char *name;
 };
 
-struct node_number_integer_data {
-  int val;
+struct node_number_data {
+  double val;
 };
 
 struct node_tag {
@@ -56,16 +56,16 @@ struct node_tag {
     struct node_print_data print;
     struct node_iff_data iff;
     struct node_id_data id;
-    struct node_number_integer_data number_integer;
+    struct node_number_data number;
   } data;
 };
 
 struct node_tag *ast_make_string(char *s);
-struct node_tag *ast_make_numbered_line(int linum, struct node_tag *stmt);
+struct node_tag *ast_make_numbered_line(double linum, struct node_tag *stmt);
 struct node_tag *ast_make_print(struct node_tag *expr);
 struct node_tag *ast_make_if(struct node_tag *pred, struct node_tag *stmt);
 struct node_tag *ast_make_id(char *name);
-struct node_tag *ast_make_number_integer(int val);
+struct node_tag *ast_make_number(double val);
 
 extern int yyparse();
 extern void yy_scan_string(char *);
diff --git a/usbasic.l b/usbasic.l
index 02d66ea..007807f 100644
--- a/usbasic.l
+++ b/usbasic.l
@@ -17,9 +17,8 @@
 ">"                 return GT;
 ">="                return GTE;
 "=="                return EQ;
-[0-9]+              { yylval.num_int = atoi(yytext); return NUMBER_INTEGER; }
+[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?  { yylval.number = atof(yytext); return NUMBER; }
 [a-zA-Z][a-zA-Z0-9]* { yylval.id = strdup(yytext); return IDENTIFIER; }
 \"([^\\\"]|\\.)*\"   { yylval.str = strdup(yytext); return STRING; }
 .                   return *yytext;
-
 %%
diff --git a/usbasic.y b/usbasic.y
index 38d8a2f..75d973e 100644
--- a/usbasic.y
+++ b/usbasic.y
@@ -12,13 +12,13 @@ void yyerror(const char *s) {
 %}
 
 %union {
-  int num_int;
+  double number;
   char *id;
   char *str;
   struct node_tag *node;
 }
 
-%token <num_int> NUMBER_INTEGER
+%token <number> NUMBER
 %token <id> IDENTIFIER
 %token <str> STRING
 %token PRINT IF THEN GOTO LT LTE GT GTE EQ
@@ -34,7 +34,7 @@ void yyerror(const char *s) {
 
 %%
 line:
-    NUMBER_INTEGER statement {
+    NUMBER statement {
       $$ = ast_make_numbered_line($1, $2);
     }
     ;
@@ -55,8 +55,8 @@ expression:
     | STRING {
       $$ = ast_make_string($1);
     }
-    | NUMBER_INTEGER {
-      $$ = ast_make_number_integer($1);
+    | NUMBER {
+      $$ = ast_make_number($1);
     }
     /* | expression '+' expression */
     /* | expression '-' expression */