Obelus function has been added
This commit is contained in:
parent
6f0479024f
commit
5dd0be7493
|
@ -19,20 +19,41 @@
|
||||||
*/
|
*/
|
||||||
package serene.simple;
|
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 {
|
public class NumberNode extends Node {
|
||||||
private final Long value;
|
private boolean isDouble = false;
|
||||||
|
private Double doubleValue;
|
||||||
|
private Long longValue;
|
||||||
|
|
||||||
public NumberNode(Long v) {
|
public NumberNode(String v) {
|
||||||
this.value = v;
|
if (v.contains(".")) {
|
||||||
|
this.doubleValue = Double.parseDouble(v);
|
||||||
|
this.isDouble = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.longValue = Long.parseLong(v, 10);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(IScope scope) {
|
public Object eval(IScope scope) {
|
||||||
return this.value;
|
return this.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,13 +138,13 @@ public class Reader {
|
||||||
|
|
||||||
ch = inputStream.read();
|
ch = inputStream.read();
|
||||||
|
|
||||||
while (Character.isDigit((char) ch)) {
|
while (Character.isDigit((char) ch) || (char) ch == '.') {
|
||||||
number = number + (char) ch;
|
number = number + (char) ch;
|
||||||
ch = inputStream.read();
|
ch = inputStream.read();
|
||||||
}
|
}
|
||||||
|
|
||||||
inputStream.unread(ch);
|
inputStream.unread(ch);
|
||||||
return new NumberNode(Long.parseLong(number, 10));
|
return new NumberNode(number);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Node readString(PushbackReader inputStream) throws IOException {
|
private static Node readString(PushbackReader inputStream) throws IOException {
|
||||||
|
|
|
@ -27,6 +27,7 @@ import serene.simple.builtin.QuitFn;
|
||||||
import serene.simple.builtin.PlusFn;
|
import serene.simple.builtin.PlusFn;
|
||||||
import serene.simple.builtin.MinusFn;
|
import serene.simple.builtin.MinusFn;
|
||||||
import serene.simple.builtin.TimesFn;
|
import serene.simple.builtin.TimesFn;
|
||||||
|
import serene.simple.builtin.ObelusFn;
|
||||||
import serene.simple.builtin.NewFn;
|
import serene.simple.builtin.NewFn;
|
||||||
|
|
||||||
public class RootScope extends AScope {
|
public class RootScope extends AScope {
|
||||||
|
@ -37,6 +38,7 @@ public class RootScope extends AScope {
|
||||||
put("+", new PlusFn());
|
put("+", new PlusFn());
|
||||||
put("-", new MinusFn());
|
put("-", new MinusFn());
|
||||||
put("*", new TimesFn());
|
put("*", new TimesFn());
|
||||||
|
put("/", new ObelusFn());
|
||||||
put("System", System.class);
|
put("System", System.class);
|
||||||
put("Boolean", Boolean.class);
|
put("Boolean", Boolean.class);
|
||||||
put("String", String.class);
|
put("String", String.class);
|
||||||
|
|
|
@ -33,6 +33,9 @@ public class SNumber {
|
||||||
public IOps multiply(Object x);
|
public IOps multiply(Object x);
|
||||||
public IOps multiply(Double x);
|
public IOps multiply(Double x);
|
||||||
public IOps multiply(Long x);
|
public IOps multiply(Long x);
|
||||||
|
public IOps divide(Object x);
|
||||||
|
public IOps divide(Double x);
|
||||||
|
public IOps divide(Long x);
|
||||||
|
|
||||||
public Object value();
|
public Object value();
|
||||||
}
|
}
|
||||||
|
@ -101,6 +104,25 @@ public class SNumber {
|
||||||
return y.multiply(this.v);
|
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() {
|
public Object value() {
|
||||||
return this.v;
|
return this.v;
|
||||||
}
|
}
|
||||||
|
@ -167,6 +189,24 @@ public class SNumber {
|
||||||
return new DoubleNumber(this.v * x.doubleValue());
|
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() {
|
public Object value() {
|
||||||
return this.v;
|
return this.v;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
/**
|
||||||
|
* 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.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<Object> 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue