Skip to content

Reference

Shigeru Chiba edited this page Oct 10, 2024 · 5 revisions

BlueScript Reference

Introduction

BlueScript is a scripting language that borrows heavily from TypeScript syntax. However, not all TypeScript syntax is currently supported in BlueScript. Notable features that are not yet supported include exceptions, certain built-in objects, and async/await functionality.

BlueScript also diverges from TypeScript in some key semantics, particularly in its type system. Unlike TypeScript, BlueScript separates integer and float types, treating them as distinct.

Primitive Types

BlueScript provides six primitive types as well as object types:

  • integer (32bit integer)
  • number (an alias of integer)
  • float (32bit floating-point number)
  • string
  • boolean
  • null and undefined (they are the same)
  • any

Any kind of value can be implicitly converted into any type, and vice versa.

  • When an integer value is converted, the resulting value is represented as a 30bit integer.
  • When a float value is converted, the resulting value is represented as a 30bit floating-point number, where only 6 bits are allocated for an exponent instead of 8 bits.
  • When a boolean value is converted into any type, the resulting value of any type is either 0 (false) or 1 (true). Thus, it can be later converted into an integer value. Similarly, an any-type value holding 0, 0.0, null, or undefined can be converted into a boolean value false. The other any-type values can be converted into true.

Literals

  • null and undefined (they are the same)
  • true
  • false
  • Numbers such as 7 and 0.3
  • Character strings such as "text" and 'text'
  • Arrays

Built-in Objects

Array

Array Literals

Array literals are defined using square brackets, with elements separated by commas.

let arr = [1, "Foo", 42];

Type Annotations

BlueScript supports both Type[] and Array<Type> syntax for type annotations.

let iarr: integer[] = [1, 2, 3];
let iarr2: Array<integer> = [1, 2, 4];

Accessing Elements

Only numeric indices are supported for accessing array elements.

let arr = [1, 3, 4];
print(arr[0]); // 1

Accessing an index out of bounds will result in a runtime error.

let arr = [1, 2, 3];
arr[5]; // ** error: array index out of range: 5

Currently, array methods such as push, pop, map, filter, etc., are not supported in BlueScript.

Expressions and Operators

Operators

Increment and Decrement Operators

The increment and decrement operators work with numerical values and literals.

  • Postfix Increment/Decrement Operators
    The value is incremented or decremented by 1, and the original value is returned.

    let x = 1;
    print(x++); // 1
    print(x);   // 2
    
    let y = 1.0;
    print(y--); // 1.0
    print(y);   // 0.0
  • Prefix Increment/Decrement Operators
    The value is incremented or decremented by 1, and the updated value is returned.

    let x = 1;
    print(++x); // 2
    print(x);   // 2

Unary Operators

BlueScript supports the following unary operators:

  • + (Unary plus, returns the value of its operand)
  • - (Unary negation, returns the negation of its operand)
  • ~ (Bitwise NOT)
  • ! (Logical NOT)
print(-3)  // -3

Arithmetic Operators

These operators take numerical operands and return a single numerical result.

  • ** (Exponentiation)
  • * (Multiplication)
  • / (Division)
  • % (Modulus)
  • + (Addition)
  • - (Subtraction)

Relational Operators

Relational operators take numerical operands and return a boolean value.

  • < (Less than)
  • > (Greater than)
  • <= (Less than or equal to)
  • >= (Greater than or equal to)

Equality Operators

  • == and === are used as equality operators. In BlueScript, they are identical in behavior, checking both value and type.
  • != and !== are used as inequality operators, also identical in behavior, checking both value and type.

Bitwise Shift Operators

Bitwise shift operators take integer operands and return a integer value.

  • << (Left shift)
  • >> (Right shift)
  • >>> (Unsigned right shift)

Binary Bitwise Operators

Binary bitwise operators take integer operands and return a integer value.

  • & (AND)
  • | (OR)
  • ^ (XOR)

Binary Logical Operators

  • && (Logical AND)
  • || (Logical OR)

Ternary Operator

The ternary operator is used for conditional expressions:

let result = condition ? trueValue : falseValue;

Assignment Operators

BlueScript supports the following assignment operators:

  • =
  • +=, -=, *=, /= (Compound assignment operators)

Statements and Declarations

Variable Declarations

Variables in BlueScript are declared using let and const.

let x = 10;
let y: float = 5.5;
const t:boolean = true

Loops

  • while loop

    let i = 0;
    while (i < 10) {
        print(i);
        i++;
    }
  • for loop

    for (let i = 0; i < 10; i++) {
        print(i);
    }
  • break and continue

    for (let i = 0; i < 10; i++) {
        if (i == 5) {
            continue;
        }
        if (i == 8) {
            break;
        }
        print(i);
    }

Conditional Statements

  • if … else …

    let a = 10;
    if (a > 5) {
        print("Greater than 5");
    } else {
        print("Less than or equal to 5");
    }

Functions

Declarations

Functions are declared with function keyword.

function add(a:integer, b:integer):integer {
  return a + b;
}

The function definition can be overwritten only if the function’s argument types and return type are the same.

Arrow Functions

let add = (a: integer, b: integer): integer => a + b;

Currently, closures are not supported in BlueScript.

Classes

Class declarations

Classes in BlueScript are declared by using class keyword.

class Rectangle {
  height:float;
  width: float;

  constructor(height: float, width:float) {
    this.height = height;
    this.width = width;
  }

  getArea() {
    return this.height * this.width;
  }
}

Currently, Access modifiers are not supported in BlueScript.

extends

extends keyword is used to create child classes.

class Square extends Rectangle {
  constructor(sideLength:float) {
    super(sideLenght, sideLength);
  }
}

new

new keyword is used to create class instance.

let rect = new Rectangle(13, 15);

Access properties and methods

Properties and methods are accessed with dot notation.

print(rect.height) // 13
print(rect.getArea()); // 195

In the methods' definitions, the properties and methods are accessed with this keyword.

class Rectangle {
  height:float;
  width: float;

  constructor(height: float, width:float) {
    this.height = height;
    this.width = width;
  }

  getArea() {
    return this.height * this.width;
  }
}