Skip to content

Commit

Permalink
switch to yarn, upgrade eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeliso committed Jun 30, 2024
1 parent 83da3f4 commit 910be87
Show file tree
Hide file tree
Showing 24 changed files with 3,451 additions and 7,833 deletions.
30 changes: 0 additions & 30 deletions .eslintrc.json

This file was deleted.

20 changes: 16 additions & 4 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm ci
- run: npm run lint
- run: npm test
- run: npm run build
- name: Run yarn install
uses: borales/actions-yarn@v4
with:
cmd: install --frozen-lockfile
- name: Run lint
uses: borales/actions-yarn@v4
with:
cmd: run lint
- name: Run build
uses: borales/actions-yarn@v4
with:
cmd: build
- name: Run test
uses: borales/actions-yarn@v4
with:
cmd: test
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ An implementation of a parser, evaluator, printer, and visualizer for the [SKI](
In the checkout directory:

```bash
npm run build
npx ski
yarn install
yarn run build
yarn run ski
```

## Books
Expand Down
8 changes: 4 additions & 4 deletions bin/ski.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function colorizeExpression (expr: SKIExpression): string {
case 'non-terminal': {
return [
'(',
`${colorizeExpression(expr.lft)}`,
`${colorizeExpression(expr.rgt)}`,
colorizeExpression(expr.lft),
colorizeExpression(expr.rgt),
')'
].join('')
}
Expand All @@ -43,7 +43,7 @@ function formatted (expr: SKIExpression): string {
function runTUI (): number {
const term : Terminal = terminalKit.terminal
const seed = hrtime.bigint()
const randomSeed = create(`${seed}`)
const randomSeed = create(seed.toString())
const N = 32
const MAX_ITER = 100

Expand Down Expand Up @@ -83,7 +83,7 @@ function runTUI (): number {
}

if (iterations === MAX_ITER) {
term.red(`stopped evaluating after ${iterations} iterations. \n`)
term.red(`stopped evaluating after ${iterations.toString()} iterations. \n`)
}

break
Expand Down
32 changes: 32 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'
import path from "path"
import url from "url"

const __meta_url = new url.URL(import.meta.url)
const __filename = url.fileURLToPath(__meta_url)
const __dirname = path.dirname(__filename)

export default tseslint.config(
{
files: ["**/*.{ts,mjs}"],
},
{
ignores: [
"**/node_modules/**",
"build/**",
],
},
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
...tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: __dirname,
},
},
},
);
5 changes: 2 additions & 3 deletions lib/conversion/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { B, C } from '../consts/combinators'
import { LambdaVar } from '../lambda/lambda'
import { ConversionError } from './conversionError'

type LambdaAbsMixed = {
interface LambdaAbsMixed {
kind: 'lambda-abs',
name: string,
// eslint-disable-next-line no-use-before-define
body: LambdaMixed
}

Expand Down Expand Up @@ -143,7 +142,7 @@ const free = (needle: string, lm: LambdaMixed): boolean => {
return idx !== undefined
}

const freeVariables = (lm: LambdaMixed): Array<string> => {
const freeVariables = (lm: LambdaMixed): string[] => {
switch (lm.kind) {
case 'lambda-var':
return [lm.name]
Expand Down
2 changes: 1 addition & 1 deletion lib/evaluator/skiEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const stepOnceS: SKIStep<SKIExpression> =
*/
const scanStep = (
expr: SKIExpression,
steppers: Array<SKIStep<SKIExpression>>
steppers: SKIStep<SKIExpression>[]
):
SKIResult<SKIExpression> => {
for (const step of steppers) {
Expand Down
6 changes: 2 additions & 4 deletions lib/lambda/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NonTerminal, nt } from '../nonterminal'
*
* For instance, in the expression "λx:a.y", this is just "y".
*/
export type LambdaVar = {
export interface LambdaVar {
kind: 'lambda-var',
name: string
}
Expand All @@ -16,15 +16,13 @@ export const mkVar = (name: string): LambdaVar => ({
})

// λx.<body>, where x is a name
type UntypedLambdaAbs = {
interface UntypedLambdaAbs {
kind: 'lambda-abs',
name: string,
// eslint-disable-next-line no-use-before-define
body: UntypedLambda
}

export const mkUntypedAbs =
// eslint-disable-next-line no-use-before-define
(name: string, body: UntypedLambda): UntypedLambda => ({
kind: 'lambda-abs',
name,
Expand Down
6 changes: 3 additions & 3 deletions lib/parser/appendable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Appendable {

if (top === undefined) {
throw new Error('insertionSites should not have an undefined value')
} else if (top?.lft === undefined) {
} else if (top.lft === undefined) {
top.lft = newNode
this.insertionSites.push(top)
return
Expand All @@ -50,9 +50,9 @@ export class Appendable {
}

let currentNode : SyntaxExpression = this.syn
const nodeStack : Array<SyntaxExpression> = []
const nodeStack : SyntaxExpression[] = []

while (currentNode !== undefined || nodeStack.length > 0) {
while (nodeStack.length > 0) {
// traverse left, accumulating the spine on a stack
while (currentNode !== undefined) {
if (currentNode.kind === 'non-terminal') {
Expand Down
6 changes: 3 additions & 3 deletions lib/parser/recursiveDescentBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class RecursiveDescentBuffer {
}

peek (): string | null {
return this.buf[this.idx] || null
return this.buf[this.idx] ?? null
}

consume (): void {
Expand All @@ -29,14 +29,14 @@ export class RecursiveDescentBuffer {

matchCh (ch: string): void {
if (this.peek() !== ch) {
throw new ParseError(`Expected ${ch} but found ${this.peek() || 'null'}'`)
throw new ParseError(`Expected ${ch} but found ${this.peek() ?? 'null'}'`)
}

this.consume()
}

remaining (): boolean {
return this.buf[this.idx] !== undefined
return this.idx < this.buf.length
}

parseVariable (): string {
Expand Down
10 changes: 4 additions & 6 deletions lib/parser/untyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ function parseUntypedLambdaInternal (rdb: RecursiveDescentBuffer):
nextTerm = mkVar(singleVar)
}

if (nextTerm !== undefined) {
if (resultExpr === undefined) {
resultExpr = nextTerm
} else {
resultExpr = nt(resultExpr, nextTerm)
}
if (resultExpr === undefined) {
resultExpr = nextTerm
} else {
resultExpr = nt(resultExpr, nextTerm)
}
}
if (resultExpr === undefined) {
Expand Down
1 change: 0 additions & 1 deletion lib/ski/church.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const toLambda = (exp: SKIExpression): any => {
return (x: (_: any) => {(_: any): any; _: any }) =>
(y: (_: any) => any) =>
(z: any) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
x(z)(y(z))
// eslint-disable-next-line @typescript-eslint/no-unused-vars
case SKITerminalSymbol.K: return (x: any) => (_y: any) => x
Expand Down
4 changes: 2 additions & 2 deletions lib/ski/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export function prettyPrint (expr: SKIExpression): string {
case 'non-terminal': {
const printed = [
'(',
`${prettyPrint(expr.lft)}`,
`${prettyPrint(expr.rgt)}`,
prettyPrint(expr.lft),
prettyPrint(expr.rgt),
')'
]

Expand Down
26 changes: 10 additions & 16 deletions lib/ski/packer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SKIExpression } from './expression'
import { nt } from '../nonterminal'
import { term, SKITerminalSymbol } from './terminal'

export type SymbolHeap = Array<SKITerminalSymbol | undefined>;
export type SymbolHeap = (SKITerminalSymbol | undefined)[];

export type BinaryHeap = Uint8Array

Expand Down Expand Up @@ -40,10 +40,8 @@ export function heapify (exp: SKIExpression): SymbolHeap {

indexes.push(rgtIndex(idx))
nodes.push(node.rgt)
} else if (node.kind === 'terminal') {
result[idx] = node.sym
} else {
throw new Error('unexpected node kind')
result[idx] = node.sym
}
}

Expand Down Expand Up @@ -75,7 +73,7 @@ export function unheapify (heapSyms: SymbolHeap): SKIExpression {

function unheapifyFrom (heapSyms: SymbolHeap, heapIdx: number): SKIExpression {
if (heapIdx >= heapSyms.length) {
throw new Error(`heap index exceeded: ${heapIdx}. input is corrupt.`)
throw new Error(`heap index exceeded: ${heapIdx.toString()}. input is corrupt.`)
}

const heapValue = heapSyms[heapIdx]
Expand Down Expand Up @@ -108,10 +106,8 @@ function packSymbol (sym: SKITerminalSymbol | undefined): number {
return 0b01
} else if (sym === SKITerminalSymbol.K) {
return 0b10
} else if (sym === SKITerminalSymbol.I) {
return 0b11
} else {
throw new Error('Impossible.')
return 0b11
}
}

Expand All @@ -133,7 +129,7 @@ function unpackSymbol (n: number): SKITerminalSymbol | undefined {
} else if (n === 0b11) {
return SKITerminalSymbol.I
} else {
throw new Error(`The number ${n} does not correspond to a symbol in SKI.`)
throw new Error(`The number ${n.toString()} does not correspond to a symbol in SKI.`)
}
}

Expand Down Expand Up @@ -182,20 +178,18 @@ export function packSymbolHeap (ts: SymbolHeap): BinaryHeap {
export function unpackBinaryHeap (inputBytes: BinaryHeap): SymbolHeap {
const result: SymbolHeap = []

for (let i = 0; i < inputBytes.length; i++) {
const byte = inputBytes[i] || 0

for (const by of inputBytes) {
/*
* 0 b 1100 0000 = 0 x C 0
* 0 b 0011 0000 = 0 x 3 0
* 0 b 0000 1100 = 0 x 0 C
* 0 b 0000 0011 = 0 x 0 3
*/
const fourSnakeEyes = [
(byte & 0xC0) >> 6,
(byte & 0x30) >> 4,
(byte & 0x0C) >> 2,
(byte & 0x03) >> 0
(by & 0xC0) >> 6,
(by & 0x30) >> 4,
(by & 0x0C) >> 2,
(by & 0x03) >> 0
]

fourSnakeEyes
Expand Down
3 changes: 1 addition & 2 deletions lib/typed/typedLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ import {
* The type is a.
* The body of the expression is y.
*/
export type TypedLambdaAbs = {
export interface TypedLambdaAbs {
kind: 'typed-lambda-abstraction',
varName: string,
ty: Type,
// eslint-disable-next-line no-use-before-define
body: TypedLambda
}

Expand Down
2 changes: 1 addition & 1 deletion lib/typed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NonTerminal, nt } from '../nonterminal'
import { Context, TypedLambda, mkTypedAbs, typecheck } from './typedLambda'
import { TypeError } from './typeError'

export type TypeVariable = {
export interface TypeVariable {
kind: 'type-var',
typeName: string
}
Expand Down
Loading

0 comments on commit 910be87

Please sign in to comment.