First PoC version of the REPL has been done

This commit is contained in:
Sameer Rahmani 2019-12-15 21:31:25 +00:00
parent 2bfdb4ebf4
commit 5c90da24a2
6 changed files with 47 additions and 36 deletions

View File

@ -1,7 +1,7 @@
package serene.simple; package serene.simple;
public class DefSpecialForm extends SpecialForm { public class DefSpecialForm extends SpecialForm {
public DefSpecialForm(ListNode listNode) { public DefSpecialForm(ListNode<Node> listNode) {
super(listNode); super(listNode);
} }

View File

@ -25,24 +25,29 @@ public class Main {
System.out.println("Serene 'simple' v0.1.0"); System.out.println("Serene 'simple' v0.1.0");
while(true) { while(true) {
//String inputData = console.readLine("serene-> "); String inputData = console.readLine("serene-> ");
String inputData = "(def a 4)";
if (inputData == null) break; if (inputData == null) break;
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes()); ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
ListNode<Node> nodes = Reader.read(inputStream); ListNode<Node> nodes = Reader.read(inputStream);
Object result = ListNode.EMPTY; try {
Object result = ListNode.EMPTY;
for (Node n : nodes) {
result = n.eval(rootScope);
}
for (Node n : nodes) { if (result != ListNode.EMPTY) {
result = n.eval(rootScope); System.out.print(";; ");
System.out.println(result);
}
}
catch(Exception e) {
System.out.println("Error: ");
e.printStackTrace(System.out);
} }
System.out.print(";; ");
if (result != ListNode.EMPTY) {
System.out.println(result);
}
} }
} }

View File

@ -9,4 +9,8 @@ public abstract class Node {
return this == n; return this == n;
} }
public String toString() {
String className = this.getClass().getName();
return String.format("%s<%s>", className, System.identityHashCode(this));
}
} }

View File

@ -34,7 +34,6 @@ public class Reader {
public static ListNode<Node> read(PushbackReader inputStream) throws IOException { public static ListNode<Node> read(PushbackReader inputStream) throws IOException {
List<Node> nodes = new ArrayList<Node>(); List<Node> nodes = new ArrayList<Node>();
skipWhiteSpaces(inputStream); skipWhiteSpaces(inputStream);
char c = (char) inputStream.read(); char c = (char) inputStream.read();
@ -72,19 +71,19 @@ public class Reader {
} }
private static Node readSymbol(PushbackReader inputStream) throws IOException { private static Node readSymbol(PushbackReader inputStream) throws IOException {
String str = "";
String str = ""; while (true) {
int ch = inputStream.read();
while (true) { if(isWhiteSpace(ch) || ch == -1) {
int ch = inputStream.read(); inputStream.unread(ch);
if(isWhiteSpace(ch)) { break;
inputStream.unread(ch); };
break; str = str + (char) ch;
}; }
str = str + (char) ch;
}
return new SymbolNode(str); return new SymbolNode(str);
} }
private static Node readNumber(PushbackReader inputStream) throws IOException { private static Node readNumber(PushbackReader inputStream) throws IOException {
@ -109,6 +108,8 @@ public class Reader {
} }
private static boolean isWhiteSpace(int ch) { private static boolean isWhiteSpace(int ch) {
return (ch == ' ' || ch == '\t' || ch == '\f' || ch == '\r' || ch == '\n'); return (ch == ' ' || ch == '\t' ||
ch == '\f' || ch == '\r' ||
ch == '\n');
} }
} }

View File

@ -14,12 +14,6 @@ public class SpecialForm extends Node {
} }
public static Node check(ListNode<Node> l) { public static Node check(ListNode<Node> l) {
if (l != ListNode.EMPTY && l.first() instanceof SymbolNode) {
System.out.println(">>>>");
//SymbolNode g = l.first();
//System.out.println(g.);
}
if (l == ListNode.EMPTY) { if (l == ListNode.EMPTY) {
return l; return l;
} else if (l.first().equals(DEF)) { } else if (l.first().equals(DEF)) {

View File

@ -1,25 +1,32 @@
package serene.simple; package serene.simple;
public class SymbolNode extends Node { public class SymbolNode extends Node {
public final String name; public String name;
public SymbolNode(String name) { public SymbolNode(String name) {
this.name = name; this.name = name;
} }
@Override
public boolean equals(Node other) {
if (other instanceof SymbolNode) {
SymbolNode n = (SymbolNode) other;
public boolean equals(SymbolNode n) { if (this.name.equals(n.name)) {
System.out.println("><<><>"); return true;
System.out.println(n.name); }
if (this.name == n.name) {
return true; return false;
} }
return super.equals(other);
return false;
} }
@Override @Override
public Object eval(Scope scope) { public Object eval(Scope scope) {
return scope.lookupSymbol(this.name); return scope.lookupSymbol(this.name);
} }
public String toString() {
return String.format("Symbol<%s>", this.name);
}
} }