summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNagi5Yeq <wu@x-nagi.com>2020-05-29 00:17:00 +0800
committerNagi5Yeq <wu@x-nagi.com>2020-05-29 00:17:00 +0800
commitf15a071f6d599d2a21083dfb041b18db8d482949 (patch)
treea1b33b6ffe36f14a990dcfe5b9c5e593703d7ae0
parentd4e6ae6f8237f18d9d0b51e7a6404118dffb709f (diff)
remove ListNode, use std::move in AST building
-rw-r--r--Parser.y12
-rw-r--r--node/BaseNode.hh11
-rw-r--r--node/StatementNode.cc9
-rw-r--r--node/StatementNode.hh10
4 files changed, 23 insertions, 19 deletions
diff --git a/Parser.y b/Parser.y
index 8db651e..1aba94e 100644
--- a/Parser.y
+++ b/Parser.y
@@ -91,7 +91,7 @@ program_header
;
global_decls
- : global_decls global_decl {$$=$1; $$.push_back($2);}
+ : global_decls global_decl {$$=std::move($1); $$.push_back($2);}
| {$$=std::list<std::shared_ptr<pcc::BaseNode>>();}
;
@@ -100,14 +100,14 @@ global_decl
;
functions
- : functions function SEMICOLON {$$=$1; $$.push_back($2);}
+ : functions function SEMICOLON {$$=std::move($1); $$.push_back($2);}
| {$$=std::list<std::shared_ptr<pcc::FunctionNode>>();}
;
/* ==================[var part]================== */
var_decls
- : var_decls SEMICOLON var_decl {$$=$1; auto NewChilds=std::get<0>($3); for(auto& name: NewChilds){$$.push_back({name, std::get<1>($3)});}}
+ : var_decls SEMICOLON var_decl {$$=std::move($1); auto NewChilds=std::get<0>($3); for(auto& name: NewChilds){$$.push_back({name, std::get<1>($3)});}}
| var_decl {auto NewChilds=std::get<0>($1); for(auto& name: NewChilds){$$.push_back({name, std::get<1>($1)});}}
;
@@ -116,7 +116,7 @@ var_decl
;
vars
- : vars COMMA IDENTIFIER {$$=$1; $$.push_back($3);}
+ : vars COMMA IDENTIFIER {$$=std::move($1); $$.push_back($3);}
| IDENTIFIER {$$.push_back($1);}
;
@@ -139,7 +139,7 @@ function
;
local_decls
- : local_decls local_decl {$$=$1; $$.push_back($2);}
+ : local_decls local_decl {$$=std::move($1); $$.push_back($2);}
| {$$=std::list<std::shared_ptr<pcc::BaseNode>>();}
;
@@ -269,7 +269,7 @@ function_call
;
arguments
- : arguments COMMA expression {$$=$1; $$.push_back($3);}
+ : arguments COMMA expression {$$=std::move($1); $$.push_back($3);}
| expression {$$.push_back($1);}
;
diff --git a/node/BaseNode.hh b/node/BaseNode.hh
index 71b0925..7fdb135 100644
--- a/node/BaseNode.hh
+++ b/node/BaseNode.hh
@@ -15,17 +15,6 @@ class BaseNode {
Context* context_;
};
-template <typename T, class Base = BaseNode> class ListNode : public Base {
- public:
- ListNode(Context* context)
- : Base(context) {}
- void Append(T child) { childs_.push_back(child); }
- virtual Value CodeGen();
-
- private:
- std::list<T> childs_;
-};
-
class FunctionNode;
class ProgramNode : public BaseNode {
diff --git a/node/StatementNode.cc b/node/StatementNode.cc
index 5eb17b2..c585e46 100644
--- a/node/StatementNode.cc
+++ b/node/StatementNode.cc
@@ -1,7 +1,14 @@
#include "StatementNode.hh"
namespace pcc {
-template <> Value StatementListNode::CodeGen() {
+StatementListNode::StatementListNode(Context* context)
+ : BaseNode(context) {}
+
+void StatementListNode::Append(std::shared_ptr<BaseNode> child) {
+ childs_.push_back(child);
+}
+
+Value StatementListNode::CodeGen() {
Value result = nullptr;
for (auto& child : childs_) {
result = child->CodeGen();
diff --git a/node/StatementNode.hh b/node/StatementNode.hh
index 1bd0cb6..5dea2ea 100644
--- a/node/StatementNode.hh
+++ b/node/StatementNode.hh
@@ -7,7 +7,15 @@
#include "ExprNode.hh"
namespace pcc {
-using StatementListNode = ListNode<std::shared_ptr<BaseNode>>;
+class StatementListNode : public BaseNode {
+ public:
+ StatementListNode(Context* context);
+ void Append(std::shared_ptr<BaseNode> child);
+ virtual Value CodeGen();
+
+ private:
+ std::list<std::shared_ptr<BaseNode>> childs_;
+};
class EmptyStatementNode : public BaseNode {
public: