Skip to content

Commit

Permalink
[lang1] test (import)
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Apr 1, 2024
1 parent ed51ebc commit d8a5f37
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[lang1] test `(import)`

> [lang0] 支持直接递归函数与相互递归函数,不能判断等价的地方就不判断。
[lang0] check occor
Expand Down
3 changes: 3 additions & 0 deletions docs/lang1/tests/compose.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(define (id x) x)

(define (compose f g x) (f (g x)))
16 changes: 16 additions & 0 deletions docs/lang1/tests/import.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(import "./compose.scm" id compose (rename compose c))

;; (assert-equal
;; (compose
;; (compose id id)
;; (compose id id))
;; (c (c id id) (c id id))
;; id)

(compose
(compose id id)
(compose id id))

(c (c id id) (c id id))

id
3 changes: 3 additions & 0 deletions docs/lang1/tests/import.scm.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lambda (x₄) x₄)
(lambda (x₁₀) x₁₀)
(lambda (x) x)
6 changes: 6 additions & 0 deletions src/lang0/syntax/matchStmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,28 @@ export function matchStmt(sexp: Sexp): Stmt {
),
),
],

[
["define", v("name"), v("exp")],
({ name, exp }) => Stmts.Define(matchSymbol(name), matchExp(exp)),
],

[
cons("import", cons(v("url"), v("entries"))),
({ url, entries }) =>
Stmts.Import(matchString(url), matchList(entries, matchImportEntry)),
],

[
cons("assert-equal", v("exps")),
({ exps }) => Stmts.AssertEqual(matchList(exps, matchExp)),
],

[
cons("assert-not-equal", v("exps")),
({ exps }) => Stmts.AssertNotEqual(matchList(exps, matchExp)),
],

[v("exp"), ({ exp }) => Stmts.Compute(matchExp(exp))],
])
}
Expand All @@ -55,6 +60,7 @@ function matchImportEntry(sexp: Sexp): Stmts.ImportEntry {
rename: matchSymbol(rename),
}),
],

[v("name"), ({ name }) => ({ name: matchSymbol(name) })],
])
}
14 changes: 7 additions & 7 deletions src/lang1/run/load.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Fetcher } from "@cicada-lang/framework/lib/fetcher/index.js"
import { ParsingError } from "@cicada-lang/sexp"
import fs from "node:fs"
import { createMod, type Mod } from "../mod/index.js"
import { createMod, modResolve, type Mod } from "../mod/index.js"
import { type Stmt } from "../stmt/index.js"
import { Parser } from "../syntax/index.js"

Expand All @@ -25,12 +25,12 @@ export async function load(
mod.stmts = parseStmts(text)
loadedMods.set(url.href, { mod, text })

// for (const stmt of mod.stmts) {
// if (stmt["@kind"] === "Import") {
// const importedUrl = modResolve(mod, stmt.path)
// await load(importedUrl, loadedMods)
// }
// }
for (const stmt of mod.stmts) {
if (stmt["@kind"] === "Import") {
const importedUrl = modResolve(mod, stmt.path)
await load(importedUrl, loadedMods)
}
}

return mod
} catch (error) {
Expand Down
30 changes: 29 additions & 1 deletion src/lang1/syntax/matchStmt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { cons, match, matchList, v, type Sexp } from "@cicada-lang/sexp"
import {
cons,
match,
matchList,
matchString,
matchSymbol,
v,
type Sexp,
} from "@cicada-lang/sexp"
import * as Exps from "../exp/index.js"
import * as Stmts from "../stmt/index.js"
import { type Stmt } from "../stmt/index.js"
Expand All @@ -24,6 +32,26 @@ export function matchStmt(sexp: Sexp): Stmt {
({ name, exp }) => Stmts.Define(matchName(name), matchExp(exp)),
],

[
cons("import", cons(v("url"), v("entries"))),
({ url, entries }) =>
Stmts.Import(matchString(url), matchList(entries, matchImportEntry)),
],

[v("exp"), ({ exp }) => Stmts.Compute(matchExp(exp))],
])
}

function matchImportEntry(sexp: Sexp): Stmts.ImportEntry {
return match<Stmts.ImportEntry>(sexp, [
[
["rename", v("name"), v("rename")],
({ name, rename }) => ({
name: matchSymbol(name),
rename: matchSymbol(rename),
}),
],

[v("name"), ({ name }) => ({ name: matchSymbol(name) })],
])
}

0 comments on commit d8a5f37

Please sign in to comment.