tridactyl/compiler/types/FunctionType.ts
glacambre 4d0f7c84eb
Make the generated metadata typed
This commit makes the compiler pass use different classes in order to
represent the metadata. This enables adding per-class toString/convert
functions. This enables easy type checking and conversion in the `:set`
excmd.
2018-11-04 17:24:16 +01:00

28 lines
777 B
TypeScript

import { Type } from "./Type"
export class FunctionType implements Type {
kind = "function"
constructor(public args: Type[], public ret: Type) {}
toConstructor() {
return (
`new FunctionType([` +
// Convert every argument type to its string constructor representation
this.args.map(cur => cur.toConstructor()) +
`], ${this.ret.toConstructor()})`
)
}
toString() {
return `(${this.args.map(a => a.toString()).join(", ")}) => ${this.ret.toString()}`
}
convert(argument) {
// Possible strategies:
// - eval()
// - window[argument]
// - tri.excmds[argument]
throw new Error(`Conversion to function not implemented: ${argument}`)
}
}