aboutsummaryrefslogtreecommitdiff
path: root/demos/sample-files/calc-defs.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2025-07-08 18:25:29 +0300
committerkartofen <mladenovnasko0@gmail.com>2025-07-08 18:25:29 +0300
commitd028cc9c04cf46256166434bdea68d5f5c6d310f (patch)
tree28420104516e446a06172687eae16e3bf4dfd5bd /demos/sample-files/calc-defs.c
parent919611902c39fd70afe1162883ee6bfd34f2642e (diff)
simple calculator example
Diffstat (limited to 'demos/sample-files/calc-defs.c')
-rw-r--r--demos/sample-files/calc-defs.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/demos/sample-files/calc-defs.c b/demos/sample-files/calc-defs.c
new file mode 100644
index 0000000..de1f705
--- /dev/null
+++ b/demos/sample-files/calc-defs.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+
+#include "parts/symbol.h"
+enum symbol {
+ PLUS = 0,
+ MINUS,
+ LPAREN,
+ RPAREN,
+ NUM,
+ END_INPUT,
+
+ EP, E, T,
+ SYMBOLS_END,
+};
+
+size_t total_symbols = SYMBOLS_END;
+
+IMPLEMENT_FUNCPTR(int, symbol_is_terminal, (symbol s)) { return s < EP; }
+IMPLEMENT_FUNCPTR(int, symbol_is_input_end, (symbol s)) { return s == END_INPUT; }
+IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s)) { return s < SYMBOLS_END; }
+
+#include "parts/grammar.h"
+#define PROD(LHS, _, ...) {LHS, (symbol[]){__VA_ARGS__}, sizeof((symbol[]){__VA_ARGS__})/sizeof(symbol)}
+static struct production _grammar[] = {
+ PROD(EP, ->, E, END_INPUT),
+ PROD(E, -->, E, PLUS, T),
+ PROD(E, -->, E, MINUS, T),
+ PROD(E, -->, T),
+ PROD(T, -->, LPAREN, E, RPAREN),
+ PROD(T, -->, NUM),
+};
+
+struct production *grammar = _grammar;
+size_t total_productions = sizeof(_grammar)/sizeof(*_grammar);
+
+// #include "???.h"
+char **semantic_action_str = (char *([])){
+ "v = A(0);",
+ "v = A(0) + A(2);",
+ "v = A(0) - A(2);",
+ "v = A(0);",
+ "v = A(1);",
+ "v = A(0);",
+};