General Syntax

Comments 1

// Comment
/* Block comment */
/* Nested /* block /* comment */ */ */

Atomic Types 2

  • Any - a more informative and type-safe version of void pointer. Includes a type pointer for the type and a void pointer for the value.

  • array - ordered list of values

  • bool - boolean (true or false)

  • enum - struct with constant members

  • float - alias for float32

  • float32 - thirty-two bit real

  • float64 - sixty-four bit real

  • int - alias for s64

  • null - #FIXME: find definition

  • pointer - reference to a memory location

  • procedure - callable set of instructions, with optional parameters and return value(s)

  • s16 - sixteen bit signed integer

  • s32 - thirty-two bit signed integer

  • s64 - sixty-four bit signed integer

  • s8 - eight bit signed integer

  • string - character sequence

  • struct - composite data structure stored in a contiguous block of memory

  • u16 - sixteen bit unsigned integer

  • u32 - thirty-two bit unsigned integer

  • u64 - sixty-four bit unsigned integer

  • u8 - eight bit unsigned integer

  • void - #FIXME: find definition

Declarations

Scalars 3

Use : to declare, = to assign
name : type = value name1, name2 : type = value

  • f : float; Declares f, explicitly typed to float (default value is 0)4
  • x, y, z : float; Declares x, y, and z explicitly as floats with default value 0. 5
  • f : int = 1; Declares and initializes f
  • f, g, h : int = 3; Assigns 3 to each of f, g, and h. 6
  • f := 1; Declares and initializes f, implicitly typed
  • f = 1; Assigns f or generates error if not already declared
  • f, g = 1, 2; Assigns 1 to f and 2 to g or generates error if either not already declared. 7
  • c := 'c - single quote for characters (u8 literals)

Pointers and Addresses

Use ^ for pointer, * for address8, ! for ownership

e : Entity;

pointer : ^Entity;
pointer = *e;

owned : node *! = null;
other : node *  = *graph.node;

Arrays

Provide static size allocations in square brackets9: [3]
Use two periods to create a dynamically sized array9: [..]
Array indices are zero-based (the first element is at index 0).
see also: Iteration.

a: [10] int; // A static array of 10 integers
b: [..] int; // A dynamic array of integers
print("size of array: %", a.count);

Arrays are a datatype built into the compiler, to improve performance and avoid some of the errors common in C. 10 11

Structs

Vector3 :: struct {
    x: float; // could also write:  x, y, z: float;
    y: float;
    z: float;
}

Enums

Enum members start at 0 by default, but can be initialized to arbitrary values.12

My_Enum :: enum u16 {
    VALUE_ZERO = 0,
    VALUE_ONE,
    VALUE_THREE = VALUE_TWO,
    VALUE_FOUR = MIDDLE_VALUE, // declared outside of enum
    VALUE_HIGH,                // trailing comma is fine
}

MIDDLE_VALUE := 8;

Introspection for enums includes count, value range, and names.13

printf("My_Enum ranges from %d to %d.\n", My_Enum.lowest_value, My_Enum.highest_value);
printf("My_Enum has %d members:\n", My_Enum.count);
for My_Enum.names {
    printf("  name: %s: value: %d\n", My_Enum.names[it_index], My_Enum.values[it_index]);
}

Enum values can be treated with strict or loose typing.14

x : My_Enum:strict;
x = My_Enum.VALUE_THREE; // valid
x = 10; // compiler error, even though 10 is a valid u16

y : My_Enum:loose;
y = 10; // valid
y = cast(My_Enum.VALUE_THREE, My_Enum.loose);

Functions and lambdas 15

Function declarations follow a style of progressive enhancement that allows for straight-forward maturation of code.
See also: Factorability.

Anonymous code block (lambda)

                              { return x * x; };

Anonymous code block with captured scope 16

                              [y] { return x * x + y; };

Anonymous function

          (x: float) -> float { return x * x; };
          (x: float) -> float [y] { return x * x + y; };

Named Function

square :: (x: float) -> float { return x * x; };
     f :: (x: float) -> float [y] { return x * x + y; };

process_array :: (float array[], (x: float) -> float) {
    /* use 2nd arg (proc) to modify 1st (array) */
}

Functions also support default arguments 17, named arguments 18, multiple return values 19, and default return values 20

Default Arguments

defaults :: (a: int = 9, b: int = -9) { printf("a is %d;\nb is %d.\n", a, b); };

Named Arguments

hello :: (name: string, phrase: string) {
  printf("My name is %s.  %s\n", name, phrase);
};
hello("fred", "booya"); // fine.
hello("booya", "fred"); // oops. but compiler can't help us.
hello(phrase = "booya", name = "fred"); // fine again.
hello("fred", phrase = "booya"); // also fine.
hello(name = "fred", "booya"); // error: unnamed arguments can't follow named ones.

Multiple Return Values

foo :: () -> int, int {
    return 3, 5;
}

x : int;
y : int;

x, y = foo();
printf("x is %d; y is %d.\n", x, y);

Default Return Values

fun :: (x: int) -> first: string = "Hello", second: string = "Sailor!" {
    if x == 0 return;
    if x == 1 return "Simple";
    if x == 2 return "Simple", "Simon";
    if x == 3 return second = "Simple", first = "Simon";

    return second = "Dolly"
}

a, b: string;

a, b = fun(0); // Hello Sailor!
a, b = fun(1); // Simple Sailor!
a, b = fun(2); // Simple Simon
a, b = fun(3); // Simon Simple
a, b = fun(4); // Hello Dolly

Initialization

variable declarations are automatically initialized to type defaults.
initialization can be implicit (accept default), explicit (provide value), or blocked (---).21 22

Vector2_implicit :: struct {
    x: float;
    y: float;
}
Vector2_explicit :: struct {
    x: float = 3;
    y: float = 5;
}
Vector2_blocked :: struct {
    x: float = ---;
    y: float = ---;
}

vi: Vector2_implicit;        print("% %\n", vi.x, vi.y); // "0 0"
ve: Vector2_explicit;        print("% %\n", ve.x, ve.y); // "3 5"
vb: Vector2_blocked;         print("% %\n", vb.x, vb.y); // undefined, possibly zeroes
veb: Vector2_explicit = ---; print("% %\n", ve.x, ve.y); // undefined, possibly zeroes

Iteration

for 23 24

for - supports named or implicit (it) iterators over ranges or arrays
break - exits current scope
continue - skips to next iteration
return - exits current function with value
remove <it> - deletes iterator from array being visited 25

for n : 1..count {
    printf("n is: %d\n", n);
}

results : int[];
for r : results {
    if r == 3 then remove r;
}
for results printf("results[%d] == %d\n", it_index, it);
for 1..10 {
    if it == 3 then continue;
    if it == 5 then return;
    if it == 8 then break; // will never reach this code
}

while

while i < count {
    if i == 5 then {
        i += 1;
        continue;
    }
    if i == 9 then break;
}

Flow Control

if

defer 26

defer - any statement can be deferred, which executes after the enclosing scope closes

defer delete test_node;

f := fopen("foo", "rb");
defer fclose(f);

main := () {
    printf("One!\n");
    defer printf("Three!\n");
    printf("Two!\n");
}

Allocation / Destruction 27

new - allocates memory, initializes value, casts for assignment
delete - frees memory


  1. c-style comments, but with proper nesting support.
    “Demo: Base language, compile-time execution.” YouTube, uploaded by Jonathan Blow, Oct 31, 2014, https://youtu.be/UTqZNujQOlA?t=705 

  2. the basic language types.
    “Demo: Base language, compile-time execution.” YouTube, uploaded by Jonathan Blow, Oct 31, 2014, https://youtu.be/UTqZNujQOlA?t=803 

  3. declarations and assignment.
    “A Programming Language for Games, talk #2.” YouTube, uploaded by Jonathan Blow, Sep 26, 2014, https://youtu.be/5Nc68IdNKdg?t=1666 

  4. default values are always zero. the default value…is the zero value for your type.
    “Q&A for Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 11, 2014, https://youtu.be/K45J_9jns7w?t=232 

  5. so [“x comma y equals foo”, generalized] gives you comma separated declarations (x, y, z: float;).
    “Arguments and Return Values” YouTube, uploaded by Jonathan Blow, Mar 10, 2015, https://youtu.be/CttIYXCUeVY?t=2282 

  6. if it’s a single-valued expression on the right-hand side, we individually assign that to all variables.
    “Arguments and Return Values” YouTube, uploaded by Jonathan Blow, Mar 10, 2015, https://youtu.be/CttIYXCUeVY?t=2558 

  7. you can do comma-separated expressions on the right-hand side.
    “Arguments and Return Values” YouTube, uploaded by Jonathan Blow, Mar 10, 2015, https://youtu.be/CttIYXCUeVY?t=2356 

  8. for pointers, use Pascal-style pointy hat (^) instead of ampersand (&). still use star (*) to dereference a pointer.
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=769 

  9. we have a basic static array ([N] int)…dynamic array ([..] int)…empty brackets (array: [] int) [which is] a pointer to the data array[], and a length value, array.count.
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=1598  2

  10. i don’t think you should build every data type into the compiler. Most data types should be defined by your program to be what they want. But some things are very common, so you want them to be very fast, and you want them to compile quickly because you’re going to have them all over your program.
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=1565 

  11. C’s biggest mistake is conflating pointers with arrays.
    C’s Biggest Mistake. Walter Bright, Dec 22, 2009, http://www.drdobbs.com/architecture-and-design/cs-biggest-mistake/228701625 

  12. enums are typed, can be named or anonymous, and can refer to values declared elsewhere.
    “Demo: Base language, compile-time execution.” YouTube, uploaded by Jonathan Blow, Oct 31, 2014, https://youtu.be/UTqZNujQOlA?t=1058 

  13. an enum is actually syntactic sugar for a struct that has some elements that tells you things about the enum.
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=2684 

  14. enums are strict types, distinct from all other types declared in the program, and can be treated with strict or loose typing behavior on a per-statement basis.
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=2906 

  15. basic function syntax.
    “A Programming Language for Games, talk #2.” YouTube, uploaded by Jonathan Blow, Sep 26, 2014, https://youtu.be/5Nc68IdNKdg?t=2686 

  16. capture is a property of the code block and not the function header.
    “A Programming Language for Games, talk #2.” YouTube, uploaded by Jonathan Blow, Sep 26, 2014, https://youtu.be/5Nc68IdNKdg?t=3041 

  17. like what you would see in C when you delcare a procedure, and it’s got an argument a that’s an integer and now you can say it equals 9, and b is an integer and it equals 9. So just like in C++, for example, if you don’t pass those arguments, then you’ll get those default values.
    “Arguments and Return Values” YouTube, uploaded by Jonathan Blow, Mar 10, 2015, https://youtu.be/CttIYXCUeVY?t=156 

  18. named arguments are useful when you have a function with a whole lot of arguments and the arguments are sometimes of the same type as each other, and so the type checker isn’t going to catch you if you pass them in the wrong order, or you accidentally forget one, or there’s some default arguments at the end. If you don’t have default arguments in your language it’s less of a problem [..] so if you have default arguments, then I think you want named arguments.
    “Arguments and Return Values” YouTube, uploaded by Jonathan Blow, Mar 10, 2015, https://youtu.be/CttIYXCUeVY?t=212 

  19. here’s what multiple return values are like. I’ve got some function foo :: () -> int, int. It takes no arguments, and it returns, instead of one int, it returns two ints. It returns int, int, and I’m going to return 3, 5;. I’ve got an int x and an int y, and you can take both values by just saying x, y = foo();.
    “Arguments and Return Values” YouTube, uploaded by Jonathan Blow, Mar 10, 2015, https://youtu.be/CttIYXCUeVY?t=1539 

  20. you can have default values on your return values, just like with arguments.
    “Arguments and Return Values” YouTube, uploaded by Jonathan Blow, Mar 10, 2015, https://youtu.be/CttIYXCUeVY?t=2940 

  21. every single thing in the language always gets initialized to its default value, which is usually zero unless you specify a different default value. but, for performance reasons, you have the ability to say, “well, actually, let’s not initialize this thing.”
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=1075 

  22. dash-dash-dash (---) means don’t initialize this.
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=1238 

  23. iteration over elements with for.
    “Demo: Base language, compile-time execution.” YouTube, uploaded by Jonathan Blow, Oct 31, 2014, https://youtu.be/UTqZNujQOlA?t=1678 

  24. for.. break, continue, return.
    “Demo: Base language, compile-time execution.” YouTube, uploaded by Jonathan Blow, Oct 31, 2014, https://youtu.be/UTqZNujQOlA?t=1968 

  25. the remove primitive removes the current element from the array. is inherent in the loop in the way break or continue are.
    “Demo: Iteration and arrays, uninitialized values, enums.” YouTube, uploaded by Jonathan Blow, Dec 10, 2014, https://youtu.be/-UPFH0eWHEI?t=2106 

  26. defer is not a macro, it is a core part of the language understood by the compiler and debugger.
    “Demo: Base language, compile-time execution.” YouTube, uploaded by Jonathan Blow, Oct 31, 2014, https://youtu.be/UTqZNujQOlA?t=1365 

  27. new and delete are cleaner than c++.
    “Demo: Base language, compile-time execution.” YouTube, uploaded by Jonathan Blow, Oct 31, 2014, https://youtu.be/UTqZNujQOlA?t=1265 

jailang 2019 pixeldroid
https://github.com/pixeldroid/jailang
programming pages theme v0.5.21 (https://github.com/pixeldroid/programming-pages)