diff options
author | Himbeer <himbeer@disroot.org> | 2025-05-05 11:54:58 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2025-05-05 11:54:58 +0200 |
commit | 828f7cfe05eec5547837867101fc662c3dea60f1 (patch) | |
tree | d42580915bc74937c9d5bf14f678679bc3ab3d35 /include | |
parent | c640c9856b499af2ed5faedbf03e9945d74aafa9 (diff) |
Define overhauled AST data structures
Diffstat (limited to 'include')
-rw-r--r-- | include/cmd.h | 66 | ||||
-rw-r--r-- | include/expr.h | 289 | ||||
-rw-r--r-- | include/literal.h | 20 | ||||
-rw-r--r-- | include/parse.h | 170 |
4 files changed, 259 insertions, 286 deletions
diff --git a/include/cmd.h b/include/cmd.h deleted file mode 100644 index 2cf69b4..0000000 --- a/include/cmd.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef CERC_CMD_H -#define CERC_CMD_H -#include "type.h" - -struct ast_return { - struct ast_expr *value; -}; - -struct ast_break { - const char *label; - struct ast_expr *value; -}; - -struct ast_declaration { - bool mut; - const char *name; - struct ast_expr *value; -}; - -enum statement { - STM_RETURN, - STM_BREAK, - STM_CONTINUE, - STM_DECL, - STM_DEFER, -}; - -struct ast_statement { - enum statement action; - union { - struct ast_return ret; - struct ast_break brk; - struct ast_declaration decl; - struct assign_e *assign; - struct call_e *call; - } stmt; -}; - -struct ast_func { - bool exported; - const char *name; - struct type *ret; - struct ast_param *params; - int paramsz, paramlen; - struct ast_block *block; -}; -struct ast_block { - const char *label; - struct ast_cmd *cmds; - int cmdsz, cmdlen; -}; - -enum command { - CMD_BLOCK, - CMD_STATEMENT, -}; - -struct ast_cmd { - enum command kind; - union { - struct ast_block blk; - struct ast_statement stmt; - } cmd; -}; - -#endif diff --git a/include/expr.h b/include/expr.h index 5f57349..9d4d50a 100644 --- a/include/expr.h +++ b/include/expr.h @@ -1,249 +1,178 @@ #ifndef CERC_EXPR_H #define CERC_EXPR_H #include <stdbool.h> -#include "cmd.h" #include "lex.h" +#include "literal.h" #include "type.h" -struct number_e { - struct number value; - struct type *type; +enum valuekind { + V_LITERAL, + V_NAME, }; -struct array_e { - struct cer_array *meta; - struct disjunction_e *elems; - int elemsz, elemlen; -}; - -struct elseif { - struct disjunction_e *cond; - struct ast_block blk; -}; - -struct if_e { - struct disjunction_e *cond; - struct ast_block blk; - struct elseif *alts; - struct ast_block *alt; - int altsz, altlen; -}; - -struct for_e { - struct ast_assign *init, *action; - struct disjunction_e *cond; - struct ast_block blk; -}; - -enum control { - CTL_IF, - CTL_FOR, -}; - -struct control_e { - enum control kind; +struct value_e { + enum valuekind kind; union { - struct if_e branch; - struct for_e loop; - } ctl; + struct literal lit; + const char *name; + } v; }; -enum literal { - LIT_BOOL, - LIT_STRING, - LIT_NUMBER, - LIT_ARRAY, -}; - -struct literal_e { - enum literal kind; +struct grp_e { + int isgrouped; union { - bool b; - char *str; - struct number_e num; - struct array_e arr; - } lit; -}; - -struct location_e { - struct path *path; - struct loc_postfix *subs; - int subsz, sublen; -}; - -struct assign_e { - struct location_e dst; - struct ast_expr *value; + struct expr *grouped; + struct value_e value; + } inner; }; struct call_e { - struct location_e *loc; - struct disjunction_e *args; + struct expr *args; int argsz, arglen; }; -enum named { - NMD_ASSIGN, - NMD_CALL, - NMD_LOCATION, +enum access { + AC_NONE, + AC_FIELD, + AC_INDEX, + AC_CALL, }; -struct named_e { - enum named kind; - struct location_e loc; +struct access_e { + struct grp_e grp; + enum access kind; union { - struct assign_e assign; + const char *field; + struct expr *index; struct call_e call; - } nmd; + } ac; + struct access_e *next; }; -enum primary { - PRM_CONTROL, - PRM_GROUPING, - PRM_LITERAL, - PRM_NAMED, +struct tag_e { + int dotagof; + struct access_e access; }; -struct primary_e { - enum primary kind; - union { - struct control_e ctl; - struct disjunction_e *grp; - struct literal_e lit; - struct named_e nmd; - } prim; +enum signop { + SIGN_NONE, + SIGN_POS, + SIGN_NEG, }; -enum unary_postfix { - UPS_NONE, - UPS_DEREF, - UPS_TRY, - UPS_ASSERT, +struct sign_e { + enum signop op; + struct tag_e tag; }; -struct unarypost_e { - enum unary_postfix op; - struct primary_e prim; +enum invop { + INV_NONE, + INV_BITS, + INV_BOOL, }; -enum unary_prefix { - UPR_NONE, - UPR_NEG, - UPR_INVERT, - UPR_BNEG, -}; - -struct unarypre_e { - enum unary_prefix op; - struct unarypost_e post; +struct inv_e { + enum invop op; + union { + struct inv_e *inv; + struct sign_e sign; + } oper; }; -struct bitfactor_e { - struct unarypre_e lhs, *rhs; - int rhssz, rhslen; +struct deref_e { + int doderef; + union { + struct deref_e *deref; + struct inv_e inv; + } inner; }; -struct bitsummand_e { - struct bitfactor_e lhs, *rhs; - int rhssz, rhslen; +struct ref_e { + int doref; + struct deref_e inner; }; -struct bin_e { - struct bitsummand_e lhs, *rhs; - int rhssz, rhslen; +struct as_e { + struct ref_e lhs; + struct type *cast; }; -enum factor { - FCT_MUL, - FCT_DIV, - FCT_REM, +struct bitand_e { + struct as_e lhs; + struct bitand_e *rhs; }; -struct factor_rhs { - enum factor factor; - struct bin_e bin; +struct bitxor_e { + struct bitand_e lhs; + struct bitxor_e *rhs; }; -struct factor_e { - struct bin_e lhs; - struct factor_rhs *rhs; - int rhssz, rhslen; +struct bitor_e { + struct bitxor_e lhs; + struct bitor_e *rhs; }; -enum numeral { - NUM_ADD, - NUM_SUB, +enum productop { + PRODUCT_NONE, + PRODUCT_MUL, + PRODUCT_DIV, + PRODUCT_REM, }; -struct num_rhs { - enum numeral num; - struct factor_e factor; +struct product_e { + struct bitor_e lhs; + enum productop op; + struct product_e *rhs; }; -struct num_e { - struct factor_e lhs; - struct num_rhs *rhs; - int rhssz, rhslen; +enum sumop { + SUM_NONE, + SUM_ADD, + SUM_SUB, }; -enum term { - TRM_NUM, - TRM_SHL, - TRM_SHR, +struct sum_e { + struct product_e lhs; + enum sumop op; + struct sum_e *rhs; }; -struct term_e { - enum term term; - struct num_e lhs, *rhs; +enum shiftop { + SHIFT_NONE, + SHIFT_LEFT, + SHIFT_RIGHT, }; -enum comparison { - CMP_LT, - CMP_LE, - CMP_GT, - CMP_GE, +struct shift_e { + struct sum_e lhs; + enum shiftop op; + struct shift_e *rhs; }; -struct cmp_rhs { - enum comparison cmp; - struct term_e term; +enum cmpop { + CMP_NONE, + CMP_LESS, + CMP_LESS_EQ, + CMP_EQ, + CMP_NEQ, + CMP_GREATER_EQ, + CMP_GREATER, }; struct cmp_e { - struct term_e lhs; - struct cmp_rhs *rhs; - int rhssz, rhslen; -}; - -struct eq_e { - bool invert; - struct cmp_e lhs, *rhs; -}; - -enum boolean { - B_EQ, - B_CMP, -}; - -struct bool_e { - enum boolean kind; - union { - struct eq_e eq; - struct cmp_e cmp; - } cond; + struct shift_e lhs; + enum cmpop op; + struct shift_e *rhs; }; struct conjunction_e { - // At least one required - struct bool_e *bools; - int boolsz, boollen; + struct cmp_e lhs; + struct conjunction_e *rhs; }; -// Entry point -struct disjunction_e { - // At least one required - struct conjunction_e *cons; - int consz, conlen; +struct expr { + struct conjunction_e lhs; + struct expr *rhs; }; #endif diff --git a/include/literal.h b/include/literal.h new file mode 100644 index 0000000..912dce0 --- /dev/null +++ b/include/literal.h @@ -0,0 +1,20 @@ +#ifndef CERC_LITERAL_H +#define CERC_LITERAL_H +enum litkind { + LIT_INT, + LIT_FLOAT, + LIT_CHR, + LIT_STR, +}; + +struct literal { + enum litkind kind; + union { + int n; + double x; + int c; + const char *s; + } v; +}; + +#endif diff --git a/include/parse.h b/include/parse.h index 5b97353..64d1eac 100644 --- a/include/parse.h +++ b/include/parse.h @@ -2,77 +2,167 @@ #define CERC_PARSE_H #include "expr.h" -struct ast_expr { - struct disjunction_e dis; +struct ast_local { + const char *name; + struct type type; + struct expr *init; }; -struct ast_externfunc { - const char *name; - struct type ret; - struct type *params; - int paramsz, paramlen; +struct ast_assign { + struct expr lhs; + struct expr rhs; +}; + +enum crement { + INCREMENT, + DECREMENT, +}; + +struct ast_elsebody { + struct ast_stmt *stmts; + int stmtsz, stmtlen; +}; + +struct ast_else { + int cond; + union { + struct ast_if *conditional; + struct ast_elsebody body; + } action; +}; + +struct ast_if { + struct expr cond; + struct ast_stmt *stmts; + int stmtsz, stmtlen; + struct ast_else *alt; +}; + +struct ast_for { + struct ast_stmt *init; + struct expr cond; + struct ast_stmt *step; + struct ast_stmt *stmts; + int stmtsz, stmtlen; +}; + +struct ast_return { + struct expr *value; +}; + +enum stmt { + S_LOCALVAR, + S_ASSIGN, + S_ADDASSIGN, + S_SUBASSIGN, + S_MULASSIGN, + S_DIVASSIGN, + S_REMASSIGN, + S_ANDASSIGN, + S_ORASSIGN, + S_XORASSIGN, + S_INCR, + S_DECR, + S_IF, + S_FOR, + S_RETURN, + S_EXPR, }; -struct ast_param { +struct ast_stmt { + enum stmt kind; + union { + struct ast_local localvar; + struct ast_assign assign; + enum crement cre; + struct ast_if conditional; + struct ast_for loop; + struct ast_return ret; + struct expr e; + } stmt; +}; + +struct ast_include { + const char *path; +}; + +struct ast_global { const char *name; + int ispub; + int isextern; struct type type; }; -enum subfield { - SUB_INDEX, - SUB_FIELD, +struct ast_const { + const char *name; + struct expr value; }; -struct loc_postfix { - enum subfield sub; - union { - struct ast_expr index; - const char *field; - } id; +struct ast_enumvar { + const char *name; + struct expr *override; }; -enum const_global { - CST_TYPE, - CST_BOOL, - CST_NUMBER, - CST_STRING, +struct ast_enum { + struct expr init; + struct ast_enumvar *variants; + int variantsz, variantlen; }; -struct ast_const_global { +struct ast_field { const char *name; - enum const_global kind; - union { - struct type type; - bool b; - struct number num; - char *str; - } value; + struct type type; }; -struct ast_import { - struct path path; +struct ast_struct { const char *name; + int align; + struct ast_field *fields; + int fieldsz, fieldlen; +}; + +struct ast_union { + const char *name; + struct type *types; + int typesz, typelen; +}; + +struct ast_func { + int ispub; + int isextern; + const char *name; + struct ast_field *params; + int paramsz, paramlen; + struct type *ret; + struct ast_stmt *stmts; + int stmtsz, stmtlen; }; enum toplevel { - TOP_EXTERNFUNC, - TOP_FUNC, + TOP_INCLUDE, + TOP_GLOBAL, TOP_CONST, + TOP_ENUM, + TOP_STRUCT, + TOP_UNION, + TOP_FUNC, }; struct ast_toplevel { enum toplevel kind; union { - struct ast_externfunc *extfn; - struct ast_func *function; - struct ast_const_global *constant; - } decl; + struct ast_include include; + struct ast_global globalvar; + struct ast_const constant; + struct ast_enum enumconst; + struct ast_struct structdecl; + struct ast_union uniondecl; + struct ast_func function; + } tl; }; struct ast_unit { - struct ast_import *imports; struct ast_toplevel *tops; - int impsz, implen; int topsz, toplen; }; |