edu.mit.ai.psg.jeva
Class EvalMethods

java.lang.Object
  |
  +--edu.mit.ai.psg.jeva.EvalMethods

public class EvalMethods
extends Object

Centralized static methods for evaluating nodes while catching exceptions. Top level: EvalMethods.evaluate(node, env) verifies types before evaluating.

To evaluate subnodes, nodes call EvalMethods.eval(node, env) rather than calling the subnode eval method directly, so that when an error is thrown, this will wrap error in an EvaluatingError which identifies what node was being evaluated at the time. Similarly, nodes call EvalMethods.getType(expressionNode, env) and EvalMethods.verifyTypes(statementNode, env) rather than calling subnode methods directly so that when an error is thrown, this will wrap the error in a VerifyingError identifying node and env.

Evaluation may also throw other exceptions.

Calls evalHook before and after each construct is evaluated. See edu.mit.ai.psg.jeva.examples for examples of using evalHook for tracing.

Author:
CarlManning, caroma@ai.mit.edu
Copyright (c) 1997, 1998, 1999 Massachusetts Institute of Technology
See Also:
VerifyingException, IllegalTypeException, IllegalNameException, IllegalConstructException, AbruptCompletionException, ThrowException, ReturnException, BreakException, ContinueException, VerifyingError, EvaluatingError

Inner Class Summary
static class EvalMethods.AbruptData
          EvalHook data with AbruptCompletionException (includes ReturnException, ThrowException, BreakException, ContinueException)
static class EvalMethods.EvalHookData
          EvalHook is passed instance of EvalHookData: .
static interface EvalMethods.EvalHookError
           
static class EvalMethods.EvalHookPostData
           
static class EvalMethods.EvalHookPreData
           
static class EvalMethods.PreEvalData
          EvalHook data prior to evaluating expression or statement
static class EvalMethods.PreEvalToStoreData
          EvalHook data prior to evaluating expression to store target for assignment
static class EvalMethods.ResultData
          EvalHook data with expression result value
static class EvalMethods.ResultEnvData
          EvalHook data with statement result environment
static class EvalMethods.ResultStoreData
          EvalHook data with expression store target for assignment
 
Field Summary
static IEnv defaultEnv
           
static IJevaVisitor evalHook
          if EvalHook is non null, it is called before and after each node is evaluated, with the appropriate EvalMethods#EvalHookData.
 
Method Summary
static Object eval(IExpressionNode node)
           
static Object eval(IExpressionNode enode, IEnv env)
           
static IEnv eval(IStatementNode node)
           
static IEnv eval(IStatementNode snode, IEnv env)
           
static void eval(JNCompilationUnit node, String[] arguments)
          Finds public class of previously verified compilation unit, runs public static void main(String[]) method of this public class if one exists.
static void eval(JNCompilationUnit node, String[] arguments, IEnv env)
          Calls "public static void main(String[])" method of public class in compilation unit.
static IStore evalToStore(IExpressionStoreNode node)
           
static IStore evalToStore(IExpressionStoreNode esnode, IEnv env)
           
static Object evaluate(IExpressionNode node)
          verify types then evaluate node with default env
static Object evaluate(IExpressionNode node, IEnv env)
          verify types then evaluate node with given env
static IEnv evaluate(IStatementNode node)
          verify types then evaluate node with default env
static IEnv evaluate(IStatementNode node, IEnv env)
          verify types then evaluate node with given env
static void evaluate(JNCompilationUnit node, String[] arguments)
          verify types with default env, then find class with public static void main(String[]) method and call it with arguments.
static void evaluate(JNCompilationUnit node, String[] arguments, IEnv env)
          verify types with env, then find public class with public static void main(String[]) method and call it with arguments.
static IClass getType(IExpressionNode node)
           
static IClass getType(IExpressionNode node, IEnv env)
           
static boolean isConstant(IExpressionNode node)
           
static boolean isConstant(IExpressionNode node, IEnv env)
           
static IEnv verifyTypes(IStatementNode node)
           
static IEnv verifyTypes(IStatementNode node, IEnv env)
           
static IEnv verifyTypes(JNCompilationUnit node)
           
static IEnv verifyTypes(JNCompilationUnit node, IEnv env)
          Scans compilation unit for class declarations, computes type signatures of methods, and verifies types in fields and methods.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultEnv

public static IEnv defaultEnv

evalHook

public static IJevaVisitor evalHook
if EvalHook is non null, it is called before and after each node is evaluated, with the appropriate EvalMethods#EvalHookData. See edu.mit.ai.psg.jeva.examples for examples using evalHook for tracing.
Method Detail

evaluate

public static Object evaluate(IExpressionNode node)
                       throws VerifyingException,
                              ThrowException
verify types then evaluate node with default env
Returns:
value of expression evaluated in default env
Throws:
VerifyingException - if expression is illegal
ThrowException - if completes abruptly with an exception

evaluate

public static IEnv evaluate(IStatementNode node)
                     throws VerifyingException,
                            AbruptCompletionException
verify types then evaluate node with default env
Returns:
default env possibly extended by statement (e.g,. if local decl)
Throws:
VerifyingException - if statement is illegal
AbruptCompletionException - if completes abruptly

evaluate

public static void evaluate(JNCompilationUnit node,
                            String[] arguments)
                     throws VerifyingException,
                            ClassNotFoundException,
                            ThrowException
verify types with default env, then find class with public static void main(String[]) method and call it with arguments.
Throws:
VerifyingException - if compilation unit is illegal
ClassNotFoundException - if no public class with main method found.
ThrowException - if call completes with an exception

evaluate

public static Object evaluate(IExpressionNode node,
                              IEnv env)
                       throws VerifyingException,
                              ThrowException
verify types then evaluate node with given env
Returns:
value of expression evaluated in default env
Throws:
VerifyingException - if illegal expression
ThrowException - if completes abruptly with an exception

evaluate

public static IEnv evaluate(IStatementNode node,
                            IEnv env)
                     throws VerifyingException,
                            AbruptCompletionException
verify types then evaluate node with given env
Returns:
value of expression evaluated in default env
Throws:
VerifyingException - if illegal statement
AbruptCompletionException - if completes abruptly

evaluate

public static void evaluate(JNCompilationUnit node,
                            String[] arguments,
                            IEnv env)
                     throws VerifyingException,
                            ClassNotFoundException,
                            ThrowException
verify types with env, then find public class with public static void main(String[]) method and call it with arguments.
Throws:
VerifyingException - if compilation unit is illegal
ClassNotFoundException - if no public class with main found
ThrowException - if call completes with an exception

getType

public static IClass getType(IExpressionNode node)
                      throws VerifyingException
Returns:
the IClass representing the Java type of this expression in default env, after type checking subexpressions.
Throws:
VerifyingException - if expression is illegal.

isConstant

public static boolean isConstant(IExpressionNode node)
                          throws VerifyingException
Returns:
true if this expression has constant value in root env.
Throws:
VerifyingException - if expression is illegal.

verifyTypes

public static IEnv verifyTypes(IStatementNode node)
                        throws VerifyingException
Returns:
defaultEnv, extended by this statement if it is a local declaration, after verifying types, but without evaluating initializers.
Throws:
VerifyingException - if statement is illegal.

verifyTypes

public static IEnv verifyTypes(JNCompilationUnit node)
                        throws VerifyingException
Returns:
defaultEnv, as modified by this compilation unit after verifying its types, but without evaluating initializers.
Throws:
VerifyingException - if compilation unit is illegal.

eval

public static Object eval(IExpressionNode node)
                   throws ThrowException
Parameters:
node - previously verified expression
Returns:
the value of expression in root env.
Throws:
ThrowException - if completes abruptly with an exception

eval

public static IEnv eval(IStatementNode node)
                 throws AbruptCompletionException
Parameters:
node - previously verified statement
Returns:
IEnv, extended by statement if it is a local declaration.
Throws:
AbruptCompletionException - if completes abruptly

eval

public static void eval(JNCompilationUnit node,
                        String[] arguments)
                 throws ClassNotFoundException,
                        ThrowException
Finds public class of previously verified compilation unit, runs public static void main(String[]) method of this public class if one exists.
Parameters:
node - previously verified compilation unit
Returns:
IEnv, as modified by this compilation unit.
Throws:
ClassNotFoundException - if no public class with main found.
ThrowException - if completes abruptly with an exception

evalToStore

public static IStore evalToStore(IExpressionStoreNode node)
                          throws ThrowException
Parameters:
node - previously verified storable expression
Returns:
IStore for getting or setting the location denoted by this expression in root env.
Throws:
ThrowException - if completes abruptly with an exception .

getType

public static IClass getType(IExpressionNode node,
                             IEnv env)
                      throws VerifyingException
Returns:
IClass representing Java type of this expression in given env after type checking subexpressions.
Throws:
VerifyingException - if expression is illegal.

isConstant

public static boolean isConstant(IExpressionNode node,
                                 IEnv env)
                          throws VerifyingException
Returns:
true if this expression has constant value in given env.
Throws:
VerifyingException - if expression is illegal.

verifyTypes

public static IEnv verifyTypes(IStatementNode node,
                               IEnv env)
                        throws VerifyingException
Returns:
IEnv, extended by this statement if it is a declaration but without evaluating initializers, after verifying types.
Throws:
VerifyingException - if expression is illegal.

verifyTypes

public static IEnv verifyTypes(JNCompilationUnit node,
                               IEnv env)
                        throws VerifyingException
Scans compilation unit for class declarations, computes type signatures of methods, and verifies types in fields and methods. (If processing many units, it may be better to scan all, then compute all, then verify all, to reduce file system searching for names.)
Returns:
env; no additions are visible outside compilation unit.
Throws:
VerifyingException - if expression is illegal.

eval

public static Object eval(IExpressionNode enode,
                          IEnv env)
                   throws ThrowException
Returns:
the value of this expression in given env.
Throws:
ThrowException - if completes abruptly with an exception

evalToStore

public static IStore evalToStore(IExpressionStoreNode esnode,
                                 IEnv env)
                          throws ThrowException
Returns:
an IStore to get or set the location denoted by this expression.
Throws:
ThrowException - if completes abruptly with an exception

eval

public static IEnv eval(IStatementNode snode,
                        IEnv env)
                 throws AbruptCompletionException
Returns:
env possibly extended by this statement in given env.
Throws:
AbruptCompletionException - if completes abruptly

eval

public static void eval(JNCompilationUnit node,
                        String[] arguments,
                        IEnv env)
                 throws ThrowException,
                        ClassNotFoundException
Calls "public static void main(String[])" method of public class in compilation unit.
Throws:
ClassNotFoundException - if no public class with main method found.
ThrowException - if completes abruptly with an exception

Copyright (c) 1996-1999
Massachusetts Institute of Technology

Feedback: jeva-bugs@ai.mit.edu