Construct a script engine using C language (5)
Author: kevin_qing
Please note
Syntax check, constant merging and the new idiom method tree are implemented in reduce normalization functions.
Syntax tree node Definition
Struct gtreenode {
Uint32_t type;
Uint32_t value;
};
Struct gtreenode1: Public gtreenode {
Gtreenode * Child1;
};
Struct gtreenode2: Public gtreenode1 {
Gtreenode * child2;
};
Struct gtreenode3: Public gtreenode2 {
Gtreenode * Child3;
};
Struct gtreenoden: Public gtreenode {
Array <gtreenode> Childs;
};
Type indicates the node type, such as if switch op_2 op_1 smt_seq (statement sequence) cpd_smt (composite statement) Case VAR numbers.
Value is a value of the corresponding type, such as the case label, string ID, constant value, and op_2 operation.
Gtreenode1 corresponds to the 1 RMB operation, such as not
Gtreenode2 corresponds to 2 meta operations, such as IF-SMT, +-*/% and other arithmetic logic operations
Gtreenode2 corresponds to a 3 yuan operation, typically a IF-SMT-ELSE-SMT
Sequence of gtreenoden statements
The syntax tree is generated inside the Statute function, and sub-nodes are added through stacks. Similar to token.
When a syntax tree node is generated, you can perform a semantic check and check whether the operands are constants. Instead, you can perform a constant merge optimization code.
When generating code, traverse the syntax tree in sequence. It is mainly because lable requires backtracking, and there are no other difficulties.
To be continued