BaseScope has been replaced by AScope and IScope interface
This commit is contained in:
parent
0ad9b22974
commit
c8b9aa3571
|
@ -1,53 +0,0 @@
|
||||||
/**
|
|
||||||
* Serene (simple) - A PoC lisp to collect data on Serenes concepts
|
|
||||||
* Copyright (C) 2019-2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or
|
|
||||||
* modify it under the terms of the GNU General Public License
|
|
||||||
* as published by the Free Software Foundation; either version 2
|
|
||||||
* of the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
||||||
* MA 02110-1301, USA.
|
|
||||||
*/
|
|
||||||
package serene.simple;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public abstract class BaseScope {
|
|
||||||
private final Map<String, Object> symbolsMapping = new HashMap<String, Object>();
|
|
||||||
private final BaseScope parent;
|
|
||||||
|
|
||||||
public BaseScope() {
|
|
||||||
this(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BaseScope(BaseScope parent) {
|
|
||||||
this.parent = parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object lookupSymbol(String symbolName) {
|
|
||||||
if (this.symbolsMapping.containsKey(symbolName)) {
|
|
||||||
return this.symbolsMapping.get(symbolName);
|
|
||||||
}
|
|
||||||
else if (this.parent != null) {
|
|
||||||
return this.parent.lookupSymbol(symbolName);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new RuntimeException(String.format("Symbol '%s' is not defined in this scope.",
|
|
||||||
symbolName));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void insertSymbol(String symbolName, Object symbolValue) {
|
|
||||||
this.symbolsMapping.put(symbolName, symbolValue);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -25,7 +25,7 @@ public class DefSpecialForm extends SpecialForm {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
SymbolNode sym = (SymbolNode) this.node.rest().first();
|
SymbolNode sym = (SymbolNode) this.node.rest().first();
|
||||||
scope.insertSymbol(sym.name,
|
scope.insertSymbol(sym.name,
|
||||||
this.node.rest().rest().first().eval(scope));
|
this.node.rest().rest().first().eval(scope));
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class FalseNode extends Node {
|
||||||
public boolean isTruthy = false;
|
public boolean isTruthy = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
return Boolean.FALSE;
|
return Boolean.FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ public class FnSpecialForm extends SpecialForm {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(final BaseScope parentScope) {
|
public Object eval(final IScope parentScope) {
|
||||||
final ListNode<Node> formalParams = (ListNode<Node>) this.node.rest().first();
|
final ListNode<Node> formalParams = (ListNode<Node>) this.node.rest().first();
|
||||||
final ListNode<Node> body = this.node.rest().rest();
|
final ListNode<Node> body = this.node.rest().rest();
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ package serene.simple;
|
||||||
|
|
||||||
public class FunctionNode extends Node {
|
public class FunctionNode extends Node {
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
*/
|
*/
|
||||||
package serene.simple;
|
package serene.simple;
|
||||||
|
|
||||||
|
|
||||||
public interface INamespace {
|
public interface INamespace {
|
||||||
public Object lookupSymbol(String symbolName);
|
public Object lookupSymbol(String symbolName);
|
||||||
public void insertSymbol(String symbolName, Object symbolValue);
|
public void insertSymbol(String symbolName, Object symbolValue);
|
||||||
|
|
|
@ -33,7 +33,7 @@ public class IfSpecialForm extends SpecialForm {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(final BaseScope scope) {
|
public Object eval(final IScope scope) {
|
||||||
Object result = this.pred.eval(scope);
|
Object result = this.pred.eval(scope);
|
||||||
if (result == null || (result instanceof Boolean && (Boolean) result == false)) {
|
if (result == null || (result instanceof Boolean && (Boolean) result == false)) {
|
||||||
return this.elseNode.eval(scope);
|
return this.elseNode.eval(scope);
|
||||||
|
|
|
@ -68,7 +68,7 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
SymbolNode firstElement = (SymbolNode) this.first();
|
SymbolNode firstElement = (SymbolNode) this.first();
|
||||||
List<Object> args = new ArrayList<Object>();
|
List<Object> args = new ArrayList<Object>();
|
||||||
|
|
||||||
|
@ -89,16 +89,16 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object evalBuiltin(BaseScope scope, AFn fn, List<Object> args) {
|
public Object evalBuiltin(IScope scope, AFn fn, List<Object> args) {
|
||||||
fn.setArguments(args);
|
fn.setArguments(args);
|
||||||
return fn.eval(scope);
|
return fn.eval(scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object evalFn(BaseScope scope, Function<Object, Object> fn, List<Object> args) {
|
public Object evalFn(IScope scope, Function<Object, Object> fn, List<Object> args) {
|
||||||
return fn.apply(args.toArray());
|
return fn.apply(args.toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object evalInterop(BaseScope scope, SymbolNode firstElement, List<Object> rest)
|
public Object evalInterop(IScope scope, SymbolNode firstElement, List<Object> rest)
|
||||||
throws SereneException {
|
throws SereneException {
|
||||||
|
|
||||||
String mName = firstElement.name.substring(1, firstElement.name.length());
|
String mName = firstElement.name.substring(1, firstElement.name.length());
|
||||||
|
@ -136,7 +136,7 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object evalInteropProperty(BaseScope scope, Object target, String propertyName)
|
public Object evalInteropProperty(IScope scope, Object target, String propertyName)
|
||||||
throws SereneException {
|
throws SereneException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -26,7 +26,7 @@ import java.io.FileInputStream;
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
private static String licenseHeader = "\nSerene(simple), Copyright (C) 2019-2020" +
|
private static String licenseHeader = "\nSerene(simple), Copyright (C) 2019-2020 " +
|
||||||
"Sameer Rahmani <lxsameer@gnu.org>\n" +
|
"Sameer Rahmani <lxsameer@gnu.org>\n" +
|
||||||
"Serene(simple) comes with ABSOLUTELY NO WARRANTY;\n" +
|
"Serene(simple) comes with ABSOLUTELY NO WARRANTY;\n" +
|
||||||
"This is free software, and you are welcome\n" +
|
"This is free software, and you are welcome\n" +
|
||||||
|
@ -49,7 +49,7 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void startRepl() throws IOException {
|
private static void startRepl() throws IOException {
|
||||||
BaseScope rootScope = new RootScope();
|
RootScope rootScope = new RootScope();
|
||||||
|
|
||||||
Console console = System.console();
|
Console console = System.console();
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void runSerene(String filePath) throws IOException {
|
private static void runSerene(String filePath) throws IOException {
|
||||||
BaseScope rootScope = new RootScope();
|
IScope rootScope = new RootScope();
|
||||||
|
|
||||||
ListNode<Node> nodes = Reader.read(new FileInputStream(filePath));
|
ListNode<Node> nodes = Reader.read(new FileInputStream(filePath));
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class NilNode extends Node {
|
||||||
public boolean isTruthy = false;
|
public boolean isTruthy = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ package serene.simple;
|
||||||
public abstract class Node {
|
public abstract class Node {
|
||||||
public boolean isTruthy = true;
|
public boolean isTruthy = true;
|
||||||
|
|
||||||
public abstract Object eval(BaseScope scope);
|
public abstract Object eval(IScope scope);
|
||||||
|
|
||||||
public boolean equals(Node n) {
|
public boolean equals(Node n) {
|
||||||
return this == n;
|
return this == n;
|
||||||
|
|
|
@ -27,7 +27,7 @@ public class NumberNode extends Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ public class QuoteSpecialForm extends SpecialForm {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(final BaseScope scope) {
|
public Object eval(final IScope scope) {
|
||||||
return this.node.rest().first();
|
return this.node.rest().first();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,16 @@
|
||||||
package serene.simple;
|
package serene.simple;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import serene.simple.builtin.PrintlnFn;
|
import serene.simple.builtin.PrintlnFn;
|
||||||
import serene.simple.builtin.QuitFn;
|
import serene.simple.builtin.QuitFn;
|
||||||
import serene.simple.builtin.PlusFn;
|
import serene.simple.builtin.PlusFn;
|
||||||
import serene.simple.builtin.NewFn;
|
import serene.simple.builtin.NewFn;
|
||||||
|
|
||||||
public class RootScope extends BaseScope {
|
public class RootScope extends AScope {
|
||||||
private final BaseScope parent;
|
|
||||||
private final HashMap<String, Object> symbolsMapping = new HashMap<String, Object>() {{
|
private final Map<String, Object> symbolsMapping = new HashMap<String, Object>() {{
|
||||||
put("println", new PrintlnFn());
|
put("println", new PrintlnFn());
|
||||||
put("quit", new QuitFn());
|
put("quit", new QuitFn());
|
||||||
put("+", new PlusFn());
|
put("+", new PlusFn());
|
||||||
|
@ -36,30 +38,23 @@ public class RootScope extends BaseScope {
|
||||||
put("String", String.class);
|
put("String", String.class);
|
||||||
put("new", new NewFn());
|
put("new", new NewFn());
|
||||||
}};
|
}};
|
||||||
// "+", PlusFn,
|
// "+", PlusFn,
|
||||||
// "-", MinusFn,
|
// "-", MinusFn,
|
||||||
// "*", TimesFn,
|
// "*", TimesFn,
|
||||||
// "/", ObelusFn,
|
// "/", ObelusFn,
|
||||||
// "mod", ModFn,
|
// "mod", ModFn,
|
||||||
// "now", NowFn,
|
// "now", NowFn,;
|
||||||
|
|
||||||
|
|
||||||
public RootScope() {
|
public RootScope() {
|
||||||
this.parent = null;
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RootScope(IScope parent) {
|
||||||
|
this.parentScope = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object lookupSymbol(String symbolName) {
|
public Map<String, Object> symbols() {
|
||||||
if (this.symbolsMapping.containsKey(symbolName)) {
|
return this.symbolsMapping;
|
||||||
return this.symbolsMapping.get(symbolName);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new RuntimeException(String.format("Symbol '%s' is not defined in this scope.",
|
|
||||||
symbolName));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void insertSymbol(String symbolName, Object symbolValue) {
|
|
||||||
this.symbolsMapping.put(symbolName, symbolValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,12 @@
|
||||||
package serene.simple;
|
package serene.simple;
|
||||||
|
|
||||||
|
|
||||||
public class Scope extends BaseScope{
|
public class Scope extends AScope {
|
||||||
private final BaseScope parent;
|
public Scope() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
public Scope() {
|
public Scope(IScope parent) {
|
||||||
this(new RootScope());
|
this.parentScope = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Scope(BaseScope parent) {
|
|
||||||
this.parent = parent;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ public class SpecialForm extends Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) throws SereneException {
|
public Object eval(IScope scope) throws SereneException {
|
||||||
throw new SereneException("Can't use SpecialForm directly");
|
throw new SereneException("Can't use SpecialForm directly");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ public class StringNode extends Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ public class SymbolNode extends Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
return scope.lookupSymbol(this.name);
|
return scope.lookupSymbol(this.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ package serene.simple;
|
||||||
|
|
||||||
public class TrueNode extends Node {
|
public class TrueNode extends Node {
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import serene.simple.BaseScope;
|
import serene.simple.IScope;
|
||||||
import serene.simple.SereneException;
|
import serene.simple.SereneException;
|
||||||
import static serene.simple.Utils.getClassOf;
|
import static serene.simple.Utils.getClassOf;
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ public class NewFn extends AFn {
|
||||||
return "new";
|
return "new";
|
||||||
};
|
};
|
||||||
|
|
||||||
public Object eval(BaseScope scope) throws SereneException {
|
public Object eval(IScope scope) throws SereneException {
|
||||||
List<Object> args = this.arguments();
|
List<Object> args = this.arguments();
|
||||||
|
|
||||||
if (args.size() == 0) {
|
if (args.size() == 0) {
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
*/
|
*/
|
||||||
package serene.simple.builtin;
|
package serene.simple.builtin;
|
||||||
|
|
||||||
import serene.simple.BaseScope;
|
import serene.simple.IScope;
|
||||||
import serene.simple.SNumber;
|
import serene.simple.SNumber;
|
||||||
import serene.simple.SereneException;
|
import serene.simple.SereneException;
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public class PlusFn extends AFn {
|
||||||
return "+";
|
return "+";
|
||||||
};
|
};
|
||||||
|
|
||||||
public Object eval(BaseScope scope) throws SereneException{
|
public Object eval(IScope scope) throws SereneException{
|
||||||
SNumber.IOps result = (SNumber.IOps) new SNumber.LongNumber();
|
SNumber.IOps result = (SNumber.IOps) new SNumber.LongNumber();
|
||||||
|
|
||||||
for(Object x: this.arguments()) {
|
for(Object x: this.arguments()) {
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
*/
|
*/
|
||||||
package serene.simple.builtin;
|
package serene.simple.builtin;
|
||||||
|
|
||||||
import serene.simple.BaseScope;
|
import serene.simple.IScope;
|
||||||
|
|
||||||
|
|
||||||
public class PrintlnFn extends AFn {
|
public class PrintlnFn extends AFn {
|
||||||
|
@ -27,7 +27,7 @@ public class PrintlnFn extends AFn {
|
||||||
return "println";
|
return "println";
|
||||||
};
|
};
|
||||||
|
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
String output = "";
|
String output = "";
|
||||||
|
|
||||||
for(Object x: this.arguments()) {
|
for(Object x: this.arguments()) {
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
*/
|
*/
|
||||||
package serene.simple.builtin;
|
package serene.simple.builtin;
|
||||||
|
|
||||||
import serene.simple.BaseScope;
|
import serene.simple.IScope;
|
||||||
|
|
||||||
|
|
||||||
public class QuitFn extends AFn {
|
public class QuitFn extends AFn {
|
||||||
|
@ -27,7 +27,7 @@ public class QuitFn extends AFn {
|
||||||
return "quit";
|
return "quit";
|
||||||
};
|
};
|
||||||
|
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(IScope scope) {
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue