MinusFn has been added
This commit is contained in:
parent
c8b9aa3571
commit
1e304032f0
|
@ -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<Node> 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<Node> 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();
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue