diff --git a/TODO.md b/TODO.md index 58b23257..17199d52 100644 --- a/TODO.md +++ b/TODO.md @@ -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` diff --git a/src/lang/definitions/NodeDefinition.ts b/src/lang/definitions/NodeDefinition.ts index ffd1baf6..192e64cd 100644 --- a/src/lang/definitions/NodeDefinition.ts +++ b/src/lang/definitions/NodeDefinition.ts @@ -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 = new Map() - constructor( public mod: Mod, public kind: NodeKind, @@ -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) } diff --git a/src/lang/mod/Mod.ts b/src/lang/mod/Mod.ts index ecc3a539..3274bbd4 100644 --- a/src/lang/mod/Mod.ts +++ b/src/lang/mod/Mod.ts @@ -1,6 +1,8 @@ import { Definition } from "../definition" +import { Rule } from "../rule" export type Mod = { url: URL definitions: Map + rules: Map } diff --git a/src/lang/mod/createMod.ts b/src/lang/mod/createMod.ts index 616da9cc..28b58ab2 100644 --- a/src/lang/mod/createMod.ts +++ b/src/lang/mod/createMod.ts @@ -4,8 +4,9 @@ import { defineBuiltInOperators } from "./defineBuiltInOperators" export function createMod(url: URL): Mod { const definitions: Map = new Map() + const rules = new Map() - const mod = { url, definitions } + const mod = { url, definitions, rules } defineBuiltInOperators(mod) diff --git a/src/lang/mod/defineRule.ts b/src/lang/mod/defineRule.ts new file mode 100644 index 00000000..f5a9ab5c --- /dev/null +++ b/src/lang/mod/defineRule.ts @@ -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, +): void { + mod.rules.set( + `${start} ${end}`, + new Rule( + mod, + modLookupNodeDefinitionOrFail(mod, start), + modLookupNodeDefinitionOrFail(mod, end), + words, + ), + ) +} diff --git a/src/lang/mod/modLookupRule.ts b/src/lang/mod/modLookupRule.ts new file mode 100644 index 00000000..10564f74 --- /dev/null +++ b/src/lang/mod/modLookupRule.ts @@ -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}`) +} diff --git a/src/lang/mod/modLookupRuleByPorts.ts b/src/lang/mod/modLookupRuleByPorts.ts index 2e8eba36..f12035e0 100644 --- a/src/lang/mod/modLookupRuleByPorts.ts +++ b/src/lang/mod/modLookupRuleByPorts.ts @@ -1,6 +1,7 @@ import { Port } from "../graph" import { Rule } from "../rule" import { Mod } from "./Mod" +import { modLookupRule } from "./modLookupRule" export function modLookupRuleByPorts( mod: Mod, @@ -8,6 +9,6 @@ export function modLookupRuleByPorts( 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) } } diff --git a/src/lang/stmts/Defrule.ts b/src/lang/stmts/Defrule.ts index bf01e68e..cdcf61db 100644 --- a/src/lang/stmts/Defrule.ts +++ b/src/lang/stmts/Defrule.ts @@ -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" @@ -14,12 +13,6 @@ export class Defrule implements Stmt { ) {} async execute(mod: Mod): Promise { - 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) } }