Exemplo n.º 1
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));
        }
Exemplo n.º 2
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);
        }