Esempio n. 1
0
        protected void RegisterParameters(ExecEnvironment environment, IEnumerable <Value> arguments)
        {
            if (arguments == null || parameters == null)
            {
                return;
            }
            if (arguments.Count() != parameters.Count())
            {
                throw new RuntimeException("Wrong number of parameters");
            }

            var parametersList = parameters.Zip(arguments);
            var variables      = new List <Variable>();

            foreach (var param in parametersList)
            {
                if (param.First.Type != param.Second.Type)
                {
                    throw new RuntimeException("Cannot cast " + param.Second.Type + " to " + param.First.Type);
                }
                var variable = new Variable(param.First.Type, param.First.Name);
                variable.Value = param.Second;
                variables.Add(variable);
            }
            foreach (var value in variables)
            {
                environment.AddVariable(value);
            }
        }
Esempio n. 2
0
        public void Execute(ExecEnvironment environment)
        {
            Variable variable = derefVariable.GetVariable(environment);
            Value    right    = expression.Evaluate(environment);

            if (variable.Type == TypeValue.Matrix)
            {
                var matrix = variable.Value as Matrix_t;
                if (derefVariable.Left != null && derefVariable.Right != null)
                {
                    AssignToMatrixByRange(environment, matrix, right);
                }
                else
                {
                    AssignToMatrix(environment, variable, right);
                }
                return;
            }
            if (variable.Type != right.Type)
            {
                throw new RuntimeException("Cannot cast " + right.Type + " to " + variable.Type);
            }

            variable.Value = (assignmentOperator.Operator != "=") ?
                             ExpressionEvaluator.EvaluateArthmeticAssignment(variable.Value, right, assignmentOperator) :
                             right;
        }
Esempio n. 3
0
        private void AssignToMatrixByRange(ExecEnvironment environment, Matrix_t matrix, Value value)
        {
            Int_t left1  = derefVariable.Left.FirstExpr.Evaluate(environment) as Int_t;
            Int_t left2  = derefVariable.Left.SecondExpr.Evaluate(environment) as Int_t;
            Int_t right1 = derefVariable.Right.FirstExpr.Evaluate(environment) as Int_t;
            Int_t right2 = derefVariable.Right.SecondExpr.Evaluate(environment) as Int_t;

            if (left1 is null || left2 is null || right1 is null || right2 is null)
            {
                throw new RuntimeException("Index has to be integer");
            }

            try {
                if (left1.Value == left2.Value && right1.Value == right2.Value)
                {
                    var intValue = value as Int_t;
                    if (intValue is null)
                    {
                        throw new RuntimeException("Cannot assign to int variable");
                    }
                    matrix[left1.Value, right1.Value] = intValue.Value;
                }
                else
                {
                    throw new ArthmeticOperationException("Matrix with range can be only r-value");
                }
            }
            catch (Exception) {
                throw new RuntimeException("Index out of range");
            }
        }
Esempio n. 4
0
 public bool Evaluate(ExecEnvironment environment)
 {
     return(ExpressionEvaluator.EvaluateSimpleConditional(
                leftExpression.Evaluate(environment),
                rightExpression.Evaluate(environment),
                equalityOperator
                ));
 }
Esempio n. 5
0
        public Value Evaluate(ExecEnvironment environment)
        {
            var variable = GetVariable(environment);

            if (Left is null || Right is null)
            {
                return(variable.Value);
            }
Esempio n. 6
0
 public Value Evaluate(ExecEnvironment environment)
 {
     return(ExpressionEvaluator.Evaluate(
                leftExpression.Evaluate(environment),
                rightExpression.Evaluate(environment),
                additiveOperator
                ));
 }
Esempio n. 7
0
 public void Execute(ExecEnvironment environment)
 {
     while (conditional.Evaluate(environment))
     {
         if (environment.ReturnFlag)
         {
             break;
         }
         statement.Execute(environment);
     }
 }
Esempio n. 8
0
 public void Execute(ExecEnvironment environment)
 {
     for (first.Execute(environment); conditional.Evaluate(environment); second.Execute(environment))
     {
         if (environment.ReturnFlag)
         {
             break;
         }
         statement.Execute(environment);
     }
 }
Esempio n. 9
0
 public override void Execute(ExecEnvironment environment, IEnumerable <Value> arguments)
 {
     if (arguments == null || arguments.Count() <= 0)
     {
         throw new RuntimeException("Function: print requires one argument");
     }
     foreach (var expr in arguments)
     {
         System.Console.WriteLine(expr);
     }
 }
Esempio n. 10
0
        public void Execute(ExecEnvironment environment)
        {
            if (variable.Type == TypeValue.Matrix)
            {
                AssignMatrix(environment);
            }
            else if (!(expression is null))
            {
                variable.Value = expression.Evaluate(environment);
            }

            environment.AddVariable(variable);
        }
Esempio n. 11
0
        public virtual void Execute(ExecEnvironment environment, IEnumerable <Value> arguments)
        {
            environment.OnFunctionCall();
            RegisterParameters(environment, arguments);

            blockStatement.Execute(environment);

            if (environment.ReturnFlag && Type == TypeValue.Void)
            {
                throw new RuntimeException("Function must return result");
            }
            environment.OnReturnFromFunction();
        }
Esempio n. 12
0
        private void AssignToMatrix(ExecEnvironment environment, Variable variable, Value right)
        {
            if (right.Type != TypeValue.Matrix)
            {
                throw new RuntimeException("Cannot cast " + right.Type + " to " + TypeValue.Matrix);
            }
            var value  = right as Matrix_t;
            var matrix = variable.Value as Matrix_t;

            if (matrix.Value.SizeX != value.Value.SizeX || matrix.Value.SizeY != value.Value.SizeY)
            {
                throw new RuntimeException("Sizes of matrices is various");
            }
            variable.Value = value;
        }
Esempio n. 13
0
 public void Execute(ExecEnvironment environment)
 {
     if (conditional.Evaluate(environment))
     {
         statementIf.Execute(environment);
     }
     else
     {
         if (environment.ReturnFlag)
         {
             return;
         }
         statementElse?.Execute(environment);
     }
 }
Esempio n. 14
0
        private void AssignMatrix(ExecEnvironment environment)
        {
            Int_t first  = variable.First.Evaluate(environment) as Int_t;
            Int_t second = variable.Second.Evaluate(environment) as Int_t;

            if (first is null || second is null)
            {
                throw new RuntimeException("Cannot define matrix without size");
            }
            if (!(expression is null))
            {
                var matrix = expression.Evaluate(environment) as Matrix_t;
                if (matrix.Value.SizeX != first.Value || matrix.Value.SizeY != second.Value)
                {
                    throw new RuntimeException("Sizes of matrices is various");
                }
                variable.Value = matrix;
            }
Esempio n. 15
0
        public int Execute()
        {
            var environment = new ExecEnvironment(functions);

            foreach (var def in definitions)
            {
                def.Execute(environment);
            }
            Function main = environment.GetFunctionByName("main");

            if (main is null)
            {
                throw new RuntimeException("Cannot find main function");
            }
            main.Execute(environment, null);
            var returnedValue = (environment.GetReturnedValue() as Int_t);

            return(returnedValue is null ?
                   throw new RuntimeException("Wrong return type") :
                   returnedValue.Value);
        }
Esempio n. 16
0
        public Value Evaluate(ExecEnvironment environment)
        {
            Function fun    = environment.GetFunctionByName(name);
            var      values = new List <Value>();

            foreach (var arg in arguments)
            {
                values.Add(arg.Evaluate(environment));
            }
            fun.Execute(environment, values);
            if (fun.Type == TypeValue.Void)
            {
                return(null);
            }
            var value = environment.GetReturnedValue();

            if (value.Type != fun.Type)
            {
                throw new RuntimeException("Wrong return type");
            }
            return(value);
        }
Esempio n. 17
0
 public bool Evaluate(ExecEnvironment environment)
 => isNegated ? !parenConditional.Evaluate(environment) : parenConditional.Evaluate(environment);
Esempio n. 18
0
 public bool Evaluate(ExecEnvironment environment)
 => leftConditional.Evaluate(environment) || rightConditional.Evaluate(environment);
Esempio n. 19
0
 public Value Evaluate(ExecEnvironment environment)
 => isNegated?
 ExpressionEvaluator.GetNegative(expression.Evaluate(environment))
     : expression.Evaluate(environment);
Esempio n. 20
0
 public void Execute(ExecEnvironment environment)
 {
     this.Evaluate(environment);
 }