Skip to content

Commit

Permalink
乗除算を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
corgi0901 committed Aug 2, 2020
1 parent 0946bf3 commit 3862655
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 4 deletions.
3 changes: 2 additions & 1 deletion BNF.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
program = add
add = num ("+" num | "-" num)*
add = mul ("+" mul | "-" mul)*
mul = num ("*" num | "/" num)*
20 changes: 20 additions & 0 deletions generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ vector<Operation> Generator::codegen(Node* node)

break;

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

operations.push_back( Operation{ OP_POP, REG_GR0 } );
operations.push_back( Operation{ OP_POP, REG_GR1 } );
operations.push_back( Operation{ OP_MUL, REG_GR0, REG_GR1 } );

break;

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

operations.push_back( Operation{ OP_POP, REG_GR0 } );
operations.push_back( Operation{ OP_POP, REG_GR1 } );
operations.push_back( Operation{ OP_DIV, REG_GR1, REG_GR0 } );

break;

default:
break;
}
Expand Down
23 changes: 20 additions & 3 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,31 @@ Node* Parser::num(void)

Node* Parser::add(void)
{
Node* node = num();
Node* node = mul();

while(1){
if(consume("+")){
node = new Node{ ND_ADD, node, num() };
node = new Node{ ND_ADD, node, mul() };
}
else if(consume("-")){
node = new Node{ ND_SUB, node, num() };
node = new Node{ ND_SUB, node, mul() };
}
else{
return node;
}
}
};

Node* Parser::mul(void)
{
Node* node = num();

while(1){
if(consume("*")){
node = new Node{ ND_MUL, node, num() };
}
else if(consume("/")){
node = new Node{ ND_DIV, node, num() };
}
else{
return node;
Expand Down
1 change: 1 addition & 0 deletions parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Parser
Node* program(void);
Node* num(void);
Node* add(void);
Node* mul(void);

bool consume(const char* str);

Expand Down
4 changes: 4 additions & 0 deletions rook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ typedef enum {
ND_NUM, // 数値
ND_ADD, // +
ND_SUB, // -
ND_MUL, // *
ND_DIV, // /
} NodeKind;

typedef struct Node Node;
Expand All @@ -42,6 +44,8 @@ typedef enum {
OP_POP, // Pop
OP_ADD, // 加算
OP_SUB, // 減算
OP_MUL, // 乗算
OP_DIV, // 除算
} OP_CODE;

typedef struct {
Expand Down
4 changes: 4 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ test 9 "7+2"
test 4 "10-6"
test 8 "12 + 4 - 8"
test 10 "12 - 4 + 2"
test 8 "2 * 4"
test 3 "12 / 4"
test 10 "5 * 3 - 5"
test 9 "2 + 2 * 5 - 3"

echo "OK"
10 changes: 10 additions & 0 deletions tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ vector<Token> Tokenizer::tokenize(char* input)
tokens.push_back(token);
input++;
}
else if(strncmp(input, "*", 1) == 0){
Token token = { TK_OP, input, 1 };
tokens.push_back(token);
input++;
}
else if(strncmp(input, "/", 1) == 0){
Token token = { TK_OP, 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
10 changes: 10 additions & 0 deletions vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ DWORD VM::run(vector<Operation>& code)
push(reg[op->operand] - reg[op->operand2]);
break;
}
case OP_MUL:
{
push(reg[op->operand] * reg[op->operand2]);
break;
}
case OP_DIV:
{
push(reg[op->operand] / reg[op->operand2]);
break;
}
default:
break;
}
Expand Down

0 comments on commit 3862655

Please sign in to comment.