Turn main into an ordinary function.

This commit is contained in:
Stefano Sanfilippo 2015-03-06 13:40:40 +01:00
parent a509a61bfb
commit 5d043566dc
7 changed files with 44 additions and 28 deletions

View File

@ -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;

View File

@ -80,7 +80,7 @@ void CppEmitter::emitStatements(PointerList<Statement> const& node) {
}
}
void CppEmitter::emit(Main const& main) {
void CppEmitter::emitMain(Function const& main) {
stream << "int main() {\n";
indent();
emitStatements(main.getBody());

View File

@ -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<Statement> const& node);
void emitBranchCondition(SemiExpression const& node);
void emitBranchCase(BranchCase const& node);
void emitMain(Function const& main);
void indent();
void dedent();

View File

@ -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;

View File

@ -100,7 +100,6 @@
Function* funval;
FunArg *argval;
PointerList<FunArg> *arglistval;
Main* mainval;
}
%type<intval> NUMBER
@ -114,7 +113,7 @@
%type<callval> fun_call
%type<argval> arg_decl
%type<arglistval> args_decl args
%type<funval> fun_decl
%type<funval> fun_decl main
%type<printval> print_stmt
%type<inputval> input_stmt
%type<abortval> abort_stmt
@ -130,7 +129,6 @@
%type<semiexpval> semi_expression
%type<idval> variable
%type<numericval> numeric
%type<mainval> main
%type<boolval> pointer
%start program
@ -182,7 +180,7 @@ arg_decl:
;
main:
MAIN statements {
$$ = new Main($2);
$$ = makeMain($2);
}
;
statements:

35
Nodes.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "Nodes.hpp"
#include <string>
using namespace monicelli;
static const std::string MONICELLI_ENTRYPOINT = "__Monicelli_main";
Function *monicelli::makeMain(PointerList<Statement> *body) {
PointerList<FunArg> *noargs = new PointerList<FunArg>();
return new Function(
new Id(new std::string(MONICELLI_ENTRYPOINT)),
Type::VOID, noargs, body
);
}

View File

@ -371,22 +371,7 @@ private:
};
class Main: public Emittable {
public:
Main(PointerList<Statement> *s): body(s) {}
virtual void emit(Emitter *emitter) const {
emitter->emit(*this);
}
PointerList<Statement> const& getBody() const {
return *body;
}
private:
Pointer<PointerList<Statement>> body;
};
Function *makeMain(PointerList<Statement> *body);
class FunArg {
public:
@ -499,8 +484,8 @@ public:
emitter->emit(*this);
}
void setMain(Main *m) {
main = Pointer<Main>(m);
void setMain(Function *m) {
main = Pointer<Function>(m);
}
void addFunction(Function *f) {
@ -512,7 +497,7 @@ public:
delete m;
}
boost::optional<Main const&> getMain() const {
boost::optional<Function const&> getMain() const {
maybe_return(main);
}
@ -525,7 +510,7 @@ public:
}
private:
Pointer<Main> main;
Pointer<Function> main;
PointerList<Function> functions;
std::unordered_set<Module> modules;
};