diff options
| author | Nagi5Yeq <wu@x-nagi.com> | 2020-05-13 00:40:23 +0800 |
|---|---|---|
| committer | Nagi5Yeq <wu@x-nagi.com> | 2020-05-13 00:40:23 +0800 |
| commit | c7d52437db42232d98e585a52ed2fdf75eb7f1af (patch) | |
| tree | 46f6c75f49de6c53937b53266fcf153cc0f8c510 | |
| parent | 61d670dda6f95aa414b4ea4a7dcaba25890b9e6b (diff) | |
code refactoring
| -rw-r--r-- | Driver.cc | 1 | ||||
| -rw-r--r-- | Driver.hh | 5 | ||||
| -rw-r--r-- | Lexer.hh | 21 | ||||
| -rw-r--r-- | Lexer.l | 26 | ||||
| -rw-r--r-- | Parser.y | 66 | ||||
| -rw-r--r-- | node/BaseNode.hh | 7 | ||||
| -rw-r--r-- | node/LiteralNode.cc | 2 | ||||
| -rw-r--r-- | node/LiteralNode.hh | 4 | ||||
| -rw-r--r-- | node/Node.hh | 8 |
9 files changed, 79 insertions, 61 deletions
@@ -1,4 +1,5 @@ #include "Driver.hh" +#include "Parser.hh" namespace pcc { Driver::Driver(Context* context) @@ -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<StringLiteralNode>(&driver, s), loc); +} +} // namespace Lexer +} // namespace pcc + +#endif // !PCC_LEXER_H @@ -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<char>(std::tolower(static_cast<unsigned char>(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 @@ -3,11 +3,13 @@ %code requires { #include <string> +#include <memory> #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 <int> INTEGER_LITERAL %type <float> REAL_LITERAL -%type <char*> STRING_LITERAL %type <char*> IDENTIFIER +%type <std::shared_ptr<pcc::StringLiteralNode>> 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 <llvm/IR/Value.h> -#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 <string> + #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 |
