Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Add support for enumerated types #6

@LeonardMH

Description

@LeonardMH

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestksx-langRelated to the KerboScript Extended language itself

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions