Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 09, 2012
@jokeofweek Added catch-all to the Loader in order to simply display errors. Also
modified Loader to work with PrintStream objects instead of
PrintWriter objects.
a216ac3
@jokeofweek Added support for set!. fc4e460
View
28 src/com/gummy/core/Loader.java
@@ -1,6 +1,8 @@
package com.gummy.core;
import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
@@ -9,21 +11,25 @@
public class Loader {
- public static void load(PushbackInputStream in, PrintWriter out,
+ public static void load(PushbackInputStream in, PrintStream out,
Environment context) throws IOException {
Object current;
String retVal;
-
- while (true) {
- current = Reader.read(in);
- if (current == Constant.EOF)
- break;
+ try {
+ while (true) {
+ current = Reader.read(in);
+ if (current == Constant.EOF)
+ break;
- retVal = Writer.getString(Expression.eval(
- Expression.analyze(current), context));
-
- if (!retVal.isEmpty())
- System.out.println(retVal);
+ retVal = Writer.getString(Expression.eval(
+ Expression.analyze(current), context));
+
+ if (!retVal.isEmpty())
+ out.println(retVal);
+ }
+ } catch (Exception ex) {
+ out.println("Error - " + ex.getClass().getName() + " - "
+ + ex.getMessage());
}
}
View
31 src/com/gummy/primitives/Assignment.java
@@ -0,0 +1,31 @@
+package com.gummy.primitives;
+
+import com.gummy.core.Environment;
+import com.gummy.core.Marshall;
+import com.gummy.types.Constant;
+import com.gummy.types.Expression;
+import com.gummy.types.Pair;
+
+/**
+ * This primitive updates a variable in a given environment with a specified
+ * value.
+ *
+ * @author Dominic Charley-Roy
+ *
+ */
+public class Assignment extends Expression {
+ private Pair arguments;
+
+ public Assignment(Object arguments) {
+ this.arguments = Marshall.getPair(arguments);
+ }
+
+ @Override
+ public Object eval(Environment environment) {
+ environment.updateValue(Marshall.getSymbol(arguments.getCar()),
+ Expression.eval(Marshall.getPair(arguments.getCdr()).getCar(),
+ environment));
+ return Constant.UNSPECIFIED;
+ }
+
+}
View
4 src/com/gummy/repl/REPL.java
@@ -13,12 +13,10 @@
public class REPL {
private Environment global;
- private PrintWriter out;
public REPL(){
global = new Environment();
//Environment.initialize(global);
- out = new PrintWriter(System.out);
}
public void run() throws IOException{
@@ -27,7 +25,7 @@ public void run() throws IOException{
while (true){
System.out.print("> ");
Loader.load(new PushbackInputStream(new ByteArrayInputStream(
- reader.readLine().getBytes())), out, global);
+ reader.readLine().getBytes())), System.out, global);
}
}
View
6 src/com/gummy/types/Expression.java
@@ -3,6 +3,7 @@
import com.gummy.core.Environment;
import com.gummy.core.InterpreterException;
import com.gummy.core.Marshall;
+import com.gummy.primitives.Assignment;
import com.gummy.primitives.Definition;
import com.gummy.primitives.Variable;
@@ -125,6 +126,11 @@ public static Object getSpecialForm(Object car, Pair cdr) {
// and then all other values as analyzed values.
return new Definition(new Pair(cdr.getCar(),
analyzePair(cdr.getCdr())));
+ } else if ("set!".equals(form)) {
+ // Call set! with a raw symbol / pair as the first argument
+ // and then all other values as analyzed values.
+ return new Assignment(new Pair(cdr.getCar(),
+ analyzePair(cdr.getCdr())));
}
}
return null;

No commit comments for this range

Something went wrong with that request. Please try again.