Skip to content

Commit

Permalink
Add logic for evaluating arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
asynchroza committed Nov 15, 2023
1 parent 9e14d80 commit f03b06a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions nulascript/eval/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ Storage* evaluateBlockStatement(std::vector<Statement*> statements,
return result;
}

std::vector<Storage*> evaluateArgs(std::vector<Expression*> arguments,
Environment* env) {
std::vector<Storage*> evaluatedArgs;
Storage* evaluated;

for (auto arg : arguments) {
evaluated = evaluate(arg, env);
evaluatedArgs.push_back(evaluated);
if (isErrorStorage(evaluated))
return evaluatedArgs;
}

return evaluatedArgs;
}

Storage* evaluate(Node* node, Environment* env) {
if (checkBase(node, typeid(Program))) {
auto program = dynamic_cast<Program*>(node);
Expand Down Expand Up @@ -232,6 +247,18 @@ Storage* evaluate(Node* node, Environment* env) {
return new FunctionStorage(func->arguments, func->code, env);
}

else if (checkBase(node, typeid(Invocation))) {
auto invoc = dynamic_cast<Invocation*>(node);
auto evaluatedInvoc = evaluate(invoc->function, env);
if (isErrorStorage(evaluatedInvoc))
return evaluatedInvoc;

auto arguments = evaluateArgs(invoc->arguments, env);
if (isErrorStorage(arguments.at(arguments.size() - 1))) {
return arguments[0];
}
}

return createError("No implementation found for this functionality");
}

Expand Down

0 comments on commit f03b06a

Please sign in to comment.