Skip to content

Commit

Permalink
rename Exp to Word
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 28, 2023
1 parent 1b7d287 commit f9a74c8
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 42 deletions.
11 changes: 9 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
rename `Exp` to `Word`

extract `netRemoveNode`

extract `netRemoveEdge`
Expand All @@ -10,6 +8,15 @@ extract `netCleanUpWires`
extract `netStep`
extract `netRun`

quit using `def/` and `defs/`

- `Mod` should have namespaces for
- types
- nodes
- rules
- nets
- operators

rule should be part of `Mod` not `Node`

quit using `Action`
Expand Down
8 changes: 4 additions & 4 deletions src/lang/defs/NetDef.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Def } from "../def"
import { Exp } from "../exp"
import { Net } from "../graph"
import { Mod } from "../mod"
import { Word } from "../word"

export class NetDef extends Def {
constructor(
public mod: Mod,
public name: string,
public exps: Array<Exp>,
public words: Array<Word>,
) {
super()
}

refer(net: Net): void {
for (const exp of this.exps) {
exp.apply(this.mod, net)
for (const word of this.words) {
word.apply(this.mod, net)
}
}
}
1 change: 0 additions & 1 deletion src/lang/exp/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lang/graph/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class Action {
net.portStack.push(...input)

// NOTE Reconnect by rule.
for (const exp of this.rule.exps) {
exp.apply(mod, net)
for (const word of this.rule.words) {
word.apply(mod, net)
}

reconnectOutput(net, output)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/rule/Rule.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as Defs from "../defs"
import { Exp } from "../exp"
import { Mod } from "../mod"
import { Word } from "../word"

export class Rule {
constructor(
public mod: Mod,
public start: Defs.NodeDef,
public end: Defs.NodeDef,
public exps: Array<Exp>,
public words: Array<Word>,
) {}
}
6 changes: 3 additions & 3 deletions src/lang/stmts/DefnetStmt.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as Defs from "../defs"
import { Exp } from "../exp"
import { Mod } from "../mod"
import { Span } from "../span"
import { Stmt } from "../stmt"
import { Word } from "../word"

export class DefnetStmt extends Stmt {
constructor(
public name: string,
public exps: Array<Exp>,
public words: Array<Word>,
public span: Span,
) {
super()
}

async execute(mod: Mod): Promise<void> {
mod.define(this.name, new Defs.NetDef(mod, this.name, this.exps))
mod.define(this.name, new Defs.NetDef(mod, this.name, this.words))
}
}
6 changes: 3 additions & 3 deletions src/lang/stmts/DefruleStmt.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Exp } from "../exp"
import { Mod } from "../mod"
import { Rule } from "../rule"
import { Span } from "../span"
import { Stmt } from "../stmt"
import { Word } from "../word"

export class DefruleStmt extends Stmt {
constructor(
public start: string,
public end: string,
public exps: Array<Exp>,
public words: Array<Word>,
public span: Span,
) {
super()
Expand All @@ -20,7 +20,7 @@ export class DefruleStmt extends Stmt {

startNodeDef.defineRule(
endNodeDef,
new Rule(mod, startNodeDef, endNodeDef, this.exps),
new Rule(mod, startNodeDef, endNodeDef, this.words),
)
}
}
22 changes: 11 additions & 11 deletions src/lang/syntax/matchStmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
} from "@cicada-lang/sexp/lib/match"
import { list, v } from "@cicada-lang/sexp/lib/pattern-exp"
import { Sexp } from "@cicada-lang/sexp/lib/sexp"
import { Exp } from "../exp"
import * as Exps from "../exps"
import { Stmt } from "../stmt"
import * as Stmts from "../stmts"
import { Word } from "../word"
import * as Exps from "../words"

export function matchStmt(sexp: Sexp): Stmt {
return match<Stmt>(sexp, [
Expand All @@ -34,29 +34,29 @@ export function matchStmt(sexp: Sexp): Stmt {
),
],
[
list(["defnet", v("name")], v("exps")),
({ name, input, output, exps }) =>
new Stmts.DefnetStmt(matchSymbol(name), matchExps(exps), sexp.span),
list(["defnet", v("name")], v("words")),
({ name, input, output, words }) =>
new Stmts.DefnetStmt(matchSymbol(name), matchExps(words), sexp.span),
],
[
list(["defrule", [v("start"), v("end")]], v("exps")),
({ start, end, exps }) =>
list(["defrule", [v("start"), v("end")]], v("words")),
({ start, end, words }) =>
new Stmts.DefruleStmt(
matchSymbol(start),
matchSymbol(end),
matchExps(exps),
matchExps(words),
sexp.span,
),
],
])
}

function matchExps(sexp: Sexp): Array<Exp> {
function matchExps(sexp: Sexp): Array<Word> {
return matchList(sexp, matchExp)
}

function matchExp(sexp: Sexp): Exp {
return match<Exp>(sexp, [
function matchExp(sexp: Sexp): Word {
return match<Word>(sexp, [
[
list(["let"], v("names")),
({ names }) => new Exps.Let(matchList(names, matchSymbol), sexp.span),
Expand Down
14 changes: 7 additions & 7 deletions src/lang/types/TypeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import * as Types from "."
import { Type } from "../type"

export class TypeBuilder {
exps: Array<string>
words: Array<string>

constructor(exps: Array<string>) {
this.exps = exps
constructor(words: Array<string>) {
this.words = words
}

build(): Array<Type> {
const types: Array<Type> = []
for (const exp of this.exps) {
if (exp === "*") {
for (const word of this.words) {
if (word === "*") {
const t = types.pop()
if (t === undefined) {
throw new Error(
[
`Fail to build type,`,
`I expect a type on the stack when applying *`,
` exps: ${this.exps}`,
` words: ${this.words}`,
].join("\n"),
)
} else {
types.push(new Types.PrincipalType(t))
}
} else {
types.push(new Types.AtomType(exp))
types.push(new Types.AtomType(word))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lang/types/buildTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TypeBuilder } from "."
import { Type } from "../type"

export function buildTypes(exps: Array<string>): Array<Type> {
return new TypeBuilder(exps).build()
export function buildTypes(words: Array<string>): Array<Type> {
return new TypeBuilder(words).build()
}
2 changes: 1 addition & 1 deletion src/lang/exp/Exp.ts → src/lang/word/Word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Net } from "../graph"
import { Mod } from "../mod"
import { Span } from "../span"

export abstract class Exp {
export abstract class Word {
abstract span: Span
abstract apply(mod: Mod, net: Net): void
}
1 change: 1 addition & 0 deletions src/lang/word/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Word"
4 changes: 2 additions & 2 deletions src/lang/exps/Call.ts → src/lang/words/Call.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Exp } from "../exp"
import { Net } from "../graph"
import { Mod } from "../mod"
import { Span } from "../span"
import { Word } from "../word"

export class Call extends Exp {
export class Call extends Word {
constructor(
public name: string,
public span: Span,
Expand Down
4 changes: 2 additions & 2 deletions src/lang/exps/Let.ts → src/lang/words/Let.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Exp } from "../exp"
import { Net } from "../graph"
import { Mod } from "../mod"
import { Span } from "../span"
import { Word } from "../word"

export class Let extends Exp {
export class Let extends Word {
constructor(
public names: Array<string>,
public span: Span,
Expand Down
File renamed without changes.

0 comments on commit f9a74c8

Please sign in to comment.