Skip to content

Commit

Permalink
Extract EvaluateSingleOperand helper method in expression evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Feb 4, 2022
1 parent 3b4a8c3 commit b96007f
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions EOBot/Interpreter/States/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,7 @@ public ExpressionEvaluator(IEnumerable<IScriptEvaluator> evaluators)
// expression_tail is optional
evalRes = await Evaluator<ExpressionTailEvaluator>().EvaluateAsync(input);
if (evalRes.Result == EvalResult.NotMatch)
{
if (input.OperationStack.Count == 0)
return StackEmptyError(input.Current());

// convert to variable token (resolve identifier) so consumer of expression result can use it
var (localResult, localReason, singleOperand) = GetOperand(input.SymbolTable, input.OperationStack.Pop());
if (localResult != EvalResult.Ok)
return (localResult, localReason, singleOperand);

input.OperationStack.Push(singleOperand);

return Success();
}
return EvaluateSingleOperand(input);
else if (evalRes.Result == EvalResult.Failed)
return evalRes;

Expand Down Expand Up @@ -70,19 +58,7 @@ public ExpressionEvaluator(IEnumerable<IScriptEvaluator> evaluators)
// expression_tail is optional, if not set no need to evaluate operation stack below / return early
evalRes = await Evaluator<ExpressionTailEvaluator>().EvaluateAsync(input);
if (evalRes.Result == EvalResult.NotMatch)
{
if (input.OperationStack.Count == 0)
return StackEmptyError(input.Current());

// convert to variable token (resolve identifier) so consumer of expression result can use it
var (localResult, localReason, singleOperand) = GetOperand(input.SymbolTable, input.OperationStack.Pop());
if (localResult != EvalResult.Ok)
return (localResult, localReason, singleOperand);

input.OperationStack.Push(singleOperand);

return Success();
}
return EvaluateSingleOperand(input);
else if (evalRes.Result == EvalResult.Failed)
return evalRes;
}
Expand Down Expand Up @@ -138,6 +114,21 @@ public ExpressionEvaluator(IEnumerable<IScriptEvaluator> evaluators)
return Success();
}

private (EvalResult, string, BotToken) EvaluateSingleOperand(ProgramState input)
{
if (input.OperationStack.Count == 0)
return StackEmptyError(input.Current());

// convert to variable token (resolve identifier) so consumer of expression result can use it
var (localResult, localReason, singleOperand) = GetOperand(input.SymbolTable, input.OperationStack.Pop());
if (localResult != EvalResult.Ok)
return (localResult, localReason, singleOperand);

input.OperationStack.Push(singleOperand);

return Success();
}

// todo: a lot of this code is the same as what's in AssignmentEvaluator::Assign, see if it can be split out/shared
private (EvalResult, string, BotToken) GetOperand(Dictionary<string, (bool, IIdentifiable)> symbols, BotToken nextToken)
{
Expand Down

0 comments on commit b96007f

Please sign in to comment.