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
|
import std.stdio;
import std.string;
import std.conv;
import parser;
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 OpCode {
OP_ADD,
OP_LESS,
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_TYPE_CHECK_NUMBER,
OP_TYPE_CHECK_BOOLEAN,
}
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);
}
}
|