public ForNode(string variable, AstNode start, AstNode end, AstNode step, AstNode body) { Variable = variable; Children.Add(start); Children.Add(end); Children.Add(step); Children.Add(body); }
/// <summary> /// Interpret the specified ast. /// </summary> /// <param name="ast">Ast.</param> public void Interpret(AstNode ast) { labels = new Dictionary<string, int>(); scanLabels(ast); for (position = 0; position < ast.Children.Count; position++) executeStatement(ast.Children[position]); }
private static AstNode parseFunctionCall(Parser parser, AstNode left) { if (parser.MatchToken(TokenType.Parentheses, "(")) { var functionCall = parseFunctionCall(parser, new FunctionCallNode(left, ArgListNode.Parse(parser))); //parser.ExpectToken(TokenType.Parentheses, ")"); return functionCall; } else return left; }
/// <summary> /// Initializes a new instance of the <see cref="TIBASIC.Parser.BinaryOpNode"/> class. /// </summary> /// <param name="binaryOperation">Binary operation.</param> /// <param name="left">Left.</param> /// <param name="right">Right.</param> public BinaryOpNode(BinaryOperation binaryOperation, AstNode left, AstNode right) { BinaryOperation = binaryOperation; Children.Add(left); Children.Add(right); }
/// <summary> /// Initializes a new instance of the <see cref="TIBASIC.Parser.RepeatNode"/> class. /// </summary> /// <param name="predicate">Predicate.</param> /// <param name="body">Body.</param> public RepeatNode(AstNode predicate, AstNode body) { Children.Add(predicate); Children.Add(body); }
private object evaluateNode(AstNode node) { if (node is NumberNode) return ((NumberNode)node).Number; else if (node is StringNode) return ((StringNode)node).String; else if (node is FunctionCallNode) { FunctionCallNode functionNode = (FunctionCallNode)node; IFunction target = evaluateNode(functionNode.Target) as IFunction; if (target == null) throw new Exception("Attempt to run a non-valid function!"); object[] arguments = new object[functionNode.Arguments.Children.Count]; for (int i = 0; i < functionNode.Arguments.Children.Count; i++) arguments[i] = evaluateNode(functionNode.Arguments.Children[i]); return target.Invoke(arguments); } else if (node is IdentifierNode) { string identifier = ((IdentifierNode)node).Identifier; if (identifier == "getKey") return readLine(true); if (variables.ContainsKey(identifier)) return variables[identifier]; else throw new Exception("Identifier: " + identifier + " is not a variable!"); } else if (node is BinaryOpNode) return interpretBinaryOperation((BinaryOpNode)node); else throw new Exception("Unexpected node in interpreter: " + node); }
private void scanLabels(AstNode ast) { for (int i = 0; i < ast.Children.Count; i++) { if (ast.Children[i] is LblNode) { string label = ((LblNode)ast.Children[i]).Label; if (labels.ContainsKey(label)) throw new Exception("Label: " + label + " is already declared!"); labels.Add(label, position); } } }
private void executeStatement(AstNode node) { if (node is CodeBlockNode) foreach (AstNode subNode in node.Children) executeStatement(subNode); else if (node is ConditionalNode) { var ifNode = (ConditionalNode)node; bool eval = (bool)evaluateNode(ifNode.Predicate); if (eval) executeStatement(ifNode.Body); else if (!eval && ifNode.Children.Count > 2) executeStatement(ifNode.ElseBody); } else if (node is WhileNode) { var whileNode = (WhileNode)node; while ((bool)evaluateNode(whileNode.Predicate)) executeStatement(whileNode.Body); } else if (node is RepeatNode) { RepeatNode repeatNode = (RepeatNode)node; do executeStatement(repeatNode.Body); while (!(bool)evaluateNode(repeatNode.Predicate)); } else if (node is ForNode) { var forNode = (ForNode)node; if (!variables.ContainsKey(forNode.Variable)) variables.Add(forNode.Variable, 0); for (variables[forNode.Variable] = evaluateNode(forNode.Start); Convert.ToDouble(variables[forNode.Variable]) < Convert.ToDouble(evaluateNode(forNode.End)); variables[forNode.Variable] = Convert.ToDouble(variables[forNode.Variable]) + Convert.ToDouble(evaluateNode(forNode.Step))) executeStatement(forNode.Body); } else if (node is DispNode) { var dispNode = (DispNode)node; foreach (AstNode subNode in dispNode.Args.Children) OnConsoleOutput(new ConsoleOutputEventArgs { Output = evaluateNode(subNode).ToString() }); OnConsoleOutput(new ConsoleOutputEventArgs { Output = "\n" }); } else if (node is InputNode) { foreach (AstNode subNode in ((InputNode)node).Variables.Children) { string variable = ((IdentifierNode)subNode).Identifier; if (variables.ContainsKey(variable)) variables.Remove(variable); variables.Add(variable, Console.ReadLine()); } } else if (node is PromptNode) { foreach (AstNode subNode in ((PromptNode)node).Variables.Children) { string variable = ((IdentifierNode)subNode).Identifier; if (variables.ContainsKey(variable)) variables.Remove(variable); Console.Write(variable + "? "); variables.Add(variable, Console.ReadLine()); } } else if (node is PrgmNode) Interpret(new Parser.Parser(new Scanner().Scan(File.ReadAllText(((PrgmNode)node).PrgmPath))).Parse()); else if (node is LblNode) ; else if (node is GotoNode) { string label = ((GotoNode)node).Label; if (!labels.ContainsKey(label)) throw new Exception("Label " + label + " does not exist!"); position = labels[label]; } else evaluateNode(node); }
/// <summary> /// Initializes a new instance of the <see cref="TIBASIC.Parser.FunctionCallNode"/> class. /// </summary> /// <param name="target">Target.</param> /// <param name="arguments">Arguments.</param> public FunctionCallNode(AstNode target, ArgListNode arguments) { Children.Add(target); Children.Add(arguments); }
/// <summary> /// Initializes a new instance of the <see cref="TIBASIC.Parser.ConditionalNode"/> class. /// </summary> /// <param name="predicate">Predicate.</param> /// <param name="body">Body.</param> /// <param name="elseBody">Else body.</param> public ConditionalNode(AstNode predicate, AstNode body, AstNode elseBody) { Children.Add(predicate); Children.Add(body); Children.Add(elseBody); }