Several issues with scopes have been fixed
This commit is contained in:
parent
479f341799
commit
d02bdaf158
4
Makefile
4
Makefile
|
@ -1,4 +1,8 @@
|
||||||
repl:
|
repl:
|
||||||
gradle compileJava && rlwrap java -cp build/classes/java/main/ serene.simple.Main
|
gradle compileJava && rlwrap java -cp build/classes/java/main/ serene.simple.Main
|
||||||
|
|
||||||
|
run:
|
||||||
|
gradle compileJava && java -cp build/classes/java/main/ serene.simple.Main ${PWD}/test.srns
|
||||||
|
|
||||||
docs:
|
docs:
|
||||||
npx docco src/**/*.java
|
npx docco src/**/*.java
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serene (simple) - A PoC lisp to collect data on Serenes concepts
|
* Serene (simple) - A PoC lisp to collect data on Serenes concepts
|
||||||
* Copyright (C) 2019-2020 Sameer Rahmani <lxsameer@gnu.org>
|
* Copyright (C) 2019-2020 Sameer Rahmani <lxsameer@gnu.org>
|
||||||
|
@ -47,7 +48,7 @@ public abstract class AScope implements IScope {
|
||||||
public Object lookupSymbol(String symbolName) {
|
public Object lookupSymbol(String symbolName) {
|
||||||
Object value = this.getLocalSymbol(symbolName);
|
Object value = this.getLocalSymbol(symbolName);
|
||||||
|
|
||||||
if (value != null) {
|
if (!(value instanceof Null)) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
else if (this.parent() != null) {
|
else if (this.parent() != null) {
|
||||||
|
@ -68,6 +69,8 @@ public abstract class AScope implements IScope {
|
||||||
return this.symbols().get(symbolName);
|
return this.symbols().get(symbolName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return new Null();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class Null {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class CondSpecialForm extends SpecialForm {
|
||||||
throw new SereneException("Let bindings should be a list of pairs.");
|
throw new SereneException("Let bindings should be a list of pairs.");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.conditions = (ListNode<Node>) listNode.rest().first();
|
this.conditions = (ListNode<Node>) listNode.rest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -38,9 +38,11 @@ public class FnSpecialForm extends SpecialForm {
|
||||||
|
|
||||||
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(
|
||||||
formalParams.length,
|
String.format("Wrong number of arguments. Expected: %s, Got: %s. ARGS: %s",
|
||||||
args.length));
|
formalParams.length,
|
||||||
|
args.length,
|
||||||
|
args.toString()));
|
||||||
}
|
}
|
||||||
// Map parameter values to formal parameter names
|
// Map parameter values to formal parameter names
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
|
@ -109,8 +109,8 @@ public class Main {
|
||||||
|
|
||||||
private static void runSerene(String filePath) throws IOException {
|
private static void runSerene(String filePath) throws IOException {
|
||||||
IScope rootScope = new RootScope();
|
IScope rootScope = new RootScope();
|
||||||
|
FileInputStream input = new FileInputStream(filePath);
|
||||||
ListNode<Node> nodes = Reader.read(new FileInputStream(filePath));
|
ListNode<Node> nodes = Reader.read(input);
|
||||||
|
|
||||||
for (Node n : nodes) {
|
for (Node n : nodes) {
|
||||||
n.eval(rootScope);
|
n.eval(rootScope);
|
||||||
|
|
|
@ -67,7 +67,15 @@ public class Reader {
|
||||||
char c = (char) inputStream.read();
|
char c = (char) inputStream.read();
|
||||||
while ((byte) c != -1) {
|
while ((byte) c != -1) {
|
||||||
if (c == ';') {
|
if (c == ';') {
|
||||||
break;
|
while(true) {
|
||||||
|
char commentChar = (char) inputStream.read();
|
||||||
|
|
||||||
|
if (commentChar == '\n') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c = (char) inputStream.read();
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
inputStream.unread(c);
|
inputStream.unread(c);
|
||||||
|
|
|
@ -44,11 +44,14 @@ public class RootScope extends AScope {
|
||||||
put("or", new OrFn());
|
put("or", new OrFn());
|
||||||
put("not", new NotFn());
|
put("not", new NotFn());
|
||||||
put("conj", new ConjFn());
|
put("conj", new ConjFn());
|
||||||
|
put("cons", new ConjFn());
|
||||||
|
// put("size", new SizeFn());
|
||||||
|
// put("reverse", new SizeFn());
|
||||||
put("list", new ListFn());
|
put("list", new ListFn());
|
||||||
put("first", new FirstFn());
|
put("first", new FirstFn());
|
||||||
put("rest", new RestFn());
|
put("rest", new RestFn());
|
||||||
// put("cond", new CondFn());
|
// put("doc", new DocFn());
|
||||||
//put("reduce", new ReduceFn());
|
// put("reduce", new ReduceFn());
|
||||||
put("System", System.class);
|
put("System", System.class);
|
||||||
put("Boolean", Boolean.class);
|
put("Boolean", Boolean.class);
|
||||||
put("String", String.class);
|
put("String", String.class);
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class ConjFn extends AFn {
|
||||||
ListNode<Object> list = (ListNode<Object>) args.get(0);
|
ListNode<Object> list = (ListNode<Object>) args.get(0);
|
||||||
|
|
||||||
for (Object x : args.subList(1, args.size())) {
|
for (Object x : args.subList(1, args.size())) {
|
||||||
list = new ListNode<Object>(x, list);
|
list = list.cons(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
*/
|
*/
|
||||||
package serene.simple.builtin;
|
package serene.simple.builtin;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import serene.simple.IScope;
|
import serene.simple.IScope;
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,12 +30,17 @@ public class PrintlnFn extends AFn {
|
||||||
};
|
};
|
||||||
|
|
||||||
public Object eval(IScope scope) {
|
public Object eval(IScope scope) {
|
||||||
String output = "";
|
List<Object> args = this.arguments();
|
||||||
|
|
||||||
for(Object x: this.arguments()) {
|
if (args.size() == 0) {
|
||||||
output = String.format(
|
System.out.println("");
|
||||||
"%s %s", output,
|
return null;
|
||||||
(String) x.toString());
|
}
|
||||||
|
|
||||||
|
String output = args.get(0).toString();
|
||||||
|
|
||||||
|
for(Object x: args.subList(1, args.size())) {
|
||||||
|
output = output + " " + x.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println(output);
|
System.out.println(output);
|
||||||
|
|
Loading…
Reference in New Issue