package node.stmtNode; import node.*; import node.exprNode.*; import env.*; import type.*; import type.basicType.*; import runEnv.*; import runEnv.basicValue.*; import text.*; import java.io.*; public class BuiltinNode extends StmtNode { private ExprListNode actualParams; public BuiltinNode( ExprListNode actualParams ) { this.actualParams = actualParams; } public String toString() { return "builtin( " + actualParams + " )"; } public void genEnv( Env env ) { super.genEnv( env ); actualParams.genEnv( env ); } public void setType() { } public void checkType() { actualParams.checkType(); } public void eval( RunEnv runEnv ) throws ReturnException { Multiple params = actualParams.evalMultiple( runEnv ); builtin( params ); } private void builtin( Multiple params ) throws ReturnException { String name = params.getValue( 0 ).stringValue(); if ( "read".equals( name ) ) { try { throw new ReturnException( new CharValue( ( char ) System.in.read() ) ); } catch (IOException exception ) { throw new Error( "Unable to read system input" ); } } else if ( "print".equals( name ) ) { String s = params.getValue( 1 ).stringValue(); Print.interp().print( s ); } else if ( "println".equals( name ) ) { String s = params.getValue( 1 ).stringValue(); Print.interp().println( s ); } else if ( "chr".equals( name ) ) { int value = params.getValue( 1 ).intValue(); throw new ReturnException( new CharValue( ( char ) value ) ); } else if ( "ord".equals( name ) ) { char value = params.getValue( 1 ).charValue(); throw new ReturnException( new IntValue( ( int ) value ) ); } else throw new Error( "Unknown builtin( " + name + ", ... )" ); } }