Skip to content

Commit 0bd315a

Browse files
committed
WIP noImplicitAny
1 parent dd5cd18 commit 0bd315a

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

src/node-types.ts

+12
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ export interface TypedIfNode extends AstIfNode, Typed {
156156
elseBranch: TypedNode
157157
}
158158

159+
//
160+
// Nodes with shared traits
161+
//
162+
export type TypedLiteralNode = TypedStringNode | TypedBooleanNode | TypedNumeralNode
163+
164+
//
165+
// Guard functions
166+
//
167+
export function isTypedLiteralNode (node: TypedNode): node is TypedLiteralNode {
168+
return ['Bool', 'Str', 'Numeral'].includes(node.type)
169+
}
170+
159171
//
160172
// Runtime values
161173
// TODO

src/unify.ts

+18-34
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import PeachError from './errors'
22
import { isArray, isEqual } from './array'
3+
import { zip } from './util'
4+
import { TypedNode, TypedLiteralNode, TypedDestructuredArrayNode, Value, isTypedLiteralNode } from './node-types'
5+
6+
// FIXME tagged union - with didMatch: false, there are never bindings.
7+
type binding = { [name: string]: Value }
38

4-
// FIXME union type - with didMatch: false, there are never bindings.
59
interface unification {
610
didMatch: boolean,
7-
bindings: object
11+
bindings: binding
812
}
913

10-
export default function unify (patterns, values) : unification {
14+
export default function unify (patterns: TypedNode[], values: Value[]): unification {
1115
if (patterns.length !== values.length) {
1216
return didNotMatch
1317
}
@@ -25,24 +29,23 @@ export default function unify (patterns, values) : unification {
2529
return didMatch(bindings)
2630
}
2731

28-
function unifyOne (pattern, value) {
29-
// TODO value equality operator
30-
if (isValue(pattern) && pattern.value === value) {
32+
function unifyOne (pattern: TypedNode, value: Value): binding {
33+
if (isTypedLiteralNode(pattern) && pattern.value === value) {
3134
// the pattern matched, but there is nothing to bind
3235
return {}
3336
}
3437

35-
if (isName(pattern)) {
38+
if (pattern.type === 'Name') {
3639
// the pattern matched; return a new binding
3740
return { [pattern.name]: value }
3841
}
3942

4043
// TODO generic value equality
41-
if (isArray(pattern) && isEqual(pattern.values, value)) {
44+
if (pattern.type === 'Array' && isEqual(pattern.values, value)) {
4245
return {}
4346
}
4447

45-
if (isDestructuredArray(pattern)) {
48+
if (pattern.type === 'DestructuredArray') {
4649
return destructure(pattern, value)
4750
}
4851

@@ -51,14 +54,14 @@ function unifyOne (pattern, value) {
5154
}
5255

5356
// TODO this will need to change when Array is a wrapped type
54-
function destructure ({ head, tail }, array) {
55-
if (array.length === 0) {
57+
function destructure ({ head, tail }: TypedDestructuredArrayNode, values: Value[]): binding {
58+
if (values.length === 0) {
5659
throw new PeachError(`Empty arrays cannot be destructured because they don't have a head`)
5760
}
5861

59-
const boundHead = unifyOne(head, array[0])
62+
const boundHead = unifyOne(head, values[0])
6063
if (boundHead !== null) {
61-
const boundTail = unifyOne(tail, array.slice(1))
64+
const boundTail = unifyOne(tail, values.slice(1))
6265
if (boundTail) {
6366
return Object.assign(boundHead, boundTail)
6467
}
@@ -67,33 +70,14 @@ function destructure ({ head, tail }, array) {
6770
return null
6871
}
6972

70-
const didNotMatch : unification = {
73+
const didNotMatch: unification = {
7174
didMatch: false,
7275
bindings: {}
7376
}
7477

75-
function didMatch (bindings) : unification {
78+
function didMatch (bindings: binding) : unification {
7679
return {
7780
didMatch: true,
7881
bindings
7982
}
8083
}
81-
82-
// TODO these belong with type definitions
83-
function isName ({ type }) {
84-
return type === 'Name'
85-
}
86-
87-
function isValue ({ type }) {
88-
return ['Bool', 'Str', 'Numeral'].includes(type)
89-
}
90-
91-
function isDestructuredArray ({ type }) {
92-
return type === 'DestructuredArray'
93-
}
94-
95-
// TODO stdlib
96-
function zip (arrayA, arrayB) {
97-
return arrayA.map((e, i) => [e, arrayB[i]])
98-
}
99-

src/util.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// _.extend, but immutable by default
2-
export function extend (source, ...extensions) {
2+
export function extend (source: any, ...extensions: any[]) {
33
return Object.assign({}, source, ...extensions)
44
}
55

66
export const clone = extend
77

88
// shortcut for creating an object with the given prototype and
99
// properties with default behaviour
10-
export function create (proto, properties = null) {
10+
export function create (proto: any, properties: any = null) {
1111
return Object.assign(Object.create(proto), properties)
1212
}
1313

@@ -20,3 +20,10 @@ export function restAndLast<T> (arr: T[]): [T[], T] {
2020
export function last<T> (arr: T[]): T {
2121
return arr[arr.length - 1]
2222
}
23+
24+
export function zip<A, B> (a: A[], b: B[]): [A, B][] {
25+
return a.map((aValue, i): [A, B] => {
26+
const bValue = b[i]
27+
return [aValue, bValue]
28+
})
29+
}

0 commit comments

Comments
 (0)