Skip to content

Commit 95c14f7

Browse files
committed
連続した式の実行に対応
1 parent 3edccb3 commit 95c14f7

File tree

9 files changed

+51
-21
lines changed

9 files changed

+51
-21
lines changed

BNF.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
program = add
1+
program = add ("," add | "\n" add)*
22
add = mul ("+" mul | "-" mul)*
33
mul = unary ("*" unary | "/" unary)*
44
unary = ("+" | "-")? primary

generator.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
using namespace std;
77

8-
vector<Operation> Generator::codegen(Node* node)
8+
void Generator::gen(Node* node)
99
{
1010
switch(node->kind){
1111
case ND_NUM:
1212
operations.push_back( Operation{ OP_PUSH, node->val } );
1313
break;
1414

1515
case ND_ADD:
16-
codegen(node->left);
17-
codegen(node->right);
16+
gen(node->left);
17+
gen(node->right);
1818

1919
operations.push_back( Operation{ OP_POP, REG_GR0 } );
2020
operations.push_back( Operation{ OP_POP, REG_GR1 } );
@@ -23,8 +23,8 @@ vector<Operation> Generator::codegen(Node* node)
2323
break;
2424

2525
case ND_SUB:
26-
codegen(node->left);
27-
codegen(node->right);
26+
gen(node->left);
27+
gen(node->right);
2828

2929
operations.push_back( Operation{ OP_POP, REG_GR0 } );
3030
operations.push_back( Operation{ OP_POP, REG_GR1 } );
@@ -33,8 +33,8 @@ vector<Operation> Generator::codegen(Node* node)
3333
break;
3434

3535
case ND_MUL:
36-
codegen(node->left);
37-
codegen(node->right);
36+
gen(node->left);
37+
gen(node->right);
3838

3939
operations.push_back( Operation{ OP_POP, REG_GR0 } );
4040
operations.push_back( Operation{ OP_POP, REG_GR1 } );
@@ -43,8 +43,8 @@ vector<Operation> Generator::codegen(Node* node)
4343
break;
4444

4545
case ND_DIV:
46-
codegen(node->left);
47-
codegen(node->right);
46+
gen(node->left);
47+
gen(node->right);
4848

4949
operations.push_back( Operation{ OP_POP, REG_GR0 } );
5050
operations.push_back( Operation{ OP_POP, REG_GR1 } );
@@ -55,6 +55,16 @@ vector<Operation> Generator::codegen(Node* node)
5555
default:
5656
break;
5757
}
58+
};
59+
60+
vector<Operation> Generator::codegen(vector<Node*> &nodes)
61+
{
62+
vector<Node*>::iterator node;
63+
64+
for(node = nodes.begin(); node != nodes.end(); node++){
65+
gen(*node);
66+
operations.push_back( Operation{ OP_POP, REG_GR0 } );
67+
}
5868

5969
return operations;
6070
};

generator.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ class Generator
1010
{
1111
private:
1212
vector<Operation> operations;
13+
void gen(Node* node);
1314

1415
public:
15-
vector<Operation> codegen(Node* node);
16+
vector<Operation> codegen(vector<Node*> &nodes);
1617
};
1718

1819
#endif // GENERATOR_H_

main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ int main(int argc, char* argv[])
2323
vector<Token> tokens = tokenizer.tokenize(argv[1]);
2424

2525
// 構文解析
26-
Node* node = parser.parse(tokens);
26+
vector<Node*> nodes = parser.parse(tokens);
2727

2828
// コード生成
29-
vector<Operation> code = generator.codegen(node);
29+
vector<Operation> code = generator.codegen(nodes);
3030

3131
// コード実行
3232
cout << vm.run(code) << endl;

parser.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77

88
using namespace std;
99

10-
Node* Parser::program(void)
10+
vector<Node*> Parser::program(void)
1111
{
12-
return add();
12+
vector<Node*> nodes;
13+
14+
nodes.push_back(add());
15+
16+
while(consume(",") || consume("\n")){
17+
nodes.push_back(add());
18+
}
19+
20+
return nodes;
1321
};
1422

1523
Node* Parser::num(void)
@@ -112,7 +120,7 @@ void Parser::expect(const char* str)
112120
}
113121
};
114122

115-
Node* Parser::parse(vector<Token>& tokens)
123+
vector<Node*> Parser::parse(vector<Token>& tokens)
116124
{
117125
token = tokens.begin();
118126
return program();

parser.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Parser
1111
private:
1212
vector<Token>::iterator token; // 現在注目しているトークン
1313

14-
Node* program(void);
14+
vector<Node*> program(void);
1515
Node* num(void);
1616
Node* add(void);
1717
Node* mul(void);
@@ -22,7 +22,7 @@ class Parser
2222
void expect(const char* str);
2323

2424
public:
25-
Node* parse(vector<Token>& tokens);
25+
vector<Node*> parse(vector<Token>& tokens);
2626
};
2727

2828
#endif // PARSER_HPP_

test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ test -3 "-5+2"
3232
test 5 "1++4"
3333
test 21 "(-10+3)*-3"
3434
test -7 "-(3+4)"
35+
test 12 "1+2, 4*3"
3536

3637
echo "OK"

tokenizer.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ vector<Token> Tokenizer::tokenize(char* input)
1313
vector<Token> tokens;
1414

1515
while(*input){
16-
if(isspace(*input)){
16+
if(strncmp(input, "\n", 1) == 0){
17+
Token token = { TK_RESERVED, input, 1 };
18+
tokens.push_back(token);
19+
input++;
20+
}
21+
else if(isspace(*input)){
1722
input++;
1823
}
1924
else if(strncmp(input, "+", 1) == 0){
@@ -46,6 +51,11 @@ vector<Token> Tokenizer::tokenize(char* input)
4651
tokens.push_back(token);
4752
input++;
4853
}
54+
else if(strncmp(input, ",", 1) == 0){
55+
Token token = { TK_RESERVED, input, 1 };
56+
tokens.push_back(token);
57+
input++;
58+
}
4959
else if(isdigit(*input)){
5060
Token token = { TK_NUM, input, 0 };
5161
for(; isdigit(*input); input++) token.len++;

vm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ DWORD VM::run(vector<Operation>& code)
6262
op++;
6363
}
6464

65-
// スタックトップの値を実行結果とする
66-
return pop();
65+
// GR0レジスタの値を実行結果とする
66+
return reg[REG_GR0];
6767
};

0 commit comments

Comments
 (0)