Further simplification of the AST hieararchy and Emitter iface.
This commit is contained in:
parent
8554bda556
commit
248afa02c8
|
@ -159,8 +159,8 @@ void CppEmitter::emit(Loop const& loop) {
|
||||||
stream << ")";
|
stream << ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppEmitter::emit(BranchCase const& node) {
|
void CppEmitter::emitBranchCase(BranchCase const& node) {
|
||||||
node.getCondition().emit(this);
|
emitBranchCondition(node.getCondition());
|
||||||
stream << ") {\n";
|
stream << ") {\n";
|
||||||
indent();
|
indent();
|
||||||
emitStatements(node.getBody());
|
emitStatements(node.getBody());
|
||||||
|
@ -179,7 +179,7 @@ void CppEmitter::emit(Branch const& branch) {
|
||||||
if (body.getCases().size() > 0) {
|
if (body.getCases().size() > 0) {
|
||||||
BranchCase *last = body.getCases().back();
|
BranchCase *last = body.getCases().back();
|
||||||
for (BranchCase *cas: body.getCases()) {
|
for (BranchCase *cas: body.getCases()) {
|
||||||
cas->emit(this);
|
emitBranchCase(*cas);
|
||||||
if (cas != last) {
|
if (cas != last) {
|
||||||
stream << " else if (";
|
stream << " else if (";
|
||||||
var.emit(this);
|
var.emit(this);
|
||||||
|
@ -340,7 +340,7 @@ void CppEmitter::emit(BinaryExpression const& node) {
|
||||||
node.getRight().emit(this);
|
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);
|
bool braces = (dynamic_cast<SimpleExpression const*>(&node.getLeft()) == nullptr);
|
||||||
|
|
||||||
stream << ' ' << node.getOperator() << ' ';
|
stream << ' ' << node.getOperator() << ' ';
|
||||||
|
|
|
@ -40,7 +40,6 @@ public:
|
||||||
virtual void emit(Abort const&) override;
|
virtual void emit(Abort const&) override;
|
||||||
virtual void emit(Assert const&) override;
|
virtual void emit(Assert const&) override;
|
||||||
virtual void emit(FunctionCall const&) override;
|
virtual void emit(FunctionCall const&) override;
|
||||||
virtual void emit(BranchCase const&) override;
|
|
||||||
virtual void emit(Branch const&) override;
|
virtual void emit(Branch const&) override;
|
||||||
virtual void emit(Main const&) override;
|
virtual void emit(Main const&) override;
|
||||||
virtual void emit(Function const&) override;
|
virtual void emit(Function const&) override;
|
||||||
|
@ -51,7 +50,6 @@ public:
|
||||||
virtual void emit(Integer const&) override;
|
virtual void emit(Integer const&) override;
|
||||||
virtual void emit(Float const&) override;
|
virtual void emit(Float const&) override;
|
||||||
virtual void emit(BinaryExpression const&) override;
|
virtual void emit(BinaryExpression const&) override;
|
||||||
virtual void emit(BinarySemiExpression const&) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void emitIndent();
|
void emitIndent();
|
||||||
|
@ -59,6 +57,8 @@ private:
|
||||||
void emitFunctionParams(PointerList<FunArg> const& funargs);
|
void emitFunctionParams(PointerList<FunArg> const& funargs);
|
||||||
void emitFunctionArglist(PointerList<Expression> const& args);
|
void emitFunctionArglist(PointerList<Expression> const& args);
|
||||||
void emitStatements(PointerList<Statement> const& node);
|
void emitStatements(PointerList<Statement> const& node);
|
||||||
|
void emitBranchCondition(SemiExpression const& node);
|
||||||
|
void emitBranchCase(BranchCase const& node);
|
||||||
|
|
||||||
void indent();
|
void indent();
|
||||||
void dedent();
|
void dedent();
|
||||||
|
|
|
@ -35,14 +35,12 @@ class Input;
|
||||||
class Abort;
|
class Abort;
|
||||||
class Assert;
|
class Assert;
|
||||||
class FunctionCall;
|
class FunctionCall;
|
||||||
class BranchCase;
|
|
||||||
class Branch;
|
class Branch;
|
||||||
class Main;
|
class Main;
|
||||||
class Function;
|
class Function;
|
||||||
class Module;
|
class Module;
|
||||||
class Program;
|
class Program;
|
||||||
class BinaryExpression;
|
class BinaryExpression;
|
||||||
class BinarySemiExpression;
|
|
||||||
|
|
||||||
|
|
||||||
class Emitter {
|
class Emitter {
|
||||||
|
@ -56,7 +54,6 @@ public:
|
||||||
virtual void emit(Abort const&) = 0;
|
virtual void emit(Abort const&) = 0;
|
||||||
virtual void emit(Assert const&) = 0;
|
virtual void emit(Assert const&) = 0;
|
||||||
virtual void emit(FunctionCall const&) = 0;
|
virtual void emit(FunctionCall const&) = 0;
|
||||||
virtual void emit(BranchCase const&) = 0;
|
|
||||||
virtual void emit(Branch const&) = 0;
|
virtual void emit(Branch const&) = 0;
|
||||||
virtual void emit(Main const&) = 0;
|
virtual void emit(Main const&) = 0;
|
||||||
virtual void emit(Function const&) = 0;
|
virtual void emit(Function const&) = 0;
|
||||||
|
@ -67,7 +64,6 @@ public:
|
||||||
virtual void emit(Integer const&) = 0;
|
virtual void emit(Integer const&) = 0;
|
||||||
virtual void emit(Float const&) = 0;
|
virtual void emit(Float const&) = 0;
|
||||||
virtual void emit(BinaryExpression const&) = 0;
|
virtual void emit(BinaryExpression const&) = 0;
|
||||||
virtual void emit(BinarySemiExpression const&) = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
107
Nodes.hpp
107
Nodes.hpp
|
@ -41,6 +41,11 @@ enum class Type {
|
||||||
VOID
|
VOID
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class Operator {
|
||||||
|
PLUS, MINUS, TIMES, DIV,
|
||||||
|
SHL, SHR,
|
||||||
|
LT, GT, GTE, LTE, EQ
|
||||||
|
};
|
||||||
|
|
||||||
class Emittable {
|
class Emittable {
|
||||||
public:
|
public:
|
||||||
|
@ -54,19 +59,29 @@ public:
|
||||||
virtual void emit(Emitter *) const {}
|
virtual void emit(Emitter *) const {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class SemiExpression: public Emittable {
|
|
||||||
public:
|
|
||||||
virtual void emit(Emitter *) const {}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Expression: public Emittable {
|
class Expression: public Emittable {
|
||||||
public:
|
public:
|
||||||
virtual void emit(Emitter *) const {}
|
virtual void emit(Emitter *) const {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class SimpleExpression: public Expression {
|
class SimpleExpression: public Expression {
|
||||||
|
};
|
||||||
|
|
||||||
|
class SemiExpression {
|
||||||
public:
|
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:
|
public:
|
||||||
BranchCase(SemiExpression *c, PointerList<Statement> *b): condition(c), body(b) {}
|
BranchCase(SemiExpression *c, PointerList<Statement> *b): condition(c), body(b) {}
|
||||||
|
|
||||||
virtual void emit(Emitter *emitter) const {
|
|
||||||
emitter->emit(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
SemiExpression const& getCondition() const {
|
SemiExpression const& getCondition() const {
|
||||||
return *condition;
|
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 {
|
class BinaryExpression: public Expression {
|
||||||
public:
|
public:
|
||||||
BinaryExpression(Expression *l, Operator op, Expression *r):
|
BinaryExpression(Expression *l, Operator op, Expression *r):
|
||||||
|
@ -615,92 +619,69 @@ public:
|
||||||
ExpShr(Expression *l, Expression *r): BinaryExpression(l, Operator::SHR, r) {}
|
ExpShr(Expression *l, Expression *r): BinaryExpression(l, Operator::SHR, r) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SemiExpEq: public SemiExpression {
|
||||||
class BinarySemiExpression: public SemiExpression {
|
|
||||||
public:
|
public:
|
||||||
BinarySemiExpression(Operator op, Expression *l): op(op), left(l) {}
|
SemiExpEq(Expression *l): SemiExpression(Operator::EQ, 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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpEq: public BinarySemiExpression {
|
class SemiExpLt: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpEq(Expression *l): BinarySemiExpression(Operator::EQ, l) {}
|
SemiExpLt(Expression *l): SemiExpression(Operator::LT, l) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpLt: public BinarySemiExpression {
|
class SemiExpGt: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpLt(Expression *l): BinarySemiExpression(Operator::LT, l) {}
|
SemiExpGt(Expression *l): SemiExpression(Operator::GT, l) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpGt: public BinarySemiExpression {
|
class SemiExpLte: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpGt(Expression *l): BinarySemiExpression(Operator::GT, l) {}
|
SemiExpLte(Expression *l): SemiExpression(Operator::LTE, l) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpLte: public BinarySemiExpression {
|
class SemiExpGte: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpLte(Expression *l): BinarySemiExpression(Operator::LTE, l) {}
|
SemiExpGte(Expression *l): SemiExpression(Operator::GTE, l) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpGte: public BinarySemiExpression {
|
class SemiExpPlus: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpGte(Expression *l): BinarySemiExpression(Operator::GTE, l) {}
|
SemiExpPlus(Expression *l): SemiExpression(Operator::PLUS, l) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpPlus: public BinarySemiExpression {
|
class SemiExpMinus: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpPlus(Expression *l): BinarySemiExpression(Operator::PLUS, l) {}
|
SemiExpMinus(Expression *l): SemiExpression(Operator::MINUS, l) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpMinus: public BinarySemiExpression {
|
class SemiExpTimes: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpMinus(Expression *l): BinarySemiExpression(Operator::MINUS, l) {}
|
SemiExpTimes(Expression *l): SemiExpression(Operator::TIMES, l) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpTimes: public BinarySemiExpression {
|
class SemiExpDiv: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpTimes(Expression *l): BinarySemiExpression(Operator::TIMES, l) {}
|
SemiExpDiv(Expression *l): SemiExpression(Operator::DIV, l) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpDiv: public BinarySemiExpression {
|
class SemiExpShl: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpDiv(Expression *l): BinarySemiExpression(Operator::DIV, l) {}
|
SemiExpShl(Expression *l): SemiExpression(Operator::SHR, l) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SemiExpShl: public BinarySemiExpression {
|
class SemiExpShr: public SemiExpression {
|
||||||
public:
|
public:
|
||||||
SemiExpShl(Expression *l): BinarySemiExpression(Operator::SHR, l) {}
|
SemiExpShr(Expression *l): SemiExpression(Operator::SHL, l) {}
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class SemiExpShr: public BinarySemiExpression {
|
|
||||||
public:
|
|
||||||
SemiExpShr(Expression *l): BinarySemiExpression(Operator::SHL, l) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
Reference in New Issue
Block a user