Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile error: redefinition of enumerator #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@
#define AST_H

enum {
BE_LT, BE_LTE,
BE_GT, BE_GTE,
BE_EQ, BE_NEQ,
BE_ADD, BE_SUB,
BE_MUL, BE_DIV,
K_NUM, K_CALL,
E_ASSIGN, VAR_NAME,
ARRAY_ACCESS, RETURN,
WHILE, IF_THEN,
COMPOUND, PARAM,
FUNCTION, VAR_DECL
TOKEN_BE_LT,
TOKEN_BE_LTE,
TOKEN_BE_GT,
TOKEN_BE_GTE,
TOKEN_BE_EQ,
TOKEN_BE_NEQ,
TOKEN_BE_ADD,
TOKEN_BE_SUB,
TOKEN_BE_MUL,
TOKEN_BE_DIV,
TOKEN_K_NUM,
TOKEN_K_CALL,
TOKEN_E_ASSIGN,
TOKEN_VAR_NAME,
TOKEN_ARRAY_ACCESS,
TOKEN_RETURN,
TOKEN_WHILE,
TOKEN_IF_THEN,
TOKEN_COMPOUND,
TOKEN_PARAM,
TOKEN_FUNCTION,
TOKEN_VAR_DECL
} kind;

struct list; // forward declaration
Expand Down
44 changes: 22 additions & 22 deletions ast_cons.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,57 @@ static AST* make_binexpr(int lineno, int type, AST* lhs, AST* rhs)

AST* lt(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_LT, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_LT, lhs, rhs);
}

AST* lte(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_LTE, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_LTE, lhs, rhs);
}

AST* gt(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_GT, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_GT, lhs, rhs);
}

AST* gte(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_GTE, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_GTE, lhs, rhs);
}

AST* eq(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_EQ, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_EQ, lhs, rhs);
}

AST* neq(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_NEQ, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_NEQ, lhs, rhs);
}

AST* add(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_ADD, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_ADD, lhs, rhs);
}

AST* sub(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_SUB, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_SUB, lhs, rhs);
}

AST* mul(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_MUL, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_MUL, lhs, rhs);
}

AST* divide(AST* lhs, AST* rhs)
{
return make_binexpr(yylineno, BE_DIV, lhs, rhs);
return make_binexpr(yylineno, TOKEN_BE_DIV, lhs, rhs);
}

AST* call(char* name, list* args)
{
AST* ast = make_ast(yylineno, K_CALL);
AST* ast = make_ast(yylineno, TOKEN_K_CALL);
//printf(name);
ast->d.call.name_ = name;
ast->d.call.args_ = args;
Expand All @@ -82,52 +82,52 @@ AST* call(char* name, list* args)

AST* assign(AST* to, AST* expr)
{
AST* temp = make_ast(yylineno, E_ASSIGN);
AST* temp = make_ast(yylineno, TOKEN_E_ASSIGN);
temp->d.assign.to_ = to;
temp->d.assign.expr_ = expr;
return temp;
}

AST* variable(char* name)
{
AST* temp = make_ast(yylineno, VAR_NAME);
AST* temp = make_ast(yylineno, TOKEN_VAR_NAME);
temp->d.variable.name_ = name;
return temp;
}

AST* num(int val)
{
AST* ast = make_ast(yylineno, K_NUM);
AST* ast = make_ast(yylineno, TOKEN_K_NUM);
ast->d.val_ = val;
return ast;
}

AST* access(char* name, AST* expr)
{
AST* temp = make_ast(yylineno, ARRAY_ACCESS);
AST* temp = make_ast(yylineno, TOKEN_ARRAY_ACCESS);
temp->d.access.name_ = name;
temp->d.access.expr_ = expr;
return temp;
}

AST* ret(AST *expr)
{
AST* temp = make_ast(yylineno, RETURN);
AST* temp = make_ast(yylineno, TOKEN_RETURN);
temp->d.ret.expr_ = expr;
return temp;
}

AST* while_loop(AST* expr, AST* stmt)
{
AST* temp = make_ast(yylineno, WHILE);
AST* temp = make_ast(yylineno, TOKEN_WHILE);
temp->d.while_loop.expr_ = expr;
temp->d.while_loop.stmt_ = stmt;
return temp;
}

AST* if_then(AST* expr, AST* if_stmt, AST* else_stmt)
{
AST* temp = make_ast(yylineno, IF_THEN);
AST* temp = make_ast(yylineno, TOKEN_IF_THEN);
temp->d.if_then.expr_ = expr;
temp->d.if_then.if_stmt_ = if_stmt;
temp->d.if_then.else_stmt_ = else_stmt;
Expand All @@ -136,15 +136,15 @@ AST* if_then(AST* expr, AST* if_stmt, AST* else_stmt)

AST* compound_stmt(list* decls, list* stmts)
{
AST* temp = make_ast(yylineno, COMPOUND);
AST* temp = make_ast(yylineno, TOKEN_COMPOUND);
temp->d.compound.decls_ = decls;
temp->d.compound.stmts_ = stmts;
return temp;
}

AST* param(int type_spec, char* name, int ref)
{
AST* temp = make_ast(yylineno, PARAM);
AST* temp = make_ast(yylineno, TOKEN_PARAM);
temp->d.param.type_spec_ = type_spec;
temp->d.param.name_ = name;
temp->d.param.ref_ = ref;
Expand All @@ -153,7 +153,7 @@ AST* param(int type_spec, char* name, int ref)

AST* function(int ret_type, char* name, list* params, AST* body)
{
AST* temp = make_ast(yylineno, FUNCTION);
AST* temp = make_ast(yylineno, TOKEN_FUNCTION);
temp->d.function.ret_type_ = ret_type;
temp->d.function.name_ = name;
temp->d.function.params_ = params;
Expand All @@ -163,7 +163,7 @@ AST* function(int ret_type, char* name, list* params, AST* body)

AST* var_decl(int type_spec, char* name, int size, int ref)
{
AST* temp = make_ast(yylineno, VAR_DECL);
AST* temp = make_ast(yylineno, TOKEN_VAR_DECL);
temp->d.var_decl.type_spec_ = type_spec;
temp->d.var_decl.name_ = name;
temp->d.var_decl.size_ = size;
Expand Down
64 changes: 32 additions & 32 deletions cgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,66 +28,66 @@ static void codegen_(AST* node)
{
switch (node->kind_)
{
case K_NUM:
case TOKEN_K_NUM:
{ char buf[15];
sprintf(buf,"%d", node->d.val_);
emit3("mov", "eax", buf);
} break;
// FALL THROUGH
case BE_LT:
case BE_LTE:
case BE_GT:
case BE_GTE:
case BE_EQ:
case BE_NEQ:
case BE_ADD:
case BE_SUB:
case BE_MUL:
case BE_DIV:
case TOKEN_BE_LT:
case TOKEN_BE_LTE:
case TOKEN_BE_GT:
case TOKEN_BE_GTE:
case TOKEN_BE_EQ:
case TOKEN_BE_NEQ:
case TOKEN_BE_ADD:
case TOKEN_BE_SUB:
case TOKEN_BE_MUL:
case TOKEN_BE_DIV:
codegen_bexpr(node);
break;

case K_CALL:
case TOKEN_K_CALL:
codegen(node->d.call.args_);
break;

case E_ASSIGN:
case TOKEN_E_ASSIGN:
codegen_assign(node);
break;

case ARRAY_ACCESS:
case TOKEN_ARRAY_ACCESS:
codegen_array_access(node);
break;

case VAR_NAME:
case TOKEN_VAR_NAME:
codegen_variable(node);
break;

case RETURN:
case TOKEN_RETURN:
codegen_ret(node);
break;

case WHILE:
case TOKEN_WHILE:
codegen_while(node);
break;

case IF_THEN:
case TOKEN_IF_THEN:
codegen_if_then(node);
break;

case COMPOUND:
case TOKEN_COMPOUND:
codegen_compound(node);
break;

case PARAM:
case TOKEN_PARAM:
codegen_param(node);
break;

case FUNCTION:
case TOKEN_FUNCTION:
codegen_function(node);
break;

case VAR_DECL:
case TOKEN_VAR_DECL:
codegen_var_decl(node);
break;

Expand All @@ -108,40 +108,40 @@ static void codegen_bexpr(AST* node)

switch(node->kind_)
{
case BE_LT:
case TOKEN_BE_LT:
emit3("cmp","eax","ebx");
emit2("setl","esi");
break;
case BE_LTE:
case TOKEN_BE_LTE:
emit3("cmp","eax","ebx");
emit2("setle","esi");
break;
case BE_GT:
case TOKEN_BE_GT:
emit3("cmp","eax","ebx");
emit2("setg","esi");
break;
case BE_GTE:
case TOKEN_BE_GTE:
emit3("cmp","eax","ebx");
emit2("setge","esi");
break;
case BE_EQ:
case TOKEN_BE_EQ:
emit3("cmp","eax","ebx");
emit2("sete","esi");
break;
case BE_NEQ:
case TOKEN_BE_NEQ:
emit3("cmp","eax","ebx");
emit2("setne","esi");
break;
case BE_ADD:
case TOKEN_BE_ADD:
emit3("add","eax","ebx");
break;
case BE_SUB:
case TOKEN_BE_SUB:
emit3("sub","eax","ebx");
break;
case BE_MUL:
case TOKEN_BE_MUL:
emit3("imul","eax","ebx");
break;
case BE_DIV:
case TOKEN_BE_DIV:
emit3("idiv","eax","ebx");
break;
default:
Expand Down
Loading