[antineutrino] r486 committed - Added quick-and-dirty interactive shell.

0 views
Skip to first unread message

antine...@googlecode.com

unread,
Oct 3, 2010, 12:48:01 AM10/3/10
to neutri...@googlegroups.com
Revision: 486
Author: christian.plesner.hansen
Date: Sat Oct 2 21:40:01 2010
Log: Added quick-and-dirty interactive shell.
http://code.google.com/p/antineutrino/source/detail?r=486

Added:
/trunk/lib/org/neutrino/shell
/trunk/lib/org/neutrino/shell/Shell.n
/trunk/scripts/shell.sh
/trunk/src/org/neutrino/main/Shell.java
Modified:
/trunk/src/org/neutrino/compiler/CompilerModule.java
/trunk/src/org/neutrino/pib/Universe.java
/trunk/src/org/neutrino/runtime/MethodLookupHelper.java
/trunk/src/org/neutrino/runtime/Native.java

=======================================
--- /dev/null
+++ /trunk/lib/org/neutrino/shell/Shell.n Sat Oct 2 21:40:01 2010
@@ -0,0 +1,3 @@
+@native("exit") def exit(value);
+
+def quit() -> exit(0);
=======================================
--- /dev/null
+++ /trunk/scripts/shell.sh Sat Oct 2 21:40:01 2010
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+. `dirname $0`/shared.sh
+run_main org.neutrino.main.Shell
=======================================
--- /dev/null
+++ /trunk/src/org/neutrino/main/Shell.java Sat Oct 2 21:40:01 2010
@@ -0,0 +1,121 @@
+package org.neutrino.main;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.List;
+
+import org.neutrino.compiler.CompilerModule;
+import org.neutrino.pib.BinaryBuilder;
+import org.neutrino.pib.Module;
+import org.neutrino.pib.Universe;
+import org.neutrino.plankton.PSeed;
+import org.neutrino.plankton.PValue;
+import org.neutrino.runtime.Interpreter;
+import org.neutrino.runtime.RValue;
+import org.neutrino.syntax.Parser;
+import org.neutrino.syntax.Scanner;
+import org.neutrino.syntax.SyntaxError;
+import org.neutrino.syntax.Token;
+
+public class Shell {
+
+ public static @Flags.Flag("pib-path") String pibPath;
+
+ public static void main(String[] rawArgs) throws IOException {
+ Flags.parseArguments(rawArgs, Shell.class);
+ assert pibPath != null : "No --pib-path has been specified";
+ File file = new File(pibPath);
+ PValue value = Universe.getPlankton().read(new FileInputStream(file));
+ Universe universe = ((PSeed) value).grow(Universe.class);
+ universe.initialize();
+ (new Shell(universe)).readEvalPrint();
+ }
+
+ private static final String SHORT_NAME = "shell";
+ private static final String LONG_NAME = "org::neutrino::shell";
+ private static final String DUMMY_VAR_NAME = "__shell_result__";
+
+ private final BufferedReader in;
+ private final Interpreter inter = new Interpreter();
+ private final Module shellModule;
+ private final Universe universe;
+
+ private Shell(Universe universe) {
+ in = new BufferedReader(new InputStreamReader(System.in));
+ this.shellModule = universe.getModule(LONG_NAME);
+ this.universe = universe;
+ }
+
+ public String readLine() throws IOException {
+ System.out.print("n> ");
+ return in.readLine();
+ }
+
+ private void readEvalPrint() throws IOException {
+ while (true) {
+ String next = readLine();
+ if (next == null) {
+ System.out.println("");
+ return;
+ }
+ try {
+ RValue result = eval(next);
+ if (result != null)
+ System.out.println(result);
+ } catch (SyntaxError se) {
+ se.printStackTrace();
+ } catch (AssertionError ae) {
+ ae.printStackTrace();
+ }
+ }
+ }
+
+ private RValue eval(String source) throws IOException, SyntaxError {
+ // Create a new instance of the shell module, living on top of the
+ // shell module provided by lib.
+ CompilerModule module =
CompilerModule.createToplevel().ensureModule(SHORT_NAME, LONG_NAME);
+ boolean isDeclaration = isDeclaration(source);
+ if (isDeclaration) {
+ module.includeSource("shell", source);
+ } else {
+ // If the source is not a declaration we wrap it in a dummy one.
+ module.includeSource("shell", "def " + DUMMY_VAR_NAME + " := (" +
source + ");");
+ }
+ // Compile the declaration into a parallel universe.
+ module.parseAll();
+ BinaryBuilder binary = Universe.builder();
+ module.writeToBinary(binary);
+ Universe subUniverse = binary.getResult();
+ subUniverse.initialize();
+ subUniverse.setParallelUniverse(universe);
+ if (isDeclaration) {
+ // If the input was a declaration we destructively update the
+ // state of the persistent shell module.
+ Module subShellModule = subUniverse.getModule(LONG_NAME);
+ shellModule.defs.putAll(subShellModule.defs);
+ shellModule.methods.addAll(subShellModule.methods);
+ shellModule.protos.putAll(subShellModule.protos);
+ return null;
+ } else {
+ // If the input was an expression we fetch the value of the
+ // dummy variable which will cause the declaration to be evaluated.
+ return subUniverse.getGlobal(DUMMY_VAR_NAME, inter);
+ }
+ }
+
+ private boolean isDeclaration(String source) throws SyntaxError {
+ List<Token> tokens = Scanner.tokenize(source);
+ boolean isDeclaration;
+ try {
+ Parser.parse(tokens);
+ isDeclaration = true;
+ } catch (SyntaxError se) {
+ isDeclaration = false;
+ }
+ return isDeclaration;
+ }
+
+}
=======================================
--- /trunk/src/org/neutrino/compiler/CompilerModule.java Wed Sep 29
19:06:59 2010
+++ /trunk/src/org/neutrino/compiler/CompilerModule.java Sat Oct 2
21:40:01 2010
@@ -1,14 +1,14 @@
package org.neutrino.compiler;

-import org.neutrino.pib.BinaryBuilder;
-import org.neutrino.pib.ModuleBuilder;
-import org.neutrino.syntax.SyntaxError;
-
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

+import org.neutrino.pib.BinaryBuilder;
+import org.neutrino.pib.ModuleBuilder;
+import org.neutrino.syntax.SyntaxError;
+
/**
* A single neutrino module.
*
@@ -39,7 +39,7 @@
String shortName = file.getName();
String fullName;
if (name == null) fullName = shortName;
- else fullName = name + "." + shortName;
+ else fullName = name + "::" + shortName;
CompilerModule child = ensureModule(shortName, fullName);
child.includeFromPath(file);
} else {
=======================================
--- /trunk/src/org/neutrino/pib/Universe.java Sat Oct 2 21:38:35 2010
+++ /trunk/src/org/neutrino/pib/Universe.java Sat Oct 2 21:40:01 2010
@@ -34,6 +34,7 @@
static final String TAG = "org::neutrino::pib::Universe";

public @SeedMember Map<String, Module> modules;
+ private Universe parallelUniverse = null;

public Universe(Map<String, Module> modules) {
this.modules = modules;
@@ -44,6 +45,14 @@
public static BinaryBuilder builder() {
return new BinaryBuilder();
}
+
+ public void setParallelUniverse(Universe universe) {
+ this.parallelUniverse = universe;
+ }
+
+ public Universe getParallelUniverse() {
+ return this.parallelUniverse;
+ }

public void initialize() {
for (Module module : modules.values())
@@ -56,7 +65,9 @@
if (value != null)
return value;
}
- return null;
+ return (parallelUniverse == null)
+ ? null
+ : parallelUniverse.getEntryPoint(name);
}

public RValue getGlobal(String name, Interpreter inter) {
@@ -65,7 +76,9 @@
if (value != null)
return value;
}
- return null;
+ return (parallelUniverse == null)
+ ? null
+ : parallelUniverse.getGlobal(name, inter);
}


@@ -85,6 +98,10 @@
public Set<Map.Entry<String, Module>> getModules() {
return modules.entrySet();
}
+
+ public Module getModule(String name) {
+ return modules.get(name);
+ }

@Override
public String toString() {
=======================================
--- /trunk/src/org/neutrino/runtime/MethodLookupHelper.java Thu Sep 30
23:25:05 2010
+++ /trunk/src/org/neutrino/runtime/MethodLookupHelper.java Sat Oct 2
21:40:01 2010
@@ -1,11 +1,12 @@
package org.neutrino.runtime;

-import org.neutrino.pib.Module;
-import org.neutrino.pib.Parameter;
-
import java.util.ArrayList;
import java.util.List;

+import org.neutrino.pib.Module;
+import org.neutrino.pib.Parameter;
+import org.neutrino.pib.Universe;
+
public class MethodLookupHelper {

private final Module origin;
@@ -14,14 +15,15 @@
public MethodLookupHelper(Module origin) {
this.origin = origin;
}
+
+

public RLambda lookupMethod(String name, int argc, Frame frame) {
if ("()".equals(name))
return (RLambda) frame.stack.get(frame.stack.size() - argc);
matches.clear();
- for (Module module : origin.getUniverse().modules.values()) {
- searchModule(module, name, argc, frame);
- }
+ Universe uni = origin.getUniverse();
+ searchUniverse(name, argc, frame, uni);
RMethod result = null;
if (matches.size() == 0) {
return null;
@@ -34,6 +36,17 @@
}
return new RLambda(origin, result.getCode(), null);
}
+
+
+
+ private void searchUniverse(String name, int argc, Frame frame,
+ Universe universe) {
+ for (Module module : universe.modules.values())
+ searchModule(module, name, argc, frame);
+ Universe parallel = universe.getParallelUniverse();
+ if (parallel != null)
+ searchUniverse(name, argc, frame, parallel);
+ }

private void searchModule(Module module, String name, int argc, Frame
frame) {
loop: for (RMethod method : module.methods) {
=======================================
--- /trunk/src/org/neutrino/runtime/Native.java Sat Oct 2 21:39:30 2010
+++ /trunk/src/org/neutrino/runtime/Native.java Sat Oct 2 21:40:01 2010
@@ -241,6 +241,15 @@
}
};

+ @Marker("exit") static final Impl EXIT = new Impl() {
+ @Override
+ public RValue call(Arguments args) {
+ RInteger value = (RInteger) args.getArgument(0);
+ System.exit(value.getValue());
+ return RNull.getInstance();
+ }
+ };
+
private static final Map<String, Impl> IMPLS = new HashMap<String,
Impl>() {{
try {
for (Field field : Native.class.getDeclaredFields()) {

Reply all
Reply to author
Forward
0 new messages