-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypesConstansts.ts
More file actions
75 lines (64 loc) · 2.13 KB
/
typesConstansts.ts
File metadata and controls
75 lines (64 loc) · 2.13 KB
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
export const locs = {
stackBegin: Symbol('stackBegin'),
ramPointer: Symbol('ramPointer'),
builtinScopeSym: Symbol('builtInScope'),
globalScopeSym: Symbol('globalScope'),
allocateScopeSym: Symbol('allocateScope'),
ramBegin: 1000000n,
}
export type CompilationContext = {
scope: FintScope,
meta: FintMeta,
}
// used to represent operations that can be precalculated at compile time
export class Reducible {
constructor(
public dependents: CompileData[],
public reducer: (val: bigint[]) => bigint,
public labels: symbol[],
){
if(dependents.some(dep => typeof dep === 'object' && !(dep instanceof BreakPoint) && dep.labels.length))
throw new Error('A reducible argument cannot be pointed to');
}
}
export class HangingLabel {
public labels: symbol[];
constructor(label: symbol){
this.labels = [label]; // easier to polymorphic with other options
}
}
export class BreakPoint {
constructor(public label: string){}
}
export type CompileData = number | bigint | symbol | { value: number | bigint | symbol, labels: symbol[] } | Reducible | HangingLabel;
export type RootLevelCompileData = CompileData | BreakPoint;
export class FintMeta {
constructor(
public line: number,
public column: number,
public indent: number,
public startOfLine: boolean,
){}
}
export class FintScope {
private readonly values: string[];
constructor(public readonly parent?: FintScope, public location?: symbol){
this.values = [];
}
get(key: string): {up: number, forward: number} | { location: symbol, forward: number } | undefined {
const index = this.values.indexOf(key);
if(index !== -1) {
if(this.location) return {location: this.location, forward: index};
return {up: 0, forward: index};
}
if(!this.parent) return undefined;
const parentReponse = this.parent.get(key);
if(!parentReponse) return undefined;
if('location' in parentReponse) return parentReponse;
return {up: parentReponse.up + 1, forward: parentReponse.forward};
}
add(key: string){
this.values.push(key);
}
}
export const builtinScope = new FintScope(undefined, locs.builtinScopeSym);