From c7d52437db42232d98e585a52ed2fdf75eb7f1af Mon Sep 17 00:00:00 2001 From: Nagi5Yeq Date: Wed, 13 May 2020 00:40:23 +0800 Subject: code refactoring --- Driver.cc | 1 + Driver.hh | 5 ---- Lexer.hh | 21 +++++++++++++++++ Lexer.l | 26 +++++++++------------ Parser.y | 66 +++++++++++++++++++++++++---------------------------- node/BaseNode.hh | 7 +++--- node/LiteralNode.cc | 2 +- node/LiteralNode.hh | 4 +++- node/Node.hh | 8 +++++++ 9 files changed, 79 insertions(+), 61 deletions(-) create mode 100644 Lexer.hh create mode 100644 node/Node.hh diff --git a/Driver.cc b/Driver.cc index a0e2815..6c3509d 100644 --- a/Driver.cc +++ b/Driver.cc @@ -1,4 +1,5 @@ #include "Driver.hh" +#include "Parser.hh" namespace pcc { Driver::Driver(Context* context) diff --git a/Driver.hh b/Driver.hh index 042db1f..5f0d18d 100644 --- a/Driver.hh +++ b/Driver.hh @@ -5,11 +5,6 @@ #include "Context.hh" #include "Location.hh" -#include "Parser.hh" - -#define YY_DECL pcc::Parser::symbol_type yylex(pcc::Driver& driver) - -YY_DECL; namespace pcc { class Driver { diff --git a/Lexer.hh b/Lexer.hh new file mode 100644 index 0000000..1558d14 --- /dev/null +++ b/Lexer.hh @@ -0,0 +1,21 @@ +#ifndef PCC_LEXER_H +#define PCC_LEXER_H + +#include "Node.hh" +#include "Parser.hh" + +#define YY_DECL pcc::Parser::symbol_type yylex(pcc::Driver& driver) + +YY_DECL; + +namespace pcc { +namespace Lexer { +Parser::symbol_type make_STRING_LITERAL(char* s, pcc::Driver& driver, + const Parser::location_type& loc) { + return Parser::make_STRING_LITERAL( + std::make_shared(&driver, s), loc); +} +} // namespace Lexer +} // namespace pcc + +#endif // !PCC_LEXER_H diff --git a/Lexer.l b/Lexer.l index aef2259..cd1b634 100644 --- a/Lexer.l +++ b/Lexer.l @@ -7,13 +7,12 @@ #include "Log.hh" #include "Driver.hh" -#include "Location.hh" +#include "Lexer.hh" using pcc::Parser; using pcc::Location; namespace pcc { - class Lexer; char ToLower(char c){ return static_cast(std::tolower(static_cast(c))); } @@ -175,7 +174,7 @@ white [\t\f\r ] } } *p++='\0'; - return Parser::make_STRING_LITERAL(str, loc); + return pcc::Lexer::make_STRING_LITERAL(str, driver, loc); } {identifier} { @@ -196,17 +195,14 @@ white [\t\f\r ] %% namespace pcc { - int Driver::StartLexer(std::string &filename) - { - yyin=std::fopen(filename.c_str(),"r"); - if(yyin==NULL){ - Log(PCC_ERROR, "Failed to open file %s", filename.c_str()); - return -1; - } - return 0; - } - - void Driver::StopLexer(){ - std::fclose(yyin); +int Driver::StartLexer(std::string& filename) { + yyin = std::fopen(filename.c_str(), "r"); + if (yyin == NULL) { + Log(PCC_ERROR, "Failed to open file %s", filename.c_str()); + return -1; } + return 0; } + +void Driver::StopLexer() { std::fclose(yyin); } +} // namespace pcc diff --git a/Parser.y b/Parser.y index 56a9a9d..522bd78 100644 --- a/Parser.y +++ b/Parser.y @@ -3,11 +3,13 @@ %code requires { #include +#include #include "Location.hh" namespace pcc { class Driver; + class StringLiteralNode; } } @@ -17,6 +19,10 @@ namespace pcc { %code { #include "Driver.hh" +#include "Node.hh" + +#define YY_DECL pcc::Parser::symbol_type yylex(pcc::Driver& driver) +YY_DECL; } %define api.namespace {pcc} @@ -37,50 +43,52 @@ namespace pcc { %type INTEGER_LITERAL %type REAL_LITERAL -%type STRING_LITERAL %type IDENTIFIER +%type > STRING_LITERAL + %% /* ==================[global part]================== */ program - : program_decl global_decls function_decls + : program_header decls functions ; -program_decl +program_header : PROGRAM IDENTIFIER SEMICOLON ; -global_decls - : var_block +decls + : decls decl SEMICOLON + | ; -function_decls - : function_decls function_decl +decl + : VAR var_decls SEMICOLON + ; + +functions + : functions function SEMICOLON | ; /* ==================[var part]================== */ -var_block - : VAR var_decls SEMICOLON - ; - var_decls : var_decls SEMICOLON var_decl | var_decl ; var_decl - : vars COLON var_type + : vars COLON type ; vars : vars COMMA IDENTIFIER | IDENTIFIER -var_type +type : INTEGER | REAL | BOOLEAN @@ -90,47 +98,43 @@ var_type /* ==================[function part]================== */ -function_decl - : function_header function_body +function + : function_header decls statement_block ; function_header - : FUNCTION IDENTIFIER LPARENTHESIS var_decls RPARENTHESIS COLON var_type SEMICOLON - ; - -function_body - : var_block statement_block SEMICOLON - | statement_block SEMICOLON + : FUNCTION IDENTIFIER LPARENTHESIS var_decls RPARENTHESIS COLON type SEMICOLON ; /* ==================[statements part]================== */ statements - : statements statement SEMICOLON - | + : statements SEMICOLON statement + | statement ; statement - : closed_statement - | open_statement + : open_statement + | closed_statement ; open_statement : IF expression THEN statement | IF expression THEN closed_statement ELSE open_statement + | WHILE expression DO open_statement ; closed_statement : normal_statement | IF expression THEN closed_statement ELSE closed_statement + | WHILE expression DO closed_statement ; normal_statement : statement_block | empty_statement | assign_statement - | while_statement - | expression_statement + | expression ; statement_block @@ -145,14 +149,6 @@ assign_statement : lvalue ASSIGN expression ; -while_statement - : WHILE expression DO statement - ; - -expression_statement - : function_call - ; - /* ==================[expression part]================== */ expression diff --git a/node/BaseNode.hh b/node/BaseNode.hh index ae0ccea..60e25bd 100644 --- a/node/BaseNode.hh +++ b/node/BaseNode.hh @@ -1,12 +1,11 @@ -#ifndef PCC_BASE_H -#define PCC_BASE_H +#ifndef PCC_BASENODE_H +#define PCC_BASENODE_H #include -#include "Driver.hh" - namespace pcc { using Value = llvm::Value*; +class Driver; class BaseNode { public: diff --git a/node/LiteralNode.cc b/node/LiteralNode.cc index 57cc65b..dd84ff9 100644 --- a/node/LiteralNode.cc +++ b/node/LiteralNode.cc @@ -1,4 +1,4 @@ -#include "LiteralNode.hh" +#include "Node.hh" namespace pcc { StringLiteralNode::StringLiteralNode(Driver* driver, char* value) diff --git a/node/LiteralNode.hh b/node/LiteralNode.hh index 2e9b43a..4f9a61a 100644 --- a/node/LiteralNode.hh +++ b/node/LiteralNode.hh @@ -1,6 +1,8 @@ #ifndef PCC_LITERALNODE_H #define PCC_LITERALNODE_H +#include + #include "BaseNode.hh" namespace pcc { @@ -10,7 +12,7 @@ class StringLiteralNode : public BaseNode { Value CodeGen() override; private: - char* value_; + std::string value_; }; } // namespace pcc diff --git a/node/Node.hh b/node/Node.hh new file mode 100644 index 0000000..3a02689 --- /dev/null +++ b/node/Node.hh @@ -0,0 +1,8 @@ +#ifndef PCC_NODE_H +#define PCC_NODE_H + +#include "Driver.hh" +#include "BaseNode.hh" +#include "LiteralNode.hh" + +#endif // !PCC_NODE_H -- cgit