Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Return statements are now not allowed outside of functions (mostly)
Browse files Browse the repository at this point in the history
  • Loading branch information
DemonExposer committed Jul 19, 2022
1 parent b422849 commit 780d4d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 9 additions & 2 deletions Tokens/Operators/N-Ary/MultilineStatementOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
namespace Interpreter.Tokens.Operators.N_Ary;

public class MultilineStatementOperator : NAryOperator {
public bool IsPartOfFunction = false;

public MultilineStatementOperator() {
Symbol = "{}";
}

public override Object Evaluate(List<TrieDictionary<Object>> vars) {
foreach (Token t in Children) {
if (t is ReturnStatement) // TODO: add flag to check if this is part of a function
if (t is ReturnStatement) {
if (!IsPartOfFunction)
throw new FormatException("Line " + t.Line + ": return statement not allowed outside of function");

return t.Evaluate(vars);
}

if (t is BinaryStatement) {
if (t is BinaryStatement bs) {
((MultilineStatementOperator) bs.Right).IsPartOfFunction = IsPartOfFunction;
Object obj = t.Evaluate(vars);
if (obj != null!)
return obj;
Expand Down
4 changes: 3 additions & 1 deletion Types/Function/FunctionBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
namespace Interpreter.Types.Function;

public class FunctionBody {
public MultilineStatementOperator expressions;
public MultilineStatementOperator expressions = null!;

public FunctionBody(MultilineStatementOperator expressions) {
this.expressions = expressions;
if (this.expressions != null!)
this.expressions.IsPartOfFunction = true;
}

public virtual Object Execute(Object[] args, TrieDictionary<Object> vars, List<TrieDictionary<Object>> topScopeVars) => expressions.Evaluate(new List<TrieDictionary<Object>> {topScopeVars[0], vars});
Expand Down

0 comments on commit 780d4d0

Please sign in to comment.