2018-09-22 12:34:26 +02:00
|
|
|
import { Type } from "./Type"
|
|
|
|
|
|
|
|
export class FunctionType implements Type {
|
2019-03-26 06:34:40 +01:00
|
|
|
public kind = "function"
|
2018-09-22 12:34:26 +02:00
|
|
|
|
2019-05-31 13:25:51 +02:00
|
|
|
constructor(public args: Type[], public ret: Type, public isDotDotDot = false, public isQuestion = false) {}
|
2018-09-22 12:34:26 +02:00
|
|
|
|
2019-03-26 06:34:40 +01:00
|
|
|
public toConstructor() {
|
2018-09-22 12:34:26 +02:00
|
|
|
return (
|
|
|
|
`new FunctionType([` +
|
|
|
|
// Convert every argument type to its string constructor representation
|
|
|
|
this.args.map(cur => cur.toConstructor()) +
|
2019-05-31 13:25:51 +02:00
|
|
|
`], ${this.ret.toConstructor()}, ${this.isDotDotDot}, ${this.isQuestion})`
|
2018-09-22 12:34:26 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:34:40 +01:00
|
|
|
public toString() {
|
2018-09-22 12:34:26 +02:00
|
|
|
return `(${this.args.map(a => a.toString()).join(", ")}) => ${this.ret.toString()}`
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:34:40 +01:00
|
|
|
public convert(argument) {
|
2018-09-22 12:34:26 +02:00
|
|
|
// Possible strategies:
|
|
|
|
// - eval()
|
|
|
|
// - window[argument]
|
|
|
|
// - tri.excmds[argument]
|
|
|
|
throw new Error(`Conversion to function not implemented: ${argument}`)
|
|
|
|
}
|
|
|
|
}
|