From 1db2fcce7b403790e978be31ec17810d6d013e3f Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 19 Apr 2021 16:31:32 -0400 Subject: [PATCH] added scaffolding for constant expression evaluation and code generation --- cerise/cerise.h | 21 +++++++++++++++++++++ cerise/examples/const.c | 17 +++++++++++++++++ cerise/examples/generate.sh | 2 ++ cerise/parser.c | 18 +++++++++--------- 4 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 cerise/examples/const.c create mode 100755 cerise/examples/generate.sh diff --git a/cerise/cerise.h b/cerise/cerise.h index e085def..625b5c0 100644 --- a/cerise/cerise.h +++ b/cerise/cerise.h @@ -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 index 0000000..cedf8ba --- /dev/null +++ b/cerise/examples/const.c @@ -0,0 +1,17 @@ +#include + +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 index 0000000..bd87e1e --- /dev/null +++ b/cerise/examples/generate.sh @@ -0,0 +1,2 @@ +#!/bin/sh +cc -c -S -o - "$@" | sed -e 's/^\s*\.globl.*$/\n\n&/' diff --git a/cerise/parser.c b/cerise/parser.c index 4b0816b..7e38a25 100644 --- a/cerise/parser.c +++ b/cerise/parser.c @@ -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"); } -- 2.49.0