git » unicorn-sparkle-basic.git » commit 48c92d4

Add types.{h,c}

author Alan Dipert
2024-04-19 23:59:20 UTC
committer Alan Dipert
2024-04-19 23:59:20 UTC
parent 06fc6270903f2035ac27a14b2d8deb8dae3d7ff0

Add types.{h,c}

Makefile +6 -3
ast.h +0 -0
parse.c +2 -1
parse.h +1 -1
types.c +27 -0
types.h +22 -0

diff --git a/Makefile b/Makefile
index 82b1ee3..f9c084b 100644
--- a/Makefile
+++ b/Makefile
@@ -8,8 +8,8 @@ YFLAGS=-d
 
 all: usbasic
 
-usbasic: lex.yy.o usbasic.tab.o parse.o vec.o lines.o main.o
-	$(CC) $(CFLAGS) -o usbasic lex.yy.o usbasic.tab.o parse.o vec.o lines.o main.o -lgc -lfl -lreadline -lhistory
+usbasic: lex.yy.o usbasic.tab.o types.o parse.o vec.o lines.o main.o
+	$(CC) $(CFLAGS) -o usbasic lex.yy.o usbasic.tab.o types.o parse.o vec.o lines.o main.o -lgc -lfl -lreadline -lhistory
 
 lines.o: lines.c lines.h vec.h parse.h
 	$(CC) $(CFLAGS) -c lines.c
@@ -17,7 +17,10 @@ lines.o: lines.c lines.h vec.h parse.h
 vec.o: vec.c vec.h
 	$(CC) $(CFLAGS) -c vec.c
 
-parse.o: parse.c usbasic.tab.h parse.h
+types.o: types.c types.h
+	$(CC) $(CFLAGS) -c types.c
+
+parse.o: parse.c usbasic.tab.h parse.h types.h types.c
 	$(CC) $(CFLAGS) -c parse.c
 
 main.o: main.c parse.h vec.h
diff --git a/ast.h b/ast.h
new file mode 100644
index 0000000..e69de29
diff --git a/parse.c b/parse.c
index 676b2ed..3afff7b 100644
--- a/parse.c
+++ b/parse.c
@@ -5,6 +5,7 @@
  * text.
  */
 
+#include "types.h"
 #include "parse.h"
 #include <gc.h>
 #include <stdio.h>
@@ -51,7 +52,7 @@ struct node_tag *ast_make_id(char *name) {
 }
 
 struct node_tag *ast_make_number(double val) {
-  struct node_tag *new_node = ast_alloc(NODE_NUMBER_INTEGER);
+  struct node_tag *new_node = ast_alloc(NODE_NUMBER);
   new_node->data.number.val = val;
   return new_node;
 }
diff --git a/parse.h b/parse.h
index a760095..8e63377 100644
--- a/parse.h
+++ b/parse.h
@@ -24,7 +24,7 @@ enum NODE_TYPE {
   NODE_GT,
   NODE_GTE,
   NODE_EQ,
-  NODE_NUMBER_INTEGER,
+  NODE_NUMBER,
   NODE_STRING,
   NODE_ID
 };
diff --git a/types.c b/types.c
new file mode 100644
index 0000000..7df8a07
--- /dev/null
+++ b/types.c
@@ -0,0 +1,27 @@
+/*
+ * This file is part of Unicorn Sparkle Basic and is released under
+ * CC0 1.0 Universal License.  See LICENSE.txt file or
+ * https://creativecommons.org/publicdomain/zero/1.0/ for full license
+ * text.
+ */
+
+#include "types.h"
+#include <gc.h>
+
+static struct value *type_alloc(enum TYPE value_type) {
+  struct value *new_value = GC_malloc(sizeof(struct value));
+  new_value->type = value_type;
+  return new_value;
+}
+
+struct value *type_make_string(char *s) {
+  struct value *new_value = type_alloc(TYPE_STRING);
+  new_value->data.string = s;
+  return new_value;
+}
+
+struct value *type_make_number(double d) {
+  struct value *new_value = type_alloc(TYPE_NUMBER);
+  new_value->data.number = d;
+  return new_value;  
+}
diff --git a/types.h b/types.h
new file mode 100644
index 0000000..2bf47e5
--- /dev/null
+++ b/types.h
@@ -0,0 +1,22 @@
+/*
+ * This file is part of Unicorn Sparkle Basic and is released under
+ * CC0 1.0 Universal License.  See LICENSE.txt file or
+ * https://creativecommons.org/publicdomain/zero/1.0/ for full license
+ * text.
+ */
+
+enum TYPE {
+  TYPE_NUMBER,
+  TYPE_STRING
+};
+
+struct value {
+  enum TYPE type;
+  union {
+    double number;
+    char *string;
+  } data;
+};
+
+struct value *type_make_string(char *s);
+struct value *type_make_number(double d);