-
Notifications
You must be signed in to change notification settings - Fork 2
Add support for enumerated types #6
Description
kOS/KerboScript does not offer any support for enumerations. You must instead use integers (obtuse to the programmer) or strings (space wasting and computationally expensive). It would be nice to have an enumerated type.
Proposed syntax
NOTE: This syntax worries me mostly because it is the first allowed syntax that would not be at the beginning of the line.
@ksx enum:def runmode has (BOOT, RAMPUP, DETACH, TAKEOFF, CIRCULARIZE, BLOOM).
Where enum:def indicates that we are defining an enumeration, runmode is the name of the enumerated type, and the contents of the brackets {} are the valid values.
Or it would also be valid to split this across multiple lines such as (yes, the trailing comma is intentional, but not required):
@ksx enum:def runmode has (
BOOT,
RAMPUP,
DETACH,
TAKEOFF,
CIRCULARIZE,
BLOOM,
).
You could then use the enum in the following way...
if (@ksx enum:eq runmode:BOOT) {
// wait for ship to be unpacked
} else if (@ksx enum:neq runmode:RAMPUP) {
// takeoff, etc.
} else {
// done I suppose
}Transpilation
The enum definition statement would be in-place expanded to:
set runmode to 0. // BOOT
The enum comparison statements would be transpiled to the following:
if (runmode = 0) {
// wait for ship to be unpacked
} else if (runmode <> 1) {
// takeoff, etc.
} else {
// done I suppose
}
Of course, this would also mean that gt, gte, lt, and lte are also valid suffixes of enum, corresponding to >, >=, <, and <=operators respectively.
Bonus: Enum Membership Operators
It would be nice to have an expansion such as:
set result to @ksx enum:in (runmode, 21).
Which tests (and returns true) if 21 is an allowed value of the runmode enum type, as well as the inverse operator nin.
This is not a hard requirement for the first pass of enum implementation.