Further simplification of the AST hieararchy and Emitter iface.

This commit is contained in:
Stefano Sanfilippo 2015-03-05 22:28:45 +01:00
parent 8554bda556
commit 248afa02c8
4 changed files with 50 additions and 73 deletions

View File

@ -159,8 +159,8 @@ void CppEmitter::emit(Loop const& loop) {
stream << ")";
}
void CppEmitter::emit(BranchCase const& node) {
node.getCondition().emit(this);
void CppEmitter::emitBranchCase(BranchCase const& node) {
emitBranchCondition(node.getCondition());
stream << ") {\n";
indent();
emitStatements(node.getBody());
@ -179,7 +179,7 @@ void CppEmitter::emit(Branch const& branch) {
if (body.getCases().size() > 0) {
BranchCase *last = body.getCases().back();
for (BranchCase *cas: body.getCases()) {
cas->emit(this);
emitBranchCase(*cas);
if (cas != last) {
stream << " else if (";
var.emit(this);
@ -340,7 +340,7 @@ void CppEmitter::emit(BinaryExpression const& node) {
node.getRight().emit(this);
}
void CppEmitter::emit(BinarySemiExpression const& node) {
void CppEmitter::emitBranchCondition(SemiExpression const& node) {
bool braces = (dynamic_cast<SimpleExpression const*>(&node.getLeft()) == nullptr);
stream << ' ' << node.getOperator() << ' ';

View File

@ -40,7 +40,6 @@ public:
virtual void emit(Abort const&) override;
virtual void emit(Assert const&) override;
virtual void emit(FunctionCall const&) override;
virtual void emit(BranchCase const&) override;
virtual void emit(Branch const&) override;
virtual void emit(Main const&) override;
virtual void emit(Function const&) override;
@ -51,7 +50,6 @@ public:
virtual void emit(Integer const&) override;
virtual void emit(Float const&) override;
virtual void emit(BinaryExpression const&) override;
virtual void emit(BinarySemiExpression const&) override;
private:
void emitIndent();
@ -59,6 +57,8 @@ private:
void emitFunctionParams(PointerList<FunArg> const& funargs);
void emitFunctionArglist(PointerList<Expression> const& args);
void emitStatements(PointerList<Statement> const& node);
void emitBranchCondition(SemiExpression const& node);
void emitBranchCase(BranchCase const& node);
void indent();
void dedent();

View File

@ -35,14 +35,12 @@ class Input;
class Abort;
class Assert;
class FunctionCall;
class BranchCase;
class Branch;
class Main;
class Function;
class Module;
class Program;
class BinaryExpression;
class BinarySemiExpression;
class Emitter {
@ -56,7 +54,6 @@ public:
virtual void emit(Abort const&) = 0;
virtual void emit(Assert const&) = 0;
virtual void emit(FunctionCall const&) = 0;
virtual void emit(BranchCase const&) = 0;
virtual void emit(Branch const&) = 0;
virtual void emit(Main const&) = 0;
virtual void emit(Function const&) = 0;
@ -67,7 +64,6 @@ public:
virtual void emit(Integer const&) = 0;
virtual void emit(Float const&) = 0;
virtual void emit(BinaryExpression const&) = 0;
virtual void emit(BinarySemiExpression const&) = 0;
};
}

107
Nodes.hpp
View File

@ -41,6 +41,11 @@ enum class Type {
VOID
};
enum class Operator {
PLUS, MINUS, TIMES, DIV,
SHL, SHR,
LT, GT, GTE, LTE, EQ
};
class Emittable {
public:
@ -54,19 +59,29 @@ public:
virtual void emit(Emitter *) const {}
};
class SemiExpression: public Emittable {
public:
virtual void emit(Emitter *) const {}
};
class Expression: public Emittable {
public:
virtual void emit(Emitter *) const {}
};
class SimpleExpression: public Expression {
};
class SemiExpression {
public:
virtual void emit(Emitter *) const {}
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;
};
@ -299,14 +314,10 @@ private:
};
class BranchCase: public Emittable {
class BranchCase {
public:
BranchCase(SemiExpression *c, PointerList<Statement> *b): condition(c), body(b) {}
virtual void emit(Emitter *emitter) const {
emitter->emit(*this);
}
SemiExpression const& getCondition() const {
return *condition;
}
@ -520,13 +531,6 @@ private:
};
enum class Operator {
PLUS, MINUS, TIMES, DIV,
SHL, SHR,
LT, GT, GTE, LTE, EQ
};
class BinaryExpression: public Expression {
public:
BinaryExpression(Expression *l, Operator op, Expression *r):
@ -615,92 +619,69 @@ public:
ExpShr(Expression *l, Expression *r): BinaryExpression(l, Operator::SHR, r) {}
};
class BinarySemiExpression: public SemiExpression {
class SemiExpEq: public SemiExpression {
public:
BinarySemiExpression(Operator op, Expression *l): op(op), left(l) {}
virtual void emit(Emitter *emitter) const {
emitter->emit(*this);
}
Expression const& getLeft() const {
return *left;
}
Operator getOperator() const {
return op;
}
private:
Operator op;
Pointer<Expression> left;
SemiExpEq(Expression *l): SemiExpression(Operator::EQ, l) {}
};
class SemiExpEq: public BinarySemiExpression {
class SemiExpLt: public SemiExpression {
public:
SemiExpEq(Expression *l): BinarySemiExpression(Operator::EQ, l) {}
SemiExpLt(Expression *l): SemiExpression(Operator::LT, l) {}
};
class SemiExpLt: public BinarySemiExpression {
class SemiExpGt: public SemiExpression {
public:
SemiExpLt(Expression *l): BinarySemiExpression(Operator::LT, l) {}
SemiExpGt(Expression *l): SemiExpression(Operator::GT, l) {}
};
class SemiExpGt: public BinarySemiExpression {
class SemiExpLte: public SemiExpression {
public:
SemiExpGt(Expression *l): BinarySemiExpression(Operator::GT, l) {}
SemiExpLte(Expression *l): SemiExpression(Operator::LTE, l) {}
};
class SemiExpLte: public BinarySemiExpression {
class SemiExpGte: public SemiExpression {
public:
SemiExpLte(Expression *l): BinarySemiExpression(Operator::LTE, l) {}
SemiExpGte(Expression *l): SemiExpression(Operator::GTE, l) {}
};
class SemiExpGte: public BinarySemiExpression {
class SemiExpPlus: public SemiExpression {
public:
SemiExpGte(Expression *l): BinarySemiExpression(Operator::GTE, l) {}
SemiExpPlus(Expression *l): SemiExpression(Operator::PLUS, l) {}
};
class SemiExpPlus: public BinarySemiExpression {
class SemiExpMinus: public SemiExpression {
public:
SemiExpPlus(Expression *l): BinarySemiExpression(Operator::PLUS, l) {}
SemiExpMinus(Expression *l): SemiExpression(Operator::MINUS, l) {}
};
class SemiExpMinus: public BinarySemiExpression {
class SemiExpTimes: public SemiExpression {
public:
SemiExpMinus(Expression *l): BinarySemiExpression(Operator::MINUS, l) {}
SemiExpTimes(Expression *l): SemiExpression(Operator::TIMES, l) {}
};
class SemiExpTimes: public BinarySemiExpression {
class SemiExpDiv: public SemiExpression {
public:
SemiExpTimes(Expression *l): BinarySemiExpression(Operator::TIMES, l) {}
SemiExpDiv(Expression *l): SemiExpression(Operator::DIV, l) {}
};
class SemiExpDiv: public BinarySemiExpression {
class SemiExpShl: public SemiExpression {
public:
SemiExpDiv(Expression *l): BinarySemiExpression(Operator::DIV, l) {}
SemiExpShl(Expression *l): SemiExpression(Operator::SHR, l) {}
};
class SemiExpShl: public BinarySemiExpression {
class SemiExpShr: public SemiExpression {
public:
SemiExpShl(Expression *l): BinarySemiExpression(Operator::SHR, l) {}
};
class SemiExpShr: public BinarySemiExpression {
public:
SemiExpShr(Expression *l): BinarySemiExpression(Operator::SHL, l) {}
SemiExpShr(Expression *l): SemiExpression(Operator::SHL, l) {}
};
} // namespace