#pragma once #include "engine/address.hpp" #include "engine/lexer.hpp" #include "engine/value.hpp" #include #include #include 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 lhs; std::shared_ptr rhs; RangeExpr(std::shared_ptr l, std::shared_ptr r) : Expr(Kind::Range), lhs(std::move(l)), rhs(std::move(r)) {} }; struct UnaryExpr : Expr { char op = '+'; std::shared_ptr operand; UnaryExpr(char o, std::shared_ptr e) : Expr(Kind::Unary), op(o), operand(std::move(e)) {} }; struct PercentExpr : Expr { std::shared_ptr operand; explicit PercentExpr(std::shared_ptr e) : Expr(Kind::Percent), operand(std::move(e)) {} }; struct BinaryExpr : Expr { BinOp op; std::shared_ptr lhs; std::shared_ptr rhs; BinaryExpr(BinOp o, std::shared_ptr l, std::shared_ptr r) : Expr(Kind::Binary), op(o), lhs(std::move(l)), rhs(std::move(r)) {} }; struct CallExpr : Expr { std::string name; std::vector> args; CallExpr(std::string n, std::vector> 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 ast; bool ok = false; std::string error; }; class Parser { public: static ParseResult parse_formula(std::string_view formula); }; } // namespace htab