Skip to content

Commit

Permalink
9cc unary operator version
Browse files Browse the repository at this point in the history
  • Loading branch information
mylifewithviolin committed May 4, 2023
1 parent 74e8900 commit 72dd79a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net6.0/9cc2cs.dll",
"args": ["1","5 + 6 * 7"],
"args": ["1","-10 + 20"],
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
Expand Down
112 changes: 101 additions & 11 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static int Main(string[] args)
// 抽象構文木を下りながらコード生成
gen(node);

// スタックトップに式全体の値が残っているはずなので
// スタックトップに式全体の値が残っているはずなので
// それをRAXにロードして関数からの返り値とする
Console.Write(" pop rax\n");

Expand Down Expand Up @@ -261,16 +261,30 @@ static int Main(string[] args)
// return node;
// }
// }
// Node *expr() {
// Node *node = mul();

// for (;;) {
// if (consume('+'))
// node = new_binary(ND_ADD, node, mul());
// else if (consume('-'))
// node = new_binary(ND_SUB, node, mul());
// else
// return node;
// }
// }
static Node expr(List<Token> tokenList,ref int curIndex) {
Node node = mul(tokenList,ref curIndex);
Token token = getToken(tokenList,curIndex);//次のトークン
for (;;) {
if (consume(token,"+",ref curIndex)){
node = new_node(NodeKind.ND_ADD, node, mul(tokenList,ref curIndex));
//node = new_node(NodeKind.ND_ADD, node, mul(tokenList,ref curIndex));
node = new_binary(NodeKind.ND_ADD, node, mul(tokenList,ref curIndex));
token = getToken(tokenList,curIndex);//次のトークン
}
else if (consume(token,"-",ref curIndex)){
node = new_node(NodeKind.ND_SUB, node, mul(tokenList,ref curIndex));
//node = new_node(NodeKind.ND_SUB, node, mul(tokenList,ref curIndex));
node = new_binary(NodeKind.ND_SUB, node, mul(tokenList,ref curIndex));
token = getToken(tokenList,curIndex);//次のトークン
}
else
Expand All @@ -290,22 +304,58 @@ static int Main(string[] args)
// return node;
// }
// }
// mul = unary ("*" unary | "/" unary)*
// Node *mul() {
// //Node *node = primary();
// Node *node = unary();
// for (;;) {
// if (consume('*'))
// node = new_binary(ND_MUL, node, unary());
// else if (consume('/'))
// node = new_binary(ND_DIV, node, unary());
// else
// return node;
// }
// }
static Node mul(List<Token> tokenList,ref int curIndex) {
Node node = primary(tokenList,ref curIndex);
//Node node = primary(tokenList,ref curIndex);
Node node = unary(tokenList,ref curIndex);
Token token = getToken(tokenList,curIndex);//次のトークン
for (;;) {
if (consume(token,"*",ref curIndex)){
node = new_node(NodeKind.ND_MUL, node, primary(tokenList,ref curIndex));
//node = new_node(NodeKind.ND_MUL, node, primary(tokenList,ref curIndex));
node = new_binary(NodeKind.ND_MUL, node, primary(tokenList,ref curIndex));
token = getToken(tokenList,curIndex);//次のトークン
}
else if (consume(token,"/",ref curIndex)){
node = new_node(NodeKind.ND_DIV, node, primary(tokenList,ref curIndex));
//node = new_node(NodeKind.ND_DIV, node, primary(tokenList,ref curIndex));
node = new_binary(NodeKind.ND_DIV, node, primary(tokenList,ref curIndex));
token = getToken(tokenList,curIndex);//次のトークン
}
else
return node;
}
}
// unary = ("+" | "-")? unary
// // | primary}
// Node *unary() {
// if (consume('+'))
// return unary();
// if (consume('-'))
// return new_binary(ND_SUB, new_num(0), unary());
// return primary();
// }
static Node unary(List<Token> tokenList,ref int curIndex) {
Token token = getToken(tokenList,curIndex);//次のトークン
if (consume(token,"+",ref curIndex)){
return unary(tokenList,ref curIndex);
}
else if (consume(token,"-",ref curIndex)){
return new_binary(NodeKind.ND_SUB,new_num(0), unary(tokenList,ref curIndex));
}
else
return primary(tokenList,ref curIndex);
}

// Node *primary() {
// // 次のトークンが"("なら、"(" expr ")"のはず
Expand All @@ -318,6 +368,15 @@ static int Main(string[] args)
// // そうでなければ数値のはず
// return new_node_num(expect_number());
// }
// Node *primary() {
// if (consume('(')) {
// Node *node = expr();
// expect(')');
// return node;
// }

// return new_num(expect_number());
// }
static Node primary(List<Token> tokenList,ref int curIndex) {
Token token = getToken(tokenList,curIndex);//次のトークン
// 次のトークンが"("なら、"(" expr ")"のはず
Expand All @@ -330,7 +389,8 @@ static int Main(string[] args)
}

// そうでなければ数値のはず
return new_node_num(expect_number(token,ref curIndex));
//return new_node_num(expect_number(token,ref curIndex));
return new_num(expect_number(token,ref curIndex));
}

static Token getToken(List<Token> tokenList,int curIndex) {
Expand All @@ -345,9 +405,27 @@ static int Main(string[] args)
// node->rhs = rhs;
// return node;
// }
static Node new_node(NodeKind kind, Node lhs, Node rhs) {
// Node *new_node(NodeKind kind) {
// Node *node = calloc(1, sizeof(Node));
// node->kind = kind;
// return node;
// }
// static Node new_node(NodeKind kind, Node lhs, Node rhs) {
static Node new_node(NodeKind kind) {
Node node = new();
node.kind = kind;
// node.lhs = lhs;
// node.rhs = rhs;
return node;
}
// Node *new_binary(NodeKind kind, Node *lhs, Node *rhs) {
// Node *node = new_node(kind);
// node->lhs = lhs;
// node->rhs = rhs;
// return node;
// }
static Node new_binary(NodeKind kind, Node lhs, Node rhs) {
Node node = new_node(kind);
node.lhs = lhs;
node.rhs = rhs;
return node;
Expand All @@ -359,12 +437,24 @@ static int Main(string[] args)
// node->val = val;
// return node;
// }
static Node new_node_num(int val) {
Node node = new();
node.kind = NodeKind.ND_NUM;
// static Node new_node_num(int val) {
// Node node = new();
// node.kind = NodeKind.ND_NUM;
// node.val = val;
// return node;
// }

// Node *new_num(int val) {
// Node *node = new_node(ND_NUM);
// node->val = val;
// return node;
// }
static Node new_num(int val) {
Node node = new_node(NodeKind.ND_NUM);
node.val = val;
return node;
}

// エラーを報告するための関数
// printfと同じ引数を取る
// void error(char *fmt, ...) {
Expand Down

0 comments on commit 72dd79a

Please sign in to comment.