// ISCNode members public void Evaluate(Scope scope) { // Predefined functinos if (Function.Name == "putchar") { foreach (IExprNode expr in Parameters) { expr.Evaluate(scope); Console.WriteLine(expr.IntValue); } return; } Scope globalScope = scope.GlobalScope; if (FuncDeclsContainer.Contains(Function)) throw new InterpreterException("Call to undefined function: " + Function.Name); Scope childScope = new Scope(globalScope); FuncDefNode funcDef = globalScope.FuncDefs.Get(Function); for (int i = 0; i < Parameters.Count; ++i) { Parameters[i].Evaluate(scope); childScope.Add(funcDef.Parameters[i].Name, new IntVariable(Parameters[i].IntValue)); } funcDef.Body.Evaluate(childScope); }
// ISCNode members public void Evaluate(Scope scope) { Debug.Assert(Left != null && Right != null, "Null operand in binary expression"); Left.Evaluate(scope); Right.Evaluate(scope); switch (Type) { case BinaryExpressionType.ADD: intValue_ = Left.IntValue + Right.IntValue; break; case BinaryExpressionType.SUB: intValue_ = Left.IntValue - Right.IntValue; break; case BinaryExpressionType.MUL: intValue_ = Left.IntValue * Right.IntValue; break; case BinaryExpressionType.DIV: intValue_ = Left.IntValue / Right.IntValue; break; case BinaryExpressionType.MOD: intValue_ = Left.IntValue % Right.IntValue; break; case BinaryExpressionType.ASSIGN: IdentNode identNode = Left as IdentNode; if (identNode == null) throw new InterpreterException("Left operand of '=' must be an lvalue"); if (!scope.IsDefined(identNode.Name)) throw new InterpreterException("Undefined variable: " + identNode.Name); intValue_ = Right.IntValue; scope.Get(identNode.Name).Value = intValue_; break; default: throw new InterpreterException("Unexpected expression type"); } }
// ISCNode members public void Evaluate(Scope scope) { foreach (ISCNode statement in Statements) { if (statement is CompoundStatementNode) { Scope childScope = new Scope(scope); statement.Evaluate(childScope); } else if (statement is IExprNode) { statement.Evaluate(scope); } else if (statement is VarDefNode) { scope.Add(statement as VarDefNode); } } // TODO: Evaluate compound statement }
// ISCNode members public void Evaluate(Scope parentScope) { // Nothing to do here }
public void Evaluate(Scope parentScope) { // Really nothing to do here }
public void Run() { Scope scope = new Scope(globalScope_); mainFuncDef_.Body.Evaluate(scope); }
// ISCNode members public void Evaluate(Scope parentScope) { }
// ISCNode members public void Evaluate(Scope scope) { if (!scope.IsDefined(Name)) throw new InterpreterException("Undefined variable: " + Name); intValue_ = scope.Get(Name).Value; }
public Scope(Scope parent = null) { Parent = parent; }
// ISCNode members public void Evaluate(Scope parentScope) { // TODO: evaluate root node }