Skip to content

Commit

Permalink
residualizing references to imported identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
azizghuloum committed Dec 15, 2024
1 parent b6226d5 commit e1cf8bf
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 21 deletions.
90 changes: 90 additions & 0 deletions rtsc/.rts/testing-file-extensions.rts.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cid": "rtsc/testing-file-extensions.rts rewrite-ts-visualized 0.0.0",
"cookie": "rewrite-ts-002",
"exported_identifiers": {
"x": [
{
Expand All @@ -19,5 +20,94 @@
}
}
]
},
"context": {
"l1": {"type": "lexical", "name": "y_2"},
"l3": {"type": "lexical", "name": "x_4"},
"l5": {
"type": "syntax_rules_transformer",
"clauses": [
{
"pattern": {
"type": "loc",
"t": {
"type": "atom",
"wrap": {
"marks": [
"rtsc/testing-file-extensions.rts rewrite-ts-visualized 0.0.0",
["top", null]
],
"subst": [
{
"rib_id": "r0",
"cu_id": "rtsc/testing-file-extensions.rts rewrite-ts-visualized 0.0.0"
},
null
],
"aes": null
},
"tag": "identifier",
"content": "foo",
"src": {
"type": "atom",
"tag": "identifier",
"content": "foo",
"src": {
"type": "origin",
"p": 61,
"e": 65,
"f": {
"package": {
"name": "rewrite-ts-visualized",
"version": "0.0.0"
},
"path": "rtsc/testing-file-extensions.rts"
}
}
}
},
"p": {"type": "top"}
},
"template": {
"type": "atom",
"wrap": {
"marks": [
"rtsc/testing-file-extensions.rts rewrite-ts-visualized 0.0.0",
["top", null]
],
"subst": [
{
"rib_id": "r0",
"cu_id": "rtsc/testing-file-extensions.rts rewrite-ts-visualized 0.0.0"
},
null
],
"aes": null
},
"tag": "identifier",
"content": "x",
"src": {
"type": "atom",
"tag": "identifier",
"content": "x",
"src": {
"type": "origin",
"p": 66,
"e": 68,
"f": {
"package": {
"name": "rewrite-ts-visualized",
"version": "0.0.0"
},
"path": "rtsc/testing-file-extensions.rts"
}
}
}
}
}
]
},
"l6": {"type": "type", "name": "t_local_7"},
"l8": {"type": "type", "name": "t_global_9"}
}
}
23 changes: 21 additions & 2 deletions src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ async function preexpand_forms(
case "lexical":
case "type":
case "ts":
case "imported_lexical":
return next(loc);
case "core_syntax": {
const { name } = binding;
Expand Down Expand Up @@ -1243,15 +1244,33 @@ async function postexpand_body(
const resolution = await resolve(content, wrap, context, unit, sort_env[sort], helpers);
switch (resolution.type) {
case "bound": {
const { binding } = resolution;
const { binding, label } = resolution;
switch (binding.type) {
case "ts":
case "type":
case "lexical": {
return cont(rename(loc, binding.name), modular, imp, counter);
}
case "imported_lexical": {
const existing = (imp[label.cuid] ?? {})[label.name];
if (existing) {
return cont(rename(loc, existing.new_name), modular, imp, counter);
} else {
const { name } = binding;
const new_name = `${name}_${counter}`;
const new_counter = counter + 1;
const new_imp: import_req = {
...imp,
[label.cuid]: {
...(imp[label.cuid] ?? {}),
[label.name]: { type: "value", new_name },
},
};
return cont(rename(loc, new_name), modular, new_imp, new_counter);
}
}
default: {
debug(loc, `unhandled ${binding.type}`);
debug(loc, `unhandled ${binding.type} in postexpand_body`);
}
}
}
Expand Down
79 changes: 63 additions & 16 deletions src/library-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import {
} from "./preexpand-helpers";
import { AST, source_file } from "./ast";
import { normalize } from "node:path";
import { CompilationUnit, Context, Loc } from "./syntax-structures";
import { Binding, CompilationUnit, Context, Loc } from "./syntax-structures";
import stringify from "json-stringify-pretty-compact";
import { init_global_context } from "./global-module";

const cookie = "rewrite-ts-002";

type module_state =
| { type: "initial" }
| { type: "stale"; cid: string; pkg: Package; pkg_relative_path: string }
Expand All @@ -28,6 +30,7 @@ type module_state =
pkg: Package;
pkg_relative_path: string;
exported_identifiers: { [name: string]: import_resolution[] };
context: Context;
}
| { type: "error"; reason: string };

Expand Down Expand Up @@ -98,22 +101,27 @@ class RtsModule implements imported_module {
const json_mtime = await mtime(json_path);
const my_mtime = await mtime(this.path);
assert(my_mtime !== undefined);
//console.log({ cid, my_mtime, json_path });
if (my_mtime >= (json_mtime ?? 0)) {
this.state = { type: "stale", cid, pkg, pkg_relative_path };
} else {
const json = JSON.parse(await fs.readFile(json_path, { encoding: "utf8" }));
assert(json.cid === cid);
assert(json.exported_identifiers !== undefined, `no exported_identifiers in ${json_path}`);
console.error("TODO: check dependencies");
this.state = {
type: "fresh",
cid,
pkg,
pkg_relative_path,
exported_identifiers: json.exported_identifiers,
};
return;
}
const json = JSON.parse(await fs.readFile(json_path, { encoding: "utf8" }));
if (json.cookie !== cookie) {
this.state = { type: "stale", cid, pkg, pkg_relative_path };
return;
}
assert(json.cid === cid);
assert(json.exported_identifiers !== undefined, `no exported_identifiers in ${json_path}`);
assert(json.context !== undefined, `no exported_identifiers in ${json_path}`);
console.error("TODO: check dependencies");
this.state = {
type: "fresh",
cid,
pkg,
pkg_relative_path,
exported_identifiers: json.exported_identifiers,
context: json.context,
};
}

async recompile() {
Expand All @@ -140,7 +148,9 @@ class RtsModule implements imported_module {
return mod;
},
resolve_label: async (label) => {
throw new Error(`TODO resolve_label ${label.name} ${label.cuid}`);
const mod = await this.find_module_by_cid(label.cuid);
if (!mod) throw new Error(`cannot find module with cuid = ${label.cuid}`);
return mod.resolve_label(label.name);
},
},
global_unit: this.global_unit,
Expand All @@ -157,7 +167,12 @@ class RtsModule implements imported_module {
context,
);
const exported_identifiers = get_exported_identifiers_from_rib(modular.explicit);
const json_content = { cid: this.state.cid, exported_identifiers };
const json_content = {
cid: this.state.cid,
cookie,
exported_identifiers,
context,
};
const code_path = this.get_generated_code_absolute_path();
await fs.mkdir(dirname(code_path), { recursive: true });
await fs.writeFile(code_path, await pprint(loc));
Expand Down Expand Up @@ -186,6 +201,38 @@ class RtsModule implements imported_module {
return resolutions;
}

async get_cid(): Promise<string> {
//await this.ensureUpToDate();
switch (this.state.type) {
case "fresh":
case "stale":
return this.state.cid;
default:
throw new Error(`invalid state`);
}
}

async find_module_by_cid(cid: string): Promise<imported_module | undefined> {
if ((await this.get_cid()) === cid) return this;
for (const m of this.imported_modules) {
const r = await m.find_module_by_cid(cid);
if (r) return r;
}
return undefined;
}

async resolve_label(name: string): Promise<Binding> {
assert(this.state.type === "fresh");
const context = this.state.context;
const binding = context[name];
switch (binding.type) {
case "lexical":
return { type: "imported_lexical", cuid: this.state.cid, name: binding.name };
default:
throw new Error(`unhandled binding type ${binding.type}`);
}
}

private get_imported_modules_for_path(import_path: string, loc: Loc): imported_module {
const mod = this.library_manager.do_import(import_path, this.path);
if (this.imported_modules.includes(mod)) return mod;
Expand Down
3 changes: 3 additions & 0 deletions src/preexpand-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export type imported_module = {
imported_modules: imported_module[];
resolve_exported_identifier: (name: string, loc: Loc) => Promise<import_resolution[]>;
ensureUpToDate(): Promise<void>;
get_cid(): Promise<string>;
find_module_by_cid(cid: string): Promise<imported_module | undefined>;
resolve_label(name: string): Promise<Binding>;
};

export type manager = {
Expand Down
1 change: 1 addition & 0 deletions src/syntax-structures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type Binding =
| { type: "type"; name: string }
| { type: "core_syntax"; name: string; pattern: STX }
| { type: "syntax_rules_transformer"; clauses: { pattern: Loc; template: STX }[] }
| { type: "imported_lexical"; name: string; cuid: string }
| { type: "ts"; name: string };

export type Context = { [label: string]: Binding };
6 changes: 6 additions & 0 deletions test-project/.rts/main.rts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cid": "test-project/main.rts rewrite-ts-visualized 0.0.0",
"cookie": "rewrite-ts-002",
"exported_identifiers": {},
"context": {}
}
1 change: 1 addition & 0 deletions test-project/.rts/main.rts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(x_2_1);

Check failure on line 1 in test-project/.rts/main.rts.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Cannot find name 'x_2_1'.
4 changes: 3 additions & 1 deletion test-project/.rts/mod.rts.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cid": "test-project/mod.rts rewrite-ts-visualized 0.0.0",
"cookie": "rewrite-ts-002",
"exported_identifiers": {
"x": [
{
Expand All @@ -10,5 +11,6 @@
}
}
]
}
},
"context": {"l1": {"type": "lexical", "name": "x_2"}}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"jsx": "react-jsx",
"skipLibCheck": true
},
"exclude": ["./tests", "./scribblings", "./test-project"]
"exclude": ["./tests", "./scribblings"]
}
2 changes: 1 addition & 1 deletion tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"root":["./files.test.ts","./generate-stdlibs.test.ts","./vite.config.ts","./vitest.config.ts","./rtsc/testing-file-extensions.rts.ts","./rtsc/watch.ts","./src/assert.ts","./src/ast.ts","./src/expander.ts","./src/fs-helpers.ts","./src/generate-stdlibs.ts","./src/generated-stdlibs.ts","./src/global-module.ts","./src/library-manager.ts","./src/llhelpers.ts","./src/parse.ts","./src/pprint.ts","./src/preexpand-handlers.ts","./src/preexpand-helpers.ts","./src/proxy-code.ts","./src/serialize.ts","./src/stx-error.ts","./src/stx.ts","./src/syntax-core-patterns.ts","./src/syntax-structures.ts","./src/tags.ts","./src/zipper.ts","./ui/astvis.tsx","./ui/app.tsx","./ui/editor.tsx","./ui/main.tsx","./ui/vite-env.d.ts"],"version":"5.7.2"}
{"root":["./files.test.ts","./generate-stdlibs.test.ts","./vite.config.ts","./vitest.config.ts","./rtsc/testing-file-extensions.rts.ts","./rtsc/watch.ts","./src/assert.ts","./src/ast.ts","./src/expander.ts","./src/fs-helpers.ts","./src/generate-stdlibs.ts","./src/generated-stdlibs.ts","./src/global-module.ts","./src/library-manager.ts","./src/llhelpers.ts","./src/parse.ts","./src/pprint.ts","./src/preexpand-handlers.ts","./src/preexpand-helpers.ts","./src/proxy-code.ts","./src/serialize.ts","./src/stx-error.ts","./src/stx.ts","./src/syntax-core-patterns.ts","./src/syntax-structures.ts","./src/tags.ts","./src/zipper.ts","./test-project/index.ts","./test-project/main.rts.ts","./test-project/mod.rts.ts","./ui/astvis.tsx","./ui/app.tsx","./ui/editor.tsx","./ui/main.tsx","./ui/vite-env.d.ts"],"errors":true,"version":"5.7.2"}

0 comments on commit e1cf8bf

Please sign in to comment.