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
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
SymbolNode sym = (SymbolNode) this.node.rest().first();
|
||||
scope.insertSymbol(sym.name,
|
||||
this.node.rest().rest().first().eval(scope));
|
||||
|
|
|
@ -23,7 +23,7 @@ public class FalseNode extends Node {
|
|||
public boolean isTruthy = false;
|
||||
|
||||
@Override
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ public class FnSpecialForm extends SpecialForm {
|
|||
}
|
||||
|
||||
@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> body = this.node.rest().rest();
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ package serene.simple;
|
|||
|
||||
public class FunctionNode extends Node {
|
||||
@Override
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
package serene.simple;
|
||||
|
||||
|
||||
public interface INamespace {
|
||||
public Object lookupSymbol(String symbolName);
|
||||
public void insertSymbol(String symbolName, Object symbolValue);
|
||||
|
|
|
@ -33,7 +33,7 @@ public class IfSpecialForm extends SpecialForm {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object eval(final BaseScope scope) {
|
||||
public Object eval(final IScope scope) {
|
||||
Object result = this.pred.eval(scope);
|
||||
if (result == null || (result instanceof Boolean && (Boolean) result == false)) {
|
||||
return this.elseNode.eval(scope);
|
||||
|
|
|
@ -68,7 +68,7 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
SymbolNode firstElement = (SymbolNode) this.first();
|
||||
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);
|
||||
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());
|
||||
}
|
||||
|
||||
public Object evalInterop(BaseScope scope, SymbolNode firstElement, List<Object> rest)
|
||||
public Object evalInterop(IScope scope, SymbolNode firstElement, List<Object> rest)
|
||||
throws SereneException {
|
||||
|
||||
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 {
|
||||
|
||||
try {
|
||||
|
|
|
@ -26,7 +26,7 @@ import java.io.FileInputStream;
|
|||
|
||||
|
||||
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" +
|
||||
"Serene(simple) comes with ABSOLUTELY NO WARRANTY;\n" +
|
||||
"This is free software, and you are welcome\n" +
|
||||
|
@ -49,7 +49,7 @@ public class Main {
|
|||
}
|
||||
|
||||
private static void startRepl() throws IOException {
|
||||
BaseScope rootScope = new RootScope();
|
||||
RootScope rootScope = new RootScope();
|
||||
|
||||
Console console = System.console();
|
||||
|
||||
|
@ -92,7 +92,7 @@ public class Main {
|
|||
}
|
||||
|
||||
private static void runSerene(String filePath) throws IOException {
|
||||
BaseScope rootScope = new RootScope();
|
||||
IScope rootScope = new RootScope();
|
||||
|
||||
ListNode<Node> nodes = Reader.read(new FileInputStream(filePath));
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ public class NilNode extends Node {
|
|||
public boolean isTruthy = false;
|
||||
|
||||
@Override
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ package serene.simple;
|
|||
public abstract class Node {
|
||||
public boolean isTruthy = true;
|
||||
|
||||
public abstract Object eval(BaseScope scope);
|
||||
public abstract Object eval(IScope scope);
|
||||
|
||||
public boolean equals(Node n) {
|
||||
return this == n;
|
||||
|
|
|
@ -27,7 +27,7 @@ public class NumberNode extends Node {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ public class QuoteSpecialForm extends SpecialForm {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object eval(final BaseScope scope) {
|
||||
public Object eval(final IScope scope) {
|
||||
return this.node.rest().first();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,14 +20,16 @@
|
|||
package serene.simple;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import serene.simple.builtin.PrintlnFn;
|
||||
import serene.simple.builtin.QuitFn;
|
||||
import serene.simple.builtin.PlusFn;
|
||||
import serene.simple.builtin.NewFn;
|
||||
|
||||
public class RootScope extends BaseScope {
|
||||
private final BaseScope parent;
|
||||
private final HashMap<String, Object> symbolsMapping = new HashMap<String, Object>() {{
|
||||
public class RootScope extends AScope {
|
||||
|
||||
private final Map<String, Object> symbolsMapping = new HashMap<String, Object>() {{
|
||||
put("println", new PrintlnFn());
|
||||
put("quit", new QuitFn());
|
||||
put("+", new PlusFn());
|
||||
|
@ -36,30 +38,23 @@ public class RootScope extends BaseScope {
|
|||
put("String", String.class);
|
||||
put("new", new NewFn());
|
||||
}};
|
||||
// "+", PlusFn,
|
||||
// "-", MinusFn,
|
||||
// "*", TimesFn,
|
||||
// "/", ObelusFn,
|
||||
// "mod", ModFn,
|
||||
// "now", NowFn,
|
||||
|
||||
// "+", PlusFn,
|
||||
// "-", MinusFn,
|
||||
// "*", TimesFn,
|
||||
// "/", ObelusFn,
|
||||
// "mod", ModFn,
|
||||
// "now", NowFn,;
|
||||
|
||||
public RootScope() {
|
||||
this.parent = null;
|
||||
this(null);
|
||||
}
|
||||
|
||||
public RootScope(IScope parent) {
|
||||
this.parentScope = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object lookupSymbol(String symbolName) {
|
||||
if (this.symbolsMapping.containsKey(symbolName)) {
|
||||
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);
|
||||
public Map<String, Object> symbols() {
|
||||
return this.symbolsMapping;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,14 +20,12 @@
|
|||
package serene.simple;
|
||||
|
||||
|
||||
public class Scope extends BaseScope{
|
||||
private final BaseScope parent;
|
||||
public class Scope extends AScope {
|
||||
public Scope() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public Scope() {
|
||||
this(new RootScope());
|
||||
}
|
||||
|
||||
public Scope(BaseScope parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
public Scope(IScope parent) {
|
||||
this.parentScope = parent;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ public class SpecialForm extends Node {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object eval(BaseScope scope) throws SereneException {
|
||||
public Object eval(IScope scope) throws SereneException {
|
||||
throw new SereneException("Can't use SpecialForm directly");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ public class StringNode extends Node {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ public class SymbolNode extends Node {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
return scope.lookupSymbol(this.name);
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ package serene.simple;
|
|||
|
||||
public class TrueNode extends Node {
|
||||
@Override
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import serene.simple.BaseScope;
|
||||
import serene.simple.IScope;
|
||||
import serene.simple.SereneException;
|
||||
import static serene.simple.Utils.getClassOf;
|
||||
|
||||
|
@ -40,7 +40,7 @@ public class NewFn extends AFn {
|
|||
return "new";
|
||||
};
|
||||
|
||||
public Object eval(BaseScope scope) throws SereneException {
|
||||
public Object eval(IScope scope) throws SereneException {
|
||||
List<Object> args = this.arguments();
|
||||
|
||||
if (args.size() == 0) {
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
*/
|
||||
package serene.simple.builtin;
|
||||
|
||||
import serene.simple.BaseScope;
|
||||
import serene.simple.IScope;
|
||||
import serene.simple.SNumber;
|
||||
import serene.simple.SereneException;
|
||||
|
||||
|
@ -29,7 +29,7 @@ public class PlusFn extends AFn {
|
|||
return "+";
|
||||
};
|
||||
|
||||
public Object eval(BaseScope scope) throws SereneException{
|
||||
public Object eval(IScope scope) throws SereneException{
|
||||
SNumber.IOps result = (SNumber.IOps) new SNumber.LongNumber();
|
||||
|
||||
for(Object x: this.arguments()) {
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
*/
|
||||
package serene.simple.builtin;
|
||||
|
||||
import serene.simple.BaseScope;
|
||||
import serene.simple.IScope;
|
||||
|
||||
|
||||
public class PrintlnFn extends AFn {
|
||||
|
@ -27,7 +27,7 @@ public class PrintlnFn extends AFn {
|
|||
return "println";
|
||||
};
|
||||
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
String output = "";
|
||||
|
||||
for(Object x: this.arguments()) {
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
*/
|
||||
package serene.simple.builtin;
|
||||
|
||||
import serene.simple.BaseScope;
|
||||
import serene.simple.IScope;
|
||||
|
||||
|
||||
public class QuitFn extends AFn {
|
||||
|
@ -27,7 +27,7 @@ public class QuitFn extends AFn {
|
|||
return "quit";
|
||||
};
|
||||
|
||||
public Object eval(BaseScope scope) {
|
||||
public Object eval(IScope scope) {
|
||||
System.exit(0);
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue