From 1e304032f0e021c6e2ded28bf6adfbd5e692ef75 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Mon, 30 Dec 2019 22:30:09 +0000 Subject: [PATCH] MinusFn has been added --- src/main/java/serene/simple/Reader.java | 20 +++++++-- src/main/java/serene/simple/RootScope.java | 5 ++- src/main/java/serene/simple/SNumber.java | 50 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/main/java/serene/simple/Reader.java b/src/main/java/serene/simple/Reader.java index 93ca246..4536469 100644 --- a/src/main/java/serene/simple/Reader.java +++ b/src/main/java/serene/simple/Reader.java @@ -31,17 +31,21 @@ import java.util.List; public class Reader { public static Node readNode(PushbackReader inputStream) throws IOException, IllegalArgumentException, SereneException { char c = (char) inputStream.read(); + char nextChar = (char) inputStream.read(); + + inputStream.unread(nextChar); inputStream.unread(c); if (c == '(') { return readList(inputStream); } - else if (Character.isDigit(c)) { - return readNumber(inputStream); - } else if (c == '"') { return readString(inputStream); } + else if (Character.isDigit(c) || + (c == '-' && Character.isDigit(nextChar))) { + return readNumber(inputStream); + } else if (c == ')') { throw new IllegalArgumentException("Unmatch paranthesis."); } @@ -51,7 +55,9 @@ public class Reader { } public static ListNode read(InputStream inputStream) throws IOException, IllegalArgumentException, SereneException { - return read(new PushbackReader(new InputStreamReader(inputStream))); + // The number two is the buffer size, we need a bigger buffer than + // just one byte to be able to read ahead and undo reads + return read(new PushbackReader(new InputStreamReader(inputStream), 2)); } public static ListNode read(PushbackReader inputStream) throws IOException, IllegalArgumentException, SereneException { @@ -126,6 +132,12 @@ public class Reader { int ch = inputStream.read(); String number = ""; + if (Character.isDigit((char) ch) || ch == '-') { + number = number + (char) ch; + } + + ch = inputStream.read(); + while (Character.isDigit((char) ch)) { number = number + (char) ch; ch = inputStream.read(); diff --git a/src/main/java/serene/simple/RootScope.java b/src/main/java/serene/simple/RootScope.java index e755265..b5a0332 100644 --- a/src/main/java/serene/simple/RootScope.java +++ b/src/main/java/serene/simple/RootScope.java @@ -25,6 +25,7 @@ import java.util.Map; import serene.simple.builtin.PrintlnFn; import serene.simple.builtin.QuitFn; import serene.simple.builtin.PlusFn; +import serene.simple.builtin.MinusFn; import serene.simple.builtin.NewFn; public class RootScope extends AScope { @@ -33,13 +34,13 @@ public class RootScope extends AScope { put("println", new PrintlnFn()); put("quit", new QuitFn()); put("+", new PlusFn()); + put("-", new MinusFn()); put("System", System.class); put("Boolean", Boolean.class); put("String", String.class); put("new", new NewFn()); }}; - // "+", PlusFn, - // "-", MinusFn, + // "*", TimesFn, // "/", ObelusFn, // "mod", ModFn, diff --git a/src/main/java/serene/simple/SNumber.java b/src/main/java/serene/simple/SNumber.java index e699b04..e97b4c3 100644 --- a/src/main/java/serene/simple/SNumber.java +++ b/src/main/java/serene/simple/SNumber.java @@ -27,6 +27,9 @@ public class SNumber { public IOps add(Object x); public IOps add(Double x); public IOps add(Long x); + public IOps subtract(Object x); + public IOps subtract(Double x); + public IOps subtract(Long x); public Object value(); } @@ -61,6 +64,25 @@ public class SNumber { return y.add(this.v); } + public IOps subtract(Object x) throws SereneException { + if (x instanceof Long) { + return this.subtract((long) x); + } + else if (x instanceof Double) { + return this.subtract((Double) x); + } + throw new SereneException("Can't cast anything beside Long and Double"); + } + + public IOps subtract(Long x) { + return new LongNumber(this.v - x); + } + + public IOps subtract(Double x) { + DoubleNumber y = new DoubleNumber(x); + return y.subtract(this.v); + } + public Object value() { return this.v; } @@ -91,8 +113,36 @@ public class SNumber { return new DoubleNumber(this.v + x.doubleValue()); } + public IOps subtract(Object x) throws SereneException { + if (x instanceof Long) { + return this.subtract((long) x); + } + else if (x instanceof Double) { + return this.subtract((Double) x); + } + throw new SereneException("Can't cast anything beside Long and Double"); + } + + public IOps subtract(Double x) { + return new DoubleNumber(this.v - x); + } + + public IOps subtract(Long x) { + return new DoubleNumber(this.v - x.doubleValue()); + } + public Object value() { return this.v; } } + + public static IOps createNumber(Object num) { + if (num instanceof Long) { + return new SNumber.LongNumber((long) num); + } + else if (num instanceof Double) { + return new SNumber.DoubleNumber((Double) num); + } + throw new SereneException("Can't cast anything beside Long and Double"); + } }