private object EvaluateCallExpression(BoundCallExpression node) { if (node.Function == BuiltinFunctions.Version) { Console.WriteLine(info.Version); return(null); } else { var locals = new Dictionary <VariableSymbol, object>(); for (int i = 0; i < node.Arguments.Length; i++) { var parameter = node.Function.Parameters[i]; var value = EvaluateExpression(node.Arguments[i]); locals.Add(parameter, value); } _locals.Push(locals); var statement = _functions[node.Function]; var result = EvaluateStatement(statement); _locals.Pop(); return(result); } }
private object EvaluateCallExpression(BoundCallExpression node) { if (node.Function == BuiltinFunctions.Input) { return(Console.ReadLine()); } else if (node.Function == BuiltinFunctions.Print) { var message = (string)EvaluateExpression(node.Arguments[0]); Console.WriteLine(message); return(null); } else if (node.Function == BuiltinFunctions.Random) { var max = (int)EvaluateExpression(node.Arguments[0]); random = random ?? new Random(); return(random.Next(max)); } else { throw new Exception($"Unexpected function {node.Function}"); } }
private void EmitCallExpression(ILProcessor ilProcessor, BoundCallExpression node) { foreach (var argument in node.Arguments) { EmitExpression(ilProcessor, argument); } if (node.Function == BuiltinFunctions.Input) { ilProcessor.Emit(OpCodes.Call, _consoleReadLineReference); } else if (node.Function == BuiltinFunctions.Print) { ilProcessor.Emit(OpCodes.Call, _consoleWriteLineReference); } else if (node.Function == BuiltinFunctions.Rnd) { throw new NotImplementedException(); } else { var methodDefinition = _methods[node.Function]; ilProcessor.Emit(OpCodes.Call, methodDefinition); } }
private object?EvaluateCallExpression(BoundCallExpression node) { if (node.Function is DeclaredFunctionSymbol declaredFunctionSymbol) { var locals = new Dictionary <VariableSymbol, object>(); for (var i = 0; i < node.Arguments.Length; i++) { var parameter = declaredFunctionSymbol.Parameters[i]; var value = this.EvaluateExpression(node.Arguments[i]) !; locals.Add(parameter, value); } this.Locals.Push(locals); var statement = this.Functions[declaredFunctionSymbol]; var result = this.EvaluateStatement(statement); _ = this.Locals.Pop(); return(result); } else if (node.Function is BuiltinFunctionSymbol builtinFunctionSymbol) { if (builtinFunctionSymbol == BuiltinFunctions.Input) { return(Console.ReadLine()); } else if (builtinFunctionSymbol == BuiltinFunctions.Print) { var value = this.EvaluateExpression(node.Arguments[0]) !; Console.WriteLine(value); return(null); } else if (builtinFunctionSymbol == BuiltinFunctions.Rnd) { var max = (int)this.EvaluateExpression(node.Arguments[0]) !; if (this.Random == null) { this.Random = new Random(); } return(this.Random.Next(max)); } else { throw new Exception(); } } else { throw new Exception(); } }
private object EvaluateCallExpression(BoundCallExpression node) { if (node.Function == BuiltinFunctions.Input) { return(Console.ReadLine()); } else if (node.Function == BuiltinFunctions.Print) { var message = (string)EvaluateExpression(node.Arguments[0]); Console.WriteLine(message); return(null); } else if (node.Function == BuiltinFunctions.Rnd) { var max = (int)EvaluateExpression(node.Arguments[0]); if (_random == null) { _random = new Random(); } return(_random.Next(max)); } else { var locals = new Dictionary <VariableSymbol, object>(); for (int i = 0; i < node.Arguments.Length; i++) { var parameter = node.Function.Parameters[i]; var value = EvaluateExpression(node.Arguments[i]); locals.Add(parameter, value); } _locals.Push(locals); var statement = _program.Functions[node.Function]; object result; try { result = EvaluateStatement(statement); } finally { _locals.Pop(); } return(result); } }
protected override BoundExpression RewriteCallExpression(BoundCallExpression node) { // since statements can exist inside of a block which is an argument, when we flatten the block statements // can get out of order if there are side effects in any of the arguments. In order to prevent this we need // to break out the evaluation of each argument and assign to a temporary variable in the correct order. // we can then access this temp variable later when we call the function var args = node.Arguments .Select(RewriteExpression) .Select(expr => CreateTemporary(expr, "ctemp")) .ToImmutableArray(); var rewritten = node.Expression == null ? null : this.RewriteExpression(node.Expression); return(new BoundCallExpression(node.Syntax, node.Method, rewritten, args)); }
private object EvaluateCallExpression(BoundCallExpression node) { if (node.Function == BuiltinFunction.Input) { return(Console.ReadLine()); } else if (node.Function == BuiltinFunction.Print) { var message = (string)EvaluateExpression(node.Arguments[0]); Console.Write(message); //TODO: fix bug in REPL that causes this to only print after being followed by a "printLn" call return(null); } else if (node.Function == BuiltinFunction.PrintLine) { var message = (string)EvaluateExpression(node.Arguments[0]); Console.WriteLine(message); return(null); } else if (node.Function == BuiltinFunction.Rnd) { var max = (int)EvaluateExpression(node.Arguments[0]); if (_random == null) { _random = new Random(); } return(_random.Next(max)); } else { var locals = new Dictionary <VariableSymbol, object>(); for (int i = 0; i < node.Arguments.Length; i++) { var parameter = node.Function.Parameters[i]; var value = EvaluateExpression(node.Arguments[i]); locals.Add(parameter, value); } _locals.Push(locals); var statement = _functions[node.Function]; var result = EvaluateStatement(statement); _locals.Pop(); return(result); } }
private static void WriteBoundCallExpression(this IndentedTextWriter writer, BoundCallExpression node) { writer.WriteFunction(node.Symbol !.Name); writer.Write('('); for (var i = 0; i < node.Arguments.Length; i++) { writer.WriteBoundNode(node.Arguments[i]); if (i < node.Arguments.Length - 1) { writer.Write(", "); } } writer.Write(')'); }
private object EvalulateCallExpression(BoundCallExpression node) { if (node.Function == BuiltinFunctions.Input) { return(Console.ReadLine()); } else if (node.Function == BuiltinFunctions.Print) { var message = (string)EvaluateExpression(node.Arguments[0]); Console.WriteLine(message); return(null); } else { throw new Exception($"Unknown function \"{node.Function.Name}\"."); } }
public override void VisitCallExpression(BoundCallExpression node) { _writer.WriteIdentifier(node.Method.Name); _writer.WritePunctuation("("); var iterator = node.Arguments.GetEnumerator(); if (iterator.MoveNext()) { while (true) { iterator.Current.Accept(this); if (!iterator.MoveNext()) { break; } _writer.WritePunctuation(", "); } } _writer.WritePunctuation(")"); }
public virtual void VisitCallExpression(BoundCallExpression node) => this.DefaultVisit(node);
public BoundCallStatement(BoundCallExpression call) { Call = call; }