-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.ts
191 lines (182 loc) · 4.88 KB
/
object.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
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
import { Memory } from './memory';
export abstract class BoObject {
abstract str(): string;
compare(other: BoObject): number {
if (this === other) return 0;
else return -1;
}
clone(): BoObject {
return this;
}
}
export class Num extends BoObject {
constructor(public value: number) {
super();
}
str(): string {
return this.value.toString();
}
compare(other: BoObject): number {
if (other instanceof Num) {
const result = this.value - other.value;
if (result === 0) return 0;
else if (result < 0) return -1;
else return 1;
}
return -1;
}
}
export class Str extends BoObject {
constructor(public value: string) {
super();
}
str(): string {
return `"${this.value}"`;
}
compare(other: BoObject): number {
if (other instanceof Str) {
if (this.value === other.value) return 0;
else if (this.value < other.value) return -1;
else return 1; //this.value > other.value
}
return -1;
}
concat(other: BoObject): BoObject {
if (other instanceof Str) return new Str(this.value + other.value);
else return new Str(this.value + other.str());
}
}
export class Nil extends BoObject {
private static instance = new Nil();
private constructor() {
super();
}
str(): string {
return 'nil';
}
compare(other: BoObject): number {
if (other instanceof Nil) return 0;
else return -1;
}
static getInstance(): Nil {
return Nil.instance;
}
}
export const NIL = Nil.getInstance();
export class Message extends BoObject {
receiver: BoObject | undefined;
slotName: string;
args?: BoObject[];
constructor(
receiver: BoObject | undefined,
slotName: string,
args?: BoObject[]
) {
super();
this.receiver = receiver;
if (slotName !== '.') {
this.slotName = slotName;
this.args = args;
} else if (args?.length === 1 && args[0] instanceof Message) {
this.slotName = args[0].slotName;
this.args = args[0].args;
} else {
throw new Error(
`ERROR new Message(${receiver?.str()},${slotName},${args
?.map((e) => e.str())
.join()})`
);
}
}
str(): string {
const receiverStr = this.receiver ? this.receiver.str() : '';
let argsStr: string;
if (this.args) {
argsStr = '(' + this.args.map((e) => e.str()).join(', ') + ')';
} else {
argsStr = '';
}
if (this.slotName === '.' && this.args?.length === 1)
return receiverStr + '.' + this.args![0].str();
else if (this.receiver === undefined) return this.slotName + argsStr;
else return receiverStr + '.' + this.slotName + argsStr;
}
}
export class Fun extends BoObject {
argList: string[];
body: BoObject;
createdEnv: Memory;
constructor(args: BoObject[], env: Memory) {
super();
if (args.length === 0) {
throw new Error('ERROR new Fun(no-argument)');
}
this.body = args[args.length - 1];
this.argList = args.slice(0, args.length - 1).map((e) => {
if (e instanceof Message && !e.receiver && !e.args) return e.slotName;
else throw new Error(`ERROR new Fun() => type error. value=${e.str()}`);
});
this.createdEnv = env;
}
str(): string {
const argStr =
this.argList.length === 0 ? '' : this.argList.join(',') + ',';
return `fun(${argStr}${this.body.str()})`;
}
}
export class Macro extends BoObject {
argList: string[];
body: BoObject;
constructor(args: BoObject[]) {
super();
if (args.length === 0) {
throw new Error('ERROR new Macro(no-argument)');
}
this.body = args[args.length - 1];
this.argList = args.slice(0, args.length - 1).map((e) => {
if (e instanceof Message && !e.receiver && !e.args) return e.slotName;
else throw new Error(`ERROR new Macro() value=${e.str()}`);
});
}
str(): string {
const argStr =
this.argList.length === 0 ? '' : this.argList.join(',') + ',';
return `macro(${argStr}${this.body.str()})`;
}
}
export class UserObject extends BoObject {
memory: Memory;
proto?: UserObject;
constructor(slot: Memory, proto?: UserObject) {
super();
this.memory = slot;
this.proto = proto;
}
compare(other: BoObject): number {
if (this === other) return 0;
else return -1;
}
str(): string {
const s = Array.from(this.memory.slots.entries())
.map(([k, v]) => {
return k + ':' + v.str();
})
.join(',');
return '{' + s + '}';
}
clone(): UserObject {
return new UserObject(this.memory.subMemory(), this);
}
define(name: string, value: BoObject): BoObject | null {
return this.memory.define(name, value);
}
update(name: string, value: BoObject): BoObject | null {
return this.memory.update(name, value);
}
assignToObject(name: string, value: BoObject): BoObject {
return this.memory.defineForce(name, value);
}
get(name: string): BoObject | undefined {
return this.memory.get(name);
}
}