Skip to content

LanguageFeatures

uzudil edited this page Sep 3, 2021 · 12 revisions

BScript is a JavaScript-like language, with similar syntax, scoping rules and closures.

Literals

  • Basic types: numbers, strings, boolean, null. All numbers are floats but are cast to int if needed (array lookups, pixel coordinates, etc.)
  • Arrays: [1, 'a'] Arrays can contain any other type. To append to an array, add an element to an index past the length of the array.
  • Maps: { "a": 1, "b": "c" } Map keys are always strings. Map elements can be looked up by the map["a"] or map.a notation.
  • Functions: x => x + 1 Functions are first-class types.
  • Numerical expressions support the usual arithmetic operations, plus % for modulo.

Constants and variables

  • Constants can only be declared outside of functions: const PI = 3.14159;
  • Variables can be global (outside of functions), local (in a function) or function parameters: x := 42;. Variable scope starts in the local function and travels out toward the global scope.

Functions

  • Define named functions via def square(n) { return n * n; }.
  • Function can take other functions as a parameter, or return a function.
  • Functions can be declared in another function.
  • Anonymous functions are declared via: x => x + 1 or (x,y) => { return x + y; }
  • Functions are evaluated in the context of their closure. For example:
def x() {
   localVar := 1;
   return b => localVar + b;
}

fx := x();
print(fx(5));

will print 6.

  • map values can be functions that take a special first argument (usually called "self" or "this") that points to the map that contains them. For example:
player := {
   "isAlive": self => self.lives > 0,
   "lives": 3,
};
trace(player.isAlive());

Control flow commands

  • if(x) { ... } if else(y) { ... } else { ... } if statement works like you think it does. The else clause is optional.
  • BScript only has one type of loop: while(x < 10) { ... }
  • Both if and while support the following conditional operations: =, !=, <, <=, >, >=.
  • More complex boolean logic can be expressed with and: && and or || operators. (These use short-circuit evaluation.)
  • The return <value> statement always needs a value to return.

Other built-in commands

  • To delete from an array or map, use del a[10]. In this case it would delete the 10th element in the array. The array/map reference can be arbitrarily complex. For example: del game.enemies()[4].inventory[5]
  • See the BuiltinFunctions and BuiltinConstants pages for more info.
  • # this is a comment Comments are allowed anywhere except inside literals (this may change soon.)
  • Bscript programs can use the following built-in functions:
    • Array functions: array_map, array_join, array_filter, array_find, array_find_index, array_foreach, choose, basic_sort, basic_sort_copy, array_reverse, array_reduce, array_remove, copy_array, array_times, array_concat, array_flatten
    • Other functions: copy_map, roll, normalize, endsWith, startsWith, asPercent, range, pow

Program execution

  • Code execution starts by running the main method.
  • Your program can be a single .b file, or a single directory containing any number of .b files.
  • In the latter case, every file in the directory is parsed and joined to produce the program.
  • When running code from directory, your code can load and save map type objects to/from files. Filenames must be a string with no path characters (., .., /, \ are not allowed)