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;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,25 +25,30 @@ 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);
|
||||||
|
|
||||||
|
try {
|
||||||
Object result = ListNode.EMPTY;
|
Object result = ListNode.EMPTY;
|
||||||
|
|
||||||
for (Node n : nodes) {
|
for (Node n : nodes) {
|
||||||
result = n.eval(rootScope);
|
result = n.eval(rootScope);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.print(";; ");
|
|
||||||
if (result != ListNode.EMPTY) {
|
if (result != ListNode.EMPTY) {
|
||||||
|
System.out.print(";; ");
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch(Exception e) {
|
||||||
|
System.out.println("Error: ");
|
||||||
|
e.printStackTrace(System.out);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void runSerene(String filePath) throws IOException {
|
private static void runSerene(String filePath) throws IOException {
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,12 +71,12 @@ 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) {
|
while (true) {
|
||||||
int ch = inputStream.read();
|
int ch = inputStream.read();
|
||||||
if(isWhiteSpace(ch)) {
|
|
||||||
|
if(isWhiteSpace(ch) || ch == -1) {
|
||||||
inputStream.unread(ch);
|
inputStream.unread(ch);
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
|
@ -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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)) {
|
||||||
|
|
|
@ -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("><<><>");
|
|
||||||
System.out.println(n.name);
|
|
||||||
if (this.name == n.name) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return super.equals(other);
|
||||||
|
}
|
||||||
|
|
||||||
@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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue