]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added scaffolding for constant expression evaluation and code generation
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 19 Apr 2021 20:31:32 +0000 (16:31 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 19 Apr 2021 20:31:32 +0000 (16:31 -0400)
cerise/cerise.h
cerise/examples/const.c [new file with mode: 0644]
cerise/examples/generate.sh [new file with mode: 0755]
cerise/parser.c

index e085defbe1c579227df41feaafc3df0893d0c437..625b5c0b3e2b3cd7422b87c2633d4e90f0bf26e5 100644 (file)
@@ -83,10 +83,31 @@ typedef struct LexFile {
     char* fpos;
 } LexFile;
 
+typedef struct {
+    enum {
+        VAL_I8, VAL_I16, VAL_I32, VAL_I64, VAL_REAL, VAL_STRING
+    } type;
+    union {
+        long long integer;
+        double floating;
+        char* text;
+    } value;
+} Value;
+
+typedef struct Symbol {
+    struct Symbol* next;
+    char* name;
+    enum{
+        SYM_CONST, SYM_VAR, SYM_TYPE, SYM_PROC
+    } type;
+} Symbol;
+
 typedef struct {
     LexFile* done;
     LexFile* file;
     Tok tok;
+    int valstack_idx;
+    Value valstack[1024];
 } Parser;
 
 void lexfile(Parser* ctx, char* path);
diff --git a/cerise/examples/const.c b/cerise/examples/const.c
new file mode 100644 (file)
index 0000000..cedf8ba
--- /dev/null
@@ -0,0 +1,17 @@
+#include <stdint.h>
+
+const int8_t A1 = 42;
+const int16_t A2 = 42;
+const int32_t A3 = 42;
+const int64_t A4 = 42;
+
+const uint8_t B1 = 42;
+const uint16_t B2 = 42;
+const uint32_t B3 = 42;
+const uint64_t B4 = 42;
+
+const float C1 = 42.0;
+const double C2 = 42.0;
+
+const char* D1 = "this is a string";
+const char D2[] = "this is another string";
\ No newline at end of file
diff --git a/cerise/examples/generate.sh b/cerise/examples/generate.sh
new file mode 100755 (executable)
index 0000000..bd87e1e
--- /dev/null
@@ -0,0 +1,2 @@
+#!/bin/sh
+cc -c -S -o - "$@" | sed -e 's/^\s*\.globl.*$/\n\n&/'
index 4b0816bd8b1f26be124a700af1a2c9bd0d3532b8..7e38a253a0e9aa3eecffa5bd55cbc44691f51bb5 100644 (file)
@@ -127,7 +127,7 @@ static char* expect_text(Parser* p, TokType type)
     return strdup(expect_val(p, type)->text);
 }
 
-static int consume(Parser* p) 
+static int consume(Parser* p)
 {
     int type = peek(p)->type;
     if (!accept(p, type))
@@ -289,13 +289,13 @@ RULE(term)
 }
 
 /*
-factor = number 
-       | string 
-       | "nil" 
-       | "true" 
-       | "false" 
-       | designator [ActualParameters] 
-       | "(" expression ")" 
+factor = number
+       | string
+       | "nil"
+       | "true"
+       | "false"
+       | designator [ActualParameters]
+       | "(" expression ")"
        | "not" factor.
 */
 
@@ -476,7 +476,7 @@ TEST_SUITE(Grammar)
         parse_rule(const_decl,
             "FOO = -1");
         parse_rule(const_decl,
-            "FOO = 1 + 1");
+            "FOO = 1 + 2 + 3");
         parse_rule(const_decl,
             "FOO = 1 + 2 * 4");
     }