Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ y.tab.h
y.tab.c
lex.yy.c
bin/*
build/*
cmake-build-debug/*

.vscode/*
.idea/*
cmake-build-debug/*
1 change: 1 addition & 0 deletions compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Compiler *create_compiler()
compiler->func_definition_list = NULL;
compiler->current_block = NULL;
compiler->declaration_list = NULL;
compiler->stmt_list = NULL;
compiler->current_line_number = 0;
return compiler;
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Expression *alloc_identifier_expression(char *identifier)
Expression *expr = alloc_expression(IDENTIFIER_EXPRESSION);
IdentifierExpression *identifier_expr = (IdentifierExpression *)malloc(sizeof(IdentifierExpression));
identifier_expr->kind = 0;
identifier_expr->name = NULL;
identifier_expr->name = identifier;
expr->u.identifier = identifier_expr;
return expr;
}
Expand Down Expand Up @@ -238,6 +238,7 @@ Block *alloc_block(StatementList *list)
{
Block *block = (Block *)malloc(sizeof(Block));
block->statement_list = list;
block->declaration_list = NULL;
return block;
}

Expand Down
1 change: 1 addition & 0 deletions compiler/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ static ErrorDefinition dkc_error_message_format[] = {
{"类型转换不匹配。"},
{"函数调用类型不匹配。"},
{"变量声明类型与表达式类型不匹配。"},
{"变量赋值类型不匹配。"},
{"返回值类型不匹配."},
{"dummy"}};

Expand Down
3 changes: 2 additions & 1 deletion compiler/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ typedef enum
EOF_IN_C_COMMENT_ERR,
EOF_IN_STRING_LITERAL_ERR,
TOO_LONG_CHARACTER_LITERAL_ERR,
TYPE_CAST_MISMATH_ERR,
TYPE_CAST_MISMATCH_ERR,
FUNCTION_CALL_TYPE_MISMATCH_ERR,
DECLARATION_TYPE_MISMATCH_ERR,
ASSIGN_TYPE_MISMATCH_ERR,
RETURN_TYPE_MISMATCH_ERR,
COMPILE_ERROR_COUNT_PLUS_1,
} CompileError;
Expand Down
55 changes: 41 additions & 14 deletions compiler/fix_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
#include "summoner.h"
#include "error.h"
#include "../include/DBG.h"
#include "../include/share.h"

extern BuiltinFun *search_builtin_function(char *name);
static Expression *fix_expression(Block *current_block, Expression *expr);
static void fix_statement_list(Block *current_block, StatementList *list, FuncDefinition *fd);

Declaration *search_declaration_in_current_block(char *identifier, Block *block)
{
if (!block)
{
return NULL;
}

Declaration *d_pos;
for (d_pos = block->declaration_list; d_pos; d_pos = d_pos->next)
{
Expand Down Expand Up @@ -56,7 +60,6 @@ fix_identifier_expression(Block *current_block, Expression *expr)
{
Declaration *decl;
BuiltinFun *builtin_func;
Compiler *compiler = get_current_compiler();

decl = search_declaration(expr->u.identifier->name, current_block);
if (decl)
Expand Down Expand Up @@ -296,7 +299,7 @@ eval_compare_expression_int(Expression *expr, int left, int right)
}

static Expression *
eval_compare_expression_double(Expression *expr, int left, int right)
eval_compare_expression_double(Expression *expr, double left, double right)
{
if (expr->kind == EQ_EXPRESSION)
{
Expand Down Expand Up @@ -531,7 +534,7 @@ fix_type_cast_expression(Block *current_block, Expression *expr)
else
{
compile_error(expr->line_number,
TYPE_CAST_MISMATH_ERR,
TYPE_CAST_MISMATCH_ERR,
MESSAGE_ARGUMENT_END);
}
return expr;
Expand Down Expand Up @@ -571,7 +574,7 @@ fix_function_call_expression(Block *current_block, Expression *expr)
Expression *func_expr = fix_expression(current_block, expr->u.func_call_expression->function);
expr->u.func_call_expression->function = func_expr;

BuiltinFun *builtin_fun = expr->u.identifier->u.builtin_func;
BuiltinFun *builtin_fun = func_expr->u.identifier->u.builtin_func;
if (builtin_fun == NULL)
{
compile_error(expr->line_number,
Expand Down Expand Up @@ -676,6 +679,7 @@ fix_expression(Block *current_block, Expression *expr)
expr = fix_type_cast_expression(current_block, expr);
break;
case FUNC_CALL_EXPRESSION:
expr = fix_function_call_expression(current_block, expr);
break;
default:
DBG_assert(0, ("bad case. kind..%d\n", expr->kind));
Expand All @@ -684,8 +688,7 @@ fix_expression(Block *current_block, Expression *expr)
}

static void
add_local_variable(FuncDefinition *fd, Declaration *decl,
bool is_parameter)
add_local_variable(FuncDefinition *fd, Declaration *decl)
{
fd->local_variable = realloc(fd->local_variable,
sizeof(Declaration *) * (fd->local_variable_count + 1));
Expand All @@ -696,8 +699,7 @@ add_local_variable(FuncDefinition *fd, Declaration *decl,

static void
add_declaration(Block *current_block, Declaration *decl,
FuncDefinition *fd, int line_number,
bool is_parameter)
FuncDefinition *fd, int line_number)
{
if (search_declaration_in_current_block(decl->name, current_block))
{
Expand All @@ -714,11 +716,13 @@ add_declaration(Block *current_block, Declaration *decl,
if (fd)
{
decl->is_local = true;
add_local_variable(fd, decl, is_parameter);
add_local_variable(fd, decl);
}
else
{
Compiler *compiler = get_current_compiler();
decl->is_local = false;
compiler->declaration_list = chain_declaration_list(compiler->declaration_list, decl);
}
}

Expand Down Expand Up @@ -771,10 +775,10 @@ fix_return_statement(Block *current_block, Statement *statement, FuncDefinition
}

static void
fix_declaration_stmt(Statement *stmt, Block *current_block, FuncDefinition *fd)
fix_declaration_stmt(Block *current_block, Statement *stmt, FuncDefinition *fd)
{
Declaration *decl = stmt->u.decl_s;
add_declaration(current_block, decl, fd, stmt->line_number, false);
add_declaration(current_block, decl, fd, stmt->line_number);
if (decl->initializer)
{
decl->initializer = fix_expression(current_block, decl->initializer);
Expand All @@ -801,6 +805,26 @@ fix_declaration_stmt(Statement *stmt, Block *current_block, FuncDefinition *fd)
}
}

static void
fix_assign_stmt(Block *current_block, Statement *stmt)
{
AssignStatement *assign_s = stmt->u.assign_s;
assign_s->left = fix_expression(current_block, assign_s->left);
assign_s->operand = fix_expression(current_block, assign_s->operand);
if (assign_s->operand->kind == INT_EXPRESSION && assign_s->left->type->basic_type == DOUBLE_TYPE)
{
assign_s->operand->kind = DOUBLE_EXPRESSION;
assign_s->operand->type->basic_type = DOUBLE_TYPE;
assign_s->operand->u.double_value = assign_s->operand->u.int_value;
}
else if (assign_s->operand->type->basic_type != assign_s->left->type->basic_type)
{
compile_error(stmt->line_number,
ASSIGN_TYPE_MISMATCH_ERR,
MESSAGE_ARGUMENT_END);
}
}

static void
fix_statement(Block *current_block, Statement *statement, FuncDefinition *fd)
{
Expand All @@ -813,7 +837,10 @@ fix_statement(Block *current_block, Statement *statement, FuncDefinition *fd)
fix_return_statement(current_block, statement, fd);
break;
case DECLARATION_STATEMENT:
fix_declaration_stmt(statement, current_block, fd);
fix_declaration_stmt(current_block, statement, fd);
break;
case ASSIGN_STATEMENT:
fix_assign_stmt(current_block, statement);
break;
default:
DBG_assert(0, ("bad case. kind..%d\n", statement->kind));
Expand Down Expand Up @@ -842,7 +869,7 @@ add_parameter_as_declaration(FuncDefinition *fd)
decl = alloc_declaration(param->name, param->type, NULL);
if (fd->block)
{
add_declaration(fd->block, decl, fd, param->line_number, true);
add_declaration(fd->block, decl, fd, param->line_number);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ definition_list:
;

definition:
func_definition { $$ = alloc_definition(); add_func_definition_to_compiler($1); }
func_definition { $$ = alloc_definition(); add_func_definition_to_compiler($1); }
| const_stmt { $$ = alloc_definition(); add_stmt_to_compiler($1); }
| variable_declaration_stmt { $$ = alloc_definition(); add_stmt_to_compiler($1); }
;
Expand All @@ -89,9 +89,9 @@ parameter:
;

stmt_list:
stmt {$$ = chain_stmt_list(NULL, $1); }
| stmt_list new_line stmt {$$ = chain_stmt_list($1, $3); }
;
stmt {$$ = chain_stmt_list(NULL, $1); }
| stmt_list new_line stmt {$$ = chain_stmt_list($1, $3); }
;

stmt:
expr { $$ = alloc_expression_stmt($1); }
Expand Down
2 changes: 1 addition & 1 deletion compiler/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void open_string_literal(void)
st_string_literal_buffer_size = 0;
}

void add_string_literal(int letter)
void add_string_literal(char letter)
{
if (st_string_literal_buffer_size == st_string_literal_buffer_alloc_size)
{
Expand Down