aboutsummaryrefslogtreecommitdiff
path: root/chunk.d
blob: 47a2d7eb11c7613489913c4cc2bf3907619fcc53 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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 bool isIn(Value val);
}

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

    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;

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

    override bool isIn(Value val) {
        if (val.type != ValueType.SEQ) {
            // how do i throw an error here?
            writeln("a non-string can't be 'in?' a string (this should be a compile error)");
            return false;
        }
        Seq seq = val.as.seq;
        if (seq.type != SeqType.STRING) {
            // how do i throw an error here?
            writeln("a non-string can't be 'in?' a string (this should be a compile error)");
            return false;
        }
        String strVal = cast(String)seq;
        return (indexOf(this.str, strVal.str) != -1);
    }

    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() {
        if (this.inner.length == 0) {
            return new List(0);
        }
        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);
    }

    Seq append(Value val) {
        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(val, length);
        return ret;
    }

    override bool isIn(Value val) {
        foreach(Value mine ; this.inner) {
            if (areValuesEqual(mine, val)) {
                return true;
            }
        }
        return false;
    }

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

enum OpCode {

    // MATH
    OP_ADD,
    OP_DIVIDE,
    OP_INCREMENT,
    OP_MULTIPLY,
    OP_NEGATE,
    OP_SUBTRACT,
    OP_ZERO,

    // BOOLEAN
    OP_EQUAL,
    OP_GREATER,
    OP_LESS,
    OP_NOT,

    // LISTS
    OP_LIST,
    OP_LIST_N,

    // SEQUENCES
    OP_CONCAT,  // No?
    OP_APPEND,
    OP_FIRST,   // No?
    OP_LENGTH,
    OP_MEMBER,
    OP_REST,

    // DEFINITIONS/VARIABLES
    OP_CONSTANT,
    OP_DEF_GLOBAL,
    OP_DEF_LOCAL,
    OP_GET_GLOBAL,
    OP_GET_LOCAL,
    OP_SET_GLOBAL,
    OP_SET_LOCAL,

    // JUMPS
    OP_JUMP,
    OP_JUMP_IF_FALSE,
    OP_JUMP_IF_TRUE,
    OP_JUMP_TO,

    // STACK THINGS
    OP_DUPLICATE,
    OP_DUPLICATE_2,
    OP_POP,
    OP_POPB,
    OP_POP_SCOPE,
    OP_ROTATE_N,

    // RANDOM
    OP_NIL,

    // FUNCTIONS
    OP_CALL,
    OP_RETURN,

    // TYPES
    OP_IS_NIL,
    OP_TYPE_CHECK_BOOLEAN,
    OP_TYPE_CHECK_LIST,
    OP_TYPE_CHECK_NUMBER,
    OP_TYPE_CHECK_SEQ,
    OP_TYPE_CHECK_STRING,
}

class Chunk {
    int count = 0;
    ubyte[] code;
    int[] lines;
    Value[] constants;

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

}