Skip to content

MS2 Syntax

GoogleFeud edited this page Mar 12, 2021 · 10 revisions

The MS2 syntax is pretty similar to javascript, but there are a few differences, missing features, and some nice additions.

Table Of Contents

Literals

Comments

Inline:

// Comment...

Multiline:

/* Comment
Comment */

Meta tags

Meta tags don't get compiled to bytecode, they get returned by the parse function. The meta tag value can be a raw literal - string, number, boolean, or null.

meta name = "something"

Numbers

1
3.14
200_000 // 200 000

Boolean

true
false

Strings

Strings literals only work with double quotes. You can use variables inside strings by placing a $ before the variable name.

"Hello, World!"
"Thank you, $variable"

Null

There is no undefined in MS2.

null

Arrays

[1, 2, null, true, "Hello!"]

Objects / Structs

Declaring a structure:

struct Person {
    name,
    age = 18, // Default values
    skills? // Nullable
}

Instantiating a structure:

const me = Person{name: "GoogleFeud"};
print(me); // {name: "GoogleFeud", age: 18}

Once a structure field has a default value, it's type cannot change.

Why strucutres?
Structures are the best way to guarantee type safety while also doing it implicitly.

Functions

This is the only way to define functions. Functions are treated as first-class citizens, they can be assigned to variables and passed as arguments.

(a, b) => {
  return a + b;
}

Functions can also have only one expression as their body:

(a, b) => a + b;

Expressions

Assignment

Returns the value. This also accounts for +=, -=, *= and /=.

variableName = "value";

Comparison

Same for >, <, >=, <=, !=. Unlike javascript, there's only strict comparison, which means 1 == "1" will yield false.

variableName == "something"

And

Same for ||. && returns the second one if both are true, and || will return the second one if the first one is falsey.

variable1 && variable

Not

!value

Call

func(arg1, arg2, arg3);

Property access

The [] notation CANNOT be used on objects!

const obj = X{a: 1, b: 2, c: 3};
obj.a;
const arr = [1, 2, 3, 4, 5];
arr[4];

Optional chaining:

const obj = X{a: { b: { c: null } } };
obj.a?.b?.c?.d;

If

if (expression) {

} else if (expression) {

} else {

}

Statements

Declaration

let name = "value";
const constantName = 3.14;

Once a let variable is declared with an initializor, it's type cannot be changed after assignment.

Loop

loop(check) {
 // Loop through an iterator
}
loop(check; after) {
   // code
}  
loop(init; check; after) {
   // code
} 
loop {
  // Infinite loop
}

Export

const something = 5;
export something;

Clone this wiki locally