Skip to content

Commit

Permalink
[mod] extract all methods of Mod
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 29, 2023
1 parent 5f3ec5b commit ff14aee
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 47 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[mod] extract all methods of `Mod`

[mod] `createMod` -- `Mod` should be a type -- instead of a class

`NodeDefinition` should not have `rules` -- use `RuleDefinition`
Expand Down
3 changes: 2 additions & 1 deletion src/lang/graph/netConnect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Action, Net, Port, createEdge } from "../graph"
import { modLookupRuleByPorts } from "../mod/modLookupRuleByPorts"

export function netConnect(net: Net, start: Port, end: Port): void {
const rule = net.mod.lookupRuleByPorts(start, end)
const rule = modLookupRuleByPorts(net.mod, start, end)

if (rule) {
net.actions.push(new Action(start, end, rule))
Expand Down
40 changes: 0 additions & 40 deletions src/lang/mod/Mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { Definition } from "../definition"
import * as Definitions from "../definitions"
import { Port } from "../graph"
import { Rule } from "../rule"
import { defineBuiltInOperators } from "./defineBuiltInOperators"

export class Mod {
Expand All @@ -10,41 +7,4 @@ export class Mod {
constructor(public url: URL) {
defineBuiltInOperators(this)
}

lookupDefinitionOrFail(name: string): Definition {
const definition = this.definitions.get(name)
if (definition === undefined) {
throw new Error(`Undefined name: ${name}`)
}

return definition
}

lookupNodeDefinitionOrFail(name: string): Definitions.NodeDefinition {
const definition = this.lookupDefinitionOrFail(name)
if (!(definition instanceof Definitions.NodeDefinition)) {
throw new Error(
`I expect a node definition, but ${name} is ${definition.constructor.name}`,
)
}

return definition
}

lookupNetDefinitionOrFail(name: string): Definitions.NetDefinition {
const definition = this.lookupDefinitionOrFail(name)
if (!(definition instanceof Definitions.NetDefinition)) {
throw new Error(
`I expect a net definition, but ${name} is ${definition.constructor.name}`,
)
}

return definition
}

lookupRuleByPorts(start: Port, end: Port): Rule | undefined {
if (start.isPrincipal && end.isPrincipal) {
return start.node.definition.lookupRule(end.node.definition)
}
}
}
3 changes: 2 additions & 1 deletion src/lang/mod/modBuildNet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Net } from "../graph"
import { createNet } from "../graph/createNet"
import { netCleanUpWires } from "../graph/netCleanUpWires"
import { Mod } from "./Mod"
import { modLookupNetDefinitionOrFail } from "./modLookupNetDefinitionOrFail"

export function modBuildNet(mod: Mod, name: string): Net {
const net = createNet(mod)
mod.lookupNetDefinitionOrFail(name).meaning(net)
modLookupNetDefinitionOrFail(mod, name).meaning(net)
netCleanUpWires(net)
return net
}
11 changes: 11 additions & 0 deletions src/lang/mod/modLookupDefinitionOrFail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Definition } from "../definition"
import { Mod } from "./Mod"

export function modLookupDefinitionOrFail(mod: Mod, name: string): Definition {
const definition = mod.definitions.get(name)
if (definition === undefined) {
throw new Error(`Undefined name: ${name}`)
}

return definition
}
17 changes: 17 additions & 0 deletions src/lang/mod/modLookupNetDefinitionOrFail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Definitions from "../definitions"
import { Mod } from "./Mod"
import { modLookupDefinitionOrFail } from "./modLookupDefinitionOrFail"

export function modLookupNetDefinitionOrFail(
mod: Mod,
name: string,
): Definitions.NetDefinition {
const definition = modLookupDefinitionOrFail(mod, name)
if (!(definition instanceof Definitions.NetDefinition)) {
throw new Error(
`I expect a net definition, but ${name} is ${definition.constructor.name}`,
)
}

return definition
}
17 changes: 17 additions & 0 deletions src/lang/mod/modLookupNodeDefinitionOrFail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Definitions from "../definitions"
import { Mod } from "./Mod"
import { modLookupDefinitionOrFail } from "./modLookupDefinitionOrFail"

export function modLookupNodeDefinitionOrFail(
mod: Mod,
name: string,
): Definitions.NodeDefinition {
const definition = modLookupDefinitionOrFail(mod, name)
if (!(definition instanceof Definitions.NodeDefinition)) {
throw new Error(
`I expect a node definition, but ${name} is ${definition.constructor.name}`,
)
}

return definition
}
13 changes: 13 additions & 0 deletions src/lang/mod/modLookupRuleByPorts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Port } from "../graph"
import { Rule } from "../rule"
import { Mod } from "./Mod"

export function modLookupRuleByPorts(
mod: Mod,
start: Port,
end: Port,
): Rule | undefined {
if (start.isPrincipal && end.isPrincipal) {
return start.node.definition.lookupRule(end.node.definition)
}
}
5 changes: 3 additions & 2 deletions src/lang/stmts/DefruleStmt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Mod } from "../mod"
import { modLookupNodeDefinitionOrFail } from "../mod/modLookupNodeDefinitionOrFail"
import { Rule } from "../rule"
import { Span } from "../span"
import { Stmt } from "../stmt"
Expand All @@ -15,8 +16,8 @@ export class DefruleStmt extends Stmt {
}

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

startNodeDefinition.defineRule(
endNodeDefinition,
Expand Down
3 changes: 2 additions & 1 deletion src/lang/words/Call.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Net } from "../graph"
import { Mod } from "../mod"
import { modLookupDefinitionOrFail } from "../mod/modLookupDefinitionOrFail"
import { Span } from "../span"
import { Word } from "../word"

Expand All @@ -17,7 +18,7 @@ export class Call extends Word {
net.portStore.delete(this.name)
net.portStack.push(found)
} else {
mod.lookupDefinitionOrFail(this.name).meaning(net)
modLookupDefinitionOrFail(mod, this.name).meaning(net)
}
}
}

0 comments on commit ff14aee

Please sign in to comment.