A very naive string node has been added
This commit is contained in:
parent
79312f29ff
commit
7f0a074ddd
|
@ -16,4 +16,4 @@ public class NumberNode extends Node {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("%s", this.value);
|
return String.format("%s", this.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,9 @@ public class Reader {
|
||||||
else if (Character.isDigit(c)) {
|
else if (Character.isDigit(c)) {
|
||||||
return readNumber(inputStream);
|
return readNumber(inputStream);
|
||||||
}
|
}
|
||||||
|
else if (c == '"') {
|
||||||
|
return readString(inputStream);
|
||||||
|
}
|
||||||
else if (c == ')') {
|
else if (c == ')') {
|
||||||
throw new IllegalArgumentException("Unmatch paranthesis.");
|
throw new IllegalArgumentException("Unmatch paranthesis.");
|
||||||
}
|
}
|
||||||
|
@ -109,6 +112,24 @@ public class Reader {
|
||||||
return new NumberNode(Long.parseLong(number, 10));
|
return new NumberNode(Long.parseLong(number, 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Node readString(PushbackReader inputStream) throws IOException {
|
||||||
|
char opening = (char) inputStream.read();
|
||||||
|
assert opening == '"' : "Strings should start with \"";
|
||||||
|
String str = "";
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
char ch = (char) inputStream.read();
|
||||||
|
|
||||||
|
if(ch == '"') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
str = str + ch;
|
||||||
|
};
|
||||||
|
|
||||||
|
return new StringNode(str);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private static void skipWhiteSpaces(PushbackReader inputStream) throws IOException {
|
private static void skipWhiteSpaces(PushbackReader inputStream) throws IOException {
|
||||||
int ch = inputStream.read();
|
int ch = inputStream.read();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
package serene.simple;
|
||||||
|
|
||||||
|
public class StringNode extends Node {
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
public StringNode(String v) {
|
||||||
|
this.value = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object eval(BaseScope scope) {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue