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;
public class DefSpecialForm extends SpecialForm {
public DefSpecialForm(ListNode listNode) {
public DefSpecialForm(ListNode<Node> listNode) {
super(listNode);
}

View File

@ -25,24 +25,29 @@ public class Main {
System.out.println("Serene 'simple' v0.1.0");
while(true) {
//String inputData = console.readLine("serene-> ");
String inputData = "(def a 4)";
String inputData = console.readLine("serene-> ");
if (inputData == null) break;
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
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) {
result = n.eval(rootScope);
if (result != ListNode.EMPTY) {
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;
}
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 {
List<Node> nodes = new ArrayList<Node>();
skipWhiteSpaces(inputStream);
char c = (char) inputStream.read();
@ -72,19 +71,19 @@ public class Reader {
}
private static Node readSymbol(PushbackReader inputStream) throws IOException {
String str = "";
String str = "";
while (true) {
int ch = inputStream.read();
while (true) {
int ch = inputStream.read();
if(isWhiteSpace(ch)) {
inputStream.unread(ch);
break;
};
str = str + (char) ch;
}
if(isWhiteSpace(ch) || ch == -1) {
inputStream.unread(ch);
break;
};
str = str + (char) ch;
}
return new SymbolNode(str);
return new SymbolNode(str);
}
private static Node readNumber(PushbackReader inputStream) throws IOException {
@ -109,6 +108,8 @@ public class Reader {
}
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) {
if (l != ListNode.EMPTY && l.first() instanceof SymbolNode) {
System.out.println(">>>>");
//SymbolNode g = l.first();
//System.out.println(g.);
}
if (l == ListNode.EMPTY) {
return l;
} else if (l.first().equals(DEF)) {

View File

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