out.flush();
}
}
public static String eval(final Environment env, String input) throws Exception {
final Compiler compiler = new Compiler(String.format("ScriptFragment$%d", counter++));
Node node = compiler.parse(input);
if (node == null)
return "";
final StringBuilder output = new StringBuilder();
node.accept(new DefaultNodeVisitor() {
@Override public void visit(CompilerDirectiveDecl decl) {
if (decl.getIdent().equals("help"))
output.append(help());
else if (decl.getIdent().equals("quit"))
env.halt();
else
output.append(String.format("Invalid directive '%s'\n", decl));
}
});
node.accept(new DefaultNodeVisitor() {
@Override public void visitAfter(ValueDefn defn) {
Value val = compiler.codegen(defn);
output.append(String.format("val %s = %s\n", defn.pattern(), val.eval()));
}
});