A very naive string node has been added

This commit is contained in:
Sameer Rahmani 2019-12-19 23:11:18 +00:00
parent 79312f29ff
commit 7f0a074ddd
3 changed files with 156 additions and 116 deletions

View File

@ -16,4 +16,4 @@ public class NumberNode extends Node {
public String toString() {
return String.format("%s", this.value);
}
}
}

View File

@ -20,6 +20,9 @@ public class Reader {
else if (Character.isDigit(c)) {
return readNumber(inputStream);
}
else if (c == '"') {
return readString(inputStream);
}
else if (c == ')') {
throw new IllegalArgumentException("Unmatch paranthesis.");
}
@ -109,6 +112,24 @@ public class Reader {
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 {
int ch = inputStream.read();

View File

@ -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;
}
}