aboutsummaryrefslogtreecommitdiff
path: root/chunk.d
blob: 5233a87aca30d3ec2a9dd199b4b22a85ed126b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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);
    }

}