aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2024-09-15 18:04:48 +0200
committerHimbeer <himbeer@disroot.org>2024-09-15 18:18:53 +0200
commit4fe74c2533021b47a308fb44ad9ec1202f7b2da9 (patch)
tree7d7b1d3ca7a7e82ac72b187ea71a14a2fbd26047
parent765155fa86f03e7779ac450455fa46c954f41a3b (diff)
Add AST data structures for (sub)units and top-level declarations
-rw-r--r--Makefile2
-rw-r--r--include/lex.h16
-rw-r--r--include/parse.h86
-rw-r--r--src/parse.c1
4 files changed, 98 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index a7848d3..3fd32b8 100644
--- a/Makefile
+++ b/Makefile
@@ -3,12 +3,14 @@
BINOUT = .bin
HDR = \
include/lex.h \
+ include/parse.h \
include/type.h \
include/utf8.h \
include/util.h
OBJ = \
src/lex.o \
src/main.o \
+ src/parse.o \
src/utf8.o
CFLAGS = -Iinclude
diff --git a/include/lex.h b/include/lex.h
index 8050c4e..8b1fdf2 100644
--- a/include/lex.h
+++ b/include/lex.h
@@ -95,16 +95,18 @@ enum lexical_token {
extern const char *tokens[];
+struct number {
+ bool isfloat;
+ union {
+ uint64_t integer;
+ double floatingpt;
+ } value;
+};
+
struct token {
enum lexical_token token;
union {
- struct {
- bool isfloat;
- union {
- uint64_t integer;
- double floatingpt;
- } value;
- } num;
+ struct number num;
const char *str;
} info;
};
diff --git a/include/parse.h b/include/parse.h
new file mode 100644
index 0000000..d3cee83
--- /dev/null
+++ b/include/parse.h
@@ -0,0 +1,86 @@
+#ifndef CERC_PARSE_H
+#define CERC_PARSE_H
+#include "lex.h"
+#include "type.h"
+
+struct ast_externfunc {
+ const char *name;
+ struct type ret;
+ struct type params[];
+};
+
+struct ast_param {
+ const char *name;
+ struct type type;
+};
+
+struct ast_cmd { /* TODO */ };
+
+struct ast_block {
+ struct ast_cmd *cmds;
+};
+
+struct ast_func {
+ bool pub, exported;
+ const char *name;
+ struct ast_block block;
+ struct type ret;
+ struct ast_param params[];
+};
+
+enum const_global {
+ CST_TYPE,
+ CST_BOOL,
+ CST_NUMBER,
+ CST_STRING,
+};
+
+struct ast_const_global {
+ bool pub;
+ const char *name;
+ enum const_global kind;
+ union {
+ struct type *type;
+ bool b;
+ struct number num;
+ const char *str;
+ } value;
+};
+
+struct ast_path {
+ const char **segments;
+};
+
+struct ast_import {
+ struct ast_path path;
+ const char *name;
+};
+
+enum toplevel {
+ TOP_EXTERNFUNC,
+ TOP_FUNC,
+ TOP_CONST,
+};
+
+struct ast_toplevel {
+ enum toplevel kind;
+ union {
+ struct ast_externfunc *extfn;
+ struct ast_func *function;
+ struct ast_const_global *constant;
+ } decl;
+};
+
+struct ast_subunit {
+ struct ast_import *imports;
+ struct ast_toplevel tops[];
+};
+
+struct ast_unit {
+ struct ast_subunit *root;
+ struct ast_subunit imports[];
+};
+
+void parse(struct lexer *lexer, struct ast_unit *ast);
+
+#endif
diff --git a/src/parse.c b/src/parse.c
new file mode 100644
index 0000000..406752d
--- /dev/null
+++ b/src/parse.c
@@ -0,0 +1 @@
+#include "parse.h"