Skip to content

Commit

Permalink
NodeDefinition should not have rules
Browse files Browse the repository at this point in the history
- `Mod` should have `rules`
- `defineRule`
- `modLookupRule`
  • Loading branch information
xieyuheng committed Jul 29, 2023
1 parent 98cb022 commit 5abd727
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 24 deletions.
6 changes: 4 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
`NodeDefinition` should not have `rules` -- use `RuleDefinition`
[mod] drop mod prefix of lookup

`Node` should not have `definition` -- which is used to `getRule`
`NodeDefinition.fullName`

`Node` should not have `definition` -- which is used to `lookupRule`

quit using `Action`

Expand Down
11 changes: 0 additions & 11 deletions src/lang/definitions/NodeDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import { Definition } from "../definition"
import { Net, Node, createNode } from "../graph"
import { netConnect } from "../graph/netConnect"
import { Mod } from "../mod"
import { Rule } from "../rule"
import { Type } from "../type"

export type NodeKind = "Cons" | "Elim"

export class NodeDefinition implements Definition {
private rules: Map<string, Rule> = new Map()

constructor(
public mod: Mod,
public kind: NodeKind,
Expand All @@ -22,14 +19,6 @@ export class NodeDefinition implements Definition {
return this.mod.url.href + "#" + this.name
}

defineRule(end: NodeDefinition, rule: Rule): void {
this.rules.set(end.fullName, rule)
}

lookupRule(end: NodeDefinition): Rule | undefined {
return this.rules.get(end.fullName)
}

build(): Node {
return createNode(this.kind, this, this.input, this.output)
}
Expand Down
2 changes: 2 additions & 0 deletions src/lang/mod/Mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Definition } from "../definition"
import { Rule } from "../rule"

export type Mod = {
url: URL
definitions: Map<string, Definition>
rules: Map<string, Rule>
}
3 changes: 2 additions & 1 deletion src/lang/mod/createMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { defineBuiltInOperators } from "./defineBuiltInOperators"

export function createMod(url: URL): Mod {
const definitions: Map<string, Definition> = new Map()
const rules = new Map()

const mod = { url, definitions }
const mod = { url, definitions, rules }

defineBuiltInOperators(mod)

Expand Down
21 changes: 21 additions & 0 deletions src/lang/mod/defineRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Rule } from "../rule"
import { Word } from "../word"
import { Mod } from "./Mod"
import { modLookupNodeDefinitionOrFail } from "./modLookupNodeDefinitionOrFail"

export function defineRule(
mod: Mod,
start: string,
end: string,
words: Array<Word>,
): void {
mod.rules.set(
`${start} ${end}`,
new Rule(
mod,
modLookupNodeDefinitionOrFail(mod, start),
modLookupNodeDefinitionOrFail(mod, end),
words,
),
)
}
10 changes: 10 additions & 0 deletions src/lang/mod/modLookupRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Rule } from "../rule"
import { Mod } from "./Mod"

export function modLookupRule(
mod: Mod,
start: string,
end: string,
): Rule | undefined {
return mod.rules.get(`${start} ${end}`)
}
3 changes: 2 additions & 1 deletion src/lang/mod/modLookupRuleByPorts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Port } from "../graph"
import { Rule } from "../rule"
import { Mod } from "./Mod"
import { modLookupRule } from "./modLookupRule"

export function modLookupRuleByPorts(
mod: Mod,
start: Port,
end: Port,
): Rule | undefined {
if (start.isPrincipal && end.isPrincipal) {
return start.node.definition.lookupRule(end.node.definition)
return modLookupRule(mod, start.node.name, end.node.name)
}
}
11 changes: 2 additions & 9 deletions src/lang/stmts/Defrule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Mod } from "../mod"
import { modLookupNodeDefinitionOrFail } from "../mod/modLookupNodeDefinitionOrFail"
import { Rule } from "../rule"
import { defineRule } from "../mod/defineRule"
import { Span } from "../span"
import { Stmt } from "../stmt"
import { Word } from "../word"
Expand All @@ -14,12 +13,6 @@ export class Defrule implements Stmt {
) {}

async execute(mod: Mod): Promise<void> {
const startNodeDefinition = modLookupNodeDefinitionOrFail(mod, this.start)
const endNodeDefinition = modLookupNodeDefinitionOrFail(mod, this.end)

startNodeDefinition.defineRule(
endNodeDefinition,
new Rule(mod, startNodeDefinition, endNodeDefinition, this.words),
)
defineRule(mod, this.start, this.end, this.words)
}
}

0 comments on commit 5abd727

Please sign in to comment.