Functions have been done
This commit is contained in:
parent
730c9a91cb
commit
3e35e1fe8a
2
Makefile
2
Makefile
|
@ -1,2 +1,2 @@
|
||||||
repl:
|
repl:
|
||||||
gradle compileJava && rlwrap java -cp bin/main/ serene.simple.Main
|
gradle compileJava && rlwrap java -cp build/classes/java/main/ serene.simple.Main
|
||||||
|
|
|
@ -14,12 +14,9 @@ public class FnSpecialForm extends SpecialForm {
|
||||||
|
|
||||||
return new Function<Object, Object>() {
|
return new Function<Object, Object>() {
|
||||||
@Override
|
@Override
|
||||||
public Object apply(Object arg) {
|
public Object apply(Object arguments) {
|
||||||
Object args[] = {arg};
|
Object[] args = (Object[]) arguments;
|
||||||
return this.apply(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object apply(Object... args) {
|
|
||||||
Scope scope = new Scope(parentScope);
|
Scope scope = new Scope(parentScope);
|
||||||
if (args.length != formalParams.length) {
|
if (args.length != formalParams.length) {
|
||||||
throw new RuntimeException(String.format("Wrong number of arguments. Expected: %s, Got: %s.",
|
throw new RuntimeException(String.format("Wrong number of arguments. Expected: %s, Got: %s.",
|
||||||
|
@ -39,6 +36,7 @@ public class FnSpecialForm extends SpecialForm {
|
||||||
for (Node node : body) {
|
for (Node node : body) {
|
||||||
output = node.eval(scope);
|
output = node.eval(scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -44,16 +44,16 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
Function f = (Function) this.first().eval(scope);
|
Function<Object, Object> f = (Function) this.first().eval(scope);
|
||||||
List<Object> args = new ArrayList<Object>();
|
List<Object> args = new ArrayList<Object>();
|
||||||
|
|
||||||
for (T node : this.rest) {
|
for (T node : this.rest()) {
|
||||||
args.add(node.eval(scope));
|
args.add(node.eval(scope));
|
||||||
}
|
}
|
||||||
return f.apply(args.toArray());
|
return f.apply(args.toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public T first() {
|
public T first() {
|
||||||
if (this != EMPTY) {
|
if (this != EMPTY) {
|
||||||
return this.first;
|
return this.first;
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ 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 = "(fn (x) x)";
|
||||||
|
|
||||||
if (inputData == null) break;
|
if (inputData == null) break;
|
||||||
|
|
||||||
|
@ -34,6 +35,7 @@ public class Main {
|
||||||
|
|
||||||
try {
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,7 +76,7 @@ public class Reader {
|
||||||
while (true) {
|
while (true) {
|
||||||
int ch = inputStream.read();
|
int ch = inputStream.read();
|
||||||
|
|
||||||
if(isWhiteSpace(ch) || ch == -1) {
|
if(isWhiteSpace(ch) || isClosingChar(ch) || ch == -1) {
|
||||||
inputStream.unread(ch);
|
inputStream.unread(ch);
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
|
@ -100,16 +100,20 @@ public class Reader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void skipWhiteSpaces(PushbackReader inputStream) throws IOException {
|
private static void skipWhiteSpaces(PushbackReader inputStream) throws IOException {
|
||||||
int ch = inputStream.read();
|
int ch = inputStream.read();
|
||||||
while (isWhiteSpace(ch)) {
|
|
||||||
ch = inputStream.read();
|
while (isWhiteSpace(ch)) {
|
||||||
}
|
ch = inputStream.read();
|
||||||
inputStream.unread(ch);
|
}
|
||||||
|
inputStream.unread(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isWhiteSpace(int ch) {
|
private static boolean isWhiteSpace(int ch) {
|
||||||
return (ch == ' ' || ch == '\t' ||
|
return (ch == ' ' || ch == '\t' || ch == '\f' || ch == '\r' || ch == '\n');
|
||||||
ch == '\f' || ch == '\r' ||
|
}
|
||||||
ch == '\n');
|
|
||||||
|
private static boolean isClosingChar(int ch) {
|
||||||
|
return (ch == ')' || ch == ']' ||
|
||||||
|
ch == '}');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue