I am in the process of converting my lexer and parser to TypeScript. You can find the current JavaScript-only code here. To simplify, I have created an example pseudocode:
type X = {
type: string
}
type A = X & {
list: Array<A | B | C>
}
type B = X & {
value: A | B | number
}
type C = X & {
value: string
}
const a1: A = { type: 'a', list: [] }
const a2: A = { type: 'a', list: [] }
const a3: A = { type: 'a', list: [] }
const b1: B = { type: 'b', value: a1 }
const b2: B = { type: 'b', value: b1 }
const b3: B = { type: 'b', value: 200 }
const c1: C = { type: 'c', value: 'foo' }
const c2: C = { type: 'c', value: 'bar' }
const c3: C = { type: 'c', value: 'baz' }
a1.list.push(b1, a2, b2, b3)
a2.list.push(a1, a3, b3, c1)
a3.list.push(b2, c2, c3)
const x = { type: 'a', list: [a1, a2, a3] }
handle(x)
function handle(x: A | B) {
if (x.type === 'a') {
x.list.forEach(handle)
} else if (x.type === 'b') {
if (typeof x.value === 'number') {
console.log(x.value)
} else {
handle(x.value)
}
} else { // c
console.log(x.value)
}
}
In the handle
function, no typing has been implemented. How do you approach this situation?
In the parser code (which also includes a separate lexer module), certain operations are carried out like:
while (i < list.length) {
const token = list[i++]
// console.log(token.form, stack)
switch (token.form) {
case `term-open`: {
const node = stack[stack.length - 1]
const term = {
form: 'term',
link: []
}
node.leaf.push(term)
stack.push(term)
break
}
case `term-close`: {
stack.pop()
break
}
case `open-parenthesis`: {
const node = stack[stack.length - 1]
const site = {
form: 'site',
leaf: [],
site: []
}
node.site.push(site)
stack.push(site)
break
}
case `close-parenthesis`: {
stack.pop()
break
}
}
The important aspect is
const node = stack[stack.length - 1]
, where the context determines the type of node. How would you appropriately handle such scenarios in TypeScript? Is casting as a specific type the correct approach?
What would be the proper way to iterate through the pseudocode above (TypeScript) and accurately log the leaf values with the correct typing?