-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_formula_listener.h
76 lines (47 loc) · 1.99 KB
/
ast_formula_listener.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include "ast.h"
#include "FormulaListener.h"
#include <stack>
class AstFormulaListener : public FormulaListener {
private:
Ast::TreeBuilder builder_;
public:
~AstFormulaListener() override = default;
void exitUnaryOp(FormulaParser::UnaryOpContext *ctx) override {
Ast::UnaryOperator op;
if (ctx->SUB()) op = Ast::UnaryOperator::MINUS;
else op = Ast::UnaryOperator::PLUS;
builder_.AddUnaryOp(op);
}
void exitParens(FormulaParser::ParensContext* ctx) override {
builder_.AddParentheses();
}
void exitLiteral(FormulaParser::LiteralContext* ctx) override {
builder_.AddLiteral(ctx->getText());
}
void exitCell(FormulaParser::CellContext* ctx) override {
builder_.AddCell(ctx->getText());
}
void exitBinaryOp(FormulaParser::BinaryOpContext* ctx) override {
Ast::BinaryOperator op;
if (ctx->ADD()) op = Ast::BinaryOperator::ADD;
else if (ctx->SUB()) op = Ast::BinaryOperator::SUB;
else if (ctx->MUL()) op = Ast::BinaryOperator::MUL;
else op = Ast::BinaryOperator::DIV;
builder_.AddBinaryOp(op);
}
void visitTerminal(antlr4::tree::TerminalNode* node) override {}
void visitErrorNode(antlr4::tree::ErrorNode* node) override {}
void enterEveryRule(antlr4::ParserRuleContext* ctx) override {}
void exitEveryRule(antlr4::ParserRuleContext* ctx) override {}
void enterMain(FormulaParser::MainContext* ctx) override {}
void exitMain(FormulaParser::MainContext* ctx) override {}
void enterUnaryOp(FormulaParser::UnaryOpContext* ctx) override {}
void enterParens(FormulaParser::ParensContext* ctx) override {}
void enterLiteral(FormulaParser::LiteralContext* ctx) override {}
void enterCell(FormulaParser::CellContext* ctx) override {}
void enterBinaryOp(FormulaParser::BinaryOpContext* ctx) override {}
Ast::Tree GetResult() {
return builder_.Build();
}
};