Skip to content

Commit

Permalink
feat: impl ptr type
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 committed Nov 4, 2021
1 parent 641a3ea commit 8cf8ae9
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
9 changes: 8 additions & 1 deletion 50cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ struct Node {
int offset;
};

typedef struct Type Type;
struct Type {
enum { INT, PTR } ty;
struct Type *ptr_to;
};

extern Node *code[];

void program();
Expand All @@ -103,7 +109,7 @@ Node *add();
Node *mul();
Node *unary();
Node *primary();
Node * define_variable();
Node *define_variable();
Node *variable();

Node *new_node(NodeKind kind, Node *lhs, Node *rhs);
Expand All @@ -119,6 +125,7 @@ struct LVar {
char *name;
int len;
int offset;
Type *type;
};

extern LVar *locals[];
Expand Down
4 changes: 4 additions & 0 deletions codegen.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "50cc.h"

void gen_lval(Node *node) {
if (node->kind == ND_DEREF) {
gen(node->lhs);
return;
}
if (node->kind != ND_LVAR) error("代入の左辺値が変数ではありません");
printf(" mov rax, rbp\n");
printf(" sub rax, %d\n", node->offset);
Expand Down
Binary file modified codegen.o
Binary file not shown.
Binary file modified main.o
Binary file not shown.
29 changes: 22 additions & 7 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Node *func() {
error("type is not found");
}
Token *tok = consume_ident();

if (tok == NULL) {
error("there is no function");
}
Expand All @@ -127,10 +128,9 @@ Node *func() {
if (!consume_type()) {
error("type of args is not found");
}
Token *tok = consume_ident();
if (tok != NULL) {
node->args[i] = define_variable(tok);
}

node->args[i] = define_variable();

if (consume(")")) {
break;
}
Expand Down Expand Up @@ -218,8 +218,7 @@ Node *stmt() {
}

if (consume_type()) {
Token *tok = consume_ident();
node = define_variable(tok);
node = define_variable();
expect(";");
return node;
}
Expand Down Expand Up @@ -330,7 +329,22 @@ Node *primary() {
return new_node_num(expect_number());
}

Node *define_variable(Token *tok) {
Node *define_variable() {
Type *type;
type = calloc(1, sizeof(Type));
type->ty = INT;
type->ptr_to = NULL;
while (consume("*")) {
Type *ptr_type;
ptr_type = calloc(1, sizeof(Type));
ptr_type->ty = PTR;
ptr_type->ptr_to = type;
type = ptr_type;
}
Token *tok = consume_ident();
if (tok == NULL) {
error("invalid variable");
}
Node *node = calloc(1, sizeof(Node));
node->kind = ND_LVAR;

Expand All @@ -349,6 +363,7 @@ Node *define_variable(Token *tok) {
} else {
lvar->offset = locals[cur_func]->offset + 8;
}
lvar->type = type;
node->offset = lvar->offset;
locals[cur_func] = lvar;

Expand Down
Binary file modified parser.o
Binary file not shown.
40 changes: 26 additions & 14 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,37 @@ assert(){
fi
}

# define variables with type "int"
assert 5 "int main() {
# pointer
assert 3 "
int main(){
int x;
x = 5;
int *y;
y = &x;
*y = 3;
return x;
}"

assert 3 "int main() {
int a;
a = 4;
return fib(a);
}
int fib(int n){
if(n == 0) return 0;
if(n == 1) return 1;
return fib(n - 1)+ fib(n -2);
}
"


# define variables with type "int"
# assert 5 "int main() {
# int x;
# x = 5;
# return x;
# }"

# assert 3 "int main() {
# int a;
# a = 4;
# return fib(a);
# }
# int fib(int n){
# if(n == 0) return 0;
# if(n == 1) return 1;
# return fib(n - 1)+ fib(n -2);
# }
# "

# basic test case
# assert 2 "main(){return 2;}"
# assert 42 "main()return 42;"
Expand Down
Binary file modified tokenize.o
Binary file not shown.

0 comments on commit 8cf8ae9

Please sign in to comment.