Skip to content

Commit

Permalink
RuleEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 29, 2023
1 parent 866b7d2 commit 1d70342
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./fetcher"
export * from "./lang/definition"
export * from "./lang/errors"
export * from "./lang/mod"
export * from "./lang/net"
Expand Down
4 changes: 2 additions & 2 deletions src/lang/mod/Mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Loader } from "../../loader"
import { Checking } from "../checking"
import { Definition } from "../definition"
import { Env } from "../env/Env.js"
import { Rule } from "../rule"
import { Stmt } from "../stmt/Stmt.js"
import { RuleEntry } from "./RuleEntry"

export type Mod = {
loader: Loader
Expand All @@ -13,6 +13,6 @@ export type Mod = {
text: string
stmts: Array<Stmt>
definitions: Map<string, Definition>
rules: Map<string, Rule>
ruleEntries: Map<string, RuleEntry>
requiredMods: Map<string, Mod>
}
8 changes: 8 additions & 0 deletions src/lang/mod/RuleEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Word } from "../word"
import { Mod } from "./Mod"

export type RuleEntry = {
name: string
mod: Mod
words: Array<Word>
}
2 changes: 1 addition & 1 deletion src/lang/mod/createMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function createMod(options: {
text: options.text,
stmts: options.stmts,
definitions: new Map(),
rules: new Map(),
ruleEntries: new Map(),
requiredMods: new Map(),
} as Mod

Expand Down
3 changes: 2 additions & 1 deletion src/lang/mod/defineRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function defineRule(
})

const key = `${firstKey} ${secondKey}`
const name = `${firstName} ${secondName}`

mod.rules.set(key, { mod, words })
mod.ruleEntries.set(key, { name, mod, words })
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { NodeWithoutId } from "../node"
import { nodeKeyWithoutId } from "../node/nodeKeyWithoutId"
import { Rule } from "../rule"
import { Mod } from "./Mod"
import { RuleEntry } from "./RuleEntry"

export function findNodeRules(mod: Mod, node: NodeWithoutId): Array<Rule> {
export function findNodeRuleEntries(
mod: Mod,
node: NodeWithoutId,
): Array<RuleEntry> {
const nodeKey = nodeKeyWithoutId(node)
const rules = []
for (const [key, rule] of mod.rules) {
const entries = []
for (const [key, entry] of mod.ruleEntries) {
const [firstKey, secondKey] = key.split(" ")
if (firstKey === nodeKey || secondKey === nodeKey) {
rules.push(rule)
entries.push(entry)
}
}

return rules
return entries
}
2 changes: 1 addition & 1 deletion src/lang/mod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * from "./Mod"
export * from "./createMod"
export * from "./define"
export * from "./defineRule"
export * from "./findNodeRules"
export * from "./findNodeRuleEntries"
export * from "./lookupDefinition"
export * from "./lookupDefinitionOrFail"
export * from "./lookupRule"
Expand Down
2 changes: 1 addition & 1 deletion src/lang/mod/lookupRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export function lookupRule(
? mod
: mod.loader.loaded.get(secondNode.url.href)

return firstMod?.rules.get(key) || secondMod?.rules.get(key)
return firstMod?.ruleEntries.get(key) || secondMod?.ruleEntries.get(key)
}

0 comments on commit 1d70342

Please sign in to comment.