108 lines
2.1 KiB
C++
108 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include "engine/address.hpp"
|
|
#include "engine/lexer.hpp"
|
|
#include "engine/value.hpp"
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace htab {
|
|
|
|
enum class BinOp {
|
|
Add,
|
|
Sub,
|
|
Mul,
|
|
Div,
|
|
Pow,
|
|
Concat,
|
|
Eq,
|
|
Neq,
|
|
Lt,
|
|
Gt,
|
|
Le,
|
|
Ge,
|
|
Intersect,
|
|
Union,
|
|
};
|
|
|
|
struct Expr {
|
|
enum class Kind {
|
|
Literal,
|
|
CellRef,
|
|
Range,
|
|
Unary,
|
|
Percent,
|
|
Binary,
|
|
Call,
|
|
UnknownName,
|
|
};
|
|
|
|
Kind kind;
|
|
explicit Expr(Kind k) : kind(k) {}
|
|
virtual ~Expr() = default;
|
|
};
|
|
|
|
struct LiteralExpr : Expr {
|
|
Value value;
|
|
explicit LiteralExpr(Value v) : Expr(Kind::Literal), value(std::move(v)) {}
|
|
};
|
|
|
|
struct CellRefExpr : Expr {
|
|
CellRef ref;
|
|
explicit CellRefExpr(CellRef r) : Expr(Kind::CellRef), ref(std::move(r)) {}
|
|
};
|
|
|
|
struct RangeExpr : Expr {
|
|
std::shared_ptr<Expr> lhs;
|
|
std::shared_ptr<Expr> rhs;
|
|
RangeExpr(std::shared_ptr<Expr> l, std::shared_ptr<Expr> r)
|
|
: Expr(Kind::Range), lhs(std::move(l)), rhs(std::move(r)) {}
|
|
};
|
|
|
|
struct UnaryExpr : Expr {
|
|
char op = '+';
|
|
std::shared_ptr<Expr> operand;
|
|
UnaryExpr(char o, std::shared_ptr<Expr> e)
|
|
: Expr(Kind::Unary), op(o), operand(std::move(e)) {}
|
|
};
|
|
|
|
struct PercentExpr : Expr {
|
|
std::shared_ptr<Expr> operand;
|
|
explicit PercentExpr(std::shared_ptr<Expr> e) : Expr(Kind::Percent), operand(std::move(e)) {}
|
|
};
|
|
|
|
struct BinaryExpr : Expr {
|
|
BinOp op;
|
|
std::shared_ptr<Expr> lhs;
|
|
std::shared_ptr<Expr> rhs;
|
|
BinaryExpr(BinOp o, std::shared_ptr<Expr> l, std::shared_ptr<Expr> r)
|
|
: Expr(Kind::Binary), op(o), lhs(std::move(l)), rhs(std::move(r)) {}
|
|
};
|
|
|
|
struct CallExpr : Expr {
|
|
std::string name;
|
|
std::vector<std::shared_ptr<Expr>> args;
|
|
CallExpr(std::string n, std::vector<std::shared_ptr<Expr>> a)
|
|
: Expr(Kind::Call), name(std::move(n)), args(std::move(a)) {}
|
|
};
|
|
|
|
struct UnknownNameExpr : Expr {
|
|
std::string name;
|
|
explicit UnknownNameExpr(std::string n) : Expr(Kind::UnknownName), name(std::move(n)) {}
|
|
};
|
|
|
|
struct ParseResult {
|
|
std::shared_ptr<Expr> ast;
|
|
bool ok = false;
|
|
std::string error;
|
|
};
|
|
|
|
class Parser {
|
|
public:
|
|
static ParseResult parse_formula(std::string_view formula);
|
|
};
|
|
|
|
} // namespace htab
|