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); } }
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; }
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"); } }
public bool Evaluate(ExecEnvironment environment) { return(ExpressionEvaluator.EvaluateSimpleConditional( leftExpression.Evaluate(environment), rightExpression.Evaluate(environment), equalityOperator )); }
public Value Evaluate(ExecEnvironment environment) { var variable = GetVariable(environment); if (Left is null || Right is null) { return(variable.Value); }
public Value Evaluate(ExecEnvironment environment) { return(ExpressionEvaluator.Evaluate( leftExpression.Evaluate(environment), rightExpression.Evaluate(environment), additiveOperator )); }
public void Execute(ExecEnvironment environment) { while (conditional.Evaluate(environment)) { if (environment.ReturnFlag) { break; } statement.Execute(environment); } }
public void Execute(ExecEnvironment environment) { for (first.Execute(environment); conditional.Evaluate(environment); second.Execute(environment)) { if (environment.ReturnFlag) { break; } statement.Execute(environment); } }
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); } }
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); }
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(); }
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; }
public void Execute(ExecEnvironment environment) { if (conditional.Evaluate(environment)) { statementIf.Execute(environment); } else { if (environment.ReturnFlag) { return; } statementElse?.Execute(environment); } }
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; }
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); }
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); }
public bool Evaluate(ExecEnvironment environment) => isNegated ? !parenConditional.Evaluate(environment) : parenConditional.Evaluate(environment);
public bool Evaluate(ExecEnvironment environment) => leftConditional.Evaluate(environment) || rightConditional.Evaluate(environment);
public Value Evaluate(ExecEnvironment environment) => isNegated? ExpressionEvaluator.GetNegative(expression.Evaluate(environment)) : expression.Evaluate(environment);
public void Execute(ExecEnvironment environment) { this.Evaluate(environment); }