Skip to content

Commit

Permalink
[builtin] Builtin as a word
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 5, 2023
1 parent c427417 commit be82189
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[builtin] `Builtin` as a word
[builtin] `Builtin` -- syntax
[builtin] add `@` as prefix to all builtins
[builtin] remove `isPrivate` from Definition
Expand Down
16 changes: 16 additions & 0 deletions src/lang/compose/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ export function compose(
}
}

case "Builtin": {
const definition = mod.builtins.get(word.name)
if (definition === undefined) {
throw new Error(
[
`[compose / Builtin] I meet undefined builtin.`,
``,
` name: ${word.name}`,
].join("\n"),
)
}

composeDefinition(env, definition, options)
return
}

case "Local": {
const port = env.stack.pop()
if (port === undefined) {
Expand Down
8 changes: 8 additions & 0 deletions src/lang/word/Word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Span } from "../span"

export type Word =
| Call
| Builtin
| Local
| PortPush
| PortReconnect
Expand All @@ -16,6 +17,13 @@ export type Call = {
span: Span
}

export type Builtin = {
"@type": "Word"
"@kind": "Builtin"
name: string
span: Span
}

export type Local = {
"@type": "Word"
"@kind": "Local"
Expand Down
4 changes: 4 additions & 0 deletions src/lang/word/formatWord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export function formatWord(word: Word): string {
return word.name
}

case "Builtin": {
return `@${word.name}`
}

case "Local": {
return `$${word.name}`
}
Expand Down

0 comments on commit be82189

Please sign in to comment.