Skip to content

Commit

Permalink
fixed missing rewrap around macro output
Browse files Browse the repository at this point in the history
  • Loading branch information
azizghuloum committed Dec 3, 2024
1 parent 064da04 commit 149bb08
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/console-2.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Status: `SyntaxError`

```
unbound identifier
unbound identifier 'consoel'
```

### Input Program
Expand Down
27 changes: 27 additions & 0 deletions examples/macro-generating-tmp-binding-1.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## `macro-generating-tmp-binding-1.ts`

### Status: `DONE`

### Input Program

```typescript
define_rewrite_rules(
[define_and_print, define_and_print(v), splice(() => {
const tmp = v; // this is hygienically inserted
console.log(tmp); // and referenced
})],
);
const x = 1;
define_and_print(x); // this will define a tmp
const tmp = 17; // which has nothing to do with this tmp
```

### Output Program

```typescript
const x_3 = 1;
const tmp_6 = x_3;
console.log(tmp_6);
const tmp_8 = 17;
```

40 changes: 31 additions & 9 deletions src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
extend_rib,
extend_context_lexical,
CorePatterns,
push_wrap,
} from "./stx";
import { change, go_down, go_next, go_right, go_up, mkzipper, wrap_loc } from "./zipper";
import { apply_syntax_rules, core_handlers } from "./syntax-core-patterns";
Expand Down Expand Up @@ -55,7 +56,7 @@ function gen_binding({
sort,
}: goodies & { sort: "type" | "value" }): Omit<goodies, "loc"> & { name: string } {
const stx = loc.t;
assert(stx.type === "atom" && stx.tag === "identifier");
assert(stx.type === "atom" && stx.tag === "identifier", stx);
return extend_rib(
rib,
stx.content,
Expand Down Expand Up @@ -191,7 +192,9 @@ async function expand_program(
({ loc, rib, counter, context, unit }) => {
// rib is filled
// context is filled also
return postexpand_program(loc, extend_unit(unit, rib_id, rib), counter, context, inspect);
return inspect(loc, "After preexpanding the program", () =>
postexpand_program(loc, extend_unit(unit, rib_id, rib), counter, context, inspect),
);
},
);
}
Expand Down Expand Up @@ -374,6 +377,14 @@ async function expand_concise_body(
return postexpand_body(gs.loc, new_unit, gs.counter, gs.context, sort, inspect);
}

function rewrap(loc: Loc, rib_id: string, cu_id: string): Loc {
return {
type: "loc",
t: push_wrap({ marks: null, subst: [{ rib_id, cu_id }, null] })(loc.t),
p: loc.p,
};
}

async function preexpand_forms(
loc: Loc,
rib: Rib,
Expand Down Expand Up @@ -432,11 +443,21 @@ async function preexpand_forms(
case "syntax_rules_transformer": {
const { clauses } = binding;
return inspect(loc, `transformer form`, () =>
apply_syntax_rules(loc, clauses, unit, counter).then(({ loc, counter }) =>
inspect(loc, `transformer output`, () =>
preexpand_forms(loc, rib, rib_id, counter, unit, context, sort, inspect),
),
),
apply_syntax_rules(loc, clauses, unit, counter).then(({ loc, counter }) => {
const rewrapped = rewrap(loc, rib_id, unit.cu_id);
return inspect(rewrapped, `transformer output`, () =>
preexpand_forms(
rewrapped,
rib,
rib_id,
counter,
unit,
context,
sort,
inspect,
),
);
}),
);
}
default:
Expand Down Expand Up @@ -1005,8 +1026,9 @@ async function postexpand_body(
}
}
}
case "unbound":
syntax_error(loc, "unbound identifier");
case "unbound": {
syntax_error(loc, `unbound identifier '${content}'`);
}
}
debug(loc, "resolved", resolution);
}
Expand Down
11 changes: 5 additions & 6 deletions src/stx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ function id_to_label(
unit: CompilationUnit,
resolution_type: "normal_env" | "types_env",
): string | undefined {
function loop(marks: Marks | null, subst: Subst): string | undefined {
function lookup(marks: Marks | null, subst: Subst): string | undefined {
if (marks === null) throw new Error("missing marks");
if (subst === null) return undefined; // unbound
if (subst[0] === shift) return loop(marks[1], subst[1]);
if (subst[0] === shift) return lookup(marks[1], subst[1]);
const env = (({ rib_id, cu_id }) => {
assert(cu_id === unit.cu_id, "unhandled imported rib?");
const rib = unit.store[rib_id];
Expand All @@ -52,12 +52,11 @@ function id_to_label(
return rib[resolution_type];
})(subst[0]);
const ls = env[name];
if (ls === undefined) return loop(marks, subst[1]);
const entry = ls.find(([ms, _]) => same_marks(ms, marks));
if (entry === undefined) return loop(marks, subst[1]);
const entry = ls?.find(([ms, _]) => same_marks(ms, marks));
if (entry === undefined) return lookup(marks, subst[1]);
return entry[1];
}
return loop(marks, subst);
return lookup(marks, subst);
}

export type Resolution =
Expand Down
9 changes: 9 additions & 0 deletions tests/macro-generating-tmp-binding-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define_rewrite_rules(
[define_and_print, define_and_print(v), splice(() => {
const tmp = v; // this is hygienically inserted
console.log(tmp); // and referenced
})],
);
const x = 1;
define_and_print(x); // this will define a tmp
const tmp = 17; // which has nothing to do with this tmp

0 comments on commit 149bb08

Please sign in to comment.