diff --git a/src/main/java/serene/simple/NumberNode.java b/src/main/java/serene/simple/NumberNode.java index 186de66..899ddfb 100644 --- a/src/main/java/serene/simple/NumberNode.java +++ b/src/main/java/serene/simple/NumberNode.java @@ -19,20 +19,41 @@ */ package serene.simple; +/** + * A poor implementation of a Node number. + * + * Why poor? + * first of all it allocates two types of number to hold one. + */ public class NumberNode extends Node { - private final Long value; + private boolean isDouble = false; + private Double doubleValue; + private Long longValue; - public NumberNode(Long v) { - this.value = v; + public NumberNode(String v) { + if (v.contains(".")) { + this.doubleValue = Double.parseDouble(v); + this.isDouble = true; + } + else { + this.longValue = Long.parseLong(v, 10); + } } @Override public Object eval(IScope scope) { - return this.value; + return this.value(); } @Override public String toString() { - return String.format("%s", this.value); + return String.format("%s", this.value()); + } + + private Object value() { + if (isDouble) { + return doubleValue; + } + return this.longValue; } } diff --git a/src/main/java/serene/simple/Reader.java b/src/main/java/serene/simple/Reader.java index 4536469..1bee0f2 100644 --- a/src/main/java/serene/simple/Reader.java +++ b/src/main/java/serene/simple/Reader.java @@ -138,13 +138,13 @@ public class Reader { ch = inputStream.read(); - while (Character.isDigit((char) ch)) { + while (Character.isDigit((char) ch) || (char) ch == '.') { number = number + (char) ch; ch = inputStream.read(); } inputStream.unread(ch); - return new NumberNode(Long.parseLong(number, 10)); + return new NumberNode(number); } private static Node readString(PushbackReader inputStream) throws IOException { diff --git a/src/main/java/serene/simple/RootScope.java b/src/main/java/serene/simple/RootScope.java index 22102db..bd9353b 100644 --- a/src/main/java/serene/simple/RootScope.java +++ b/src/main/java/serene/simple/RootScope.java @@ -27,6 +27,7 @@ import serene.simple.builtin.QuitFn; import serene.simple.builtin.PlusFn; import serene.simple.builtin.MinusFn; import serene.simple.builtin.TimesFn; +import serene.simple.builtin.ObelusFn; import serene.simple.builtin.NewFn; public class RootScope extends AScope { @@ -37,6 +38,7 @@ public class RootScope extends AScope { put("+", new PlusFn()); put("-", new MinusFn()); put("*", new TimesFn()); + put("/", new ObelusFn()); put("System", System.class); put("Boolean", Boolean.class); put("String", String.class); diff --git a/src/main/java/serene/simple/SNumber.java b/src/main/java/serene/simple/SNumber.java index a1b2e71..8a40639 100644 --- a/src/main/java/serene/simple/SNumber.java +++ b/src/main/java/serene/simple/SNumber.java @@ -33,6 +33,9 @@ public class SNumber { public IOps multiply(Object x); public IOps multiply(Double x); public IOps multiply(Long x); + public IOps divide(Object x); + public IOps divide(Double x); + public IOps divide(Long x); public Object value(); } @@ -101,6 +104,25 @@ public class SNumber { return y.multiply(this.v); } + public IOps divide(Object x) throws SereneException { + if (x instanceof Long) { + return this.divide((long) x); + } + else if (x instanceof Double) { + return this.divide((Double) x); + } + throw new SereneException("Can't cast anything beside Long and Double"); + } + + public IOps divide(Long x) { + return this.divide(x.doubleValue()); + } + + public IOps divide(Double x) { + DoubleNumber y = new DoubleNumber(x); + return y.divide(this.v); + } + public Object value() { return this.v; } @@ -167,6 +189,24 @@ public class SNumber { return new DoubleNumber(this.v * x.doubleValue()); } + public IOps divide(Object x) throws SereneException { + if (x instanceof Long) { + return this.divide((long) x); + } + else if (x instanceof Double) { + return this.divide((Double) x); + } + throw new SereneException("Can't cast anything beside Long and Double"); + } + + public IOps divide(Double x) { + return new DoubleNumber(x / this.v); + } + + public IOps divide(Long x) { + return new DoubleNumber(x.doubleValue() / this.v); + } + public Object value() { return this.v; } diff --git a/src/main/java/serene/simple/builtin/ObelusFn.java b/src/main/java/serene/simple/builtin/ObelusFn.java new file mode 100644 index 0000000..9d8c5f4 --- /dev/null +++ b/src/main/java/serene/simple/builtin/ObelusFn.java @@ -0,0 +1,52 @@ +/** + * 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.builtin; + +import java.util.List; + +import serene.simple.IScope; +import serene.simple.SNumber; +import serene.simple.SereneException; + + +public class ObelusFn extends AFn { + public String fnName() { + return "/"; + }; + + public Object eval(IScope scope) throws SereneException{ + List args = this.arguments(); + + if (args.size() == 0) { + throw new SereneException( + "ArityError: You need 1 or more parameters for this functions."); + } + + SNumber.IOps result = SNumber.createNumber(args.get(0)); + + try { + for(Object x: args.subList(1, args.size())) { + result = (SNumber.IOps) result.divide(x); + } + } + catch (IndexOutOfBoundsException e) {} + return result.value(); + } +}