MinusFn has been added

This commit is contained in:
Sameer Rahmani 2019-12-30 22:30:09 +00:00
parent c8b9aa3571
commit 1e304032f0
3 changed files with 69 additions and 6 deletions

View File

@ -31,17 +31,21 @@ import java.util.List;
public class Reader { public class Reader {
public static Node readNode(PushbackReader inputStream) throws IOException, IllegalArgumentException, SereneException { public static Node readNode(PushbackReader inputStream) throws IOException, IllegalArgumentException, SereneException {
char c = (char) inputStream.read(); char c = (char) inputStream.read();
char nextChar = (char) inputStream.read();
inputStream.unread(nextChar);
inputStream.unread(c); inputStream.unread(c);
if (c == '(') { if (c == '(') {
return readList(inputStream); return readList(inputStream);
} }
else if (Character.isDigit(c)) {
return readNumber(inputStream);
}
else if (c == '"') { else if (c == '"') {
return readString(inputStream); return readString(inputStream);
} }
else if (Character.isDigit(c) ||
(c == '-' && Character.isDigit(nextChar))) {
return readNumber(inputStream);
}
else if (c == ')') { else if (c == ')') {
throw new IllegalArgumentException("Unmatch paranthesis."); throw new IllegalArgumentException("Unmatch paranthesis.");
} }
@ -51,7 +55,9 @@ public class Reader {
} }
public static ListNode<Node> read(InputStream inputStream) throws IOException, IllegalArgumentException, SereneException { 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 { public static ListNode<Node> read(PushbackReader inputStream) throws IOException, IllegalArgumentException, SereneException {
@ -126,6 +132,12 @@ public class Reader {
int ch = inputStream.read(); int ch = inputStream.read();
String number = ""; String number = "";
if (Character.isDigit((char) ch) || ch == '-') {
number = number + (char) ch;
}
ch = inputStream.read();
while (Character.isDigit((char) ch)) { while (Character.isDigit((char) ch)) {
number = number + (char) ch; number = number + (char) ch;
ch = inputStream.read(); ch = inputStream.read();

View File

@ -25,6 +25,7 @@ import java.util.Map;
import serene.simple.builtin.PrintlnFn; import serene.simple.builtin.PrintlnFn;
import serene.simple.builtin.QuitFn; import serene.simple.builtin.QuitFn;
import serene.simple.builtin.PlusFn; import serene.simple.builtin.PlusFn;
import serene.simple.builtin.MinusFn;
import serene.simple.builtin.NewFn; import serene.simple.builtin.NewFn;
public class RootScope extends AScope { public class RootScope extends AScope {
@ -33,13 +34,13 @@ public class RootScope extends AScope {
put("println", new PrintlnFn()); put("println", new PrintlnFn());
put("quit", new QuitFn()); put("quit", new QuitFn());
put("+", new PlusFn()); put("+", new PlusFn());
put("-", new MinusFn());
put("System", System.class); put("System", System.class);
put("Boolean", Boolean.class); put("Boolean", Boolean.class);
put("String", String.class); put("String", String.class);
put("new", new NewFn()); put("new", new NewFn());
}}; }};
// "+", PlusFn,
// "-", MinusFn,
// "*", TimesFn, // "*", TimesFn,
// "/", ObelusFn, // "/", ObelusFn,
// "mod", ModFn, // "mod", ModFn,

View File

@ -27,6 +27,9 @@ public class SNumber {
public IOps add(Object x); public IOps add(Object x);
public IOps add(Double x); public IOps add(Double x);
public IOps add(Long x); public IOps add(Long x);
public IOps subtract(Object x);
public IOps subtract(Double x);
public IOps subtract(Long x);
public Object value(); public Object value();
} }
@ -61,6 +64,25 @@ public class SNumber {
return y.add(this.v); 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() { public Object value() {
return this.v; return this.v;
} }
@ -91,8 +113,36 @@ public class SNumber {
return new DoubleNumber(this.v + x.doubleValue()); 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() { public Object value() {
return this.v; 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");
}
} }