Skip to content

Commit 578c1ad

Browse files
committed
Implementing module system
1 parent eb194da commit 578c1ad

File tree

12 files changed

+3560
-324
lines changed

12 files changed

+3560
-324
lines changed

demo/std/math.sml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Math
2+
3+
val add = fun (a: _) -> fun (b: _) -> a + b;
4+
val sub = fun (a: _) -> fun (b: _) -> a - b;

demo/use_math.sml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "std/math.sml" as Math
2+
3+
println ("3 + 4 = " ^ Math.add 3 4)

demo/use_math2.sml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import "std/math.sml" as Math
2+
open Math
3+
4+
println ("3 + 4 = " ^ add 3 4)

guess.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

pnpm-lock.yaml

Lines changed: 3016 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
onlyBuiltDependencies:
2+
- unrs-resolver

src/ast.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type BinOp =
1616

1717
export type STerm =
1818
| { t: "Var"; name: string }
19+
| { t: "QVar"; mod: string; name: string }
1920
| { t: "Zero" }
2021
| { t: "Succ"; n: STerm }
2122
| { t: "Str"; value: string }
@@ -31,10 +32,24 @@ export type STerm =
3132

3233
export type ADTCase = { ctor: string; binders: string[]; body: STerm };
3334

35+
export type ImportDecl = {
36+
d: "Import"; path: string; as?: string; open?: boolean;
37+
};
38+
39+
export type ValDecl = {
40+
d: "Val"; name: string; expr: STerm;
41+
};
42+
3443
export type Decl =
35-
| { d: "TypeDecl"; name: string; params: string[]; ctors: { name: string; fields: SType[] }[] };
44+
| { d: "TypeDecl"; name: string; params: string[]; ctors: { name: string; fields: SType[] }[] }
45+
| ImportDecl
46+
| ValDecl;
3647

37-
export type Program = { decls: Decl[]; main: STerm };
48+
export type Program = {
49+
moduleName: string; // filename or explicit `module Foo`
50+
decls: Decl[];
51+
main?: STerm; // optional; default `unit`
52+
};
3853

3954
// --- Core IR (unchanged) ---
4055
export type Term =
@@ -59,4 +74,9 @@ export const TSucc = (n: Term): Term => ({tag: "Succ", n});
5974

6075
// --- Env ---
6176
export type ADT = { name: string; params: string[]; ctors: { name: string; fields: SType[] }[] };
62-
export type Env = { adts: Record<string, ADT>; vars: string[] };
77+
export type Env = {
78+
adts: Record<string, ADT>;
79+
vars: string[]; // local lexical
80+
globals: Record<string, true>; // hoisted Val names
81+
modules: Record<string, { vals: Record<string, true> }>;
82+
};

0 commit comments

Comments
 (0)