Gerbl

My custom programming language specification.

Overview

Variables

Declaring a variable:

type:variable_name = value; (* examples *) int:bank_balance; (* if no value is provided, it will be set to the default for its type; here, 0 *) string:name = "Olivia"; float:temperature = 80.65;

Conditionals

Conditions are similar to other languages.

int:foo = 0; IF (foo == 0) { foo = 1; } ELSE { foo *= 2; }

Loops

Functions

Example of function declarations, definitions, and execution:

(int, int[] -> int):example_function; example_function = (foo, bar):{ (* syntactic sugar for definition *) RETURN foo + bar[0]; } example_function = (int:biz, int[]:baz -> int):{ (* example without syntactic sugar *) RETURN biz + baz[0]; } print (example_function 10 [10, 20, 30]);

Arrays

Arrays are homogenously typed and have a fixed length. Only 1D arrays exist, do dimension control yourself.

Structs

Structs are like arrays in that they have a preset size. Unlike arrays, values in a struct can have different types, and are accessed by keys instead of indexes.

All structs have a type. You can either use a preset type, or declare your own, before defining a struct.

(* declare the struct type "point" *) STRUCT point(int:x, int:y, (point, int, int ->):move); (* initializes a new struct of type point *) point:foo = point(0, 0, (self, dx, dy):{ self[x] += dx; self[y] += dy; }); (* accessing values in the struct *) print foo[x]; foo[move] foo 10 20; foo->move 10 20; (* same as above, syntactic sugar for when function has "self" as the first parameter *)

Instead of rewriting the same code over and over to initialize multiple structs, you can create a struct factory that returns a premade struct.

Example of a struct factory (adds onto above code):

(point, int, int ->):move_point = (p, dx, dy):{ p[x] += dx; p[y] += dy; } (-> point):make_point = ():{ RETURN point(0, 0, move_point); } foo = make_point;

Linkage

You can connect code in multiple files.

The file "helpers.gbl" contains the variables (-> int):help and int:x. To access these values inside another file:

reference:helpers = "helpers.gbl"; helpers[x] = helpers[help];

The reference variable reference:helpers is syntactic sugar; while it seems like helpers.gbl is being stored in a struct, all references to its variables will be converted to direct references at compilation. This format was chosen simply to prevent name clashing.

You can also prevent other files from being able to access a variable with the keyword HIDDEN at declaration:

HIDDEN int:cant_see_me = 10;