Skip to content

Commit

Permalink
@apply as built-in to apply a node
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 13, 2023
1 parent 4a34559 commit 7375060
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 7 deletions.
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
`@apply` as built-in to apply a node

`@spread_ports` as built-in to spread ports of a node
`@ports` as built-in to spread ports of a node

remove syntax of rearrange
update docs about using `@ports` to do rearrange
Expand Down
23 changes: 23 additions & 0 deletions src/lang/builtins/apply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComposeOptions } from "../compose/compose"
import { composeNode } from "../compose/composeNode"
import { Env } from "../env"
import { formatValue } from "../value"

export function compose(env: Env, options: ComposeOptions): void {
const value = env.stack.pop()
if (value === undefined) {
throw new Error(`[@apply] I expect a value on the stack.`)
}

if (value["@kind"] !== "Node") {
throw new Error(
[
`[@apply] I expect the value on top of the stack to be a Node.`,
``,
` node: ${formatValue(value)}`,
].join("\n"),
)
}

composeNode(env, value, options)
}
2 changes: 2 additions & 0 deletions src/lang/builtins/defineBuiltinOperators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Mod } from "../mod"
import * as Type from "./Type"
import * as apply from "./apply"
import * as connect from "./connect"
import { defineBuiltinOperator } from "./defineBuiltinOperator"
import * as inspect from "./inspect"
Expand All @@ -13,5 +14,6 @@ export function defineBuiltinOperators(mod: Mod): void {
defineBuiltinOperator(mod, "connect", connect)
defineBuiltinOperator(mod, "inspect", inspect)
defineBuiltinOperator(mod, "run", run)
defineBuiltinOperator(mod, "apply", apply)
defineBuiltinOperator(mod, "Type", Type)
}
6 changes: 6 additions & 0 deletions tests/builtin/apply.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type Nat -- @Type end
node zero -- Nat :value! end
node add1 Nat :prev -- Nat :value! end

(zero) @apply @inspect
(add1) @apply @inspect
4 changes: 4 additions & 0 deletions tests/builtin/apply.i.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
net_from_port (zero₀)-value! end
net_from_port (add1₀)-value!
(add1₀)-prev value-(zero₀)
end
2 changes: 1 addition & 1 deletion tests/builtin/connect-args.error.i.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[connect (builtin)] I expect a second value on the stack.
[@connect] I expect a second value on the stack.

first: (sole₀)-value!

Expand Down
2 changes: 1 addition & 1 deletion tests/builtin/inspect-args.error.i.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[inspect (builtin)] I expect a value on the stack.
[@inspect] I expect a value on the stack.

[compose] I fail compose word.

Expand Down
2 changes: 1 addition & 1 deletion tests/builtin/rot-args.error.i.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[rot (builtin)] I expect a third value on the stack.
[@rot] I expect a third value on the stack.

first: (sole₁)-value!
second: (sole₀)-value!
Expand Down
2 changes: 1 addition & 1 deletion tests/builtin/swap-args.error.i.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[swap (builtin)] I expect a second value on the stack.
[@swap] I expect a second value on the stack.

first: (sole₀)-value!

Expand Down

0 comments on commit 7375060

Please sign in to comment.