Skip to content

DemetryCPP/saturn-eval

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome to the Saturn Eval!

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).

How to compile

chmod +x compile.sh
./compile.sh REPL.cpp

warning: requires C++20. (use 11 version of g++ or 13 of clang).

Usage

  1. include eval.hpp
  2. Create an environment:
map<string, Eval::Function *> functions;
map<string, double> variables;
// ...
  1. Create an instance of the interpreter:
auto interpreter = new Eval::Interpreter(variables, functions);
  1. Usage: interpreter.eval(expression).

Catch errors

Eval::Interpreter::eval throws Eval::Error. He has five types (type specified in Eval::Error::type):

  • UnexpectedToken: unexpected token, he specified in field token.
  • IsNotAFunction: trying to call function, whose name not know to the interpreter (her name specified in field token).
  • IsNotDefined: trying to get value of variable, whose name not know to the interpreter (her name specified in field token).
  • TooManyArgs: trying to call function, with extra arguments (func name specified in field token).
  • TooFewArgs: trying to call function without required amount of arguments (func name specified in field token).

Also Eval::Error contains index on which the error occured.