Example #1
0
            public override dynamic ExecuteCall(Ast_Scope scope)
            {
                var argLst = Args[0].Value.Value;
                var sb     = new StringBuilder();

                foreach (var v in argLst)
                {
                    if (v is Ast_Variable)
                    {
                        Ast_Variable variable = v as Ast_Variable;
                        // ToDo: Perhaps verify if i need to get the var from the scope to actually check!
                        dynamic value;
                        if (variable.Index != null)
                        {
                            value = variable.DoGetValue(variable.Index, scope);
                        }
                        else
                        {
                            value = variable.DoGetValue();
                        }
                        sb.Append(value.ToString());
                    }
                    else
                    {
                        sb.Append(v.Value);
                    }
                    sb.Append(' ');
                }
                if (sb.Length > 1)
                {
                    sb.Remove(sb.Length - 1, 1);
                }
                Console.WriteLine(sb.ToString());
                return(false);
            }
Example #2
0
        public override dynamic Execute(Ast_Scope scope)
        {
            var child = scope.CreateChild("while.execute");

            child.IsLoop = true;
            child.InLoop = true;
            while (child.InLoop)
            {
                var value = Expression.Execute(scope);
                if (value == true || (value != null && value != false && value != 0))
                {
                    foreach (Ast_Base fi in Block)
                    {
                        var res = fi.Execute(child);
                        if (res == true || res is Ast_Terminate)
                        {
                            child.InLoop = false;
                            return(true);
                        }
                    }
                }
                else
                {
                    child.InLoop = false;
                }
            }
            return(false);
        }
Example #3
0
 private void ShowScopeChildren(Ast_Scope scope, TreeNode st, TreeView tv)
 {
     foreach (var child in scope.Children)
     {
         ShowScopeTree(child, st, tv);
     }
 }
Example #4
0
        public override dynamic Execute(Ast_Scope scope)
        {
            Ast_Lambda exec;
            Ast_Scope  execScope = scope;

            if (scope.VariableExists(Name))
            {
                var v     = scope.GetVariable(Name);
                var value = v.Value;
                exec = value.Value;
                //if (Name.Contains('.'))
                //{
                //    execScope = scope.GetStructScope(Name);
                //}
            }
            else
            {
                exec = Libraries.GetMethodOrFunction(Name);
                exec.Execute(execScope); // run the prepwork.
            }
            if (exec == null)
            {
                throw new SyntaxError(Token, $"Function or procedure not found ({Name}).");
            }

            if (Block?.Count != exec.Args.Count)
            {
                if (!exec.Args.ContainsParams())
                {
                    throw new SyntaxError(Token, "Invalid number of parameters.");
                }
            }

            var i = 0;

            foreach (Ast_Expression expr in Block)
            {
                var expressionValue = expr.Execute(execScope);

                if (exec.Args[i].Value.Type == ValueType.Params)
                {
                    exec.Args[i].Value.Value.Add
                        (new VT_Any {
                        Value = expressionValue
                    });
                }
                else
                {
                    exec.Args[i].DoSetValue(expressionValue);
                    i++;
                }
            }

            if (exec.Type == AstType.Function || exec.Type == AstType.Procedure)
            {
                return(exec.ExecuteCall(execScope));
            }
            return(exec.Execute(execScope));
        }
Example #5
0
        public override dynamic Execute(Ast_Scope scope)
        {
            var value = Expression.Execute(scope);

            if (value == true || (value != null && value != false && value != 0))
            {
                return(ExecuteElif(scope));
            }
            return(false);
        }
Example #6
0
        public string ToString(Ast_Scope scope)
        {
            var idx = Execute(scope);
            var sb  = new StringBuilder();

            foreach (var i in idx)
            {
                sb.Append($"[{i}]");
            }
            return(sb.ToString());
        }
Example #7
0
        public override dynamic Execute(Ast_Scope scope)
        {
            var res = new List <dynamic>();

            foreach (var expr in Block)
            {
                Ast_Expression expression = expr as Ast_Expression;
                res.Add(expression.Execute(scope));
            }
            return(res);
        }
Example #8
0
 public override dynamic Execute(Ast_Scope scope)
 {
     foreach (Ast_Base fi in Block)
     {
         var res = fi.Execute(scope);
         if (res is Ast_Terminate || res == true)
         {
             return(true);
         }
     }
     return(false);
 }
Example #9
0
 public override dynamic Execute(Ast_Scope scope)
 {
     if (!scope.IsLoop)
     {
         var lscope = Ast_Scope.GetIteratorScope(scope);
         if (lscope == null)
         {
             throw new RuntimeError(Token, "Break used outside loop.");
         }
     }
     return(Ast_Terminate.Break);
 }
Example #10
0
 private dynamic GetContainerValue(Ast_Scope scope)
 {
     if (Container.Token.Type == TokenType.ConstantString)
     {
         return(Container.Token.Lexeme);
     }
     if (Container.Type == AstType.Variable)
     {
         var variable = (Ast_Variable)Container;
         var v        = scope.GetVariable(variable.Name);
         return(v);
     }
     throw new RuntimeError(Container.Token, $"Invalid foreach container type {Container.Token.Type}");
 }
Example #11
0
        public dynamic ExecuteIf(Ast_Scope scope)
        {
            var     child = scope.CreateChild("if.executeif");
            dynamic res   = false;

            foreach (Ast_Base fi in Block)
            {
                res = fi.Execute(child);
                if (res is Ast_Terminate)
                {
                    return(true);
                }
            }
            return(res);
        }
Example #12
0
        public dynamic ExecuteElif(Ast_Scope scope)
        {
            var     child = scope.CreateChild($"{scope.Name}->ExecuteElIf");
            dynamic res   = false;

            foreach (Ast_Base fi in Block)
            {
                res = fi.Execute(child);
                if (res is Ast_Terminate || res == true)
                {
                    return(true);
                }
            }
            return(res);
        }
Example #13
0
        public override dynamic Execute(Ast_Scope scope)
        {
            Ast_Scope blockScope = scope.CreateChild("block");
            dynamic   result     = false;

            foreach (var instruction in Block)
            {
                result = instruction.Execute(blockScope);
                if (result is Ast_Terminate)
                {
                    break;
                }
            }
            return(result);
        }
Example #14
0
 public bool ExecuteElifBlock(Ast_Scope scope)
 {
     if (ElIf == null)
     {
         return(false);
     }
     foreach (Ast_Base elif in ElIf.Block)
     {
         if (elif.Execute(scope))
         {
             return(true);
         }
     }
     return(false);
 }
Example #15
0
        public override dynamic Execute(Ast_Scope scope)
        {
            var value = Exitcode.Execute(scope);
            var estr  = $"Program terminated at {Token.Line}, {Token.Offset}. Exit code: {value}.";
            var v     = scope.Variables["exitcode"];

            if (v == null)
            {
                v = new Ast_Variable(null)
                {
                    Name = "exitcode"
                };
                scope.Variables.Add(v);
            }
            v.DoSetValue(estr);
            return(Ast_Terminate.Halt);
        }
Example #16
0
        public override dynamic ExecuteCall(Ast_Scope scope)
        {
            if (procScope == null)
            {
                procScope             = scope.CreateChild($"{Token.Lexeme}");
                procScope.CanSearchUp = false;
            }

            foreach (var arg in Args)
            {
                procScope.Variables.Append(arg.Token.Lexeme, arg.Value.Value);
            }
            foreach (var instruction in Block)
            {
                instruction.Execute(procScope);
            }
            return(false);
        }
Example #17
0
 public override dynamic ExecuteCall(Ast_Scope scope)
 {
     if (funcScope == null)
     {
         funcScope             = scope.CreateChild($"{Token.Lexeme}");
         funcScope.CanSearchUp = false;
     }
     foreach (var arg in Args)
     {
         funcScope.Variables.Append(arg.Token.Lexeme, arg.Value.Value);
     }
     funcScope.Variables.Append(ReturnVariable.Token.Lexeme, null);
     foreach (var instruction in Block)
     {
         instruction.Execute(funcScope);
     }
     return(funcScope.Variables[ReturnVariable.Token.Lexeme].DoGetValue());
 }
Example #18
0
        public override dynamic Execute(Ast_Scope scope)
        {
            var value = Expression.Execute(scope);

            if (value == true || (value != null && value != false && value != 0))
            {
                return(ExecuteIf(scope));
            }
            if (ElIf?.Block.Count > 0 && ExecuteElifBlock(scope))
            {
                return(false);
            }
            if (Else?.Block.Count > 0)
            {
                Else.Execute(scope);
            }
            return(false);
        }
Example #19
0
        private void ShowScopeTree(Ast_Scope scope, TreeNode tvn, TreeView tv)
        {
            TreeNode st = new TreeNode($"scope {scope}");

            AddScopeNode(st, tvn, tv);
            var nv = st.Nodes.Add("Variables");

            foreach (Ast_Variable v in scope.Variables)
            {
                var varnode = nv.Nodes.Add(v.ToString());
                if (v.Value.Type == ValueType.Array || v.Value.Type == ValueType.Record || v.Value.Type == ValueType.Params)
                {
                    ShowVarArrayTree(v, varnode);
                }
            }
            var cn = st.Nodes.Add("Scope");

            ShowScopeChildren(scope, cn, tv);
        }
Example #20
0
        public override dynamic Execute(Ast_Scope scope)
        {
            if (Block.Count == 0)
            {
                return(false);                  // Gatekeeper, don't run if there are no instructions in the block.
            }
            dynamic   containerValue = GetContainerValue(scope);
            Ast_Scope childScope     = scope.CreateChild("foreach");

            childScope.InLoop = true;
            childScope.IsLoop = true;
            Ast_Variable it = new Ast_Variable(null)
            {
                Name = "it"
            };

            childScope.Variables.Add(it);
            if (containerValue is string)
            {
                ExecuteForeachLoop(containerValue, childScope, it);
            }
            else if (containerValue.Type == AstType.Variable && containerValue.Value.Type == ValueType.Record)
            {
                ExecuteForeachLoop(containerValue.Value.Value.Keys, childScope, it);
            }
            else if (containerValue.Type == AstType.Variable && containerValue.Value.Type == ValueType.Array)
            {
                ExecuteForeachLoop(containerValue.Value.Value, childScope, it);
            }
            else if (containerValue.Type == AstType.Variable && containerValue.Value.Type == ValueType.String)
            {
                ExecuteForeachLoop(containerValue.Value.Value, childScope, it);
            }
            else
            {
                throw new RuntimeError(Token, "Invalid container value type");
            }
            childScope.InLoop = false;
            childScope.Variables.Remove(it);
            return(false);
        }
Example #21
0
            public override dynamic ExecuteCall(Ast_Scope scope)
            {
                if (Args?.Count != 1)
                {
                    throw new RuntimeError(Token, "Length requires 1 iterable variable as argument.");
                }
                var value = Args[0].DoGetValue();

                if (value is string)
                {
                    return(value.Length);
                }
                else if (value is IList || value is ICollection)
                {
                    return(value.Count);
                }
                else
                {
                    throw new RuntimeError(Token, $"Invalid function call: Length argument is invalid type ({value.GetType()}).");
                }
            }
Example #22
0
 private void ExecuteForeachLoop(dynamic container, Ast_Scope currentScope, Ast_Variable it)
 {
     foreach (var fo in container)
     {
         if (fo is VT_Any)
         {
             it.DoSetValue(fo.Value);
         }
         else
         {
             it.DoSetValue(fo);
         }
         foreach (var fi in Block)
         {
             var res = fi.Execute(currentScope);
             if (res is Ast_Terminate || res == true)
             {
                 break;
             }
         }
     }
 }
Example #23
0
 public override dynamic Execute(Ast_Scope scope)
 {
     throw new NotImplementedException();
 }
Example #24
0
 public override dynamic Execute(Ast_Scope scope)
 {
     Args[0]?.Value?.Value?.Clear();
     return(false);
 }
Example #25
0
 public override dynamic Execute(Ast_Scope scope)
 {
     return(false);
 }
Example #26
0
 public override dynamic Execute(Ast_Scope scope)
 {
     scope.Variables.Append(Token.Lexeme, this);
     return(false);
 }
Example #27
0
 public virtual dynamic ExecuteCall(Ast_Scope scope)
 {
     throw new NotImplementedException();
 }
Example #28
0
 public virtual dynamic Execute(Ast_Scope scope)
 {
     throw new RuntimeError(Token.ErrorToken, "Execute function not implemented.");
 }
Example #29
0
        public override dynamic Execute(Ast_Scope scope)
        {
            dynamic value = Expression.Execute(scope);

            if (value is Ast_Procedure procedure)
            {
                value = procedure.Clone(Variable.Token);
            }
            else if (value is Ast_Function function)
            {
                value = function.Clone(Variable.Token);
            }
            else if (value is Ast_Struct strct)
            {
                value = strct.Clone(Variable.Token);
            }
            else if (value is List <VT_Any> )
            {
                value = CloneArray(value);
            }
            else if (value is Dictionary <string, VT_Any> )
            {
                value = CloneRecord(value);
            }

            dynamic index = null;

            if (Variable.Index != null)
            {
                index = Variable.Index;
            }

            Ast_Variable ScopeVar;
            var          ve = scope.VariableExists(Variable.Name);

            if (!ve)
            {
                ScopeVar = scope.Variables.Append(Variable.Name, value);
            }
            else
            {
                ScopeVar = scope.GetVariable(Variable.Name);
            }

            if (index != null)
            {
                dynamic indexedv = null;
                if (Operand.Type != TokenType.OpAssign)
                {
                    indexedv = ScopeVar.DoGetValue(index, scope);
                }
                switch (Operand.Type)
                {
                case TokenType.OpAssign:
                    ScopeVar.DoSetValue(value, index, scope);
                    break;

                case TokenType.OpAssignAdd:
                    ScopeVar.DoSetValue(indexedv + value, index, scope);
                    break;

                case TokenType.OpAssignDivide:
                    if (value == 0)
                    {
                        throw new RuntimeError(Token, "Division by zero.");
                    }
                    ScopeVar.DoSetValue(indexedv / value, index, scope);
                    break;

                case TokenType.OpAssignMultiply:
                    ScopeVar.DoSetValue(indexedv * value, index, scope);
                    break;

                case TokenType.OpAssignSubtract:
                    ScopeVar.DoSetValue(indexedv - value, index, scope);
                    break;
                }
            }
            else
            {
                dynamic v = null;
                if (Operand.Type != TokenType.OpAssign)
                {
                    v = ScopeVar.DoGetValue();
                }
                switch (Operand.Type)
                {
                case TokenType.OpAssign:
                    ScopeVar.DoSetValue(value);
                    break;

                case TokenType.OpAssignAdd:
                    ScopeVar.DoSetValue(v + value);
                    break;

                case TokenType.OpAssignDivide:
                    if (value == 0)
                    {
                        throw new RuntimeError(Token, "Division by zero.");
                    }
                    ScopeVar.DoSetValue(v / value);
                    break;

                case TokenType.OpAssignMultiply:
                    ScopeVar.DoSetValue(v * value);
                    break;

                case TokenType.OpAssignSubtract:
                    ScopeVar.DoSetValue(v - value);
                    break;
                }
            }
            return(false);
        }
Example #30
0
 public abstract dynamic Execute(Ast_Scope scope);