Skip to content

Commit

Permalink
[syntax] named port
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 29, 2023
1 parent 05cbd52 commit 268df80
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 34 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# named port

[syntax] named port

`Node` with named port

`defnode` instead of `defcons` and `defelim`
Expand Down
2 changes: 1 addition & 1 deletion docs/notes/design-of-new-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Use `defnode` to define a node.

`--` separates input ports from output ports in the definition.

Use `!` as postfix to mark the principle port.
Use `!` as postfix to mark the principal port.

```inet
defnode zero -- return: Nat! end
Expand Down
13 changes: 7 additions & 6 deletions docs/tests/DiffList.inet
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
defcons sole 0 1 end
defcons sole -- return! end

defcons null 0 1 end
defcons cons 2 1 end
defcons null -- return! end

defcons diff 2 1 end
defcons cons head tail -- return! end

defelim diff_append 2 1 end
defcons diff left right -- return! end

defelim diff_append left! right -- return end

defru diff diff_append
(let start) (let fin) (let diff_list)
fin diff_list diff_open start diff
end

defelim diff_open 2 1 end
defelim diff_open diff_list! list -- return end

defru diff diff_open
(let start) (let fin) (let list)
Expand Down
8 changes: 4 additions & 4 deletions docs/tests/List.inet
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defcons sole 0 1 end
defcons sole -- return! end

defcons null 0 1 end
defcons null -- return! end

defcons cons 2 1 end
defcons cons head tail -- return! end

defelim append 2 1 end
defelim append left! right -- return end

defru null append end

Expand Down
6 changes: 3 additions & 3 deletions docs/tests/Nat.inet
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defcons zero 0 1 end
defcons zero -- return! end

defcons add1 1 1 end
defcons add1 prev -- return! end

defelim add 2 1 end
defelim add x! y -- return end

defru zero add end

Expand Down
13 changes: 9 additions & 4 deletions src/lang/stmts/Defcons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import { Span } from "../span"
import { Stmt } from "../stmt"
import { createTrivialTypes } from "../type"

export type PortExp = {
name: string
isPrincipal: boolean
}

export class Defcons implements Stmt {
constructor(
public name: string,
public inputArity: number,
public outputArity: number,
public input: Array<PortExp>,
public output: Array<PortExp>,
public span: Span,
) {}

Expand All @@ -21,8 +26,8 @@ export class Defcons implements Stmt {
mod,
"Cons",
this.name,
createTrivialTypes(this.inputArity),
createTrivialTypes(this.outputArity),
createTrivialTypes(this.input.length),
createTrivialTypes(this.output.length),
),
)
}
Expand Down
9 changes: 5 additions & 4 deletions src/lang/stmts/Defelim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { define } from "../mod/define"
import { Span } from "../span"
import { Stmt } from "../stmt"
import { createTrivialTypes } from "../type"
import { PortExp } from "./Defcons"

export class Defelim implements Stmt {
constructor(
public name: string,
public inputArity: number,
public outputArity: number,
public input: Array<PortExp>,
public output: Array<PortExp>,
public span: Span,
) {}

Expand All @@ -21,8 +22,8 @@ export class Defelim implements Stmt {
mod,
"Elim",
this.name,
createTrivialTypes(this.inputArity),
createTrivialTypes(this.outputArity),
createTrivialTypes(this.input.length),
createTrivialTypes(this.output.length),
),
)
}
Expand Down
1 change: 1 addition & 0 deletions src/lang/syntax/grammars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export const optional = pt.grammars.optional
export const dashline = pt.grammars.dashline

export * from "./name"
export * from "./port"
export * from "./stmt"
export * from "./word"
12 changes: 12 additions & 0 deletions src/lang/syntax/grammars/port.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const port = {
$grammar: {
"port:normal": [{ name: "variable_name" }],
"port:principal": [{ name: "variable_name" }, '"!"'],
},
}

export const ports = {
$grammar: {
"ports:ports": [{ ports: { $ap: ["zero_or_more", "port"] } }],
},
}
10 changes: 6 additions & 4 deletions src/lang/syntax/grammars/stmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ export const stmt = {
"stmt:defcons": [
'"defcons"',
{ name: "variable_name" },
{ inputArity: { $pattern: ["number"] } },
{ outputArity: { $pattern: ["number"] } },
{ input: "ports" },
"dashline",
{ output: "ports" },
'"end"',
],
"stmt:defelim": [
'"defelim"',
{ name: "variable_name" },
{ inputArity: { $pattern: ["number"] } },
{ outputArity: { $pattern: ["number"] } },
{ input: "ports" },
"dashline",
{ output: "ports" },
'"end"',
],
"stmt:defru": [
Expand Down
1 change: 1 addition & 0 deletions src/lang/syntax/matchers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./name_matcher"
export * from "./port_matcher"
export * from "./stmt_matcher"
export * from "./word_matcher"
22 changes: 22 additions & 0 deletions src/lang/syntax/matchers/port_matcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as pt from "@cicada-lang/partech"
import { PortExp } from "../../stmts"

export function port_matcher(tree: pt.Tree): PortExp {
return pt.matcher<PortExp>({
"port:normal": ({ name }, { span }) => ({
name: pt.str(name),
isPrincipal: false,
}),
"port:principal": ({ name }, { span }) => ({
name: pt.str(name),
isPrincipal: true,
}),
})(tree)
}

export function ports_matcher(tree: pt.Tree): Array<PortExp> {
return pt.matcher({
"ports:ports": ({ ports }) =>
pt.matchers.zero_or_more_matcher(ports).map(port_matcher),
})(tree)
}
12 changes: 6 additions & 6 deletions src/lang/syntax/matchers/stmt_matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import * as matchers from "../matchers"

export function stmt_matcher(tree: pt.Tree): Stmt {
return pt.matcher<Stmt>({
"stmt:defcons": ({ name, inputArity, outputArity }, { span }) =>
"stmt:defcons": ({ name, input, output }, { span }) =>
new Stmts.Defcons(
pt.str(name),
Number(pt.str(inputArity)),
Number(pt.str(outputArity)),
matchers.ports_matcher(input),
matchers.ports_matcher(output),
span,
),
"stmt:defelim": ({ name, inputArity, outputArity }, { span }) =>
"stmt:defelim": ({ name, input, output }, { span }) =>
new Stmts.Defelim(
pt.str(name),
Number(pt.str(inputArity)),
Number(pt.str(outputArity)),
matchers.ports_matcher(input),
matchers.ports_matcher(output),
span,
),
"stmt:defru": ({ start, end, words }, { span }) =>
Expand Down

0 comments on commit 268df80

Please sign in to comment.