@psdo/compiler 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +366 -2
- package/dist/ast.d.ts +29 -0
- package/dist/ast.js +664 -0
- package/dist/checker.d.ts +10 -0
- package/dist/checker.js +382 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +193 -0
- package/dist/errors.d.ts +16 -0
- package/dist/errors.js +29 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +57 -0
- package/dist/instrumentation.d.ts +6 -0
- package/dist/instrumentation.js +6 -0
- package/dist/interpreter.d.ts +51 -0
- package/dist/interpreter.js +610 -0
- package/dist/lexer.d.ts +44 -0
- package/dist/lexer.js +161 -0
- package/dist/messages.d.ts +85 -0
- package/dist/messages.js +39 -0
- package/dist/prelude.d.ts +1 -0
- package/dist/prelude.js +125 -0
- package/dist/session.d.ts +37 -0
- package/dist/session.js +136 -0
- package/dist/types.d.ts +216 -0
- package/dist/types.js +10 -0
- package/package.json +45 -4
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
export type Binary = "+" | "-" | "*" | "/" | "%" | "^";
|
|
2
|
+
export type Bool = "<" | ">" | "<=" | ">=" | "==" | "!=";
|
|
3
|
+
export type Logic = "and" | "or";
|
|
4
|
+
export type Unary = "-" | "not";
|
|
5
|
+
export type Direction = "TO" | "DOWNTO";
|
|
6
|
+
export type Args = Expr[];
|
|
7
|
+
export type Param = {
|
|
8
|
+
name: string;
|
|
9
|
+
byRef: boolean;
|
|
10
|
+
};
|
|
11
|
+
export type Params = Param[];
|
|
12
|
+
export type Accessor = {
|
|
13
|
+
kind: "Field";
|
|
14
|
+
name: string;
|
|
15
|
+
} | {
|
|
16
|
+
kind: "Index";
|
|
17
|
+
index: Expr;
|
|
18
|
+
};
|
|
19
|
+
export type Root = {
|
|
20
|
+
kind: "This";
|
|
21
|
+
} | {
|
|
22
|
+
kind: "Name";
|
|
23
|
+
name: string;
|
|
24
|
+
};
|
|
25
|
+
export type Target = {
|
|
26
|
+
root: Root;
|
|
27
|
+
path: Accessor[];
|
|
28
|
+
line: number;
|
|
29
|
+
};
|
|
30
|
+
export type Call = {
|
|
31
|
+
kind: "Call";
|
|
32
|
+
callee: Target;
|
|
33
|
+
args: Args;
|
|
34
|
+
line: number;
|
|
35
|
+
};
|
|
36
|
+
export type Expr = {
|
|
37
|
+
kind: "Number";
|
|
38
|
+
value: number;
|
|
39
|
+
} | {
|
|
40
|
+
kind: "Array";
|
|
41
|
+
elements: Expr[];
|
|
42
|
+
} | {
|
|
43
|
+
kind: "Path";
|
|
44
|
+
target: Target;
|
|
45
|
+
} | {
|
|
46
|
+
kind: "Binary";
|
|
47
|
+
op: Binary | Bool | Logic;
|
|
48
|
+
left: Expr;
|
|
49
|
+
right: Expr;
|
|
50
|
+
} | {
|
|
51
|
+
kind: "Unary";
|
|
52
|
+
op: Unary;
|
|
53
|
+
operand: Expr;
|
|
54
|
+
} | {
|
|
55
|
+
kind: "New";
|
|
56
|
+
name: string;
|
|
57
|
+
args: Args;
|
|
58
|
+
line: number;
|
|
59
|
+
} | Call;
|
|
60
|
+
export type Stmt = {
|
|
61
|
+
kind: "Assign";
|
|
62
|
+
target: Target;
|
|
63
|
+
value: Expr;
|
|
64
|
+
line: number;
|
|
65
|
+
} | {
|
|
66
|
+
kind: "If";
|
|
67
|
+
condition: Expr;
|
|
68
|
+
thenBranch: Stmt[];
|
|
69
|
+
elseBranch: Stmt[] | null;
|
|
70
|
+
line: number;
|
|
71
|
+
} | {
|
|
72
|
+
kind: "While";
|
|
73
|
+
condition: Expr;
|
|
74
|
+
block: Stmt[];
|
|
75
|
+
line: number;
|
|
76
|
+
} | {
|
|
77
|
+
kind: "For";
|
|
78
|
+
name: string;
|
|
79
|
+
from: Expr;
|
|
80
|
+
dir: Direction;
|
|
81
|
+
to: Expr;
|
|
82
|
+
block: Stmt[];
|
|
83
|
+
line: number;
|
|
84
|
+
} | {
|
|
85
|
+
kind: "Return";
|
|
86
|
+
value: Expr | undefined;
|
|
87
|
+
line: number;
|
|
88
|
+
} | Call;
|
|
89
|
+
export type FunctionDef = {
|
|
90
|
+
kind: "Function";
|
|
91
|
+
name: string;
|
|
92
|
+
params: Params;
|
|
93
|
+
block: Stmt[];
|
|
94
|
+
line: number;
|
|
95
|
+
};
|
|
96
|
+
export type Access = "public" | "private";
|
|
97
|
+
export type FieldDef = {
|
|
98
|
+
kind: "Field";
|
|
99
|
+
name: string;
|
|
100
|
+
access: Access;
|
|
101
|
+
line: number;
|
|
102
|
+
};
|
|
103
|
+
export type MethodDef = {
|
|
104
|
+
kind: "Method";
|
|
105
|
+
access: Access;
|
|
106
|
+
fn: FunctionDef;
|
|
107
|
+
};
|
|
108
|
+
export type ConstructorDef = {
|
|
109
|
+
kind: "Constructor";
|
|
110
|
+
params: Params;
|
|
111
|
+
block: Stmt[];
|
|
112
|
+
line: number;
|
|
113
|
+
};
|
|
114
|
+
export type ClassDef = {
|
|
115
|
+
kind: "Class";
|
|
116
|
+
name: string;
|
|
117
|
+
fields: FieldDef[];
|
|
118
|
+
ctor: ConstructorDef | null;
|
|
119
|
+
methods: MethodDef[];
|
|
120
|
+
line: number;
|
|
121
|
+
};
|
|
122
|
+
export type Program = (Stmt | FunctionDef | ClassDef)[];
|
|
123
|
+
export interface GraphNode {
|
|
124
|
+
id: string;
|
|
125
|
+
value: number;
|
|
126
|
+
}
|
|
127
|
+
export interface GraphEdge {
|
|
128
|
+
from: string;
|
|
129
|
+
to: string;
|
|
130
|
+
weight?: number;
|
|
131
|
+
}
|
|
132
|
+
export type Structure = {
|
|
133
|
+
kind: "array";
|
|
134
|
+
id: string;
|
|
135
|
+
name: string;
|
|
136
|
+
values: Value[];
|
|
137
|
+
};
|
|
138
|
+
export type FrameEvent = {
|
|
139
|
+
kind: "assign";
|
|
140
|
+
name: string;
|
|
141
|
+
value: Value;
|
|
142
|
+
location?: Location;
|
|
143
|
+
} | {
|
|
144
|
+
kind: "return";
|
|
145
|
+
name: string;
|
|
146
|
+
value: Value;
|
|
147
|
+
} | {
|
|
148
|
+
kind: "branch";
|
|
149
|
+
taken: boolean;
|
|
150
|
+
} | {
|
|
151
|
+
kind: "compare";
|
|
152
|
+
a: number;
|
|
153
|
+
b: number;
|
|
154
|
+
result: boolean;
|
|
155
|
+
aLocation?: Location;
|
|
156
|
+
bLocation?: Location;
|
|
157
|
+
} | {
|
|
158
|
+
kind: "swap";
|
|
159
|
+
a: number;
|
|
160
|
+
b: number;
|
|
161
|
+
aLocation?: Location;
|
|
162
|
+
bLocation?: Location;
|
|
163
|
+
} | {
|
|
164
|
+
kind: "visit";
|
|
165
|
+
nodeId: string;
|
|
166
|
+
} | {
|
|
167
|
+
kind: "end";
|
|
168
|
+
};
|
|
169
|
+
export type Location = {
|
|
170
|
+
structureId: string;
|
|
171
|
+
indices: number[];
|
|
172
|
+
};
|
|
173
|
+
export interface Counters {
|
|
174
|
+
comparisons: number;
|
|
175
|
+
swaps: number;
|
|
176
|
+
visits: number;
|
|
177
|
+
}
|
|
178
|
+
export declare const COUNTERS_DEFAULT: Counters;
|
|
179
|
+
export type ObjectValue = {
|
|
180
|
+
kind: "object";
|
|
181
|
+
cls: string;
|
|
182
|
+
fields: Record<string, Value>;
|
|
183
|
+
};
|
|
184
|
+
export type Value = number | Value[] | ObjectValue;
|
|
185
|
+
export type Env = Map<string, Value>;
|
|
186
|
+
export type FunctionMap = Map<string, FunctionDef>;
|
|
187
|
+
export type ClassMap = Map<string, ClassDef>;
|
|
188
|
+
export type Ref = {
|
|
189
|
+
get(): Value;
|
|
190
|
+
set(v: Value): void;
|
|
191
|
+
location?: Location;
|
|
192
|
+
};
|
|
193
|
+
export type RefEnv = Map<string, Ref>;
|
|
194
|
+
export interface Scope {
|
|
195
|
+
env: Env;
|
|
196
|
+
refs: RefEnv;
|
|
197
|
+
depth: number;
|
|
198
|
+
self?: ObjectValue;
|
|
199
|
+
}
|
|
200
|
+
export interface Memory {
|
|
201
|
+
stackDepth: number;
|
|
202
|
+
arrayCells: number;
|
|
203
|
+
}
|
|
204
|
+
export declare const MEMORY_DEFAULTS: Memory;
|
|
205
|
+
export type SessionStats = Counters & {
|
|
206
|
+
arrayCells: number;
|
|
207
|
+
};
|
|
208
|
+
export interface Frame {
|
|
209
|
+
line: number;
|
|
210
|
+
event: FrameEvent;
|
|
211
|
+
structures: Structure[];
|
|
212
|
+
env: Record<string, Value>;
|
|
213
|
+
counters: Counters;
|
|
214
|
+
memory: Memory;
|
|
215
|
+
timestampMs: number;
|
|
216
|
+
}
|
package/dist/types.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psdo/compiler",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A pseudocode language for teaching algorithms: every run produces a step-by-step trace for visualising comparisons, swaps and state",
|
|
5
|
+
"keywords": ["pseudocode", "education", "teaching", "algorithms", "interpreter", "visualisation", "repl"],
|
|
6
|
+
"author": "tristynhunt",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/tristynhunt/psdo#readme",
|
|
9
|
+
"repository": { "type": "git", "url": "git+https://github.com/tristynhunt/psdo.git" },
|
|
10
|
+
"bugs": { "url": "https://github.com/tristynhunt/psdo/issues" },
|
|
11
|
+
"engines": { "node": ">=20" },
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": ["dist"],
|
|
22
|
+
"bin": { "psdo": "./dist/cli.js" },
|
|
23
|
+
"scripts": {
|
|
24
|
+
"prelude": "tsx scripts/build-prelude.ts",
|
|
25
|
+
"prebuild": "npm run clean && npm run prelude",
|
|
26
|
+
"predev": "npm run clean && npm run prelude",
|
|
27
|
+
"build": "tsc -p tsconfig.build.json",
|
|
28
|
+
"clean": "rimraf dist",
|
|
29
|
+
"dev": "tsc -p tsconfig.build.json --watch",
|
|
30
|
+
"typecheck": "tsc --noEmit",
|
|
31
|
+
"psdo-repl": "tsx src/cli.ts",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"lexer-test": "tsx manual/lexer-test.ts",
|
|
34
|
+
"ast-test": "tsx manual/ast-test.ts",
|
|
35
|
+
"interp-test": "tsx manual/interp-test.ts",
|
|
36
|
+
"run-test": "tsx manual/run-test.ts",
|
|
37
|
+
"check": "npm run typecheck && npm test",
|
|
38
|
+
"prepublishOnly": "npm run check && npm run build"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^26.2.0",
|
|
42
|
+
"rimraf": "^6.1.3",
|
|
43
|
+
"tsx": "^4.23.12",
|
|
44
|
+
"typescript": "^7.0.2",
|
|
45
|
+
"vitest": "^4.1.11"
|
|
46
|
+
}
|
|
47
|
+
}
|