From 71d9ad9506524fcd3e124c4b4a460afa8fbf35eb Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 17 Feb 2020 15:46:38 +0800 Subject: refactor some codes to be safer. --- src/MoonP/moon_ast.h | 227 +++++++++++++++++++++++++++------------------------ 1 file changed, 119 insertions(+), 108 deletions(-) (limited to 'src/MoonP/moon_ast.h') diff --git a/src/MoonP/moon_ast.h b/src/MoonP/moon_ast.h index 86f5ef3..4525df1 100644 --- a/src/MoonP/moon_ast.h +++ b/src/MoonP/moon_ast.h @@ -13,19 +13,28 @@ using namespace parserlib; namespace MoonP { -#define AST_LEAF(type, id) \ +// str hash helper functions (djb2) +inline constexpr size_t hash(char const* input) { + return *input ? *input + 33ull * hash(input + 1) : 5381; +} + +template +constexpr typename std::enable_if::value,size_t>::type +id() { return 0; } + +#define AST_LEAF(type) \ class type##_t : public ast_node \ { \ public: \ virtual int get_type() override { return ast_type(); } \ - virtual size_t getId() const override { return id; } \ + virtual size_t getId() const override { return hash(#type); } -#define AST_NODE(type, id) \ +#define AST_NODE(type) \ class type##_t : public ast_container \ { \ public: \ virtual int get_type() override { return ast_type(); } \ - virtual size_t getId() const override { return id; } \ + virtual size_t getId() const override { return hash(#type); } \ #define AST_MEMBER(type, ...) \ type##_t() { \ @@ -33,73 +42,74 @@ public: \ } #define AST_END(type) \ -}; +}; \ +template<> constexpr size_t id() { return hash(#type); } -AST_LEAF(Num, "Num"_id) +AST_LEAF(Num) AST_END(Num) -AST_LEAF(Name, "Name"_id) +AST_LEAF(Name) AST_END(Name) -AST_NODE(Variable, "Variable"_id) +AST_NODE(Variable) ast_ptr name; AST_MEMBER(Variable, &name) AST_END(Variable) -AST_NODE(LuaKeyword, "LuaKeyword"_id) +AST_NODE(LuaKeyword) ast_ptr name; AST_MEMBER(LuaKeyword, &name) AST_END(LuaKeyword) -AST_LEAF(self, "self"_id) +AST_LEAF(self) AST_END(self) -AST_NODE(self_name, "self_name"_id) +AST_NODE(self_name) ast_ptr name; AST_MEMBER(self_name, &name) AST_END(self_name) -AST_LEAF(self_class, "self_class"_id) +AST_LEAF(self_class) AST_END(self_class) -AST_NODE(self_class_name, "self_class_name"_id) +AST_NODE(self_class_name) ast_ptr name; AST_MEMBER(self_class_name, &name) AST_END(self_class_name) -AST_NODE(SelfName, "SelfName"_id) +AST_NODE(SelfName) ast_sel name; AST_MEMBER(SelfName, &name) AST_END(SelfName) -AST_NODE(KeyName, "KeyName"_id) +AST_NODE(KeyName) ast_sel name; AST_MEMBER(KeyName, &name) AST_END(KeyName) -AST_LEAF(VarArg, "VarArg"_id) +AST_LEAF(VarArg) AST_END(VarArg) -AST_LEAF(local_flag, "local_flag"_id) +AST_LEAF(local_flag) AST_END(local_flag) -AST_LEAF(Seperator, "Seperator"_id) +AST_LEAF(Seperator) AST_END(Seperator) -AST_NODE(NameList, "NameList"_id) +AST_NODE(NameList) ast_ptr sep; ast_list names; AST_MEMBER(NameList, &sep, &names) AST_END(NameList) -AST_NODE(Local, "Local"_id) +AST_NODE(Local) ast_sel name; std::list forceDecls; std::list decls; AST_MEMBER(Local, &name) AST_END(Local) -AST_NODE(colon_import_name, "colon_import_name"_id) +AST_NODE(colon_import_name) ast_ptr name; AST_MEMBER(colon_import_name, &name) AST_END(colon_import_name) @@ -107,29 +117,29 @@ AST_END(colon_import_name) class Exp_t; class TableLit_t; -AST_LEAF(import_literal_inner, "import_literal_inner"_id) +AST_LEAF(import_literal_inner) AST_END(import_literal_inner) -AST_NODE(ImportLiteral, "ImportLiteral"_id) +AST_NODE(ImportLiteral) ast_ptr sep; ast_sel_list inners; AST_MEMBER(ImportLiteral, &sep, &inners) AST_END(ImportLiteral) -AST_NODE(ImportAs, "ImportAs"_id) +AST_NODE(ImportAs) ast_ptr literal; ast_sel target; AST_MEMBER(ImportAs, &literal, &target) AST_END(ImportAs) -AST_NODE(ImportFrom, "ImportFrom"_id) +AST_NODE(ImportFrom) ast_ptr sep; ast_sel_list names; ast_ptr exp; AST_MEMBER(ImportFrom, &sep, &names, &exp) AST_END(ImportFrom) -AST_NODE(Import, "Import"_id) +AST_NODE(Import) ast_sel content; AST_MEMBER(Import, &content) AST_END(Import) @@ -137,25 +147,25 @@ AST_END(Import) class FnArgsDef_t; class ChainValue_t; -AST_NODE(Backcall, "Backcall"_id) +AST_NODE(Backcall) ast_ptr argsDef; ast_ptr value; AST_MEMBER(Backcall, &argsDef, &value) AST_END(Backcall) -AST_NODE(ExpListLow, "ExpListLow"_id) +AST_NODE(ExpListLow) ast_ptr sep; ast_list exprs; AST_MEMBER(ExpListLow, &sep, &exprs) AST_END(ExpListLow) -AST_NODE(ExpList, "ExpList"_id) +AST_NODE(ExpList) ast_ptr sep; ast_list exprs; AST_MEMBER(ExpList, &sep, &exprs) AST_END(ExpList) -AST_NODE(Return, "Return"_id) +AST_NODE(Return) ast_ptr valueList; AST_MEMBER(Return, &valueList) AST_END(Return) @@ -163,20 +173,20 @@ AST_END(Return) class Assign_t; class Body_t; -AST_NODE(With, "With"_id) +AST_NODE(With) ast_ptr valueList; ast_ptr assigns; ast_ptr body; AST_MEMBER(With, &valueList, &assigns, &body) AST_END(With) -AST_NODE(SwitchCase, "SwitchCase"_id) +AST_NODE(SwitchCase) ast_ptr valueList; ast_ptr body; AST_MEMBER(SwitchCase, &valueList, &body) AST_END(SwitchCase) -AST_NODE(Switch, "Switch"_id) +AST_NODE(Switch) ast_ptr target; ast_ptr sep; ast_list branches; @@ -184,36 +194,36 @@ AST_NODE(Switch, "Switch"_id) AST_MEMBER(Switch, &target, &sep, &branches, &lastBranch) AST_END(Switch) -AST_NODE(IfCond, "IfCond"_id) +AST_NODE(IfCond) ast_ptr condition; ast_ptr assign; AST_MEMBER(IfCond, &condition, &assign) AST_END(IfCond) -AST_NODE(If, "If"_id) +AST_NODE(If) ast_ptr sep; ast_sel_list nodes; AST_MEMBER(If, &sep, &nodes) AST_END(If) -AST_NODE(Unless, "Unless"_id) +AST_NODE(Unless) ast_ptr sep; ast_sel_list nodes; AST_MEMBER(Unless, &sep, &nodes) AST_END(Unless) -AST_NODE(While, "While"_id) +AST_NODE(While) ast_ptr condition; ast_ptr body; AST_MEMBER(While, &condition, &body) AST_END(While) -AST_NODE(for_step_value, "for_step_value"_id) +AST_NODE(for_step_value) ast_ptr value; AST_MEMBER(for_step_value, &value) AST_END(for_step_value) -AST_NODE(For, "For"_id) +AST_NODE(For) ast_ptr varName; ast_ptr startValue; ast_ptr stopValue; @@ -225,14 +235,14 @@ AST_END(For) class AssignableNameList_t; class star_exp_t; -AST_NODE(ForEach, "ForEach"_id) +AST_NODE(ForEach) ast_ptr nameList; ast_sel loopValue; ast_ptr body; AST_MEMBER(ForEach, &nameList, &loopValue, &body) AST_END(ForEach) -AST_NODE(Do, "Do"_id) +AST_NODE(Do) ast_ptr body; AST_MEMBER(Do, &body) AST_END(Do) @@ -240,36 +250,36 @@ AST_END(Do) class CompInner_t; class Statement_t; -AST_NODE(Comprehension, "Comprehension"_id) +AST_NODE(Comprehension) ast_sel value; ast_ptr forLoop; AST_MEMBER(Comprehension, &value, &forLoop) AST_END(Comprehension) -AST_NODE(comp_value, "comp_value"_id) +AST_NODE(comp_value) ast_ptr value; AST_MEMBER(comp_value, &value) AST_END(comp_value) -AST_NODE(TblComprehension, "TblComprehension"_id) +AST_NODE(TblComprehension) ast_ptr key; ast_ptr value; ast_ptr forLoop; AST_MEMBER(TblComprehension, &key, &value, &forLoop) AST_END(TblComprehension) -AST_NODE(star_exp, "star_exp"_id) +AST_NODE(star_exp) ast_ptr value; AST_MEMBER(star_exp, &value) AST_END(star_exp) -AST_NODE(CompForEach, "CompForEach"_id) +AST_NODE(CompForEach) ast_ptr nameList; ast_sel loopValue; AST_MEMBER(CompForEach, &nameList, &loopValue) AST_END(CompForEach) -AST_NODE(CompFor, "CompFor"_id) +AST_NODE(CompFor) ast_ptr varName; ast_ptr startValue; ast_ptr stopValue; @@ -277,7 +287,7 @@ AST_NODE(CompFor, "CompFor"_id) AST_MEMBER(CompFor, &varName, &startValue, &stopValue, &stepValue) AST_END(CompFor) -AST_NODE(CompInner, "CompInner"_id) +AST_NODE(CompInner) ast_ptr sep; ast_sel_list items; AST_MEMBER(CompInner, &sep, &items) @@ -285,43 +295,43 @@ AST_END(CompInner) class TableBlock_t; -AST_NODE(Assign, "Assign"_id) +AST_NODE(Assign) ast_ptr sep; ast_sel_list values; AST_MEMBER(Assign, &sep, &values) AST_END(Assign) -AST_LEAF(update_op, "update_op"_id) +AST_LEAF(update_op) AST_END(update_op) -AST_NODE(Update, "Update"_id) +AST_NODE(Update) ast_ptr op; ast_ptr value; AST_MEMBER(Update, &op, &value) AST_END(Update) -AST_LEAF(BinaryOperator, "BinaryOperator"_id) +AST_LEAF(BinaryOperator) AST_END(BinaryOperator) -AST_LEAF(BackcallOperator, "BackcallOperator"_id) +AST_LEAF(BackcallOperator) AST_END(BackcallOperator) class AssignableChain_t; -AST_NODE(Assignable, "Assignable"_id) +AST_NODE(Assignable) ast_sel item; AST_MEMBER(Assignable, &item) AST_END(Assignable) class Value_t; -AST_NODE(exp_op_value, "exp_op_value"_id) +AST_NODE(exp_op_value) ast_sel op; ast_ptr value; AST_MEMBER(exp_op_value, &op, &value) AST_END(exp_op_value) -AST_NODE(Exp, "Exp"_id) +AST_NODE(Exp) ast_ptr value; ast_list opValues; AST_MEMBER(Exp, &value, &opValues) @@ -329,12 +339,12 @@ AST_END(Exp) class Parens_t; -AST_NODE(Callable, "Callable"_id) +AST_NODE(Callable) ast_sel item; AST_MEMBER(Callable, &item) AST_END(Callable) -AST_NODE(variable_pair, "variable_pair"_id) +AST_NODE(variable_pair) ast_ptr name; AST_MEMBER(variable_pair, &name) AST_END(variable_pair) @@ -342,13 +352,13 @@ AST_END(variable_pair) class DoubleString_t; class SingleString_t; -AST_NODE(normal_pair, "normal_pair"_id) +AST_NODE(normal_pair) ast_sel key; ast_sel value; AST_MEMBER(normal_pair, &key, &value) AST_END(normal_pair) -AST_NODE(simple_table, "simple_table"_id) +AST_NODE(simple_table) ast_ptr sep; ast_sel_list pairs; AST_MEMBER(simple_table, &sep, &pairs) @@ -361,7 +371,7 @@ class unary_exp_t; class TableLit_t; class FunLit_t; -AST_NODE(SimpleValue, "SimpleValue"_id) +AST_NODE(SimpleValue) ast_sel open; ast_ptr content; ast_ptr close; AST_MEMBER(LuaString, &open, &content, &close) AST_END(LuaString) -AST_LEAF(SingleString, "SingleString"_id) +AST_LEAF(SingleString) AST_END(SingleString) -AST_LEAF(double_string_inner, "double_string_inner"_id) +AST_LEAF(double_string_inner) AST_END(double_string_inner) -AST_NODE(double_string_content, "double_string_content"_id) +AST_NODE(double_string_content) ast_sel content; AST_MEMBER(double_string_content, &content) AST_END(double_string_content) -AST_NODE(DoubleString, "DoubleString"_id) +AST_NODE(DoubleString) ast_ptr sep; ast_list segments; AST_MEMBER(DoubleString, &sep, &segments) AST_END(DoubleString) -AST_NODE(String, "String"_id) +AST_NODE(String) ast_sel str; AST_MEMBER(String, &str) AST_END(String) -AST_NODE(DotChainItem, "DotChainItem"_id) +AST_NODE(DotChainItem) ast_ptr name; AST_MEMBER(DotChainItem, &name) AST_END(DotChainItem) -AST_NODE(ColonChainItem, "ColonChainItem"_id) +AST_NODE(ColonChainItem) ast_sel name; bool switchToDot = false; AST_MEMBER(ColonChainItem, &name) @@ -421,180 +431,180 @@ AST_END(ColonChainItem) class default_value_t; -AST_NODE(Slice, "Slice"_id) +AST_NODE(Slice) ast_sel startValue; ast_sel stopValue; ast_sel stepValue; AST_MEMBER(Slice, &startValue, &stopValue, &stepValue) AST_END(Slice) -AST_NODE(Parens, "Parens"_id) +AST_NODE(Parens) ast_ptr expr; AST_MEMBER(Parens, &expr) AST_END(Parens) -AST_NODE(Invoke, "Invoke"_id) +AST_NODE(Invoke) ast_ptr sep; ast_sel_list args; AST_MEMBER(Invoke, &sep, &args) AST_END(Invoke) -AST_LEAF(existential_op, "existential_op"_id) +AST_LEAF(existential_op) AST_END(existential_op) class InvokeArgs_t; -AST_NODE(ChainValue, "ChainValue"_id) +AST_NODE(ChainValue) ast_ptr sep; ast_sel_list items; AST_MEMBER(ChainValue, &sep, &items) AST_END(ChainValue) -AST_NODE(AssignableChain, "AssignableChain"_id) +AST_NODE(AssignableChain) ast_ptr sep; ast_sel_list items; AST_MEMBER(AssignableChain, &sep, &items) AST_END(AssignableChain) -AST_NODE(Value, "Value"_id) +AST_NODE(Value) ast_sel item; AST_MEMBER(Value, &item) AST_END(Value) -AST_LEAF(default_value, "default_value"_id) +AST_LEAF(default_value) AST_END(default_value) -AST_NODE(TableLit, "TableLit"_id) +AST_NODE(TableLit) ast_ptr sep; ast_sel_list values; AST_MEMBER(TableLit, &sep, &values) AST_END(TableLit) -AST_NODE(TableBlock, "TableBlock"_id) +AST_NODE(TableBlock) ast_ptr sep; ast_sel_list values; AST_MEMBER(TableBlock, &sep, &values) AST_END(TableBlock) -AST_NODE(class_member_list, "class_member_list"_id) +AST_NODE(class_member_list) ast_ptr sep; ast_sel_list values; AST_MEMBER(class_member_list, &sep, &values) AST_END(class_member_list) -AST_NODE(ClassBlock, "ClassBlock"_id) +AST_NODE(ClassBlock) ast_ptr sep; ast_sel_list contents; AST_MEMBER(ClassBlock, &sep, &contents) AST_END(ClassBlock) -AST_NODE(ClassDecl, "ClassDecl"_id) +AST_NODE(ClassDecl) ast_ptr name; ast_ptr extend; ast_ptr body; AST_MEMBER(ClassDecl, &name, &extend, &body) AST_END(ClassDecl) -AST_NODE(export_values, "export_values"_id) +AST_NODE(export_values) ast_ptr nameList; ast_ptr valueList; AST_MEMBER(export_values, &nameList, &valueList) AST_END(export_values) -AST_LEAF(export_op, "export_op"_id) +AST_LEAF(export_op) AST_END(export_op) -AST_NODE(Export, "Export"_id) +AST_NODE(Export) ast_sel item; AST_MEMBER(Export, &item) AST_END(Export) -AST_NODE(FnArgDef, "FnArgDef"_id) +AST_NODE(FnArgDef) ast_sel name; ast_ptr defaultValue; AST_MEMBER(FnArgDef, &name, &defaultValue) AST_END(FnArgDef) -AST_NODE(FnArgDefList, "FnArgDefList"_id) +AST_NODE(FnArgDefList) ast_ptr sep; ast_list definitions; ast_ptr varArg; AST_MEMBER(FnArgDefList, &sep, &definitions, &varArg) AST_END(FnArgDefList) -AST_NODE(outer_var_shadow, "outer_var_shadow"_id) +AST_NODE(outer_var_shadow) ast_ptr varList; AST_MEMBER(outer_var_shadow, &varList) AST_END(outer_var_shadow) -AST_NODE(FnArgsDef, "FnArgsDef"_id) +AST_NODE(FnArgsDef) ast_ptr defList; ast_ptr shadowOption; AST_MEMBER(FnArgsDef, &defList, &shadowOption) AST_END(FnArgsDef) -AST_LEAF(fn_arrow, "fn_arrow"_id) +AST_LEAF(fn_arrow) AST_END(fn_arrow) -AST_NODE(FunLit, "FunLit"_id) +AST_NODE(FunLit) ast_ptr argsDef; ast_ptr arrow; ast_ptr body; AST_MEMBER(FunLit, &argsDef, &arrow, &body) AST_END(FunLit) -AST_NODE(NameOrDestructure, "NameOrDestructure"_id) +AST_NODE(NameOrDestructure) ast_sel item; AST_MEMBER(NameOrDestructure, &item) AST_END(NameOrDestructure) -AST_NODE(AssignableNameList, "AssignableNameList"_id) +AST_NODE(AssignableNameList) ast_ptr sep; ast_list items; AST_MEMBER(AssignableNameList, &sep, &items) AST_END(AssignableNameList) -AST_NODE(InvokeArgs, "InvokeArgs"_id) +AST_NODE(InvokeArgs) ast_ptr sep; ast_sel_list args; AST_MEMBER(InvokeArgs, &sep, &args) AST_END(InvokeArgs) -AST_LEAF(const_value, "const_value"_id) +AST_LEAF(const_value) AST_END(const_value) -AST_NODE(unary_exp, "unary_exp"_id) +AST_NODE(unary_exp) ast_ptr item; AST_MEMBER(unary_exp, &item) AST_END(unary_exp) -AST_NODE(ExpListAssign, "ExpListAssign"_id) +AST_NODE(ExpListAssign) ast_ptr expList; ast_sel action; AST_MEMBER(ExpListAssign, &expList, &action) AST_END(ExpListAssign) -AST_NODE(if_else_line, "if_else_line"_id) +AST_NODE(if_else_line) ast_ptr condition; ast_ptr assign; ast_sel elseExpr; AST_MEMBER(if_else_line, &condition, &assign, &elseExpr) AST_END(if_else_line) -AST_NODE(unless_line, "unless_line"_id) +AST_NODE(unless_line) ast_ptr condition; AST_MEMBER(unless_line, &condition) AST_END(unless_line) -AST_NODE(statement_appendix, "statement_appendix"_id) +AST_NODE(statement_appendix) ast_sel item; AST_MEMBER(statement_appendix, &item) AST_END(statement_appendix) -AST_LEAF(BreakLoop, "BreakLoop"_id) +AST_LEAF(BreakLoop) AST_END(BreakLoop) -AST_NODE(Statement, "Statement"_id) +AST_NODE(Statement) ast_sel content; @@ -604,20 +614,21 @@ AST_END(Statement) class Block_t; -AST_NODE(Body, "Body"_id) +AST_NODE(Body) ast_sel content; AST_MEMBER(Body, &content) AST_END(Body) -AST_NODE(Block, "Block"_id) +AST_NODE(Block) ast_ptr sep; ast_list statements; AST_MEMBER(Block, &sep, &statements) AST_END(Block) -AST_NODE(File, "File"_id) +AST_NODE(File) ast_ptr block; AST_MEMBER(File, &block) AST_END(File) } // namespace MoonP + -- cgit v1.2.3-55-g6feb