diff options
| -rw-r--r-- | chunk.d | 28 | ||||
| -rw-r--r-- | compiler.d | 14 | ||||
| -rw-r--r-- | parser.d | 15 |
3 files changed, 56 insertions, 1 deletions
@@ -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; @@ -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; @@ -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) { |
