From f1a2446f9b826b8c36951354743ab7642e1db293 Mon Sep 17 00:00:00 2001 From: shenao78 Date: Tue, 17 Aug 2021 12:47:20 +0800 Subject: [PATCH] fix_new_line_in_definition --- compiler/create.c | 10 ++++++++++ compiler/grammar.y | 33 +++++++++++++++++++++++---------- compiler/summoner.h | 6 ++++++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/compiler/create.c b/compiler/create.c index 877aa26..2d091ff 100644 --- a/compiler/create.c +++ b/compiler/create.c @@ -302,6 +302,16 @@ FuncDefinition *chain_func_definition_list(FuncDefinition *list, FuncDefinition return list; } +Definition *alloc_definition() +{ + return (Definition *) malloc(sizeof(Definition)); +} + +DefinitionList *alloc_definition_list() +{ + return (DefinitionList *) malloc(sizeof(DefinitionList)); +} + Declaration *chain_declaration_list(Declaration *list, Declaration *next) { if (list == NULL) diff --git a/compiler/grammar.y b/compiler/grammar.y index 7861881..731c30c 100644 --- a/compiler/grammar.y +++ b/compiler/grammar.y @@ -19,6 +19,8 @@ int yyerror(const char *s); struct Block *block; struct Elseif *elseif; struct FuncDefinition *func_definition; + struct Definition *definition; + struct DefinitionList *definition_list; struct TypeSpecifier *type_specifier; struct ParameterList *parameter_list; struct ArgumentList *argument_list; @@ -38,8 +40,10 @@ int yyerror(const char *s); %type block %type elseif elseif_list %type func_definition +%type definition +%type definition_list %type type_specifier -%type parameter_list paramter +%type parameter_list parameter %type argument_list %nonassoc '=' ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN DECL_ASSIGN @@ -53,11 +57,20 @@ int yyerror(const char *s); %% -definition_or_stmt: - func_definition { add_func_definition_to_compiler($1); } - | const_stmt { add_stmt_to_compiler($1); } - | variable_declaration_stmt { add_stmt_to_compiler($1); } - ; +translation_unit: + new_line_option definition_list new_line_option + ; + +definition_list: + definition { $$ = alloc_definition_list(); } + | definition_list new_line definition + ; + +definition: + 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); } + ; func_definition: FUNCTION IDENTIFIER '(' parameter_list ')' type_specifier block { $$ = alloc_func_definition($2, $4, $6, $7); } @@ -67,12 +80,12 @@ func_definition: ; parameter_list: - paramter - | parameter_list ',' paramter { $$ = chain_parameter($1, $3); } + parameter + | parameter_list ',' parameter { $$ = chain_parameter($1, $3); } ; -paramter: - IDENTIFIER type_specifier { $$ = alloc_parameter($2, $1); } +parameter: + IDENTIFIER type_specifier { $$ = alloc_parameter($2, $1); } ; stmt_list: diff --git a/compiler/summoner.h b/compiler/summoner.h index afed3c9..9fb53f3 100644 --- a/compiler/summoner.h +++ b/compiler/summoner.h @@ -247,6 +247,12 @@ FuncDefinition *alloc_func_definition(char *name, ParameterList *parameters, Typ FuncDefinition *chain_func_definition_list(FuncDefinition *list, FuncDefinition *next); Declaration *chain_declaration_list(Declaration *list, Declaration *declaration); +typedef struct Definition {} Definition; +typedef struct DefinitionList {} DefinitionList; + +Definition *alloc_definition(); +DefinitionList *alloc_definition_list(); + typedef wchar_t SVM_Char; typedef unsigned char SVM_Byte;