-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
157 lines (130 loc) · 3.75 KB
/
types.ts
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
import { Environment } from "./Environment.ts";
import { Interpreter } from "./Interpreter.ts";
import RuntimeError from "./RuntimeError.ts";
import { Function } from "./Stmt.ts";
import Token from "./Token.ts";
export abstract class LoxCallable {
abstract arity(): number;
abstract call(interpreter: Interpreter, args: LoxObject[]): LoxObject;
}
// native 'clock' function
export class LoxClockFunction extends LoxCallable {
arity(): number {
return 0;
}
call(interpreter: Interpreter, args: LoxObject[]): LoxObject {
return Date.now().valueOf() / 1000.0;
}
}
export class LoxFunction extends LoxCallable {
private readonly declaration: Function;
private readonly closure: Environment;
private readonly isInitializer: boolean;
constructor(
declaration: Function,
closure: Environment,
isInitializer: boolean,
) {
super();
this.declaration = declaration;
this.closure = closure;
this.isInitializer = isInitializer;
}
bind(instance: LoxInstance): LoxFunction {
const environment = new Environment(this.closure);
environment.define("this", instance);
return new LoxFunction(this.declaration, environment, this.isInitializer);
}
arity(): number {
return this.declaration.params.length;
}
call(interpreter: Interpreter, args: LoxObject[]): LoxObject {
const environment = new Environment(this.closure);
for (let i = 0; i < this.declaration.params.length; i++) {
environment.define(this.declaration.params[i].lexeme, args[i]);
}
try {
interpreter.executeBlock(this.declaration.body, environment);
} catch (e) {
if (e instanceof ReturnException) {
if (this.isInitializer) return this.closure.getAt(0, "this");
return e.value;
}
}
if (this.isInitializer) return this.closure.getAt(0, "this");
return null;
}
}
export class LoxClass extends LoxCallable {
readonly name: string;
readonly superclass: LoxClass | null;
private readonly methods: Map<string, LoxFunction>;
constructor(
name: string,
superclass: LoxClass | null,
methods: Map<string, LoxFunction>,
) {
super();
this.name = name;
this.superclass = superclass;
this.methods = methods;
}
findMethod(name: string): LoxFunction | null {
if (this.methods.has(name)) {
return this.methods.get(name)!;
}
if (this.superclass != null) {
return this.superclass.findMethod(name);
}
return null;
}
public call(interpreter: Interpreter, args: LoxObject[]): LoxObject {
const instance = new LoxInstance(this);
const initializer = this.findMethod("init");
if (initializer) {
initializer.bind(instance).call(interpreter, args);
}
return instance;
}
arity(): number {
const initializer = this.findMethod("init");
if (initializer == null) return 0;
return initializer.arity();
}
toString(): string {
return this.name;
}
}
export class LoxInstance {
private _class: LoxClass;
private readonly fields = new Map<string, LoxObject>();
constructor(_class: LoxClass) {
this._class = _class;
}
toString(): string {
return this._class.name + " instance";
}
get(name: Token): LoxObject {
if (this.fields.has(name.lexeme)) return this.fields.get(name.lexeme)!;
const method = this._class.findMethod(name.lexeme);
if (method) return method.bind(this);
throw new RuntimeError(name, `Undefined property ${name.lexeme}.`);
}
set(name: Token, value: LoxObject): void {
this.fields.set(name.lexeme, value);
}
}
export class ReturnException extends Error {
readonly value: LoxObject;
constructor(value: LoxObject) {
super();
this.value = value;
}
}
export type LoxObject =
| LoxInstance
| LoxCallable
| string
| number
| boolean
| null;