public EvaluatedSignature(CallSignature call_signature, DeclarationSignature decl_signature, Scope outer_scope) { EvaluatedParameter [] parameters = new EvaluatedParameter [Math.Max(call_signature.Parameters.Length, decl_signature.Parameters.Length)]; for (int i = 0; i < parameters.Length; i++) { DeclarationParameter decl_parameter = decl_signature.Parameters.Get(i, default_value: null); CallParameter call_parameter = call_signature.Parameters.Get(i, default_value: null); Expression unevaluated_value = null; if (call_parameter != null && call_parameter.Value != null) { unevaluated_value = call_parameter.Value; } else if (decl_parameter != null) { unevaluated_value = decl_parameter.InitialValue; } FinalExpression evaluated_value = Interpreters.Execute(unevaluated_value, outer_scope).ResultValue; EvaluatedParameter evaluated_parameter = new EvaluatedParameter( name: (decl_parameter?.Name ?? default), evaluated_value: evaluated_value, ampersand: (decl_parameter?.PassedByRef ?? false) || (call_parameter?.Ampersand ?? false), is_unpack: (call_parameter?.IsUnpack ?? false) ); parameters [i] = evaluated_parameter; } Parameters = parameters.ToImmutableArray(); }
public InterpretedClass(ClassDeclarationExpression expression, Scope scope) { _rootscope = scope.Root; _name = expression.Name; _parents = expression.Parents; _interfaces = expression.Interfaces; _methods = new MethodCollection(); _fields = new FieldCollection(); _classes = new ClassCollection(); _static_variables = new VariableCollection(); ScriptScope script_scope = null; scope.FindNearestScope <ScriptScope> (ss => script_scope = ss); foreach (ClassMethodDeclarationExpression m in expression.Methods) { _methods.Add(new InterpretedMethod(m, script_scope)); } foreach (ClassFieldDeclarationExpression f in expression.Fields) { IVariable var = _static_variables.EnsureExists(f.Name); if (f.Initializer is Expression initializer_expr) { var.Value = Interpreters.Execute(initializer_expr, script_scope).ResultValue; } } }
public static Result Run(RequireFileExpression require, Scope scope) { string filepath_raw = Interpreters.Execute(require.FilePath, scope).ResultValue.GetStringValue(); scope.Root.Context.RequireFile(scope, IncludePathHelper.ResolveToRelative(filepath_raw, scope)); return(Result.NULL); }
public static Result Run(ForeachExpression block, Scope scope) { Result overall_result = Result.NULL; int i = 0; while (true) { bool condition_is_true; if (block.IsDoWhile && i == 0) { condition_is_true = true; } else { // Log.Debug (block.Condition); Result condition_result = Interpreters.Execute(block.Condition, scope); condition_is_true = condition_result.IsTrue(); } if (condition_is_true) { try { Result body_result = Interpreters.Execute(block.Body, scope); overall_result = body_result; } catch (BreakException ex) { // Log.Debug (ex); if (ex.CountOfLoops > 1) { throw new BreakException(ex.CountOfLoops - 1); } break; } catch (ContinueException ex) { // Log.Debug (ex); if (ex.CountOfLoops > 1) { throw new ContinueException(ex.CountOfLoops - 1); } } } else { break; } i++; } return(overall_result); }
public static Result Run(ImmutableArray <Expression> expressions, Scope scope) { Result res = Result.NULL; foreach (Expression expression in expressions) { res = Interpreters.Execute(expression, scope); if (res.FastReturn) { return(res); } } return(res); }
Result IFunction.Execute(EvaluatedSignature evaluated_signature, Scope outer_scope) { FunctionScope function_scope = new FunctionScope(outer_scope, this, evaluated_signature); foreach (EvaluatedParameter parameter in evaluated_signature.Parameters) { if (!parameter.Name.IsEmpty) { IVariable variable = function_scope.Variables.EnsureExists(parameter.Name); variable.Value = parameter.EvaluatedValue; } } return(Interpreters.Execute(_expression.Body, function_scope)); }
Result IMethod.Execute(IObject obj, IReadOnlyList <IClass> classes, EvaluatedSignature evaluated_signature, Scope outer_scope) { MethodScope function_scope = new MethodScope(outer_scope, this, evaluated_signature, obj, classes); foreach (EvaluatedParameter parameter in evaluated_signature.Parameters) { if (!parameter.Name.IsEmpty) { IVariable variable = function_scope.Variables.EnsureExists(parameter.Name); variable.Value = parameter.EvaluatedValue; } } return(Interpreters.Execute(_expression.Body, function_scope)); }
public static Result Run(ArrayCreateExpression expression, Scope scope) { IArray arr = new ArrayStructure(); scope.Root.Arrays.Add(arr); foreach (ArrayItemExpression item_expr in expression.Items) { FinalExpression key_expr = Interpreters.Execute(item_expr.Key, scope).ResultValue; FinalExpression value_expr = Interpreters.Execute(item_expr.Value, scope).ResultValue; ArrayKey key = new ArrayKey(key_expr.GetStringValue()); arr.Set(new ArrayItem(key, value_expr)); } return(new Result(arr.AsExpression)); }
public static Result Run(TryExpression block, Scope scope) { Result body_result = Result.NULL; Expression body_expr = block.Body; if (body_expr != null) { body_result = Interpreters.Execute(body_expr, scope); } Expression finally_expr = block.Finally; if (finally_expr != null) { Interpreters.Execute(finally_expr, scope); } return(body_result); }
public static Result Run(UnaryExpression expression, Scope scope) { Result value_result = Interpreters.Execute(expression.Value, scope); if (value_result.FastReturn) { return(value_result); } FinalExpression value = value_result.ResultValue; switch (expression.Operation) { case UnaryOp.CAST_STRING: return(new Result(new StringExpression(value.GetStringValue()))); case UnaryOp.CAST_BOOL: return(new Result(new BoolExpression(value.GetBoolValue()))); case UnaryOp.CAST_ARRAY: return(ArrayInterpreter.Run(new ArrayCreateExpression(ImmutableArray <ArrayItemExpression> .Empty), scope)); case UnaryOp.CAST_OBJECT: return(new Result(value)); case UnaryOp.LOGICAL_NEGATION: return(new Result(new BoolExpression(!value.GetBoolValue()))); case UnaryOp.IS_EMPTY: return(new Result(new BoolExpression(string.IsNullOrEmpty(value.GetStringValue())))); case UnaryOp.AT_SIGN: return(new Result(value)); default: Log.Error($"Unable to execute binary operation: {expression.Operation}"); return(Result.NULL); } }
public static Result Run(ConditionalBlockExpression block, Scope scope) { foreach (BaseIfExpression if_expr in block.Ifs) { Result if_condition_result = Interpreters.Execute(if_expr.Condition, scope); if (if_condition_result.IsTrue()) { Result if_body_result = Interpreters.Execute(if_expr.Body, scope); return(if_body_result); } } ElseExpression else_expr = block.Else; if (else_expr != null) { Result else_body_result = Interpreters.Execute(else_expr.Body, scope); return(else_body_result); } return(Result.NULL); }
public static void Resolve(ArrayAccessExpression expression, Scope scope, Action <IArray, ArrayKey> action) { FinalExpression key_expr = Interpreters.Execute(expression.Index, scope).ResultValue; ArrayKey key = new ArrayKey(key_expr.GetStringValue()); FinalExpression array_expr = Interpreters.Execute(expression.Array, scope).ResultValue; if (array_expr is ArrayPointerExpression pointer) { if (pointer.Array is IArray arr) { action(arr, key); } else { Log.Error($"Array could not be found: {pointer.Array.Name}, key: '{key}', scope: {scope}"); } } else { Log.Error($"Array access of key '{key}' is not performed on an array, but on: {array_expr}, scope: {scope}"); } }
public static Result Run(AssignExpression assign, Scope scope) { Result right_result = Interpreters.Execute(assign.Right, scope); if (right_result.FastReturn) { return(right_result); } switch (assign.Left) { case ArrayAccessExpression array_access_expression: ArrayInterpreter.Resolve(array_access_expression, scope, (arr, key) => { arr.Set(new ArrayItem(key, right_result.ResultValue)); }); break; case VariableExpression variable_expression: IVariable variable = scope.Variables.EnsureExists(variable_expression.Name); variable.Value = right_result.ResultValue; break; case StaticFieldAccessExpression static_field_access_expression: ClassInterpreter.Resolve(static_field_access_expression, scope, (var) => { var.Value = right_result.ResultValue; }); break; default: Log.Error($"Cannot execute assign expression: Left Value is of unknown type {assign.Left}"); break; } return(right_result); }
public static Result Run(BinaryExpression expression, Scope scope) { Result left_result = Interpreters.Execute(expression.Left, scope); if (left_result.FastReturn) { return(left_result); } Result right_result = Interpreters.Execute(expression.Right, scope); if (right_result.FastReturn) { return(right_result); } FinalExpression left = left_result.ResultValue; FinalExpression right = right_result.ResultValue; switch (expression.Operation) { case BinaryOp.ADD: if (left.GetScalarAffinity() == ScalarAffinity.DOUBLE || left.GetScalarAffinity() == ScalarAffinity.DOUBLE) { return(new Result(new DoubleExpression(left.GetDoubleValue() + right.GetDoubleValue()))); } else { return(new Result(new LongExpression(left.GetLongValue() + right.GetLongValue()))); } case BinaryOp.SUB: if (left.GetScalarAffinity() == ScalarAffinity.DOUBLE || left.GetScalarAffinity() == ScalarAffinity.DOUBLE) { return(new Result(new DoubleExpression(left.GetDoubleValue() - right.GetDoubleValue()))); } else { return(new Result(new LongExpression(left.GetLongValue() - right.GetLongValue()))); } case BinaryOp.MUL: if (left.GetScalarAffinity() == ScalarAffinity.DOUBLE || left.GetScalarAffinity() == ScalarAffinity.DOUBLE) { return(new Result(new DoubleExpression(left.GetDoubleValue() * right.GetDoubleValue()))); } else { return(new Result(new LongExpression(left.GetLongValue() * right.GetLongValue()))); } case BinaryOp.DIV: return(new Result(new DoubleExpression(left.GetDoubleValue() / right.GetDoubleValue()))); case BinaryOp.CONCAT: Log.Debug($"concat result: left = " + left + ", right = " + right + ", concat = " + new StringExpression(left.GetStringValue() + right.GetStringValue())); return(new Result(new StringExpression(left.GetStringValue() + right.GetStringValue()))); case BinaryOp.EQUAL: if (left.GetScalarAffinity() == ScalarAffinity.ARRAY && right.GetScalarAffinity() == ScalarAffinity.ARRAY) { return(new Result(new BoolExpression((left as ArrayPointerExpression)?.Array?.Name == (right as ArrayPointerExpression)?.Array?.Name))); } else if (left.GetScalarAffinity() == ScalarAffinity.OBJECT && right.GetScalarAffinity() == ScalarAffinity.OBJECT) { return(new Result(new BoolExpression((left as ObjectPointerExpression)?.Object?.Name == (right as ObjectPointerExpression)?.Object?.Name))); } else if (left.GetScalarAffinity() == ScalarAffinity.BOOL || right.GetScalarAffinity() == ScalarAffinity.BOOL) { return(new Result(new BoolExpression(left.GetBoolValue() == right.GetBoolValue()))); } else if (left.GetScalarAffinity() == ScalarAffinity.DOUBLE || right.GetScalarAffinity() == ScalarAffinity.DOUBLE) { return(new Result(new BoolExpression(left.GetDoubleValue() == right.GetDoubleValue()))); } else if (left.GetScalarAffinity() == ScalarAffinity.LONG || right.GetScalarAffinity() == ScalarAffinity.LONG) { return(new Result(new BoolExpression(left.GetLongValue() == right.GetLongValue()))); } else { return(new Result(new BoolExpression(left.GetStringValue() == right.GetStringValue()))); } case BinaryOp.IDENTICAL: if (left.GetScalarAffinity() == ScalarAffinity.ARRAY && right.GetScalarAffinity() == ScalarAffinity.ARRAY) { return(new Result(new BoolExpression((left as ArrayPointerExpression)?.Array?.Name == (right as ArrayPointerExpression)?.Array?.Name))); } else if (left.GetScalarAffinity() == ScalarAffinity.OBJECT && right.GetScalarAffinity() == ScalarAffinity.OBJECT) { return(new Result(new BoolExpression((left as ObjectPointerExpression)?.Object?.Name == (right as ObjectPointerExpression)?.Object?.Name))); } else if (left.GetScalarAffinity() == ScalarAffinity.BOOL && right.GetScalarAffinity() == ScalarAffinity.BOOL) { return(new Result(new BoolExpression(left.GetBoolValue() == right.GetBoolValue()))); } else if (left.GetScalarAffinity() == ScalarAffinity.DOUBLE && right.GetScalarAffinity() == ScalarAffinity.DOUBLE) { return(new Result(new BoolExpression(left.GetDoubleValue() == right.GetDoubleValue()))); } else if (left.GetScalarAffinity() == ScalarAffinity.LONG && right.GetScalarAffinity() == ScalarAffinity.LONG) { return(new Result(new BoolExpression(left.GetLongValue() == right.GetLongValue()))); } else if (left.GetScalarAffinity() == ScalarAffinity.NULL && right.GetScalarAffinity() == ScalarAffinity.NULL) { return(new Result(new BoolExpression(left.GetLongValue() == right.GetLongValue()))); } else if (left.GetScalarAffinity() == ScalarAffinity.STRING && right.GetScalarAffinity() == ScalarAffinity.STRING) { return(new Result(new BoolExpression(left.GetStringValue() == right.GetStringValue()))); } else { return(new Result(new BoolExpression(false))); } case BinaryOp.LESS_THAN: if (left.GetScalarAffinity() == ScalarAffinity.DOUBLE || right.GetScalarAffinity() == ScalarAffinity.DOUBLE) { return(new Result(new BoolExpression(left.GetDoubleValue() < right.GetDoubleValue()))); } else if (left.GetScalarAffinity() == ScalarAffinity.LONG || right.GetScalarAffinity() == ScalarAffinity.LONG) { return(new Result(new BoolExpression(left.GetLongValue() < right.GetLongValue()))); } else { return(new Result(new BoolExpression(left.GetStringValue().CompareTo(right.GetStringValue()) < 0))); } case BinaryOp.AND: return(new Result(new BoolExpression(left.GetBoolValue() && right.GetBoolValue()))); case BinaryOp.OR: return(new Result(new BoolExpression(left.GetBoolValue() || right.GetBoolValue()))); default: Log.Error($"Unable to execute binary operation: {expression.Operation}"); return(Result.NULL); } }
public static Result Run(CallParameter expression, Scope scope) { return(Interpreters.Execute(expression.Value, scope)); }
public static Result Run(BreakExpression block, Scope scope) { throw new BreakException(Interpreters.Execute(block.CountOfLoops, scope).ResultValue.GetLongValue().Clamp(1, 1000)); }
public static Result Run(ReturnExpression block, Scope scope) { throw new ReturnException(Interpreters.Execute(block.ReturnValue, scope).ResultValue); }