KerboScript Plus Plus Language Specification - griderd/Jebnix GitHub Wiki

Introduction

KerboScript++ is a free-form, case-insensitive, dynamically-typed, imperative, structured programming language based off of the KerboScript language by Kevin Laity. Kevin originally devised the KerboScript language for his kOS plugin for KSP. KerboScript++ is a spiritual successor to that language.


Hello World

In the tradition of programming languages of old, and originating with K&R C, I present the "Hello World" program in KerboScript++. print "Hello World!".

Notice that the line ends with a dot, or period. The dot operator is not a namespace separator like it is in many other languages. It instead is an end-of-statement indicator, akin to a semi-colon in C-like languages. Also notice that the string literal is wrapped in double-quotes. String literals can only be wrapped in double-quotes in KerboScript++. Single quotes will not work. The PRINT command will output the result of the expression to the right of the command to the screen.


Language Elements

KerboScript++ consists of several language elements: commands, variable declarations, functions, expressions, flight statistics, structures, enumerations, and system variables.

Commands

PRINT

The PRINT command has two variations: PRINT and PRINT... AT.

PRINT sends the result of the expression that follows it to standard output. PRINT... AT sends the result of the expression between PRINT and AT to the location indicated to the right of AT. The location must be listed as an ordered pair, (COLUMN, ROW). Each element of the ordered pair may be an expression itself.

print "Hello World!".
print "Current time: " + time:clock.
print time:date at (0,3).

Value Type Specification

The built-in variable type is simply called a Value object The Value object is the base object for all other objects in Jebnix. Internally, a Value object is a C# reference type (class) whose members contain representations of the current value in all of the different types that may be required by the script. There is a value that represents the type that is currently represented by the value. This type may change depending upon operations done on the object.

Like in C/C++, in KerboScript++, boolean values can be represented by integers. Non-zero values are true, zero is false. Internally, they are represented as System.Boolean values, as well as integers.

The only floating-point number type used by KerboScript++, referred to as a "float" in this document, is a 64-bit double-precision floating-point. Integers are 32-bit signed integers.

Uninitialized values can be NULL. As a result, it's a good idea to initialize your values right away with SET, or if appropriate, you may check for null values by using the ISNULL property.

Operators

Operators are detailed below. Standard mathematical order-of-operations is followed. Each operator has its own rules that depend upon context. Operators have underlying type precedence. This precedence is represented by a numbered list of types for each operator. Here is the list for the + (addition) operator:

  1. String
  2. Float
  3. Integer
  4. Boolean
  5. Ordered Pair

To interpret this list, start at the top. If an operation is between a string and another value, the other value will be cast to a string and the two strings will be concatenated. Otherwise, if the operation is between a float and another value, the other value will be cast into a float and the two values will be added, returning a float, and so on. While the internal type of a value may not be immediately apparent, it can be retrieved, and you can also manually cast between types in your code. This is covered later in this document. Some operations can only be performed on only a few types. In these cases, the types are checked normally from top to bottom of the list, and when the bottom of the list is reached, all remaining values are converted to this type. If a value is converted to a type by default, that type is denoted by italics.

Binary operators are covered first, then unary operators are covered.

+ Operator (Binary)

The + operator, or addition operator, performs addition, string concatenation, or logical-OR, depending upon context.

Type Precedence

  1. String
  2. Float
  3. Integer
  4. Boolean
  5. Ordered Pair

Addition on a boolean value is equivalent to logical-OR. It is implemented by doing addition on the integer representations of the values, then converting to a boolean. Ordered Pair addition is performed X to X, Y to Y.

- Operator (Binary)

The - operator, or subtraction operator, performs subtraction on its operands.

Type Precedence

  1. Float
  2. Integer
  3. Boolean
  4. Ordered Pair
  5. Float

Boolean subtraction implemented by doing subtraction on the integer representations of the values, then converting to a boolean. Ordered Pair subtraction is performed X to X, Y to Y.

* Operator

The * operator, or multiplication operator, performs multiplication on its operands.

Type Precedence

  1. Float
  2. Integer
  3. Boolean
  4. Ordered Pair/Ordered Pair
  5. Ordered Pair/Integer
  6. Integer/Ordered Pair
  7. Float

In Ordered Pair/Ordered Pair, the values provided must both be Ordered Pairs. In this case, multiplication is performed X to X and Y to Y. In the case that only one value is an Ordered Pair, the other value will be cast to an integer, then X and Y will each be multiplied by that integer.

/ Operator

The / operator, or division operator, performs division on its operands.

Type Precedence

  1. Float
  2. Integer
  3. Boolean
  4. Ordered Pair/Ordered Pair
  5. Ordered Pair/Integer
  6. Float

In Ordered Pair/Ordered Pair, the values provided must both be Ordered Pairs. In this case, division is performed X to X and Y to Y. In the case that the first value is an Ordered Pair, the second value will be cast to an integer, then X and Y will each be divided by that integer.

% Operator

The % operator, or modulus operator, gets the integer division remainder of its operands.

Type Precedence

  1. Ordered Pair/Ordered Pair
  2. Ordered Pair/Integer
  3. Integer

In Ordered Pair/Ordered Pair, the values provided must both be Ordered Pairs. In this case, modulus is performed X to X and Y to Y. In the case that the first value is an Ordered Pair, the second value will be cast to an integer, then X and Y will each be mod'ed by that integer.

^ Operator

The ^ operator, or caret operator, returns the value of the first operand raised to the power of the second.

Type Precedence

  1. Float
  2. Integer
  3. Boolean
  4. Ordered Pair/Ordered Pair
  5. Ordered Pair/Float
  6. Float

In Ordered Pair/Ordered Pair, the values provided must both be Ordered Pairs. In this case, power-raising is performed X to X and Y to Y. In the case that the first value is an Ordered Pair, the second value will be cast to an integer, then X and Y will each be raised by that integer.

& Operator

The & operator, or AND operator, performs bitwise-AND or logical-AND on the operands, depending upon the types. Logical-AND is performed only on booleans.

Type Precedence

  1. Integer
  2. Boolean
  3. Ordered Pair/Ordered Pair
  4. Ordered Pair/Integer
  5. Integer

| Operator

The | operator, or OR operator, performs bitwise-OR or logical-OR on the operands, depending upon their types. Logical-OR is performed only on booleans.

Type Precedence

  1. Integer
  2. Boolean
  3. Ordered Pair/Ordered Pair
  4. Ordered Pair/Integer
  5. Integer

== Operator / = Operator (Equality)

The equality operator checks that both the left expression and the right expression are equal, and returns a value containing True or False. This operator ALWAYS returns a boolean.

!= Operator

The non-equality operator checks that the left expression is not equal to the right expression, and returns a value containing True or False. This operator ALWAYS returns a boolean.

+ Operator (Unary)

The + operator, or plus operator, returns the value it precedes.

- Operator (Unary)

The - operator, or negative operator, returns the negative of the value it precedes, except for strings. If the value is a string, it returns the string.

~ Operator (Unary)

The ~ operator, or bitwise-NOT operator, performs bitwise-NOT on the operand it precedes.

Type Precedence

  1. Integer
  2. Boolean
  3. Ordered Pair
  4. Integer

! Operator (Unary)

The ! operator, or logical-NOT operator, performs logical-NOT on the operand it precedes. The value is assumed to be a boolean.

Type Precedence

  1. Boolean

Properties

ISNULL Property

This is a read-only property that returns a True or False value, which determines if the Value object is NULL. If this value is False, it is safe to read the Value object in question.

DECLARE PARAMETER X.
IF !(X:ISNULL)
{
    // Do something
}

Standard Library

The Jebnix Standard Library is divided into classes in C#, and namespaces on the script-side. Common Standard Library functions, such as math functions, can be called on the script side without using namespaces. These functions have namespace-free aliases. See Standard Library