aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--chunk.d28
-rw-r--r--compiler.d14
-rw-r--r--parser.d15
3 files changed, 56 insertions, 1 deletions
diff --git a/chunk.d b/chunk.d
index 47d3c73..08ba003 100644
--- a/chunk.d
+++ b/chunk.d
@@ -48,6 +48,34 @@ abstract class Seq {
abstract int length();
}
+class String : Seq {
+ string str;
+
+ this(Value value) {
+ this.str = value.as.str;
+ }
+
+ this(string str) {
+ this.str = str;
+ }
+
+ override Value first() {
+ return makeStringValue(to!string(this.str[0]));
+ }
+
+ override Seq rest() {
+ return new String(this.str[1..$]);
+ }
+
+ override int length() {
+ return to!int(str.length);
+ }
+
+ override string toString() {
+ return format("\"%s\"", this.str);
+ }
+}
+
class List : Seq {
Value[] inner;
diff --git a/compiler.d b/compiler.d
index 66ee664..ecd7785 100644
--- a/compiler.d
+++ b/compiler.d
@@ -99,6 +99,17 @@ class Compiler {
*/
}
+ void compileString(Form form, ValueType expecting) {
+ LiteralString ls = cast(LiteralString)form;
+
+ this.func.chunk.writeOp(OpCode.OP_CONSTANT, ls.line);
+ String str = new String(ls.value);
+ int idx = this.func.chunk.addConstant(makeSeqValue(str));
+ this.func.chunk.writeOp(to!ubyte(idx), ls.line);
+
+ this.advance();
+ }
+
void compileAtom(Form form, const ValueType expecting) {
Atom atom = cast(Atom)form;
/*
@@ -719,6 +730,9 @@ class Compiler {
case FormType.ATOM:
this.compileAtom(form, expecting);
break;
+ case FormType.LITERALSTRING:
+ this.compileString(form, expecting);
+ break;
case FormType.CONS:
return this.compileCons(form, expecting);
break;
diff --git a/parser.d b/parser.d
index e51b5e1..6974662 100644
--- a/parser.d
+++ b/parser.d
@@ -8,6 +8,7 @@ import dbg;
enum FormType {
ATOM,
+ LITERALSTRING,
CONS,
NIL,
SYMBOL,
@@ -240,6 +241,18 @@ class Def : Form {
}
}
+class LiteralString : Form {
+ Value value;
+
+ this(string value, int line) {
+ this.value = makeStringValue(value);
+ this.line = line;
+ this.type = FormType.LITERALSTRING;
+ this.evaluate = false;
+ this.returnType = this.value.type;
+ }
+}
+
class Atom : Form {
Value value;
@@ -419,7 +432,7 @@ class Parser {
return new ParseError("unterminated string!", line);
}
advance(); // go past last quote
- return new Atom(to!string(acc), line);
+ return new LiteralString(to!string(acc), line);
}
bool isBoundary(char ch) {