diff --git a/BitcodeEmitter.hpp b/BitcodeEmitter.hpp index a0f9585..ec8d14e 100644 --- a/BitcodeEmitter.hpp +++ b/BitcodeEmitter.hpp @@ -44,7 +44,6 @@ public: virtual void emit(Assert const&) override; virtual void emit(FunctionCall const&) override; virtual void emit(Branch const&) override; - virtual void emit(Main const&) override; virtual void emit(Function const&) override; virtual void emit(Module const&) override; virtual void emit(Program const&) override; diff --git a/CppEmitter.cpp b/CppEmitter.cpp index 1767f29..2119136 100644 --- a/CppEmitter.cpp +++ b/CppEmitter.cpp @@ -80,7 +80,7 @@ void CppEmitter::emitStatements(PointerList const& node) { } } -void CppEmitter::emit(Main const& main) { +void CppEmitter::emitMain(Function const& main) { stream << "int main() {\n"; indent(); emitStatements(main.getBody()); diff --git a/CppEmitter.hpp b/CppEmitter.hpp index 162131b..13842c0 100644 --- a/CppEmitter.hpp +++ b/CppEmitter.hpp @@ -41,7 +41,6 @@ public: virtual void emit(Assert const&) override; virtual void emit(FunctionCall const&) override; virtual void emit(Branch const&) override; - virtual void emit(Main const&) override; virtual void emit(Function const&) override; virtual void emit(Module const&) override; virtual void emit(Program const&) override; @@ -59,6 +58,7 @@ private: void emitStatements(PointerList const& node); void emitBranchCondition(SemiExpression const& node); void emitBranchCase(BranchCase const& node); + void emitMain(Function const& main); void indent(); void dedent(); diff --git a/Emitter.hpp b/Emitter.hpp index 590b108..3afccb3 100644 --- a/Emitter.hpp +++ b/Emitter.hpp @@ -55,7 +55,6 @@ public: virtual void emit(Assert const&) = 0; virtual void emit(FunctionCall const&) = 0; virtual void emit(Branch const&) = 0; - virtual void emit(Main const&) = 0; virtual void emit(Function const&) = 0; virtual void emit(Module const&) = 0; virtual void emit(Program const&) = 0; diff --git a/Monicelli.ypp b/Monicelli.ypp index 10d43bd..6399ba7 100644 --- a/Monicelli.ypp +++ b/Monicelli.ypp @@ -100,7 +100,6 @@ Function* funval; FunArg *argval; PointerList *arglistval; - Main* mainval; } %type NUMBER @@ -114,7 +113,7 @@ %type fun_call %type arg_decl %type args_decl args -%type fun_decl +%type fun_decl main %type print_stmt %type input_stmt %type abort_stmt @@ -130,7 +129,6 @@ %type semi_expression %type variable %type numeric -%type main %type pointer %start program @@ -182,7 +180,7 @@ arg_decl: ; main: MAIN statements { - $$ = new Main($2); + $$ = makeMain($2); } ; statements: diff --git a/Nodes.cpp b/Nodes.cpp new file mode 100644 index 0000000..5a39d37 --- /dev/null +++ b/Nodes.cpp @@ -0,0 +1,35 @@ +/* + * Monicelli: an esoteric language compiler + * + * Copyright (C) 2014 Stefano Sanfilippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Nodes.hpp" +#include + +using namespace monicelli; + +static const std::string MONICELLI_ENTRYPOINT = "__Monicelli_main"; + +Function *monicelli::makeMain(PointerList *body) { + PointerList *noargs = new PointerList(); + + return new Function( + new Id(new std::string(MONICELLI_ENTRYPOINT)), + Type::VOID, noargs, body + ); +} + diff --git a/Nodes.hpp b/Nodes.hpp index 328df87..1f621e5 100644 --- a/Nodes.hpp +++ b/Nodes.hpp @@ -371,22 +371,7 @@ private: }; -class Main: public Emittable { -public: - Main(PointerList *s): body(s) {} - - virtual void emit(Emitter *emitter) const { - emitter->emit(*this); - } - - PointerList const& getBody() const { - return *body; - } - -private: - Pointer> body; -}; - +Function *makeMain(PointerList *body); class FunArg { public: @@ -499,8 +484,8 @@ public: emitter->emit(*this); } - void setMain(Main *m) { - main = Pointer
(m); + void setMain(Function *m) { + main = Pointer(m); } void addFunction(Function *f) { @@ -512,7 +497,7 @@ public: delete m; } - boost::optional
getMain() const { + boost::optional getMain() const { maybe_return(main); } @@ -525,7 +510,7 @@ public: } private: - Pointer
main; + Pointer main; PointerList functions; std::unordered_set modules; };