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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
comp:
lex src/lex.l
yacc -d src/grammar.y
gcc -o bin/main y.tab.c lex.yy.c src/create.c src/interpreter.c src/compiler.c src/codegen.c main/main.c
lex compiler/lex.l
yacc -d compiler/grammar.y
gcc -o bin/main y.tab.c lex.yy.c compiler/create.c compiler/compiler.c compiler/codegen.c main/main.c
File renamed without changes.
7 changes: 4 additions & 3 deletions src/compiler.c → compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ static Compiler *st_current_compiler;

Compiler *create_compiler()
{
Compiler *compiler = malloc(sizeof(Compiler));
compiler->func_definition_list = malloc(sizeof(FuncDefinition));
Compiler *compiler = (Compiler *)malloc(sizeof(Compiler));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C编译器多隐转,也便于维护。报错的场景是sizeof的类型跟强转类型不一致吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原因是这样,我的解释器需要用STL,用c++写的,c++的编译器,malloc没有做转换编译会报错。而且我感觉后面开发很可能还是需要用到STL

compiler->func_definition_list = (FuncDefinition *)malloc(sizeof(FuncDefinition));
return compiler;
}

Expand All @@ -23,7 +23,8 @@ void set_current_compiler(Compiler *compiler)
void add_definitions_to_compiler(DefinitionList *definitions)
{
Compiler *compiler = get_current_compiler();
for (DefinitionList *pos = definitions; pos != NULL; pos = pos->next) {
for (DefinitionList *pos = definitions; pos != NULL; pos = pos->next)
{
switch (pos->definition->kind)
{
case FUNC_DEFINITION:
Expand Down
105 changes: 75 additions & 30 deletions src/create.c → compiler/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Expression *alloc_expression(ExpressionKind kind)
{
Expression *expr = malloc(sizeof(Expression));
Expression *expr = (Expression *)malloc(sizeof(Expression));
expr->kind = kind;
return expr;
}
Expand Down Expand Up @@ -47,32 +47,59 @@ Expression *alloc_unary_expression(ExpressionKind kind, Expression *unaryExpr)
Expression *alloc_binary_expression(ExpressionKind kind, Expression *left, Expression *right)
{
Expression *expr = alloc_expression(kind);
BinaryExpression *binary = malloc(sizeof(BinaryExpression));
BinaryExpression *binary = (BinaryExpression *)malloc(sizeof(BinaryExpression));
binary->left = left;
binary->right = right;
expr->u.binary_expression = binary;
return expr;
}

Statement *alloc_statement(StatementKind kind)
Expression *alloc_func_call_expression(char *identifier, ArgumentList *argument_list)
{
Statement *stmt = malloc(sizeof(Statement));
Expression *expr = alloc_expression(FUNC_CALL_EXPRESSION);
FuncCallExpression *func_call_e = (FuncCallExpression *)malloc(sizeof(FuncCallExpression));
func_call_e->identifier = identifier;
func_call_e->argument_list = argument_list;
expr->u.func_call_expression = func_call_e;
return expr;
}

ArgumentList *chain_argument_list(ArgumentList *list, Expression *expr)
{
ArgumentList *next = (ArgumentList *)malloc(sizeof(ArgumentList));
next->expr = expr;
next->next = NULL;

if (list == NULL)
{
return next;
}

list->next = next;
return list;
}

Statement *alloc_stmt(StatementKind kind)
{
Statement *stmt = (Statement *)malloc(sizeof(Statement));
stmt->kind = kind;
return stmt;
}

Statement *alloc_assign_statement(char *variable, Expression *operand)
Statement *alloc_assign_stmt(char *variable, Expression *operand)
{
Statement *stmt = alloc_statement(ASSIGN_STATEMENT);
AssignStatement *assign_s = malloc(sizeof(AssignStatement));
Statement *stmt = alloc_stmt(ASSIGN_STATEMENT);
AssignStatement *assign_s = (AssignStatement *)malloc(sizeof(AssignStatement));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外,这个赋值语句需不需要加filed表征类型。初始化是在赋值前的declaration_stmt里的?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,类型是在声明的时候,赋值你只能拿到一个变量名

assign_s->variable = variable;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

赋值之前是漏掉的么

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,之前漏了,发现了这个bug

assign_s->operand = operand;
stmt->u.assign_s = assign_s;
return stmt;
}

Statement *alloc_if_statement(Expression *condition, Block *then_block, Elseif *elseif_list, Block *else_block)
Statement *alloc_if_stmt(Expression *condition, Block *then_block, Elseif *elseif_list, Block *else_block)
{
Statement *stmt = alloc_statement(IF_STATEMENT);
IfStatement *if_s = malloc(sizeof(IfStatement));
Statement *stmt = alloc_stmt(IF_STATEMENT);
IfStatement *if_s = (IfStatement *)malloc(sizeof(IfStatement));
if_s->condition = condition;
if_s->then_block = then_block;
if_s->elseif_list = elseif_list;
Expand All @@ -83,44 +110,58 @@ Statement *alloc_if_statement(Expression *condition, Block *then_block, Elseif *

Statement *alloc_declaration_stmt(char *name, TypeSpecifier *type, Expression *initializer)
{
Statement *stmt = alloc_statement(DECLARATION_STATEMENT);
Declaration *decl_s = malloc(sizeof(Declaration));
Statement *stmt = alloc_stmt(DECLARATION_STATEMENT);
Declaration *decl_s = (Declaration *)malloc(sizeof(Declaration));
decl_s->name = name;
decl_s->type = type;
decl_s->initializer = initializer;
stmt->u.decl_s = decl_s;
return stmt;
}

Statement *alloc_block_statement(Block *block)
Statement *alloc_return_stmt(Expression *expr)
{
Statement *stmt = alloc_statement(BLOCK_STATEMENT);
stmt->u.block_s = block;
Statement *stmt = alloc_stmt(RETURN_STATEMENT);
stmt->u.expr_s = expr;
return stmt;
}

StatementList *alloc_statement_list(Statement *statement)
Statement* alloc_expression_stmt(Expression *expr)
{
StatementList *stmt_list = malloc(sizeof(StatementList));
stmt_list->statement = statement;
return stmt_list;
Statement *stmt = alloc_stmt(EXPRESSION_STATEMENT);
stmt->u.expr_s = expr;
return stmt;
}

Statement *alloc_block_stmt(Block *block)
{
Statement *stmt = alloc_stmt(BLOCK_STATEMENT);
stmt->u.block_s = block;
return stmt;
}

StatementList *chain_statement_list(StatementList *list, Statement *statement)
StatementList *chain_stmt_list(StatementList *list, Statement *statement)
{
StatementList *stmt_list = (StatementList *)malloc(sizeof(StatementList));
stmt_list->statement = statement;
stmt_list->next = NULL;

if (list == NULL)
{
return alloc_statement_list(statement);
return stmt_list;
}

StatementList *pos;
for (pos = list; pos->next; pos = pos->next)
;

pos->next = alloc_statement_list(statement);
pos->next = stmt_list;
return list;
}

Elseif *alloc_else_if(Expression *condition, Block *block)
{
Elseif *elseif = malloc(sizeof(Elseif));
Elseif *elseif = (Elseif *)malloc(sizeof(Elseif));
elseif->condition = condition;
elseif->block = block;
return elseif;
Expand Down Expand Up @@ -161,22 +202,22 @@ Block *close_block(Block *block, StatementList *stmt_list)

Block *alloc_block(StatementList *list)
{
Block *block = malloc(sizeof(Block));
Block *block = (Block *)malloc(sizeof(Block));
block->statement_list = list;
return block;
}

TypeSpecifier *alloc_type_specifier(BasicType type, char *identifier)
{
TypeSpecifier *type_s = malloc(sizeof(TypeSpecifier));
TypeSpecifier *type_s = (TypeSpecifier *)malloc(sizeof(TypeSpecifier));
type_s->basic_type = type;
type_s->identifier = identifier;
return type_s;
}

ParameterList *alloc_parameter(TypeSpecifier *type, char *identifier)
{
ParameterList *p = malloc(sizeof(ParameterList));
ParameterList *p = (ParameterList *)malloc(sizeof(ParameterList));
p->name = identifier;
p->type = type;
p->next = NULL;
Expand All @@ -198,14 +239,14 @@ ParameterList *chain_parameter(ParameterList *list, ParameterList *parameter)

Definition *alloc_definition(DefinitionKind kind)
{
Definition *definition = malloc(sizeof(Definition));
Definition *definition = (Definition *)malloc(sizeof(Definition));
definition->kind = kind;
return definition;
}

Definition *alloc_func_definition(char *name, ParameterList *parameters, TypeSpecifier *return_type, Block *block)
{
FuncDefinition *func_d = malloc(sizeof(FuncDefinition));
FuncDefinition *func_d = (FuncDefinition *)malloc(sizeof(FuncDefinition));
func_d->name = name;
func_d->parameters = parameters;
func_d->return_type = return_type;
Expand All @@ -224,7 +265,11 @@ FuncDefinition *chain_func_definition_list(FuncDefinition *list, FuncDefinition
return next;
}

list->next = next;
FuncDefinition *pos;
for (pos = list; pos->next; pos = pos->next)
;

pos->next = next;
return list;
}

Expand All @@ -237,7 +282,7 @@ Definition *alloc_declaration_definition(Statement *declaration_stmt)

DefinitionList *alloc_definition_list(Definition *definition)
{
DefinitionList *list = malloc(sizeof(DefinitionList));
DefinitionList *list = (DefinitionList *)malloc(sizeof(DefinitionList));
list->definition = definition;
list->next = NULL;
return list;
Expand Down
50 changes: 35 additions & 15 deletions src/grammar.y → compiler/grammar.y
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
%{
#include <stdio.h>
#include <stdlib.h>
#include "src/summoner.h"
#include "src/interpreter.h"
#include "compiler/summoner.h"

int yylex();
int yyerror(const char *s);
Expand All @@ -21,6 +20,7 @@ int yyerror(const char *s);
struct ParameterList *parameter_list;
struct Definition *definition;
struct DefinitionList *definition_list;
struct ArgumentList *argument_list;
}

%token <int_value> BOOL_LITERAL
Expand All @@ -30,15 +30,16 @@ int yyerror(const char *s);
%token VAR FUNCTION IF ELSE FOR RETURN BREAK CONTINUE NIL
%token BOOL_T INT_T DOUBLE_T STRING_T

%type <expression> expr bool_expr
%type <statement> stmt if_stmt declaration_stmt variable_declaration
%type <expression> expr bool_expr func_call_expr
%type <statement> stmt if_stmt declaration_stmt return_stmt variable_declaration
%type <statement_list> stmt_list
%type <block> block
%type <elseif> elseif elseif_list
%type <type_specifier> type_specifier
%type <parameter_list> parameter_list paramter
%type <definition> definition func_definition
%type <definition_list> definition_list
%type <argument_list> argument_list

%nonassoc '=' DECL_ASSIGN
%left AND OR
Expand Down Expand Up @@ -70,7 +71,10 @@ definition:

func_definition:
FUNCTION IDENTIFIER '(' parameter_list ')' type_specifier block { $$ = alloc_func_definition($2, $4, $6, $7); }
;
| FUNCTION IDENTIFIER '(' ')' type_specifier block { $$ = alloc_func_definition($2, NULL, $5, $6); }
| FUNCTION IDENTIFIER '(' parameter_list ')' block { $$ = alloc_func_definition($2, $4, NULL, $6); }
| FUNCTION IDENTIFIER '(' ')' block { $$ = alloc_func_definition($2, NULL, NULL, $5); }
;

parameter_list:
paramter
Expand All @@ -82,16 +86,17 @@ paramter:
;

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

stmt:
expr { printExprValue(evalExpression($1)); }
| IDENTIFIER '=' expr { $$ = alloc_assign_statement($1, $3); }
| block { $$ = alloc_block_statement($1); }
IDENTIFIER '=' expr { $$ = alloc_assign_stmt($1, $3); }
| expr { $$ = alloc_expression_stmt($1); }
| block { $$ = alloc_block_stmt($1); }
| if_stmt
| declaration_stmt
| return_stmt
;

declaration_stmt:
Expand All @@ -109,14 +114,18 @@ type_specifier:
BOOL_T { $$ = alloc_type_specifier(BOOLEAN_TYPE, NULL); }
| INT_T { $$ = alloc_type_specifier(INT_TYPE, NULL); }
| DOUBLE_T { $$ = alloc_type_specifier(DOUBLE_TYPE, NULL); }
| STRING_T { $$ = alloc_type_specifier(STRING_T, NULL); }
| STRING_T { $$ = alloc_type_specifier(STRING_TYPE, NULL); }
;

return_stmt:
RETURN expr { $$ = alloc_return_stmt($2); }
;

if_stmt:
IF bool_expr block { $$ = alloc_if_statement($2, $3, NULL, NULL); }
| IF bool_expr block ELSE block { $$ = alloc_if_statement($2, $3, NULL, $5); }
| IF bool_expr block elseif_list { $$ = alloc_if_statement($2, $3, $4, NULL); }
| IF bool_expr block elseif_list ELSE block { $$ = alloc_if_statement($2, $3, $4, $6); }
IF bool_expr block { $$ = alloc_if_stmt($2, $3, NULL, NULL); }
| IF bool_expr block ELSE block { $$ = alloc_if_stmt($2, $3, NULL, $5); }
| IF bool_expr block elseif_list { $$ = alloc_if_stmt($2, $3, $4, NULL); }
| IF bool_expr block elseif_list ELSE block { $$ = alloc_if_stmt($2, $3, $4, $6); }
;

elseif_list:
Expand Down Expand Up @@ -153,6 +162,7 @@ expr:
| '-' expr %prec MINUS { $$ = alloc_unary_expression(MINUS_EXPRESSION, $2); }
| '(' expr ')' { $$ = $2; }
| bool_expr
| func_call_expr
;

bool_expr:
Expand All @@ -167,6 +177,16 @@ bool_expr:
| '!' expr %prec NOT { $$ = alloc_unary_expression(NOT_EXPRESSION, $2); }
;

func_call_expr:
IDENTIFIER '(' ')' { $$ = alloc_func_call_expression($1, NULL); }
| IDENTIFIER '(' argument_list ')' { $$ = alloc_func_call_expression($1, $3); }
;

argument_list:
expr { $$ = chain_argument_list(NULL, $1); }
| argument_list ',' expr { $$ = chain_argument_list($1, $3); }
;

new_line:
'\n'
| new_line '\n'
Expand Down
2 changes: 1 addition & 1 deletion src/lex.l → compiler/lex.l
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}

[A-Za-z_][A-Za-z_0-9]* {
char *new_str = malloc(strlen(yytext) + 1);
char *new_str = (char *)malloc(strlen(yytext) + 1);
strcpy(new_str, yytext);
yylval.identifier = new_str;
return IDENTIFIER;
Expand Down
Loading