Exemple #1
0
        public Dictionary <string, Result> GetArguments(MethodPointer p, List <Result> functionParamters, IRuntime runtime)
        {
            var mName = p.NameSpace + "." + p.Name;

            if (_procedures.ContainsKey(mName))
            {
                var functionRefernece = _procedures[mName];

                //// just check if user providede all paramters
                if (functionRefernece.ArgumentList?.Count != functionParamters?.Count)
                {
                    runtime.AddError(new RuntimeError(
                                         $"Invalid number of paramters passed to function {p.NameSpace}.{p.Name}. Expected {functionRefernece.ArgumentList?.Count} provided {functionParamters?.Count}"));
                }
                else
                {
                    var args = ExtractArguments(functionParamters, functionRefernece);
                    return(args);
                }
            }
            else
            {
                runtime.AddError(new RuntimeError($"Method not found {p.NameSpace}.{p.Name}"));
            }

            return(null);
        }
Exemple #2
0
        private Result AddTimer(Mnemonic expression, IRuntime runtime)
        {
            if (runtime.GetVariable("interval").Get(runtime) is Number interval)
            {
                var id = timerPool.Keys.Count > 0 ?
                         timerPool.Keys.Max() + 1
                    : 0;

                timerPool.Add(id, new Thread(() =>
                {
                    while (true)
                    {
                        Thread.Sleep(interval.ValueAsInt);

                        _uiEventsProvider.Send(new TimerElapsed()
                        {
                            Id = id
                        });
                    }
                    ;
                }));

                timerPool[id].Start();

                return(new Result(new Number(id)));
            }
            else
            {
                runtime.AddError(new RuntimeError("No interval provided", expression));
                return(new Result());
            }
        }
Exemple #3
0
        private Result Text(Mnemonic expression, IRuntime runtime)
        {
            var text         = "";
            var textArgument = runtime.GetVariable("text").Get(runtime);

            if (textArgument is String s)
            {
                text = s.Value;
            }
            else if (textArgument is Number i)
            {
                text = i.Value.ToString();
            }
            else
            {
                runtime.AddError(new RuntimeError("Type not supported", expression));
                return(new Result());
            }

            _renderingEngine.Perform(new Text
            {
                Label    = text,
                Position = new Point
                {
                    X = GetArgument <Number>("posx", runtime).Value,
                    Y = GetArgument <Number>("posy", runtime).Value
                }
            });

            return(new Result());
        }
Exemple #4
0
        public static Result ArgumentCalcualte(Mnemonic ctx, Result left, Result right,
                                               OperatorType kind, IRuntime runtime)
        {
            if (left.HasResult() && right.HasResult())
            {
                var leftArg  = left.Get(runtime);
                var rightArg = right.Get(runtime);

                if (leftArg is String leftString && rightArg is String rightString)
                {
                    if (new[] { OperatorType.Equals, OperatorType.GreaterThen, OperatorType.GreaterThenOrEqual,
                                OperatorType.LessThen, OperatorType.LessThenOrEqual }.Contains(kind))
                    {
                        return(ArgumentCalcualte <String, string>(leftString, rightString, kind));
                    }
                    else if (kind == OperatorType.Add)
                    {
                        return(new Result(new String(Convert.ToString(leftString.Value) + Convert.ToString(rightString.Value))));
                    }
                    else
                    {
                        runtime.AddError(new RuntimeError($"Operator not suppoerted {kind} on strings.", ctx));
                    }
                }
                else if (leftArg is Number leftInt && rightArg is Number rightInt)
                {
                    return(ArgumentCalculate(leftInt, rightInt, kind, runtime, ctx));
                }
Exemple #5
0
 private bool WhileCondition(Result result, IRuntime runtime)
 {
     if (result.HasResult() &&
         result.Get(runtime) is Number resultInt)
     {
         return(resultInt.Value == 1);
     }
     runtime.AddError(
         new RuntimeError($"Invalid data after evaulation of expression to use as a while condition.", this));
     return(false);
 }
Exemple #6
0
 protected T GetArgument <T>(string label, IRuntime runtime)
     where T : class, IValue
 {
     if (runtime.GetVariable(label).Get(runtime) is T result)
     {
         return(result);
     }
     runtime.AddError(
         new RuntimeError($"Argument {label} not provided or invalid type. Was expecting {typeof(T).Name}."));
     return(null);
 }
Exemple #7
0
        public IValue Get(IRuntime runtime)
        {
            if (_result != null)
            {
                return(_result);
            }
            runtime.AddError(
                new RuntimeError("Result not provided", null));

            return(null);
        }
Exemple #8
0
        public List <Mnemonic> GetCode(MethodPointer p, IRuntime runtime)
        {
            var mName = p.NameSpace + "." + p.Name;

            if (_procedures.ContainsKey(mName))
            {
                return(_procedures[mName].Code);
            }
            runtime.AddError(new RuntimeError($"Method not found {p.NameSpace}.{p.Name}"));

            return(null);
        }
Exemple #9
0
        public GlobalScope GetScope(MethodPointer p, IRuntime runtime)
        {
            var mName = p.NameSpace + "." + p.Name;

            if (_procedures.ContainsKey(mName))
            {
                return(_procedures[mName].RootScope);
            }

            runtime.AddError(new RuntimeError($"Method not found {p.NameSpace}.{p.Name}"));

            return(null);
        }
Exemple #10
0
 public void EvaluateMethodRegistrations(Compilation binary, IRuntime runtime)
 {
     try
     {
         Evaluate(
             binary.Mnemonics.Where(m => m is MethodDeclaration ||
                                    m is Assigment)
             .ToList());
     }
     catch (Exception exception)
     {
         runtime.AddError(new RuntimeError(exception.Message));
     }
 }
Exemple #11
0
        public Result Evaluate(Compilation binary, IRuntime runtime)
        {
            try
            {
                return(Evaluate(binary.Mnemonics.Where(m => !(m is MethodDeclaration ||
                                                              m is Assigment)).ToList()));
            }
            catch (Exception exception)
            {
                runtime.AddError(new RuntimeError(exception.Message));
            }

            return(new Result());
        }
Exemple #12
0
        public override Result Execute(IRuntime runtime)
        {
            var rightResult = runtime.EvaluateCodeBlock(RightSide);

            if (LeftSide is VariableName vn)
            {
                runtime.SetVariable(vn.Pointer.Name, rightResult.Get(runtime));
            }
            else if (LeftSide is ValueTable vt)
            {
                var leftSideIndexedType = runtime.GetReference(vt.TablePointer.Name);
                if (leftSideIndexedType != null)
                {
                    var value = leftSideIndexedType;
                    if (value is ILoplaIndexedValue)
                    {
                        var loplaList = value as ILoplaIndexedValue;
                        var idx       = runtime.EvaluateCodeBlock(vt.ElementPositionInTable).Get(runtime) as Number;
                        loplaList.Set(idx.ValueAsInt, rightResult.Get(runtime));
                        runtime.SetVariable(vt.TablePointer.Name, loplaList);
                    }
                    else
                    {
                        runtime.AddError(new RuntimeError($"Cannot handle {vt.TablePointer.Name} like an array.",
                                                          this));
                    }
                }
                else
                {
                    runtime.AddError(new RuntimeError($"Table {vt.TablePointer.Name} must be initialized before use.",
                                                      this));
                }
            }

            return(new Result());
        }
Exemple #13
0
        public void Register(MethodPointer name, Method method, IRuntime runtime, string processingStackName, GlobalScope scope)
        {
            var methodName = $"{name.NameSpace}.{name.Name}";

            if (!_procedures.ContainsKey(methodName))
            {
                method.GlobalScopeName = processingStackName;
                method.RootScope       = scope;
                _procedures.Add(methodName, method);
            }
            else
            {
                runtime.AddError(new LinkingError($"Failed to reregister method {methodName} (already declared)"));
            }
        }
Exemple #14
0
        public override Result Execute(IRuntime runtime)
        {
            var value = runtime.GetReference(TablePointer.Name);

            if (value != null)
            {
                if (value is ILoplaIndexedValue tableInstance)
                {
                    if (runtime.EvaluateCodeBlock(ElementPositionInTable).Get(runtime) is Number idx)
                    {
                        var i = idx.ValueAsInt;
                        if (i >= 0 && i < tableInstance.Length())
                        {
                            return(new Result(tableInstance.Get(i)));
                        }
                        else
                        {
                            runtime.AddError(new RuntimeError($"Index {i} was out of bounds for {TablePointer.Name}", this));
                        }
                    }
                    else
                    {
                        runtime.AddError(new RuntimeError($"Index {TablePointer.Name} is not a number", this));
                    }
                }
                else
                {
                    runtime.AddError(new RuntimeError($"Value {TablePointer.Name} is not an array", this));
                }
            }
            else
            {
                runtime.AddError(new RuntimeError($"Value not defined {TablePointer.Name}", this));
            }
            return(new Result());
        }
Exemple #15
0
        public override Result Execute(IRuntime runtime)
        {
            Result result = null;

            try
            {
                result = _library.Call(_action, this, runtime);
            }
            catch (Exception e)
            {
                runtime.AddError(new RuntimeError(e.Message));
            }

            return(result);
        }
Exemple #16
0
        public Result Write(Mnemonic expression, IRuntime runtime)
        {
            var arg = runtime.GetVariable("text");

            if (arg.Get(runtime) is String line)
            {
                Console.Write(_prefix + line.Value);
            }
            else if (arg.Get(runtime) is Number i)
            {
                Console.Write(_prefix + i.Value);
            }
            else
            {
                runtime.AddError(new RuntimeError($"Type not supported {arg.Get(runtime).GetType().Name}"));
            }
            return(new Result());
        }
Exemple #17
0
        public override Result Execute(IRuntime runtime)
        {
            var left = runtime.EvaluateCodeBlock(Arguments.Args[0]);

            if (Arguments.Args.Count > 1)
            {
                if (Arguments.Args[1] is Operator op)
                {
                    var right = runtime.EvaluateCodeBlock(Arguments.Args[2]);
                    return(Expression.ArgumentCalcualte(this, left, right, op.Kind, runtime));
                }
                else
                {
                    runtime.AddError(new RuntimeError("Operator not provided.", this));
                }
            }

            return(left);
        }
Exemple #18
0
        public Result Evaluate(Mnemonic mnemonic)
        {
            if (_requestForStop)
            {
                return(new Result());
            }

            Result result = null;

            try
            {
                result = OnMnemonicExecute(mnemonic, _runtime);
            }
            catch (Exception e)
            {
                _runtime.AddError(new RuntimeError(e.Message, mnemonic));
            }

            return(result);
        }
Exemple #19
0
        public override Result Execute(IRuntime runtime)
        {
            var incomingParamters = Arguments.Args.ToList();
            var methodParamters   = new List <Result>();


            foreach (var argument in incomingParamters)
            {
                var result = runtime.EvaluateCodeBlock(argument);
                if (result.HasResult())
                {
                    methodParamters.Add(result);
                }
                else
                {
                    runtime.AddError(new RuntimeError($"Too much results or incorrect result {Pointer}", this));
                }
            }

            return(runtime.EvaluateMethodCall(Pointer, methodParamters));
        }
Exemple #20
0
        public IValue GetReference(string name, IRuntime runtime)
        {
            IValue val = null;

            foreach (var scope in ScopesStack)
            {
                val = scope.Mem.Get(new VariablePointer
                {
                    Name = name
                });
                if (val != null)
                {
                    break;
                }
            }

            if (val == null)
            {
                runtime.AddError(new RuntimeError($"Value not defined {name}."));
            }
            return(val);
        }
Exemple #21
0
        public override Result Execute(IRuntime runtime)
        {
            var ifcheck = runtime.EvaluateCodeBlock(Condition);

            Result result = null;

            if (ifcheck?.HasResult() == true &&
                ifcheck.Get(runtime) is Number resultInt)
            {
                if (resultInt.Value != 0)
                {
                    result = runtime.EvaluateCodeBlock(BlockOfCode);
                }
            }
            else
            {
                runtime.AddError(new RuntimeError(
                                     $"Invalid data after evaulation of if check expression. Exptected {nameof(Number)}. "
                                     , this));
            }

            return(result);
        }