File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11program = add
2- add = num ("+" num | "-" num)*
2+ add = mul ("+" mul | "-" mul)*
3+ mul = num ("*" num | "/" num)*
Original file line number Diff line number Diff line change @@ -32,6 +32,26 @@ vector<Operation> Generator::codegen(Node* node)
3232
3333 break ;
3434
35+ case ND_MUL:
36+ codegen (node->left );
37+ codegen (node->right );
38+
39+ operations.push_back ( Operation{ OP_POP, REG_GR0 } );
40+ operations.push_back ( Operation{ OP_POP, REG_GR1 } );
41+ operations.push_back ( Operation{ OP_MUL, REG_GR0, REG_GR1 } );
42+
43+ break ;
44+
45+ case ND_DIV:
46+ codegen (node->left );
47+ codegen (node->right );
48+
49+ operations.push_back ( Operation{ OP_POP, REG_GR0 } );
50+ operations.push_back ( Operation{ OP_POP, REG_GR1 } );
51+ operations.push_back ( Operation{ OP_DIV, REG_GR1, REG_GR0 } );
52+
53+ break ;
54+
3555 default :
3656 break ;
3757 }
Original file line number Diff line number Diff line change @@ -22,14 +22,31 @@ Node* Parser::num(void)
2222
2323Node* Parser::add (void )
2424{
25- Node* node = num ();
25+ Node* node = mul ();
2626
2727 while (1 ){
2828 if (consume (" +" )){
29- node = new Node{ ND_ADD, node, num () };
29+ node = new Node{ ND_ADD, node, mul () };
3030 }
3131 else if (consume (" -" )){
32- node = new Node{ ND_SUB, node, num () };
32+ node = new Node{ ND_SUB, node, mul () };
33+ }
34+ else {
35+ return node;
36+ }
37+ }
38+ };
39+
40+ Node* Parser::mul (void )
41+ {
42+ Node* node = num ();
43+
44+ while (1 ){
45+ if (consume (" *" )){
46+ node = new Node{ ND_MUL, node, num () };
47+ }
48+ else if (consume (" /" )){
49+ node = new Node{ ND_DIV, node, num () };
3350 }
3451 else {
3552 return node;
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ class Parser
1414 Node* program (void );
1515 Node* num (void );
1616 Node* add (void );
17+ Node* mul (void );
1718
1819 bool consume (const char * str);
1920
Original file line number Diff line number Diff line change @@ -24,6 +24,8 @@ typedef enum {
2424 ND_NUM, // 数値
2525 ND_ADD, // +
2626 ND_SUB, // -
27+ ND_MUL, // *
28+ ND_DIV, // /
2729} NodeKind;
2830
2931typedef struct Node Node;
@@ -42,6 +44,8 @@ typedef enum {
4244 OP_POP, // Pop
4345 OP_ADD, // 加算
4446 OP_SUB, // 減算
47+ OP_MUL, // 乗算
48+ OP_DIV, // 除算
4549} OP_CODE;
4650
4751typedef struct {
Original file line number Diff line number Diff line change @@ -20,5 +20,9 @@ test 9 "7+2"
2020test 4 " 10-6"
2121test 8 " 12 + 4 - 8"
2222test 10 " 12 - 4 + 2"
23+ test 8 " 2 * 4"
24+ test 3 " 12 / 4"
25+ test 10 " 5 * 3 - 5"
26+ test 9 " 2 + 2 * 5 - 3"
2327
2428echo " OK"
Original file line number Diff line number Diff line change @@ -26,6 +26,16 @@ vector<Token> Tokenizer::tokenize(char* input)
2626 tokens.push_back (token);
2727 input++;
2828 }
29+ else if (strncmp (input, " *" , 1 ) == 0 ){
30+ Token token = { TK_OP, input, 1 };
31+ tokens.push_back (token);
32+ input++;
33+ }
34+ else if (strncmp (input, " /" , 1 ) == 0 ){
35+ Token token = { TK_OP, input, 1 };
36+ tokens.push_back (token);
37+ input++;
38+ }
2939 else if (isdigit (*input)){
3040 Token token = { TK_NUM, input, 0 };
3141 for (; isdigit (*input); input++) token.len ++;
Original file line number Diff line number Diff line change @@ -46,6 +46,16 @@ DWORD VM::run(vector<Operation>& code)
4646 push (reg[op->operand ] - reg[op->operand2 ]);
4747 break ;
4848 }
49+ case OP_MUL:
50+ {
51+ push (reg[op->operand ] * reg[op->operand2 ]);
52+ break ;
53+ }
54+ case OP_DIV:
55+ {
56+ push (reg[op->operand ] / reg[op->operand2 ]);
57+ break ;
58+ }
4959 default :
5060 break ;
5161 }
You can’t perform that action at this time.
0 commit comments