Saturn Eval is a simple calculator.
It supports common operations like addition, subtraction, multiplication, whole, modulo and common division and exponentiation.
Also, it supports functions like sin(pi)
and pow(3, 2)
.
chmod +x compile.sh
./compile.sh REPL.cpp
warning: requires C++20. (use 11 version of g++ or 13 of clang).
- include eval.hpp
- Create an environment:
map<string, Eval::Function *> functions;
map<string, double> variables;
// ...
- Create an instance of the interpreter:
auto interpreter = new Eval::Interpreter(variables, functions);
- Usage:
interpreter.eval(expression)
.
Eval::Interpreter::eval
throws Eval::Error
.
He has five types (type specified in Eval::Error::type
):
UnexpectedToken
: unexpected token, he specified in fieldtoken
.IsNotAFunction
: trying to call function, whose name not know to the interpreter (her name specified in fieldtoken
).IsNotDefined
: trying to get value of variable, whose name not know to the interpreter (her name specified in fieldtoken
).TooManyArgs
: trying to call function, with extra arguments (func name specified in fieldtoken
).TooFewArgs
: trying to call function without required amount of arguments (func name specified in fieldtoken
).
Also Eval::Error
contains index on which the error occured.