diff --git a/src/main/java/serene/simple/BaseScope.java b/src/main/java/serene/simple/BaseScope.java deleted file mode 100644 index b9c2192..0000000 --- a/src/main/java/serene/simple/BaseScope.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Serene (simple) - A PoC lisp to collect data on Serenes concepts - * Copyright (C) 2019-2020 Sameer Rahmani - * - * 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 symbolsMapping = new HashMap(); - 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); - } -} diff --git a/src/main/java/serene/simple/DefSpecialForm.java b/src/main/java/serene/simple/DefSpecialForm.java index e67030c..d784f02 100644 --- a/src/main/java/serene/simple/DefSpecialForm.java +++ b/src/main/java/serene/simple/DefSpecialForm.java @@ -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)); diff --git a/src/main/java/serene/simple/FalseNode.java b/src/main/java/serene/simple/FalseNode.java index 446112a..63535c2 100644 --- a/src/main/java/serene/simple/FalseNode.java +++ b/src/main/java/serene/simple/FalseNode.java @@ -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; } diff --git a/src/main/java/serene/simple/FnSpecialForm.java b/src/main/java/serene/simple/FnSpecialForm.java index bdd26d6..7cc3a17 100644 --- a/src/main/java/serene/simple/FnSpecialForm.java +++ b/src/main/java/serene/simple/FnSpecialForm.java @@ -27,7 +27,7 @@ public class FnSpecialForm extends SpecialForm { } @Override - public Object eval(final BaseScope parentScope) { + public Object eval(final IScope parentScope) { final ListNode formalParams = (ListNode) this.node.rest().first(); final ListNode body = this.node.rest().rest(); diff --git a/src/main/java/serene/simple/FunctionNode.java b/src/main/java/serene/simple/FunctionNode.java index 85b0171..e0d0c83 100644 --- a/src/main/java/serene/simple/FunctionNode.java +++ b/src/main/java/serene/simple/FunctionNode.java @@ -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; } diff --git a/src/main/java/serene/simple/INamespace.java b/src/main/java/serene/simple/INamespace.java index ee6892a..a651717 100644 --- a/src/main/java/serene/simple/INamespace.java +++ b/src/main/java/serene/simple/INamespace.java @@ -19,6 +19,7 @@ */ package serene.simple; + public interface INamespace { public Object lookupSymbol(String symbolName); public void insertSymbol(String symbolName, Object symbolValue); diff --git a/src/main/java/serene/simple/IfSpecialForm.java b/src/main/java/serene/simple/IfSpecialForm.java index 4d38834..5f76185 100644 --- a/src/main/java/serene/simple/IfSpecialForm.java +++ b/src/main/java/serene/simple/IfSpecialForm.java @@ -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); diff --git a/src/main/java/serene/simple/ListNode.java b/src/main/java/serene/simple/ListNode.java index 6e02299..08adc1b 100644 --- a/src/main/java/serene/simple/ListNode.java +++ b/src/main/java/serene/simple/ListNode.java @@ -68,7 +68,7 @@ public class ListNode extends Node implements Iterable { } @Override - public Object eval(BaseScope scope) { + public Object eval(IScope scope) { SymbolNode firstElement = (SymbolNode) this.first(); List args = new ArrayList(); @@ -89,16 +89,16 @@ public class ListNode extends Node implements Iterable { } } - public Object evalBuiltin(BaseScope scope, AFn fn, List args) { + public Object evalBuiltin(IScope scope, AFn fn, List args) { fn.setArguments(args); return fn.eval(scope); } - public Object evalFn(BaseScope scope, Function fn, List args) { + public Object evalFn(IScope scope, Function fn, List args) { return fn.apply(args.toArray()); } - public Object evalInterop(BaseScope scope, SymbolNode firstElement, List rest) + public Object evalInterop(IScope scope, SymbolNode firstElement, List rest) throws SereneException { String mName = firstElement.name.substring(1, firstElement.name.length()); @@ -136,7 +136,7 @@ public class ListNode extends Node implements Iterable { } } - public Object evalInteropProperty(BaseScope scope, Object target, String propertyName) + public Object evalInteropProperty(IScope scope, Object target, String propertyName) throws SereneException { try { diff --git a/src/main/java/serene/simple/Main.java b/src/main/java/serene/simple/Main.java index 9cfdb48..59ed95b 100644 --- a/src/main/java/serene/simple/Main.java +++ b/src/main/java/serene/simple/Main.java @@ -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 \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 nodes = Reader.read(new FileInputStream(filePath)); diff --git a/src/main/java/serene/simple/NilNode.java b/src/main/java/serene/simple/NilNode.java index 5425908..4f3d4e3 100644 --- a/src/main/java/serene/simple/NilNode.java +++ b/src/main/java/serene/simple/NilNode.java @@ -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; } diff --git a/src/main/java/serene/simple/Node.java b/src/main/java/serene/simple/Node.java index 15dae08..97946c9 100644 --- a/src/main/java/serene/simple/Node.java +++ b/src/main/java/serene/simple/Node.java @@ -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; diff --git a/src/main/java/serene/simple/NumberNode.java b/src/main/java/serene/simple/NumberNode.java index f66e583..186de66 100644 --- a/src/main/java/serene/simple/NumberNode.java +++ b/src/main/java/serene/simple/NumberNode.java @@ -27,7 +27,7 @@ public class NumberNode extends Node { } @Override - public Object eval(BaseScope scope) { + public Object eval(IScope scope) { return this.value; } diff --git a/src/main/java/serene/simple/QuoteSpecialForm.java b/src/main/java/serene/simple/QuoteSpecialForm.java index 22d293c..21a65c5 100644 --- a/src/main/java/serene/simple/QuoteSpecialForm.java +++ b/src/main/java/serene/simple/QuoteSpecialForm.java @@ -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(); } } diff --git a/src/main/java/serene/simple/RootScope.java b/src/main/java/serene/simple/RootScope.java index 5f8ad1c..e755265 100644 --- a/src/main/java/serene/simple/RootScope.java +++ b/src/main/java/serene/simple/RootScope.java @@ -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 symbolsMapping = new HashMap() {{ +public class RootScope extends AScope { + + private final Map symbolsMapping = new HashMap() {{ 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 symbols() { + return this.symbolsMapping; } } diff --git a/src/main/java/serene/simple/Scope.java b/src/main/java/serene/simple/Scope.java index 79d19b9..d2791a3 100644 --- a/src/main/java/serene/simple/Scope.java +++ b/src/main/java/serene/simple/Scope.java @@ -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; + } } diff --git a/src/main/java/serene/simple/SpecialForm.java b/src/main/java/serene/simple/SpecialForm.java index 84bbee1..75d2036 100644 --- a/src/main/java/serene/simple/SpecialForm.java +++ b/src/main/java/serene/simple/SpecialForm.java @@ -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"); } } diff --git a/src/main/java/serene/simple/StringNode.java b/src/main/java/serene/simple/StringNode.java index 835d952..17fb6d1 100644 --- a/src/main/java/serene/simple/StringNode.java +++ b/src/main/java/serene/simple/StringNode.java @@ -27,7 +27,7 @@ public class StringNode extends Node { } @Override - public Object eval(BaseScope scope) { + public Object eval(IScope scope) { return this.value; } diff --git a/src/main/java/serene/simple/SymbolNode.java b/src/main/java/serene/simple/SymbolNode.java index ca45c99..8854aeb 100644 --- a/src/main/java/serene/simple/SymbolNode.java +++ b/src/main/java/serene/simple/SymbolNode.java @@ -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); } diff --git a/src/main/java/serene/simple/TrueNode.java b/src/main/java/serene/simple/TrueNode.java index f29fa3b..43c5ff3 100644 --- a/src/main/java/serene/simple/TrueNode.java +++ b/src/main/java/serene/simple/TrueNode.java @@ -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; } diff --git a/src/main/java/serene/simple/builtin/NewFn.java b/src/main/java/serene/simple/builtin/NewFn.java index 98e7384..c9a21b1 100644 --- a/src/main/java/serene/simple/builtin/NewFn.java +++ b/src/main/java/serene/simple/builtin/NewFn.java @@ -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 args = this.arguments(); if (args.size() == 0) { diff --git a/src/main/java/serene/simple/builtin/PlusFn.java b/src/main/java/serene/simple/builtin/PlusFn.java index acef4c0..d0b6e0f 100644 --- a/src/main/java/serene/simple/builtin/PlusFn.java +++ b/src/main/java/serene/simple/builtin/PlusFn.java @@ -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()) { diff --git a/src/main/java/serene/simple/builtin/PrintlnFn.java b/src/main/java/serene/simple/builtin/PrintlnFn.java index 7fa770a..a1cdcf8 100644 --- a/src/main/java/serene/simple/builtin/PrintlnFn.java +++ b/src/main/java/serene/simple/builtin/PrintlnFn.java @@ -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()) { diff --git a/src/main/java/serene/simple/builtin/QuitFn.java b/src/main/java/serene/simple/builtin/QuitFn.java index f66d175..ea1a570 100644 --- a/src/main/java/serene/simple/builtin/QuitFn.java +++ b/src/main/java/serene/simple/builtin/QuitFn.java @@ -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; }