aboutsummaryrefslogtreecommitdiff
path: root/compiler.d
diff options
context:
space:
mode:
Diffstat (limited to 'compiler.d')
-rw-r--r--compiler.d52
1 files changed, 36 insertions, 16 deletions
diff --git a/compiler.d b/compiler.d
index 7a198ee..a7a5bfa 100644
--- a/compiler.d
+++ b/compiler.d
@@ -91,10 +91,13 @@ class Compiler {
}
int parseVariable(Def def) {
+
+ writefln("in parseVariable(), scopeDepth is %d", scopeDepth);
+
declareVariable(def.name);
if (scopeDepth > 0) {
- //return 0;
- return localCount - 1;
+ return 0;
+ //return localCount - 1;
//return localCount;
}
@@ -106,7 +109,7 @@ class Compiler {
if (scopeDepth > 0) {
return;
}
- func.chunk.writeOp(OpCode.OP_DEFINE_GLOBAL, current.line);
+ func.chunk.writeOp(OpCode.OP_DEF_GLOBAL, current.line);
func.chunk.writeOp(to!ubyte(addr), current.line);
}
@@ -128,9 +131,13 @@ class Compiler {
}
}
- writefln("> > > creating a local at %d", scopeDepth);
+ writefln("> > > creating a local '%s' at %d", sym.name, scopeDepth);
Local loc = Local(sym, scopeDepth);
- locals ~= loc;
+ if (localCount == locals.length) {
+ locals ~= loc;
+ } else {
+ locals[localCount] = loc;
+ }
localCount++;
writefln("localcount is now %d", localCount);
}
@@ -146,8 +153,9 @@ class Compiler {
// are we setting a local?
if (scopeDepth > 0) {
- func.chunk.writeOp(OpCode.OP_SET_LOCAL, current.line);
- func.chunk.writeOp(to!ubyte(addr), current.line);
+ //func.chunk.writeOp(OpCode.OP_SET_LOCAL, current.line);
+ func.chunk.writeOp(OpCode.OP_DEF_LOCAL, current.line);
+ //func.chunk.writeOp(to!ubyte(addr), current.line);
}
// define the variable
@@ -165,7 +173,7 @@ class Compiler {
//for (int i = localCount - 1; i >= 0; i--) {
for (int i = localCount - 1; i >= 0; i--) {
Local local = locals[i];
- writefln(" > > looping, looking at '%s' (%d) (depth: %d)", local.sym.name, i, local.depth);
+ writefln(" > > looping, looking at '%s' (%d) (depth: %d) (resolving: %s)", local.sym.name, i, local.depth, sym.name);
if (local.sym.name == sym.name) {
return i;
}
@@ -177,25 +185,27 @@ class Compiler {
void compileSymbol(Form form) {
Symbol sym = cast(Symbol)form;
- OpCode getOp;
- OpCode setOp;
+ //OpCode getOp;
+ //OpCode setOp;
int arg = resolveLocal(sym);
if (arg != -1) {
writeln("compiling a LOCAL symbol");
- getOp = OpCode.OP_GET_LOCAL;
- setOp = OpCode.OP_SET_LOCAL; // do we need this here
+ //getOp = OpCode.OP_GET_LOCAL;
+ func.chunk.writeOp(OpCode.OP_GET_LOCAL, sym.line);
+ //setOp = OpCode.OP_SET_LOCAL; // do we need this here
} else {
writeln("compiling a GLOBAL symbol");
arg = func.chunk.addConstant(makeStringValue(sym.name));
- getOp = OpCode.OP_GET_GLOBAL;
- setOp = OpCode.OP_SET_GLOBAL; // do we need this here
+ //getOp = OpCode.OP_GET_GLOBAL;
+ func.chunk.writeOp(OpCode.OP_GET_GLOBAL, sym.line);
+ //setOp = OpCode.OP_SET_GLOBAL; // do we need this here
}
// add the symbol name to the chunk
//int addr = func.chunk.addConstant(makeStringValue(sym.name));
// get the variable
- func.chunk.writeOp(to!ubyte(getOp), sym.line);
+ //func.chunk.writeOp(getOp, sym.line);
func.chunk.writeOp(to!ubyte(arg), sym.line);
//advance();
@@ -222,8 +232,9 @@ class Compiler {
while (localCount > 0 &&
locals[localCount - 1].depth > scopeDepth) {
writeln("> > looping, gonna pop");
- func.chunk.writeOp(to!ubyte(OpCode.OP_POP_SCOPE), -1);
+ //func.chunk.writeOp(to!ubyte(OpCode.OP_POP_SCOPE), -1);
//func.chunk.writeOp(to!ubyte(OpCode.OP_POP), -1);
+ func.chunk.writeOp(OpCode.OP_POPB, -1);
localCount--;
}
}
@@ -277,11 +288,13 @@ class Compiler {
break;
case FormType.DEF:
this.compileDef(form);
+ writeln("DONE COMPILING DEF");
break;
case FormType.SYMBOL:
this.compileSymbol(form);
break;
case FormType.BLOCK:
+ writeln("COMPILING BLOCK");
this.compileBlock(form);
break;
default:
@@ -296,9 +309,16 @@ class Compiler {
writeln("compiling");
advance();
while(current.type != FormType.EOF) {
+ writeln("IN COMPILE LOOP");
resolve(current);
+ writeln("BOTTOM OF COMPILE LOOP");
+ if (current.type == FormType.EOF) {
+ writeln("got an EOF");
+ }
}
+ writeln("outside compile loop");
+
return finish();
}