summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpaltherr <paltherr@epfl.ch>2003-05-20 11:22:37 +0000
committerpaltherr <paltherr@epfl.ch>2003-05-20 11:22:37 +0000
commitfb71c50b8f7566d53b058914beea7db286c9593f (patch)
treefb2fc5011af3d1c616d744066ed0fb5c8c6cc318
parent82bedc921b3e588251106c0946fb8d1e96edb44b (diff)
downloadscala-fb71c50b8f7566d53b058914beea7db286c9593f.tar.gz
scala-fb71c50b8f7566d53b058914beea7db286c9593f.tar.bz2
scala-fb71c50b8f7566d53b058914beea7db286c9593f.zip
- Moved code from EntryPointCompiler into Inter...
- Moved code from EntryPointCompiler into Interpreter Removed - EntryPointCompiler Simplified Compiler
-rw-r--r--config/list/interpreter.lst1
-rw-r--r--sources/scala/tools/scalai/Compiler.java68
-rw-r--r--sources/scala/tools/scalai/EntryPointCompiler.java128
-rw-r--r--sources/scala/tools/scalai/Evaluator.java8
-rw-r--r--sources/scala/tools/scalai/Interpreter.java133
-rw-r--r--sources/scala/tools/scalai/InterpreterShell.java14
-rw-r--r--sources/scala/tools/scalai/ScalaFunction.java2
-rw-r--r--sources/scalac/Global.java7
8 files changed, 162 insertions, 199 deletions
diff --git a/config/list/interpreter.lst b/config/list/interpreter.lst
index d752e0f599..4373944faa 100644
--- a/config/list/interpreter.lst
+++ b/config/list/interpreter.lst
@@ -10,7 +10,6 @@ CodeGenerator.java
CodePromise.java
Compiler.java
Constants.java
-EntryPointCompiler.java
Environment.java
Evaluator.java
EvaluatorException.java
diff --git a/sources/scala/tools/scalai/Compiler.java b/sources/scala/tools/scalai/Compiler.java
index be58832dc8..8350baff78 100644
--- a/sources/scala/tools/scalai/Compiler.java
+++ b/sources/scala/tools/scalai/Compiler.java
@@ -45,8 +45,6 @@ public class Compiler {
private final Evaluator evaluator; // !!! remove
private final Map any_methods;
- private boolean interactive;
-
public Compiler(Global global, Evaluator evaluator) {
this.global = global;
this.definitions = global.definitions;
@@ -253,6 +251,14 @@ public class Compiler {
//########################################################################
+ public Variable getModule(Symbol symbol) {
+ return environment.lookupVariable(symbol);
+ }
+
+ public Function getMethod(Symbol symbol) {
+ return environment.lookupFunction(symbol);
+ }
+
public CodePromise compile(Symbol symbol, Tree.DefDef tree) {
assert tree.tparams.length == 0 : Debug.show(tree);
assert tree.vparams.length == 1 : Debug.show(tree);
@@ -262,12 +268,8 @@ public class Compiler {
//########################################################################
//
- public CodeContainer compile(Symbol owner, Tree tree) {
- return compile(owner, tree, new Symbol[0]); // !!!
- }
-
- public CodeContainer compile(Symbol owner, Tree tree, ValDef[] params) {
- return compile(owner, tree, Tree.symbolOf(params));
+ public void compile(Unit[] units) {
+ for (int i = 0; i < units.length; i++) declare(units[i].body);
}
public CodeContainer compile(Symbol owner, Tree tree, Symbol[] params) {
@@ -276,34 +278,14 @@ public class Compiler {
return worker.compile(tree);
}
- public CodeContainer compile(Symbol owner, ArrayList items) {
- return compile(owner, items, new Symbol[0]); // !!!
- }
-
- public CodeContainer compile(Symbol owner, ArrayList items,Symbol[]params){
- ExpressionContext context = new ExpressionContext(environment, owner);
- ExpressionCompiler worker = new ExpressionCompiler(definitions, constants, context, params);
- return worker.compile(items);
- }
-
//########################################################################
// Private Methods -
- public CodeContainer compile(Unit[] units, boolean interactive) {
- this.interactive = interactive;
- ArrayList buffer = new ArrayList();
- for (int i = 0; i < units.length; i++) declare(buffer, units[i].body);
- return compile(null, buffer);
+ private void declare(Tree[] trees) {
+ for (int i = 0; i < trees.length; i++) declare(trees[i]);
}
- //########################################################################
- // Private Methods -
-
- private void declare(ArrayList buffer, Tree[] trees) {
- for (int i = 0; i < trees.length; i++) declare(buffer, trees[i]);
- }
-
- private void declare(ArrayList buffer, Tree tree) {
+ private void declare(Tree tree) {
Symbol symbol = tree.symbol();
switch (tree) {
@@ -318,40 +300,22 @@ public class Compiler {
case PackageDef(Tree packaged, Tree.Template(Tree[] bases, Tree[] body)):
assert packaged.symbol().isPackage() : Debug.show(tree); // !!! was isJavaPackage
assert bases.length == 0 : Debug.show(tree);
- declare(buffer, body);
+ declare(body);
return;
case ValDef(_, _, _, Tree body):
assert symbol.isModule() : Debug.show(symbol);
environment.insertVariable(symbol, Variable.Module(new CodePromise(new ModuleBuilder(this, symbol, body)), null));
- if (interactive &&
- symbol.name.toString().startsWith(global.CONSOLE_S)) // !!!
- {
- global.prevPhase();
- buffer.add(global.treeGen.Ident(symbol));
- global.nextPhase();
- }
- return;
-
- case DefDef(_, _, _, _, _, _):
- // !!! impossible ? => remove !
- CodePromise function = compile(symbol, (Tree.DefDef)tree);
- environment.insertFunction(symbol, Function.Global(function));
return;
default:
- // !!! impossible ? => remove !
- buffer.add(tree);
- return;
+ throw Debug.abort("illegal case", tree);
}
}
//########################################################################
// Private Methods -
- // !!! move elsewhere ?
- public static final Name MAIN_N = Name.fromString("main");
-
private Symbol newGlobalVariable(Type type, Object value) {
Symbol symbol = new TermSymbol(Position.NOPOS,
Name.fromString(value.toString()), definitions.ROOT, 0);
@@ -464,7 +428,7 @@ public class Compiler {
}
public CodeContainer generate() {
- return compiler.compile(symbol, body);
+ return compiler.compile(symbol, body, Symbol.EMPTY_ARRAY);
}
}
diff --git a/sources/scala/tools/scalai/EntryPointCompiler.java b/sources/scala/tools/scalai/EntryPointCompiler.java
deleted file mode 100644
index 9a54aea781..0000000000
--- a/sources/scala/tools/scalai/EntryPointCompiler.java
+++ /dev/null
@@ -1,128 +0,0 @@
-/* ____ ____ ____ ____ ______ *\
-** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
-** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
-** /_____/\____/\___/\____/____/ **
-\* */
-
-// $OldId: EntryPointCompiler.java,v 1.1 2002/08/30 14:28:25 paltherr Exp $
-// $Id$
-
-package scalai;
-
-import scalac.Global;
-import scalac.Unit;
-import scalac.ast.Tree;
-import scalac.ast.parser.Sourcefile;
-import scalac.symtab.Type;
-import scalac.symtab.TypeTags;
-import scalac.symtab.Symbol;
-import scalac.util.Debug;
-import scalac.util.Name;
-import scalac.util.Position;
-
-import scalac.symtab.TermSymbol;
-
-public class EntryPointCompiler {
-
- //########################################################################
- // Private Fields
-
- private final String product;
- private final Global global;
- private final Type MAIN_TYPE; // !!! move elsewhere
-
- //########################################################################
- // Public Constructors
-
- public EntryPointCompiler(String product, Global global) {
- this.product = product;
- this.global = global;
- Name ARGS_N = Name.fromString("args");
- Symbol args = new TermSymbol(0, ARGS_N, null, 0);
- args.setInfo(
- Type.UnboxedArrayType(global.definitions.JAVA_STRING_TYPE));
- this.MAIN_TYPE = Type.MethodType(
- new Symbol[] {args}, Type.UnboxedType(TypeTags.UNIT));
- }
-
- //########################################################################
- // Public Methods
-
- public void compile(String main, String[] args) {
- String names = main.replace('/', '.') + (main.length() > 0 ? "." : "");
- if (names.length() > 0 && names.charAt(0) == '.') {
- error("illegal module name '" + main + "'");
- return;
- }
-
- Symbol module = global.definitions.ROOT;
- for (int i = 0, j; (j = names.indexOf('.', i)) >= 0; i = j + 1) {
- Name name = Name.fromString(names.substring(i, j));
- module = getModule(module, name);
- if (module == Symbol.NONE) {
- error("could not find module '" + main.substring(0, j) + "'");
- return;
- }
- if (module == Symbol.ERROR) {
- error("term '" + main.substring(0, j) + "' is not a module");
- return;
- }
- }
-
- Name name = Compiler.MAIN_N;
- Type type = MAIN_TYPE;
- Symbol method = getMethod(module, name, type);
- if (method == Symbol.NONE) {
- error("module '" + main + "' has no method '" + name + "'");
- return;
- }
- if (method == Symbol.ERROR) {
- error("module '" + main + "' has no method '" + name +
- "' with type '" + type + "'");
- return;
- }
-
- int pos = Position.NOPOS;
- global.units = new Unit[1];
- global.units[0] = new Unit(global, new Sourcefile((byte[])null),false);
- global.units[0].body = new Tree[1];
- global.units[0].body[0] = global.treeGen.Apply(
- global.treeGen.mkRef(pos, module.thisType(), method),
- new Tree[] { global.make.Literal(pos, args) });
- }
-
- //########################################################################
- // Private Methods
-
- private Symbol getModule(Symbol module, Name name) {
- Symbol symbol = module.lookup(name);
- if (symbol == Symbol.NONE || symbol.isModule()) return symbol;
- switch (symbol.type()) {
- case OverloadedType(Symbol[] alts, _):
- for (int k = 0; k < alts.length; k++)
- if (alts[k].isModule()) return alts[k];
- }
- return Symbol.ERROR;
- }
-
- private Symbol getMethod(Symbol module, Name name, Type type) {
- Symbol symbol = module.lookup(name);
- if (symbol == Symbol.NONE || isMethod(symbol, type)) return symbol;
- switch (symbol.type()) {
- case OverloadedType(Symbol[] alts, _):
- for (int k = 0; k < alts.length; k++)
- if (isMethod(alts[k], type)) return alts[k];
- }
- return Symbol.ERROR;
- }
-
- private boolean isMethod(Symbol symbol, Type type) {
- return symbol.isMethod() && symbol.type().equals(type);
- }
-
- private void error(String message) {
- global.reporter.error(product + ": " + message);
- }
-
- //########################################################################
-}
diff --git a/sources/scala/tools/scalai/Evaluator.java b/sources/scala/tools/scalai/Evaluator.java
index ea69e27880..1d19637116 100644
--- a/sources/scala/tools/scalai/Evaluator.java
+++ b/sources/scala/tools/scalai/Evaluator.java
@@ -84,6 +84,14 @@ public class Evaluator {
}
}
+ public Object evaluate(Variable module) {
+ return load(null, module);
+ }
+
+ public Object evaluate(Variable module, Function method, Object[] args) {
+ return invoke(load(null, module), method, args);
+ }
+
public Object evaluate(CodeContainer code) {
return evaluate(code, null, new Object[0]);
}
diff --git a/sources/scala/tools/scalai/Interpreter.java b/sources/scala/tools/scalai/Interpreter.java
index 4c61cfc5cb..7e5ec5446d 100644
--- a/sources/scala/tools/scalai/Interpreter.java
+++ b/sources/scala/tools/scalai/Interpreter.java
@@ -10,6 +10,11 @@
package scalai;
import scalac.Global;
+import scalac.symtab.Definitions;
+import scalac.symtab.Symbol;
+import scalac.symtab.TermSymbol;
+import scalac.symtab.Type;
+import scalac.util.Name;
import scala.runtime.InterpreterSupport;
import scala.runtime.InterpreterSupport.EvaluationResult;
@@ -17,6 +22,12 @@ import scala.runtime.InterpreterSupport.EvaluationResult;
public class Interpreter {
//########################################################################
+ // Public Constants
+
+ public static final Name MAIN_N = Name.fromString("main");
+ public static final Name ARGS_N = Name.fromString("args");
+
+ //########################################################################
// Private Fields
private final Global global;
@@ -35,6 +46,21 @@ public class Interpreter {
//########################################################################
// Public Methods
+ public EvaluatorResult invoke(String main, String[]args) {
+ Symbol module = getMainModule(main);
+ if (module == Symbol.NONE) return EvaluatorResult.Void;
+ Symbol method = getMainMethod(main, module);
+ if (method == Symbol.NONE) return EvaluatorResult.Void;
+ Variable variable = compiler.getModule(module);
+ Function function = compiler.getMethod(method);
+ try {
+ evaluator.evaluate(variable, function, args);
+ return EvaluatorResult.Void;
+ } catch (EvaluatorException exception) {
+ return EvaluatorResult.Error(exception);
+ }
+ }
+
public EvaluatorResult interpret(String input, boolean interactive) {
if (input.trim().length() == 0) return EvaluatorResult.Void;
global.compile(input + ";", interactive);
@@ -47,11 +73,25 @@ public class Interpreter {
return interpret(interactive);
}
- public EvaluatorResult interpret(boolean interactive) {
+ public EvaluatorResult toString(Object object, String type) {
+ try {
+ return EvaluatorResult.Value(evaluator.toString(object), type);
+ } catch (EvaluatorException exception) {
+ return EvaluatorResult.Error(exception);
+ }
+ }
+
+ //########################################################################
+ // Private Methods
+
+ private EvaluatorResult interpret(boolean interactive) {
try {
if (global.reporter.errors() != 0) return EvaluatorResult.Void;
- CodeContainer code = compiler.compile(global.units, interactive);
- evaluator.evaluate(code);
+ compiler.compile(global.units);
+ if (interactive) {
+ Variable console = compiler.getModule(global.console);
+ evaluator.evaluate(console);
+ }
EvaluationResult result =
InterpreterSupport.getAndResetEvaluationResult();
if (result == null) return EvaluatorResult.Void;
@@ -61,12 +101,89 @@ public class Interpreter {
}
}
- public EvaluatorResult toString(Object object, String type) {
- try {
- return EvaluatorResult.Value(evaluator.toString(object), type);
- } catch (EvaluatorException exception) {
- return EvaluatorResult.Error(exception);
+ //########################################################################
+ // Private Methods - Finding main module
+
+ private Symbol getMainModule(String main) {
+ String names = main.replace('/', '.') + (main.length() > 0 ? "." : "");
+ if (names.length() > 0 && names.charAt(0) == '.') {
+ error("illegal module name '" + main + "'");
+ return Symbol.NONE;
+ }
+ Symbol module = global.definitions.ROOT;
+ for (int i = 0, j; (j = names.indexOf('.', i)) >= 0; i = j + 1) {
+ Name name = Name.fromString(names.substring(i, j));
+ module = getModule(module, name);
+ if (module == Symbol.NONE) {
+ error("could not find module '" + main.substring(0, j) + "'");
+ return Symbol.NONE;
+ }
+ if (module == Symbol.ERROR) {
+ error("term '" + main.substring(0, j) + "' is not a module");
+ return Symbol.NONE;
+ }
+ }
+ return module;
+ }
+
+ private Symbol getModule(Symbol owner, Name name) {
+ Symbol symbol = owner.lookup(name);
+ if (symbol == Symbol.NONE || symbol.isModule()) return symbol;
+ switch (symbol.type()) {
+ case OverloadedType(Symbol[] alts, _):
+ for (int k = 0; k < alts.length; k++)
+ if (alts[k].isModule()) return alts[k];
}
+ return Symbol.ERROR;
+ }
+
+ //########################################################################
+ // Private Methods - Finding main method
+
+ private Type getMainMethodType(boolean erased) {
+ Definitions definitions = global.definitions;
+ Type argument = definitions.arrayType(definitions.JAVA_STRING_TYPE);
+ Type result = definitions.UNIT_TYPE;
+ if (erased) argument = argument.erasure();
+ if (erased) result = result.fullErasure();
+ Symbol formal = new TermSymbol(0, ARGS_N, null, 0).setInfo(argument);
+ return Type.MethodType(new Symbol[] {formal}, result);
+ }
+
+ private Symbol getMainMethod(String main, Symbol module) {
+ Symbol method = getMethod(module, MAIN_N, getMainMethodType(true));
+ if (method == Symbol.NONE) {
+ error("module '" + main + "' has no method '" + MAIN_N + "'");
+ return Symbol.NONE;
+ }
+ if (method == Symbol.ERROR) {
+ error("module '" + main + "' has no method '" + MAIN_N +
+ "' with type '" + getMainMethodType(false) + "'");
+ return Symbol.NONE;
+ }
+ return method;
+ }
+
+ private Symbol getMethod(Symbol module, Name name, Type type) {
+ Symbol symbol = module.lookup(name);
+ if (symbol == Symbol.NONE || isMethod(symbol, type)) return symbol;
+ switch (symbol.type()) {
+ case OverloadedType(Symbol[] alts, _):
+ for (int k = 0; k < alts.length; k++)
+ if (isMethod(alts[k], type)) return alts[k];
+ }
+ return Symbol.ERROR;
+ }
+
+ private boolean isMethod(Symbol symbol, Type type) {
+ return symbol.isMethod() && symbol.type().equals(type);
+ }
+
+ //########################################################################
+ // Private Methods - Signaling errors
+
+ private void error(String message) {
+ global.reporter.error("error: " + message);
}
//########################################################################
diff --git a/sources/scala/tools/scalai/InterpreterShell.java b/sources/scala/tools/scalai/InterpreterShell.java
index c51174b373..409952c136 100644
--- a/sources/scala/tools/scalai/InterpreterShell.java
+++ b/sources/scala/tools/scalai/InterpreterShell.java
@@ -88,10 +88,13 @@ public class InterpreterShell {
public void main(String[] program, String main, String[] args) {
if (interactive) showBanner();
if (program.length > 0) load(lfiles = program);
- if (global.reporter.errors() == 0 && main != null) call(main, args);
- if (interactive) loop(new DefaultDefinitionPrinter());
global.stop("total"); // !!! remove ?
- if (!interactive) global.reporter.printSummary();
+ if (global.reporter.errors() == 0) {
+ if (main != null) call(main, args);
+ } else {
+ if (!interactive) global.reporter.printSummary();
+ }
+ if (interactive) loop(new DefaultDefinitionPrinter());
}
public void loop(DefinitionPrinter printer) {
@@ -222,10 +225,7 @@ public class InterpreterShell {
// Public Methods - interpretation
public void call(String main, String[] args) {
- global.prevPhase(); // !!!
- new EntryPointCompiler(PRODUCT, global).compile(main, args);
- global.nextPhase(); // !!!
- show(interpreter.interpret(false), false);
+ show(interpreter.invoke(main, args), false);
}
public void load(String input) {
diff --git a/sources/scala/tools/scalai/ScalaFunction.java b/sources/scala/tools/scalai/ScalaFunction.java
index 7789609c76..b4a215cb5a 100644
--- a/sources/scala/tools/scalai/ScalaFunction.java
+++ b/sources/scala/tools/scalai/ScalaFunction.java
@@ -38,7 +38,7 @@ public class ScalaFunction extends CodeGenerator {
// Public Methods - CodeGenerator interface
public CodeContainer generate() {
- return compiler.compile(symbol, body, params);
+ return compiler.compile(symbol, body, Tree.symbolOf(params));
}
//########################################################################
diff --git a/sources/scalac/Global.java b/sources/scalac/Global.java
index 8aac110889..cabac41a66 100644
--- a/sources/scalac/Global.java
+++ b/sources/scalac/Global.java
@@ -329,6 +329,7 @@ public class Global {
private int module = 0;
private List imports = new ArrayList();
+ public Symbol console;
private void fix1() {
for (int i = 0; i < units.length; i++) {
@@ -363,8 +364,10 @@ public class Global {
imports.clear();
for (int i = 0; i < unit.body.length; i++) {
switch (unit.body[i]) {
- case ModuleDef(_, Name name, _, Tree.Template impl):
- if (!name.startsWith(CONSOLE_N)) break;
+ case ModuleDef(_, _, _, Tree.Template impl):
+ Symbol symbol = unit.body[i].symbol();
+ if (!symbol.name.startsWith(CONSOLE_N)) break;
+ console = symbol;
if (impl.body.length <= 0) break;
imports.add(unit.body[i].symbol());
Tree last = impl.body[impl.body.length - 1];