16 map<string, int> globalVars = {};
17 map<string, struct SFunction> globalFuncs = {};
27 int evaluate(
struct Expr &expr, map<string, int> &env = globalVars, map<string, struct SFunction> &functions = globalFuncs)
32 return expr.u.i->value;
34 return !
evaluate(expr.u.n->expr, env, functions);
39 return evaluate(expr.u.b->left, env, functions) +
evaluate(expr.u.b->right, env, functions);
41 return evaluate(expr.u.b->left, env, functions) -
evaluate(expr.u.b->right, env, functions);
43 return evaluate(expr.u.b->left, env, functions) *
evaluate(expr.u.b->right, env, functions);
45 return evaluate(expr.u.b->left, env, functions) /
evaluate(expr.u.b->right, env, functions);
47 return evaluate(expr.u.b->left, env, functions) %
evaluate(expr.u.b->right, env, functions);
49 return evaluate(expr.u.b->left, env, functions) &&
evaluate(expr.u.b->right, env, functions);
51 return evaluate(expr.u.b->left, env, functions) ||
evaluate(expr.u.b->right, env, functions);
53 return !(
evaluate(expr.u.b->left, env, functions) ||
evaluate(expr.u.b->right, env, functions));
55 return !(
evaluate(expr.u.b->left, env, functions) &&
evaluate(expr.u.b->right, env, functions));
57 return (
evaluate(expr.u.b->left, env, functions) ||
evaluate(expr.u.b->right, env, functions)) && !(
evaluate(expr.u.b->left, env, functions) &&
evaluate(expr.u.b->right, env, functions));
59 return evaluate(expr.u.b->left, env, functions) <
evaluate(expr.u.b->right, env, functions);
61 return evaluate(expr.u.b->left, env, functions) <=
evaluate(expr.u.b->right, env, functions);
63 return evaluate(expr.u.b->left, env, functions) >
evaluate(expr.u.b->right, env, functions);
65 return evaluate(expr.u.b->left, env, functions) >=
evaluate(expr.u.b->right, env, functions);
67 return evaluate(expr.u.b->left, env, functions) ==
evaluate(expr.u.b->right, env, functions);
69 return evaluate(expr.u.b->left, env, functions) !=
evaluate(expr.u.b->right, env, functions);
76 for (
auto &&i : expr.u.s->exprs)
78 result =
evaluate(i, env, functions);
82 case ExprType::Assign:
83 env.insert_or_assign(expr.u.a->name,
evaluate(expr.u.a->value, env, functions));
84 return env[expr.u.a->name];
86 return env[expr.u.id->name];
88 if (
evaluate(expr.u.if_->condition, env, functions))
90 return evaluate(expr.u.if_->thenClause, env, functions);
94 return evaluate(expr.u.if_->elseClause, env, functions);
99 while (
evaluate(expr.u.w->condition, env, functions))
101 result =
evaluate(expr.u.w->body, env, functions);
108 evaluate(expr.u.f->init, env, functions);
109 while (
evaluate(expr.u.f->condition, env, functions))
111 result =
evaluate(expr.u.f->body, env, functions);
112 evaluate(expr.u.f->update, env, functions);
120 struct SFunction function = functions.at(expr.u.c->name);
121 map<string, int> newEnv = {};
122 for (
struct Expr i : expr.u.c->args)
125 newEnv.insert_or_assign(
function.args.front(),
evaluate(i, env, functions));
126 function.args.pop_front();
128 return evaluate(
function.body, newEnv);
130 catch (
const std::out_of_range &e)
132 std::cerr <<
"Error Out of Range in Function Call" << e.what() << std::endl;
138 std::cerr <<
"Something went wrong." << std::endl;
152 map<string, int> vars = {};
153 map<string, struct SFunction> funcs = {};
154 for (
struct SFunction function : program.functions)
156 funcs.insert_or_assign(
function.name,
function);
159 for (
struct Expr expr : program.bodies)
161 result =
evaluate(expr, vars, funcs);