Skip to content

Commit

Permalink
連続した式の実行に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
corgi0901 committed Aug 4, 2020
1 parent 3edccb3 commit 95c14f7
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 21 deletions.
2 changes: 1 addition & 1 deletion BNF.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
program = add
program = add ("," add | "\n" add)*
add = mul ("+" mul | "-" mul)*
mul = unary ("*" unary | "/" unary)*
unary = ("+" | "-")? primary
Expand Down
28 changes: 19 additions & 9 deletions generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

using namespace std;

vector<Operation> Generator::codegen(Node* node)
void Generator::gen(Node* node)
{
switch(node->kind){
case ND_NUM:
operations.push_back( Operation{ OP_PUSH, node->val } );
break;

case ND_ADD:
codegen(node->left);
codegen(node->right);
gen(node->left);
gen(node->right);

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

case ND_SUB:
codegen(node->left);
codegen(node->right);
gen(node->left);
gen(node->right);

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

case ND_MUL:
codegen(node->left);
codegen(node->right);
gen(node->left);
gen(node->right);

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

case ND_DIV:
codegen(node->left);
codegen(node->right);
gen(node->left);
gen(node->right);

operations.push_back( Operation{ OP_POP, REG_GR0 } );
operations.push_back( Operation{ OP_POP, REG_GR1 } );
Expand All @@ -55,6 +55,16 @@ vector<Operation> Generator::codegen(Node* node)
default:
break;
}
};

vector<Operation> Generator::codegen(vector<Node*> &nodes)
{
vector<Node*>::iterator node;

for(node = nodes.begin(); node != nodes.end(); node++){
gen(*node);
operations.push_back( Operation{ OP_POP, REG_GR0 } );
}

return operations;
};
3 changes: 2 additions & 1 deletion generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class Generator
{
private:
vector<Operation> operations;
void gen(Node* node);

public:
vector<Operation> codegen(Node* node);
vector<Operation> codegen(vector<Node*> &nodes);
};

#endif // GENERATOR_H_
4 changes: 2 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ int main(int argc, char* argv[])
vector<Token> tokens = tokenizer.tokenize(argv[1]);

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

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

// コード実行
cout << vm.run(code) << endl;
Expand Down
14 changes: 11 additions & 3 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@

using namespace std;

Node* Parser::program(void)
vector<Node*> Parser::program(void)
{
return add();
vector<Node*> nodes;

nodes.push_back(add());

while(consume(",") || consume("\n")){
nodes.push_back(add());
}

return nodes;
};

Node* Parser::num(void)
Expand Down Expand Up @@ -112,7 +120,7 @@ void Parser::expect(const char* str)
}
};

Node* Parser::parse(vector<Token>& tokens)
vector<Node*> Parser::parse(vector<Token>& tokens)
{
token = tokens.begin();
return program();
Expand Down
4 changes: 2 additions & 2 deletions parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Parser
private:
vector<Token>::iterator token; // 現在注目しているトークン

Node* program(void);
vector<Node*> program(void);
Node* num(void);
Node* add(void);
Node* mul(void);
Expand All @@ -22,7 +22,7 @@ class Parser
void expect(const char* str);

public:
Node* parse(vector<Token>& tokens);
vector<Node*> parse(vector<Token>& tokens);
};

#endif // PARSER_HPP_
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ test -3 "-5+2"
test 5 "1++4"
test 21 "(-10+3)*-3"
test -7 "-(3+4)"
test 12 "1+2, 4*3"

echo "OK"
12 changes: 11 additions & 1 deletion tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ vector<Token> Tokenizer::tokenize(char* input)
vector<Token> tokens;

while(*input){
if(isspace(*input)){
if(strncmp(input, "\n", 1) == 0){
Token token = { TK_RESERVED, input, 1 };
tokens.push_back(token);
input++;
}
else if(isspace(*input)){
input++;
}
else if(strncmp(input, "+", 1) == 0){
Expand Down Expand Up @@ -46,6 +51,11 @@ vector<Token> Tokenizer::tokenize(char* input)
tokens.push_back(token);
input++;
}
else if(strncmp(input, ",", 1) == 0){
Token token = { TK_RESERVED, input, 1 };
tokens.push_back(token);
input++;
}
else if(isdigit(*input)){
Token token = { TK_NUM, input, 0 };
for(; isdigit(*input); input++) token.len++;
Expand Down
4 changes: 2 additions & 2 deletions vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ DWORD VM::run(vector<Operation>& code)
op++;
}

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

0 comments on commit 95c14f7

Please sign in to comment.