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/expr.h | |
parent | c640c9856b499af2ed5faedbf03e9945d74aafa9 (diff) |
Define overhauled AST data structures
Diffstat (limited to 'include/expr.h')
-rw-r--r-- | include/expr.h | 289 |
1 files changed, 109 insertions, 180 deletions
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 |