diff options
| author | Ben Winston | 2023-05-19 17:55:36 -0400 |
|---|---|---|
| committer | Ben Winston | 2023-05-19 17:55:36 -0400 |
| commit | 10c75f60c1f5fa27268ea9a850b63b777b087cbc (patch) | |
| tree | ebeb3a040ac24076e68915652c210d93f124c805 /chunk.d | |
initial commit
Diffstat (limited to 'chunk.d')
| -rw-r--r-- | chunk.d | 64 |
1 files changed, 64 insertions, 0 deletions
@@ -0,0 +1,64 @@ +import std.stdio; +import std.string; +import std.conv; + +import parser; + +enum ObjType { + FUNCTION, +} + +abstract class Obj { + ObjType type; +} + +class Function : Obj { + Chunk chunk; + string name; + + this() { + this.type = ObjType.FUNCTION; + this.chunk = new Chunk(); + this.name = ""; + } + + override string toString() { + if (name == "") { + return "<neb>"; + } else { + return name; + } + } +} + +enum OpCode { + OP_ADD, + OP_RETURN, + OP_CONSTANT, + OP_POP, +} + +class Chunk { + int count = 0; + ubyte[] code; + int[] lines; + Value[] constants; + + //int writeOp(OpCode opCode, int line) { + int writeOp(ubyte opCode, int line) { + this.code ~= opCode; + this.lines ~= line; + this.count++; + assert(this.code.length == count); + assert(this.lines.length == count); + return count; + } + + int addConstant(Value value) { + this.constants ~= value; + return to!int(this.constants.length - 1); + } + +} + + |
