Skip to content

Commit

Permalink
rename lookup to find
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 1, 2023
1 parent 656b884 commit a93b47f
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 69 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
rename lookup to find

# articles

[docs] 反应网编程 -- 加入到 演算场 的连接
Expand Down
6 changes: 3 additions & 3 deletions src/fetcher/Fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export class Fetcher {
})
}

lookupHandler(url: URL): FetcherHandler | undefined {
findHandler(url: URL): FetcherHandler | undefined {
const scheme = url.protocol.slice(0, url.protocol.length - 1)
return this.handlers[scheme]
}

formatURL(url: URL): string {
const handler = this.lookupHandler(url)
const handler = this.findHandler(url)
if (handler === undefined) {
throw new Error(
[
Expand All @@ -42,7 +42,7 @@ export class Fetcher {
}

async fetchText(url: URL): Promise<string> {
const handler = this.lookupHandler(url)
const handler = this.findHandler(url)
if (handler === undefined) {
throw new Error(
[
Expand Down
6 changes: 3 additions & 3 deletions src/lang/check/checkRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { compose } from "../compose/compose"
import { createEnv } from "../env/createEnv"
import { refreshNode } from "../freshen/refreshNode"
import { Mod } from "../mod"
import { lookupDefinitionOrFail } from "../mod/lookupDefinitionOrFail"
import { findDefinitionOrFail } from "../mod/findDefinitionOrFail"
import { createNodeFromDefinition } from "../node/createNodeFromDefinition"
import { Word } from "../word"
import { checkAllLocalsAreUsed } from "./checkAllLocalsAreUsed"
Expand All @@ -21,14 +21,14 @@ export function checkRule(

const first = createNodeFromDefinition(
env.net,
lookupDefinitionOrFail(mod, firstName),
findDefinitionOrFail(mod, firstName),
)

refreshNode(env.net, checking.typeVarCounters, first)

const second = createNodeFromDefinition(
env.net,
lookupDefinitionOrFail(mod, secondName),
findDefinitionOrFail(mod, secondName),
)

refreshNode(env.net, checking.typeVarCounters, second)
Expand Down
6 changes: 3 additions & 3 deletions src/lang/check/checkRuleIsAboutOwnNode.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Mod } from "../mod"
import { lookupDefinitionOrFail } from "../mod/lookupDefinitionOrFail"
import { findDefinitionOrFail } from "../mod/findDefinitionOrFail"

export function checkRuleIsAboutOwnNode(
mod: Mod,
firstName: string,
secondName: string,
): void {
const first = lookupDefinitionOrFail(mod, firstName)
const second = lookupDefinitionOrFail(mod, secondName)
const first = findDefinitionOrFail(mod, firstName)
const second = findDefinitionOrFail(mod, secondName)

const fetcher = mod.loader.fetcher

Expand Down
6 changes: 3 additions & 3 deletions src/lang/check/checkRuleNodeOrder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Mod } from "../mod"
import { lookupDefinitionOrFail } from "../mod/lookupDefinitionOrFail"
import { findDefinitionOrFail } from "../mod/findDefinitionOrFail"

export function checkRuleNodeOrder(
mod: Mod,
firstName: string,
secondName: string,
): void {
const first = lookupDefinitionOrFail(mod, firstName)
const first = findDefinitionOrFail(mod, firstName)

if (first["@kind"] !== "NodeDefinition") {
throw new Error(
Expand All @@ -30,7 +30,7 @@ export function checkRuleNodeOrder(
)
}

const second = lookupDefinitionOrFail(mod, secondName)
const second = findDefinitionOrFail(mod, secondName)

if (second["@kind"] !== "NodeDefinition") {
throw new Error(
Expand Down
6 changes: 3 additions & 3 deletions src/lang/compose/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { connect } from "../connect/connect"
import { Env } from "../env"
import { appendReport } from "../errors/appendReport"
import { Mod } from "../mod"
import { lookupDefinitionOrFail } from "../mod/lookupDefinitionOrFail"
import { findDefinitionOrFail } from "../mod/findDefinitionOrFail"
import { disconnectPort } from "../net/disconnectPort"
import { findPortEntry } from "../net/findPortEntry"
import { Node } from "../node"
Expand Down Expand Up @@ -35,7 +35,7 @@ export function compose(
env.locals.delete(word.name)
return
} else {
const definition = lookupDefinitionOrFail(mod, word.name)
const definition = findDefinitionOrFail(mod, word.name)
composeDefinition(env, definition, options)
return
}
Expand Down Expand Up @@ -156,7 +156,7 @@ export function compose(
}

case "NodeRearrange": {
const definition = lookupDefinitionOrFail(mod, word.name)
const definition = findDefinitionOrFail(mod, word.name)
composeNode(
env,
createNodeFromDefinition(env.net, definition),
Expand Down
4 changes: 2 additions & 2 deletions src/lang/interact/interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Checking } from "../checking"
import { compose } from "../compose/compose"
import { Edge } from "../edge"
import { Env } from "../env"
import { lookupRuleByPorts } from "../mod/lookupRuleByPorts"
import { findRuleByPorts } from "../mod/findRuleByPorts"
import { deleteNodeEntry } from "../net/deleteNodeEntry"

export type InteractOptions = {
Expand All @@ -14,7 +14,7 @@ export function interact(
activeEdge: Edge,
options: InteractOptions,
): void {
const rule = lookupRuleByPorts(env.mod, activeEdge.first, activeEdge.second)
const rule = findRuleByPorts(env.mod, activeEdge.first, activeEdge.second)
if (rule === undefined) return

for (const word of rule.words) {
Expand Down
6 changes: 3 additions & 3 deletions src/lang/mod/defineRule.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { nodeKeyWithoutId } from "../node/nodeKeyWithoutId"
import { Word } from "../word"
import { Mod } from "./Mod"
import { lookupDefinitionOrFail } from "./lookupDefinitionOrFail"
import { findDefinitionOrFail } from "./findDefinitionOrFail"

export function defineRule(
mod: Mod,
firstName: string,
secondName: string,
words: Array<Word>,
): void {
const firstDefinition = lookupDefinitionOrFail(mod, firstName)
const secondDefinition = lookupDefinitionOrFail(mod, secondName)
const firstDefinition = findDefinitionOrFail(mod, firstName)
const secondDefinition = findDefinitionOrFail(mod, secondName)

const firstKey = nodeKeyWithoutId({
url: firstDefinition.mod.url,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Definition } from "../definition"
import { Mod } from "./Mod"

export function lookupDefinition(
mod: Mod,
name: string,
): Definition | undefined {
export function findDefinition(mod: Mod, name: string): Definition | undefined {
return mod.definitions.get(name)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Definition } from "../definition"
import { Mod } from "./Mod"

export function lookupDefinitionOrFail(mod: Mod, name: string): Definition {
export function findDefinitionOrFail(mod: Mod, name: string): Definition {
const definition = mod.definitions.get(name)
if (definition === undefined) {
throw new Error(
[
`[lookupDefinitionOrFail] I meet undefined name.`,
`[findDefinitionOrFail] I meet undefined name.`,
``,
` name: ${name}`,
].join("\n"),
Expand Down
17 changes: 17 additions & 0 deletions src/lang/mod/findRuleByName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Rule } from "../rule"
import { Mod } from "./Mod"
import { findDefinitionOrFail } from "./findDefinitionOrFail"
import { findRuleByNodes } from "./findRuleByNodes"

export function findRuleByName(mod: Mod, ruleName: string): Rule | undefined {
const [firstName, secondName] = ruleName.split(" ")

const firstDefinition = findDefinitionOrFail(mod, firstName)
const secondDefinition = findDefinitionOrFail(mod, secondName)

return findRuleByNodes(
mod,
{ url: firstDefinition.mod.url, name: firstDefinition.name },
{ url: secondDefinition.mod.url, name: secondDefinition.name },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { nodeKeyWithoutId } from "../node/nodeKeyWithoutId"
import { Rule } from "../rule"
import { Mod } from "./Mod"

export function lookupRuleByNodes(
export function findRuleByNodes(
mod: Mod,
firstNode: NodeWithoutId,
secondNode: NodeWithoutId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Port } from "../port"
import { Rule } from "../rule"
import { Mod } from "./Mod"
import { lookupRuleByNodes } from "./lookupRuleByNodes"
import { findRuleByNodes } from "./findRuleByNodes"

export function lookupRuleByPorts(
export function findRuleByPorts(
mod: Mod,
first: Port,
second: Port,
): Rule | undefined {
if (first.isPrincipal && second.isPrincipal) {
return lookupRuleByNodes(mod, first.node, second.node)
return findRuleByNodes(mod, first.node, second.node)
}
}
4 changes: 2 additions & 2 deletions src/lang/mod/hasNodeDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NodeWithoutId } from "../node"
import { Mod } from "./Mod"
import { lookupDefinition } from "./lookupDefinition"
import { findDefinition } from "./findDefinition"

export function hasNodeDefinition(mod: Mod, node: NodeWithoutId): boolean {
const definition = lookupDefinition(mod, node.name)
const definition = findDefinition(mod, node.name)
if (definition === undefined) {
return false
}
Expand Down
10 changes: 5 additions & 5 deletions src/lang/mod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ export * from "./RuleEntry"
export * from "./createMod"
export * from "./define"
export * from "./defineRule"
export * from "./findDefinition"
export * from "./findDefinitionOrFail"
export * from "./findNodeRuleEntries"
export * from "./lookupDefinition"
export * from "./lookupDefinitionOrFail"
export * from "./lookupRuleByName"
export * from "./lookupRuleByNodes"
export * from "./lookupRuleByPorts"
export * from "./findRuleByName"
export * from "./findRuleByNodes"
export * from "./findRuleByPorts"
17 changes: 0 additions & 17 deletions src/lang/mod/lookupRuleByName.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lang/present/presentNodeAsNet.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { capNodeAllPorts } from "../cap"
import { Mod, lookupDefinitionOrFail } from "../mod"
import { Mod, findDefinitionOrFail } from "../mod"
import { Net, createNet } from "../net"
import { createNodeFromDefinition } from "../node/createNodeFromDefinition"

export function presentNodeAsNet(mod: Mod, nodeName: string): Net {
const net = createNet()

const definition = lookupDefinitionOrFail(mod, nodeName)
const definition = findDefinitionOrFail(mod, nodeName)
const node = createNodeFromDefinition(net, definition)
capNodeAllPorts(mod, net, node)

Expand Down
10 changes: 5 additions & 5 deletions src/lang/present/presentRuleAsNets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { capNodeNonPrinciplePorts } from "../cap"
import { compose } from "../compose/compose"
import { connect } from "../connect/connect"
import { createEnv } from "../env/createEnv"
import { Mod, lookupDefinitionOrFail } from "../mod"
import { lookupRuleByName } from "../mod/lookupRuleByName"
import { Mod, findDefinitionOrFail } from "../mod"
import { findRuleByName } from "../mod/findRuleByName"
import { Net, copyConnectedComponent, createNet } from "../net"
import { deleteNodeEntry } from "../net/deleteNodeEntry"
import { findPrincipalPort } from "../net/findPrincipalPort"
Expand All @@ -13,7 +13,7 @@ import { createNodeFromDefinition } from "../node/createNodeFromDefinition"
export function presentRuleAsNets(mod: Mod, ruleName: string): [Net, Net] {
const env = createEnv(mod)

const rule = lookupRuleByName(mod, ruleName)
const rule = findRuleByName(mod, ruleName)
if (rule === undefined) {
throw new Error(
[
Expand All @@ -28,12 +28,12 @@ export function presentRuleAsNets(mod: Mod, ruleName: string): [Net, Net] {

const first = createNodeFromDefinition(
env.net,
lookupDefinitionOrFail(mod, firstName),
findDefinitionOrFail(mod, firstName),
)

const second = createNodeFromDefinition(
env.net,
lookupDefinitionOrFail(mod, secondName),
findDefinitionOrFail(mod, secondName),
)

capNodeNonPrinciplePorts(mod, env.net, first)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/present/presentWordAsEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { collectWords } from "../compose/collectWords"
import { compose } from "../compose/compose"
import { Env } from "../env"
import { createEnv } from "../env/createEnv"
import { Mod, lookupDefinitionOrFail } from "../mod"
import { Mod, findDefinitionOrFail } from "../mod"
import { formatWord } from "../word"

export function presentWordAsEnv(mod: Mod, name: string): Env {
const definition = lookupDefinitionOrFail(mod, name)
const definition = findDefinitionOrFail(mod, name)

if (definition["@kind"] !== "WordDefinition") {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions src/lang/stmts/Claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { appendReport } from "../errors/appendReport"
import { createReport } from "../errors/createReport"
import { Mod } from "../mod"
import { define } from "../mod/define"
import { lookupDefinition } from "../mod/lookupDefinition"
import { findDefinition } from "../mod/findDefinition"
import { Span } from "../span"
import { Stmt } from "../stmt"
import { Word } from "../word"
Expand All @@ -18,7 +18,7 @@ export class Claim implements Stmt {

async execute(mod: Mod): Promise<void> {
try {
const definition = lookupDefinition(mod, this.name)
const definition = findDefinition(mod, this.name)

if (definition !== undefined) {
const definitionSpan = definitionMaybeSpan(definition)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/stmts/Define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { definitionMaybeSpan } from "../definition/definitionMaybeSpan"
import { appendReport } from "../errors/appendReport"
import { createReport } from "../errors/createReport"
import { Mod } from "../mod"
import { lookupDefinitionOrFail } from "../mod/lookupDefinitionOrFail"
import { findDefinitionOrFail } from "../mod/findDefinitionOrFail"
import { Span } from "../span"
import { Stmt } from "../stmt"
import { Word } from "../word"
Expand All @@ -17,7 +17,7 @@ export class Define implements Stmt {

async execute(mod: Mod): Promise<void> {
try {
const definition = lookupDefinitionOrFail(mod, this.name)
const definition = findDefinitionOrFail(mod, this.name)
if (definition["@kind"] !== "WordDefinition") {
throw new Error(
[
Expand Down

0 comments on commit a93b47f

Please sign in to comment.