First PoC version of the REPL has been done
This commit is contained in:
parent
2bfdb4ebf4
commit
5c90da24a2
|
@ -1,7 +1,7 @@
|
|||
package serene.simple;
|
||||
|
||||
public class DefSpecialForm extends SpecialForm {
|
||||
public DefSpecialForm(ListNode listNode) {
|
||||
public DefSpecialForm(ListNode<Node> listNode) {
|
||||
super(listNode);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,25 +25,30 @@ 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);
|
||||
|
||||
try {
|
||||
Object result = ListNode.EMPTY;
|
||||
|
||||
for (Node n : nodes) {
|
||||
result = n.eval(rootScope);
|
||||
}
|
||||
|
||||
System.out.print(";; ");
|
||||
if (result != ListNode.EMPTY) {
|
||||
System.out.print(";; ");
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
catch(Exception e) {
|
||||
System.out.println("Error: ");
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static void runSerene(String filePath) throws IOException {
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,12 +71,12 @@ public class Reader {
|
|||
}
|
||||
|
||||
private static Node readSymbol(PushbackReader inputStream) throws IOException {
|
||||
|
||||
String str = "";
|
||||
|
||||
while (true) {
|
||||
int ch = inputStream.read();
|
||||
if(isWhiteSpace(ch)) {
|
||||
|
||||
if(isWhiteSpace(ch) || ch == -1) {
|
||||
inputStream.unread(ch);
|
||||
break;
|
||||
};
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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) {
|
||||
if (this.name.equals(n.name)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue