aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2024-10-01 10:37:34 +0200
committerHimbeer <himbeer@disroot.org>2024-10-01 11:34:23 +0200
commit412123d583f748b5f3797e6a57acae22cb1ffa80 (patch)
tree4f35439f987d80a9f403583552589b0717a60d2f
parentcc4cdce558e3204eb8320981ef04d90842128ab1 (diff)
Add AST data structures for statements
-rw-r--r--doc/grammar.txt2
-rw-r--r--include/parse.h107
2 files changed, 106 insertions, 3 deletions
diff --git a/doc/grammar.txt b/doc/grammar.txt
index 8552c90..d92f1dc 100644
--- a/doc/grammar.txt
+++ b/doc/grammar.txt
@@ -16,7 +16,7 @@ constant := [ COMMENT ]
block := "{" command* "}"
-command := block | control | statement
+command := block | statement | control
control := if | for
diff --git a/include/parse.h b/include/parse.h
index 981d8c6..18207bc 100644
--- a/include/parse.h
+++ b/include/parse.h
@@ -18,10 +18,113 @@ struct ast_param {
struct type type;
};
-struct ast_cmd { /* TODO */ };
-
struct ast_block {
struct ast_cmd *cmds;
+ int cmdsz, cmdlen;
+};
+
+struct ast_return {
+ struct ast_expr value;
+};
+
+struct ast_declaration {
+ bool mut;
+ const char *name;
+ struct ast_expr value;
+};
+
+enum subfield {
+ SUB_INDEX,
+ SUB_FIELD,
+};
+
+struct loc_postfix {
+ enum subfield sub;
+ union {
+ struct ast_expr index;
+ const char *field;
+ } id;
+};
+
+struct ast_location {
+ const char *name;
+ struct loc_postfix *subs;
+ int subsz, sublen;
+};
+
+struct ast_assign {
+ struct ast_location dst;
+ struct ast_expr value;
+};
+
+enum statement {
+ STM_RETURN,
+ STM_BREAK,
+ STM_CONTINUE,
+ STM_DECL,
+ STM_ASSIGN,
+ STM_CALL,
+ STM_DEFER,
+};
+
+struct ast_statement {
+ enum statement action;
+ union {
+ struct ast_return ret;
+ const char *brklabel;
+ struct ast_declaration decl;
+ struct ast_assign assign;
+ struct call_e call;
+ } stmt;
+};
+
+struct ast_elseif {
+ struct ast_expr cond;
+ struct ast_block blk;
+};
+
+struct ast_if {
+ struct ast_expr cond;
+ struct ast_block blk;
+ struct ast_elseif *alts;
+ struct ast_block *alt;
+ int altsz, altlen;
+};
+
+struct ast_for {
+ const char *label;
+ struct ast_assignment *init;
+ struct ast_expr cond;
+ struct ast_assignment *action;
+ struct ast_block blk;
+};
+
+enum control {
+ CTL_IF,
+ CTL_FOR,
+};
+
+struct ast_control {
+ enum control kind;
+ union {
+ struct ast_if branch;
+ struct ast_for loop;
+ } ctl;
+};
+
+enum command {
+ CMD_BLOCK,
+ CMD_STATEMENT,
+ CMD_CONTROL,
+};
+
+struct ast_cmd {
+ enum command kind;
+ union {
+ struct ast_block blk;
+ struct ast_statement stmt;
+ struct ast_control ctl;
+ } cmd;
};
struct ast_func {