2015-03-05 01:16:40 +01:00
|
|
|
#ifndef NODES_HPP
|
|
|
|
#define NODES_HPP
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2014-11-29 00:47:50 +01:00
|
|
|
/*
|
2014-11-29 21:38:52 +01:00
|
|
|
* Monicelli: an esoteric language compiler
|
2014-11-29 00:47:50 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
#include "Emitter.hpp"
|
2015-03-05 01:08:47 +01:00
|
|
|
#include "Pointers.hpp"
|
|
|
|
|
2014-12-10 20:38:32 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <unordered_set>
|
2015-03-05 01:16:40 +01:00
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
|
|
|
#define maybe_return(val) \
|
|
|
|
if ((val) != nullptr) return *(val); else return boost::none;
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
namespace monicelli {
|
|
|
|
|
2014-11-27 21:53:25 +01:00
|
|
|
enum class Type {
|
|
|
|
INT,
|
|
|
|
CHAR,
|
|
|
|
FLOAT,
|
|
|
|
BOOL,
|
2014-11-30 13:03:45 +01:00
|
|
|
DOUBLE,
|
2015-03-07 14:03:07 +01:00
|
|
|
VOID,
|
|
|
|
UNKNOWN
|
2014-11-27 21:53:25 +01:00
|
|
|
};
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
enum class Operator {
|
|
|
|
PLUS, MINUS, TIMES, DIV,
|
|
|
|
SHL, SHR,
|
|
|
|
LT, GT, GTE, LTE, EQ
|
|
|
|
};
|
2014-11-28 19:40:47 +01:00
|
|
|
|
2014-11-27 20:01:06 +01:00
|
|
|
class Emittable {
|
|
|
|
public:
|
2015-03-05 01:16:40 +01:00
|
|
|
virtual ~Emittable() {}
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const = 0;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
class Statement: public Emittable {};
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
class Expression: public Emittable {};
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
class SimpleExpression: public Expression {};
|
2015-03-05 22:28:45 +01:00
|
|
|
|
|
|
|
class SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpression(Operator op, Expression *l): op(op), left(l) {}
|
|
|
|
|
|
|
|
Expression const& getLeft() const {
|
|
|
|
return *left;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operator getOperator() const {
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Operator op;
|
|
|
|
Pointer<Expression> left;
|
2014-11-28 19:32:35 +01:00
|
|
|
};
|
|
|
|
|
2014-11-27 23:52:13 +01:00
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
class Id: public SimpleExpression {
|
2014-11-28 19:32:35 +01:00
|
|
|
public:
|
2015-03-05 01:16:40 +01:00
|
|
|
explicit Id(std::string *c): value(c) {}
|
2015-03-07 14:06:16 +01:00
|
|
|
explicit Id(char const* c) {
|
|
|
|
value = Pointer<std::string>(new std::string(c));
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2014-11-27 20:01:06 +01:00
|
|
|
}
|
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
std::string const& getValue() const {
|
|
|
|
return *value;
|
2014-11-27 20:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<std::string> value;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
2015-03-08 21:01:27 +01:00
|
|
|
static inline
|
|
|
|
bool operator==(Id const& a, Id const& b) {
|
|
|
|
return a.getValue() == b.getValue();
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
class Number: public SimpleExpression {};
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
class Integer: public Number {
|
|
|
|
public:
|
|
|
|
Integer(long i): value(i) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
long getValue() const {
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
long value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Float: public Number {
|
|
|
|
public:
|
|
|
|
Float(double f): value(f) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double getValue() const {
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
double value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Return: public Statement {
|
|
|
|
public:
|
|
|
|
explicit Return(Expression *e): expression(e) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<Expression const&> getExpression() const {
|
|
|
|
maybe_return(expression);
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Expression> expression;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Loop: public Statement {
|
|
|
|
public:
|
2015-03-05 01:16:40 +01:00
|
|
|
Loop(PointerList<Statement> *b, Expression *c): body(b), condition(c) {}
|
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PointerList<Statement> const& getBody() const {
|
|
|
|
return *body;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expression const& getCondition() const {
|
|
|
|
return *condition;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2015-03-05 01:16:40 +01:00
|
|
|
Pointer<PointerList<Statement>> body;
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Expression> condition;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class VarDeclaration: public Statement {
|
|
|
|
public:
|
|
|
|
VarDeclaration(Id *n, Type t, bool p, Expression *i):
|
|
|
|
name(n), point(p), init(i), type(t) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Id const& getId() const {
|
|
|
|
return *name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isPointer() const {
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<Expression const&> getInitializer() const {
|
|
|
|
maybe_return(init);
|
|
|
|
}
|
|
|
|
|
|
|
|
Type getType() const {
|
|
|
|
return type;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Id> name;
|
2014-11-27 20:01:06 +01:00
|
|
|
bool point;
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Expression> init;
|
2014-11-27 20:01:06 +01:00
|
|
|
Type type;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Assignment: public Statement {
|
|
|
|
public:
|
|
|
|
Assignment(Id *n, Expression *v): name(n), value(v) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Id const& getName() const {
|
|
|
|
return *name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expression const& getValue() const {
|
|
|
|
return *value;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Id> name;
|
|
|
|
Pointer<Expression> value;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Print: public Statement {
|
|
|
|
public:
|
|
|
|
explicit Print(Expression *e): expression(e) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Expression const& getExpression() const {
|
|
|
|
return *expression;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Expression> expression;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Input: public Statement {
|
|
|
|
public:
|
|
|
|
explicit Input(Id *v): variable(v) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Id const& getVariable() const {
|
|
|
|
return *variable;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Id> variable;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Abort: public Statement {
|
|
|
|
public:
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Assert: public Statement {
|
|
|
|
public:
|
|
|
|
explicit Assert(Expression *e): expression(e) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Expression const& getExpression() const {
|
|
|
|
return *expression;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Expression> expression;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionCall: public Statement, public Expression {
|
|
|
|
public:
|
2015-03-05 01:16:40 +01:00
|
|
|
FunctionCall(Id *n, PointerList<Expression> *a): name(n), args(a) {}
|
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Id const& getName() const {
|
|
|
|
return *name;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerList<Expression> const& getArgs() const {
|
|
|
|
return *args;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Id> name;
|
2015-03-05 01:16:40 +01:00
|
|
|
Pointer<PointerList<Expression>> args;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class BranchCase {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 01:16:40 +01:00
|
|
|
BranchCase(SemiExpression *c, PointerList<Statement> *b): condition(c), body(b) {}
|
|
|
|
|
|
|
|
SemiExpression const& getCondition() const {
|
|
|
|
return *condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerList<Statement> const& getBody() const {
|
|
|
|
return *body;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<SemiExpression> condition;
|
2015-03-05 01:16:40 +01:00
|
|
|
Pointer<PointerList<Statement>> body;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Branch: public Statement {
|
|
|
|
public:
|
2015-03-05 01:16:40 +01:00
|
|
|
class Body {
|
2014-12-02 16:22:03 +01:00
|
|
|
public:
|
2015-03-05 01:16:40 +01:00
|
|
|
Body(PointerList<BranchCase> *c, PointerList<Statement> *e = nullptr): cases(c), els(e) {}
|
2014-12-02 16:22:03 +01:00
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
PointerList<BranchCase> const& getCases() const {
|
|
|
|
return *cases;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<PointerList<Statement> const&> getElse() const {
|
|
|
|
maybe_return(els);
|
|
|
|
}
|
2014-12-02 16:22:03 +01:00
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
private:
|
|
|
|
Pointer<PointerList<BranchCase>> cases;
|
|
|
|
Pointer<PointerList<Statement>> els;
|
2014-12-02 16:22:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Branch(Id *v, Branch::Body *b): var(v), body(b) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Id const& getVar() const {
|
|
|
|
return *var;
|
|
|
|
}
|
|
|
|
|
|
|
|
Branch::Body const& getBody() const {
|
|
|
|
return *body;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Id> var;
|
2014-12-02 16:22:03 +01:00
|
|
|
Pointer<Branch::Body> body;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-06 13:40:40 +01:00
|
|
|
Function *makeMain(PointerList<Statement> *body);
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
class FunArg {
|
2014-11-30 13:03:45 +01:00
|
|
|
public:
|
|
|
|
FunArg(Id *n, Type t, bool p): name(n), type(t), pointer(p) {}
|
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
Id const& getName() const {
|
|
|
|
return *name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type getType() const {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isPointer() const {
|
|
|
|
return pointer;
|
|
|
|
}
|
2014-11-30 13:03:45 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Pointer<Id> name;
|
|
|
|
Type type;
|
|
|
|
bool pointer;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-08 11:25:41 +01:00
|
|
|
class FunctionPrototype: public Emittable {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-08 11:25:41 +01:00
|
|
|
FunctionPrototype(Id *n, Type r, PointerList<FunArg> *a):
|
|
|
|
name(n), type(r), args(a) {}
|
2014-11-28 19:15:36 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Id const& getName() const {
|
|
|
|
return *name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type getType() const {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerList<FunArg> const& getArgs() const {
|
|
|
|
return *args;
|
|
|
|
}
|
|
|
|
|
2015-03-08 11:25:41 +01:00
|
|
|
private:
|
|
|
|
Pointer<Id> name;
|
|
|
|
Type type;
|
|
|
|
Pointer<PointerList<FunArg>> args;
|
|
|
|
};
|
|
|
|
|
2015-03-08 21:01:27 +01:00
|
|
|
static inline
|
|
|
|
bool operator==(const FunctionPrototype &a, const FunctionPrototype &b) {
|
|
|
|
return a.getName() == b.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
size_t hash_value(const monicelli::FunctionPrototype &e) {
|
|
|
|
return std::hash<std::string>()(e.getName().getValue());
|
|
|
|
}
|
2015-03-08 11:25:41 +01:00
|
|
|
|
|
|
|
class Function: public Emittable {
|
|
|
|
public:
|
|
|
|
Function(FunctionPrototype *p, PointerList<Statement> *b):
|
|
|
|
prototype(p), body(b) {}
|
|
|
|
|
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPrototype const& getPrototype() const {
|
|
|
|
return *prototype;
|
|
|
|
}
|
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
PointerList<Statement> const& getBody() const {
|
|
|
|
return *body;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
private:
|
2015-03-08 11:25:41 +01:00
|
|
|
Pointer<FunctionPrototype> prototype;
|
2015-03-05 01:16:40 +01:00
|
|
|
Pointer<PointerList<Statement>> body;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
2014-11-30 13:03:45 +01:00
|
|
|
|
2014-12-10 20:38:32 +01:00
|
|
|
class Module: public Emittable {
|
|
|
|
public:
|
2015-03-07 14:05:03 +01:00
|
|
|
enum ModuleType {
|
2014-12-10 20:38:32 +01:00
|
|
|
SYSTEM, USER
|
|
|
|
};
|
|
|
|
|
2015-03-07 14:05:03 +01:00
|
|
|
Module(const std::string &n, ModuleType s): name(n), type(s) {}
|
2015-03-05 01:16:40 +01:00
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
2014-12-10 20:38:32 +01:00
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
std::string const& getName() const {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-03-07 14:05:03 +01:00
|
|
|
ModuleType getType() const {
|
2015-03-05 01:16:40 +01:00
|
|
|
return type;
|
|
|
|
}
|
2014-12-10 20:38:32 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string name;
|
2015-03-07 14:05:03 +01:00
|
|
|
ModuleType type;
|
2014-12-10 20:38:32 +01:00
|
|
|
};
|
|
|
|
|
2015-03-08 21:01:27 +01:00
|
|
|
static inline
|
|
|
|
bool operator==(const Module &a, const Module &b) {
|
|
|
|
return (a.getName() == b.getName()) && (a.getType() == b.getType());
|
|
|
|
}
|
|
|
|
|
2015-03-08 21:37:46 +01:00
|
|
|
static inline
|
|
|
|
size_t hash_value(const monicelli::Module &e) {
|
|
|
|
return std::hash<std::string>()(e.getName()) ^ std::hash<bool>()(e.getType());
|
2014-12-10 20:38:32 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 20:01:06 +01:00
|
|
|
class Program: public Emittable {
|
|
|
|
public:
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 01:16:40 +01:00
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2015-03-06 13:40:40 +01:00
|
|
|
void setMain(Function *m) {
|
|
|
|
main = Pointer<Function>(m);
|
2014-11-27 20:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void addFunction(Function *f) {
|
|
|
|
functions.push_back(f);
|
|
|
|
}
|
|
|
|
|
2014-12-10 20:38:32 +01:00
|
|
|
void addModule(Module *m) {
|
2015-03-08 21:37:46 +01:00
|
|
|
modules.insert(m);
|
2014-12-10 20:38:32 +01:00
|
|
|
}
|
|
|
|
|
2015-03-06 13:40:40 +01:00
|
|
|
boost::optional<Function const&> getMain() const {
|
2015-03-05 01:16:40 +01:00
|
|
|
maybe_return(main);
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerList<Function> const& getFunctions() const {
|
|
|
|
return functions;
|
|
|
|
}
|
|
|
|
|
2015-03-08 21:37:46 +01:00
|
|
|
PointerSet<Module> const& getModules() const {
|
2015-03-05 01:16:40 +01:00
|
|
|
return modules;
|
|
|
|
}
|
|
|
|
|
2014-11-27 20:01:06 +01:00
|
|
|
private:
|
2015-03-06 13:40:40 +01:00
|
|
|
Pointer<Function> main;
|
2014-11-28 19:37:14 +01:00
|
|
|
PointerList<Function> functions;
|
2015-03-08 21:37:46 +01:00
|
|
|
PointerSet<Module> modules;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class BinaryExpression: public Expression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
BinaryExpression(Expression *l, Operator op, Expression *r):
|
|
|
|
left(l), op(op), right(r) {}
|
|
|
|
|
2015-03-06 14:29:52 +01:00
|
|
|
virtual bool emit(Emitter *emitter) const {
|
|
|
|
return emitter->emit(*this);
|
2015-03-05 17:25:02 +01:00
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
Expression const& getLeft() const {
|
|
|
|
return *left;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expression const& getRight() const {
|
|
|
|
return *right;
|
|
|
|
}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
Operator getOperator() const {
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2014-11-27 20:01:06 +01:00
|
|
|
private:
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Expression> left;
|
2015-03-05 17:25:02 +01:00
|
|
|
Operator op;
|
2014-11-28 19:40:47 +01:00
|
|
|
Pointer<Expression> right;
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class ExpLt: public BinaryExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
ExpLt(Expression *l, Expression *r): BinaryExpression(l, Operator::LT, r) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class ExpGt: public BinaryExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
ExpGt(Expression *l, Expression *r): BinaryExpression(l, Operator::GT, r) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class ExpLte: public BinaryExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
ExpLte(Expression *l, Expression *r): BinaryExpression(l, Operator::LTE, r) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class ExpGte: public BinaryExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
ExpGte(Expression *l, Expression *r): BinaryExpression(l, Operator::GTE, r) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class ExpPlus: public BinaryExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
ExpPlus(Expression *l, Expression *r): BinaryExpression(l, Operator::PLUS, r) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class ExpMinus: public BinaryExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
ExpMinus(Expression *l, Expression *r): BinaryExpression(l, Operator::MINUS, r) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class ExpTimes: public BinaryExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
ExpTimes(Expression *l, Expression *r): BinaryExpression(l, Operator::TIMES, r) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class ExpDiv: public BinaryExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
ExpDiv(Expression *l, Expression *r): BinaryExpression(l, Operator::DIV, r) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
class ExpShl: public BinaryExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 17:25:02 +01:00
|
|
|
ExpShl(Expression *l, Expression *r): BinaryExpression(l, Operator::SHL, r) {}
|
|
|
|
};
|
2014-11-27 20:01:06 +01:00
|
|
|
|
2015-03-05 17:25:02 +01:00
|
|
|
|
|
|
|
class ExpShr: public BinaryExpression {
|
|
|
|
public:
|
|
|
|
ExpShr(Expression *l, Expression *r): BinaryExpression(l, Operator::SHR, r) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpEq: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpEq(Expression *l): SemiExpression(Operator::EQ, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpLt: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpLt(Expression *l): SemiExpression(Operator::LT, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpGt: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpGt(Expression *l): SemiExpression(Operator::GT, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpLte: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpLte(Expression *l): SemiExpression(Operator::LTE, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpGte: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpGte(Expression *l): SemiExpression(Operator::GTE, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpPlus: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpPlus(Expression *l): SemiExpression(Operator::PLUS, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpMinus: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpMinus(Expression *l): SemiExpression(Operator::MINUS, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpTimes: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpTimes(Expression *l): SemiExpression(Operator::TIMES, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpDiv: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpDiv(Expression *l): SemiExpression(Operator::DIV, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpShl: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpShl(Expression *l): SemiExpression(Operator::SHR, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:28:45 +01:00
|
|
|
class SemiExpShr: public SemiExpression {
|
2014-11-27 20:01:06 +01:00
|
|
|
public:
|
2015-03-05 22:28:45 +01:00
|
|
|
SemiExpShr(Expression *l): SemiExpression(Operator::SHL, l) {}
|
2014-11-27 20:01:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-03-05 01:16:40 +01:00
|
|
|
#undef maybe_return
|
|
|
|
|
2014-11-27 20:01:06 +01:00
|
|
|
#endif
|
|
|
|
|