aboutsummaryrefslogtreecommitdiff
path: root/parser.d
diff options
context:
space:
mode:
Diffstat (limited to 'parser.d')
-rw-r--r--parser.d45
1 files changed, 43 insertions, 2 deletions
diff --git a/parser.d b/parser.d
index 71b835b..20231c9 100644
--- a/parser.d
+++ b/parser.d
@@ -28,6 +28,20 @@ abstract class Form {
FormType type;
bool evaluate;
int line;
+
+ //ubyte compile(Chunk chunk);
+ //ubyte[] asBytecode();
+ //abstract void compile(Chunk chunk);
+ void compile(Chunk chunk) {
+ writeln("writing default op");
+ chunk.writeOp(OpCode.OP_NIL, line);
+ }
+
+ void compile(Function func) {
+ writeln("writing default op");
+ func.chunk.writeOp(OpCode.OP_NIL, line);
+ }
+
}
class Symbol : Form {
@@ -70,6 +84,21 @@ class Cons : Form {
Form[] tail;
int argCount;
+ override void compile(Chunk chunk) {
+ if (type == FormType.NIL) {
+ writeln("writing nil on purpose");
+ chunk.writeOp(OpCode.OP_NIL, line);
+ return;
+ }
+
+ // TODO writing head first is almost certainly wrong
+ // it will depend on what head is
+ head.compile(chunk);
+ foreach (Form f ; tail) {
+ f.compile(chunk);
+ }
+ }
+
this(int line) {
this.line = line;
this.type = FormType.NIL;
@@ -108,6 +137,18 @@ class Func : Form {
class Atom : Form {
Value value;
+ override void compile(Chunk chunk) {
+ chunk.writeOp(OpCode.OP_CONSTANT, line);
+ int idx = chunk.addConstant(value);
+ chunk.writeOp(to!ubyte(idx), line);
+ }
+
+ override void compile(Function func) {
+ func.chunk.writeOp(OpCode.OP_CONSTANT, line);
+ int idx = func.chunk.addConstant(value);
+ func.chunk.writeOp(to!ubyte(idx), line);
+ }
+
this(string value, int line) {
this.value = makeStringValue(value);
this.line = line;
@@ -165,13 +206,13 @@ Value makeNumberValue(double number) {
Value makeBooleanValue(bool boolean) {
As as = { boolean: boolean };
- Value val = { ValueType.BOOLEAN };
+ Value val = { ValueType.BOOLEAN, as };
return val;
}
Value makeObjValue(Obj obj) {
As as = { obj: obj };
- Value val = { ValueType.OBJ };
+ Value val = { ValueType.OBJ, as };
return val;
}