package node.exprNode; import node.*; import env.*; import type.*; import type.basicType.*; import runEnv.*; import runEnv.basicValue.*; import text.*; public class CastNode extends ExprNode { private Type type; private ExprNode expr; public CastNode( Type type, ExprNode expr ) { this.type = type; this.expr = expr; precedence = PREC_PREFIX; } public String toString() { return "( " + type + " ) " + expr.toString( precedence ); } public void genEnv( Env env ) { super.genEnv( env ); error( "Should never invoke genEnv() for a CastNode()" ); } public Type checkType() { error( "Should never invoke checkType() for a CastNode()" ); return ErrorType.type; } public RunValue eval( RunEnv runEnv ) { RunValue exprValue = expr.eval( runEnv ); if ( type instanceof RealType ) return new RealValue( ( double ) exprValue.intValue() ); else if ( type.equals( StringType.type ) ) if ( exprValue == null ) return NullValue.value; else return new StringValue( exprValue.stringValue() ); else if ( type instanceof VoidType ) return new VoidValue(); else return exprValue; } }