aboutsummaryrefslogtreecommitdiff
path: root/chunk.d
blob: 68df45ba15abd4887b3d58f24335e016b8fdbd8d (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import std.stdio;
import std.string;
import std.conv;

import parser;
import dbg;

enum ObjType {
    FUNCTION,
    SCRIPT,
}

abstract class Obj {
    ObjType type;
}

class Function : Obj {
    Chunk chunk;
    int arity;
    string name;
    ObjType type;

    this(ObjType type, string name = "") {
        this.type = type;
        this.chunk = new Chunk();
        this.arity = 0;
        this.name = name;
    }

    override string toString() {
        if (type == ObjType.SCRIPT) {
            return "<neb>";
        } else {
            return format("<fn %s>", name);
        }
    }
}

enum SeqType {
    LIST,
    STRING,
}

abstract class Seq {
    SeqType type;
    abstract Value first();
    abstract Seq rest();
    abstract int length();
    abstract Seq concat(Seq seq);
}

class String : Seq {
    string str;

    this(Value value) {
        this.str = value.as.str;
        this.type = SeqType.STRING;
    }

    this(string str) {
        this.str = str;
        this.type = SeqType.STRING;
    }

    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 Seq concat(Seq seq) {
        if (seq.type != SeqType.STRING) {
            // how do i throw an error here?
            writeln("must concat strings to strings!");
            return this;
        }
        String strSeq = cast(String)seq;

        writefln("in concat with '%s' and '%s'", this.str, strSeq.str);

        return new String(this.str ~ strSeq.str);
    }

    override string toString() {
        return format("\"%s\"", this.str);
    }
}

class List : Seq {
    Value[] inner;

    this(int length) {
        this.inner = new Value[length];
        this.type = SeqType.LIST;
    }

    void addItemAtIndex(Value item, int idx) {
        this.inner[idx] = item;
    }

    override Value first() {
        return this.inner[0];  // this fails on NIL
    }

    override Seq rest() {
        List ret = new List(to!int(this.inner.length) - 1);
        for (int i = 1; i < this.inner.length; i++) {
            ret.addItemAtIndex(this.inner[i], i - 1);
        }
        return ret;
    }

    override int length() {
        return to!int(this.inner.length);
    }

    override Seq concat(Seq seq) {
        int length = to!int(this.inner.length);
        List ret = new List(length + 1);
        for (int i = 0; i < length; i++) {
            ret.addItemAtIndex(this.inner[i], i);
        }
        ret.addItemAtIndex(makeSeqValue(seq), length);
        return ret;
    }

    override string toString() {
        return format("list ('%s' + %d)", printableValue(this.inner[0]), this.inner.length - 1);
    }
}

enum OpCode {
    OP_ADD,
    OP_LESS,
    OP_NOT,
    OP_EQUAL,
    OP_GREATER,
    OP_NEGATE,
    OP_RETURN,
    OP_CONSTANT,
    OP_DEF_GLOBAL,
    OP_GET_GLOBAL,
    OP_SET_GLOBAL,
    OP_DEF_LOCAL,
    OP_GET_LOCAL,
    OP_SET_LOCAL,
    OP_POP,
    OP_POPB,
    OP_POP_SCOPE,
    OP_SUBTRACT,
    OP_NIL,

    OP_JUMP,
    OP_JUMP_IF_FALSE,
    OP_JUMP_IF_TRUE,

    OP_CALL,

    OP_LIST,

    OP_CONCAT,  // No?

    OP_FIRST,   // No?
    OP_REST,
    OP_LENGTH,

    OP_TYPE_CHECK_NUMBER,
    OP_TYPE_CHECK_BOOLEAN,
    OP_TYPE_CHECK_STRING,
    OP_TYPE_CHECK_LIST,
    OP_TYPE_CHECK_SEQ,
}

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);
    }

}