Skip to content

Instantly share code, notes, and snippets.

@SwanX1
Created July 18, 2023 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SwanX1/b2050c4371003584ed25fc62b415a17d to your computer and use it in GitHub Desktop.
Save SwanX1/b2050c4371003584ed25fc62b415a17d to your computer and use it in GitHub Desktop.

MOOSE, the programming language.

Table of contents

Pretext

⚠️This section only contains thoughts, not the actual spec⚠️

Link to moose.md, a compilation of my thoughts in the past.

On 4th June, 2021, I decided I'd make a programming language. Because... why not? I originally tried implementing this in TypeScript, but everything is SO DAMN DIFFICULT. So I didn't do anything for two years.

After initially discussing my decision to create a functional typed interpreted language with my friend Moose (that's a nickname, of course), on a whim I decided to name the language "moose".

The extension would be .moose or .mse, both would be supported. The MIME type would be text/moose, but not test/mse. This type is not official and shouldn't be expected to be supported by anyone.

Goals

  • Statically typed.
  • C-ish style.
  • Can be run on an interpreter, as a DSL, or compiled. Easily sandboxable.
  • Can be AOT compiled into bytecode.

Initial thought

export decl func main = (string[] args) void {
  print(getGreeting());
  decl const int a = 2;
  decl int b;
  b = 5;
  print(a + b);
  return 0;
}

decl const string DEFAULT_GREETING_NAME = "World";

decl func getGreeting = () string {
  return getGreeting(DEFAULT_GREETING_NAME);
}

decl func getGreeting = (string name) string {
  return "Hello " + name + "!";
}

Ew. decl is used too much, which can just be inferred, what is up with the equals signs inbetween function names and argument declarations, return types are just weirdly declared before the block. Okay, let's revise this.

export func main(args: string[]): void

This looks better. Basically like TypeScript, but I like it.

Types for variables are assigned before the actual assignment. This is hard to parse, so I'll introduce a let keyword (like in Rust and TypeScript). let would not be needed if const is provided.

const a: int = 2;
let b: int;
b = 5;

Now this is just TypeScript.

Further thoughts

Chained comparison operators

Most languages do not have this, but it's such a great thing and I'm surprised it isn't prevalent in the most popular ones.

if (a < b < c) {}
if (a < b && b < c) {}

Why isn't this a thing??

Template literals

fmt! in Rust, String.format in Java, these things should be easily usable without any need for other functions. JavaScript does this very well with ``.

In Moose this is achieved using ''. Normal strings are written with ", template literals strings are written with '.

string world = "Planet";
print("Hello {world}!"); // expected output: Hello {world}!
print('Hello {world}!'); // expected output: Hello Planet!
print('Hello \{world\}!'); // expected output: Hello {world}!
print('Hello \{world}!'); // expected output: Hello {world}!
print('Hello {world\}!'); // syntax error: Invalid escape sequence

Operators and special characters

Operators

This section is long, click on details to see everything.
This section follows the format of

  • operator - Short name
    example code Explanation
  • = - Assignment
    x = 10 Assigns the value 10 to the variable x.
  • + - Addition
    1 + 5 Adds the numbers 1 and 5 together.
  • - - Subtraction
    7 - 3 Subtracts 3 from 7.
  • * - Multiplication
    2 * 4 Multiplies 2 by 4.
  • / - Division
    10 / 2 Divides 10 by 2.
  • += - Addition and assignment
    x += 4 Increments the variable x by 4.
  • -= - Subtraction and assignment
    y -= 3 Decrements the variable y by 3.
  • *= - Multiplication and assignment
    z *= 2 Multiplies the variable z by 2 and assigns the result to z.
  • /= - Division and assignment
    a /= 5 Divides the variable a by 5 and assigns the result to a.
  • % - Modulo (remainder)
    10 % 3 Calculates the remainder when 10 is divided by 3.
  • %= - Modulo (remainder) and assignment
    x %= 3 Calculates the remainder when x is divided by 3 and assigns the result to x.
  • ++ - Increment
    i++ Increments the value of i by 1.
  • -- - Decrement
    j-- Decrements the value of j by 1.
  • ** - Exponentiation
    2 ** 3 Raises 2 to the power of 3.
  • **= - Exponentiation and assignment
    x **= 2 Raises the value of x to the power of 2 and assigns the result to x.
  • == - Equality
    a == b Checks if a is equal to b.
  • != - Inequality
    x != y Checks if x is not equal to y.
  • > - Greater than
    a > b Checks if a is greater than b.
  • < - Less than
    x < y Checks if x is less than y.
  • >= - Greater than or equal to
    a >= b Checks if a is greater than or equal to b.
  • <= - Less than or equal to
    x <= y Checks if x is less than or equal to y.
  • || - Logical OR
    x || y Checks if either x or y is true.
  • && - Logical AND
    a && b Checks if both a and b are true.
  • ! - Logical NOT
    !x Negates the value of x.
  • . - Member access
    object.property Accesses the property of an object.
  • ? - Ternary operator (conditional)
    x > 5 ? "yes" : "no" Evaluates the condition x > 5 and returns "yes" if true, otherwise returns "no".
  • : - Else in a ternary operator (other stuff too)
    condition ? trueValue : falseValue Separates the true value and false value in a ternary operator.
  • ~ - Bitwise NOT
    ~x Flips the bits of x (bitwise complement).
  • | - Bitwise OR
    a | b Performs a bitwise OR operation between a and b.
  • & - Bitwise AND
    x & y Performs a bitwise AND operation between x and y.
  • ^ - Bitwise XOR (exclusive OR)
    a ^ b Performs a bitwise XOR operation between a and b.
  • ~= - Bitwise NOT and assignment
    x ~= y Performs a bitwise NOT operation between x and y and assigns the result to x.
  • |= - Bitwise OR and assignment
    x |= y Performs a bitwise OR operation between x and y and assigns the result to x.
  • &= - Bitwise AND and assignment
    x &= y Performs a bitwise AND operation between x and y and assigns the result to x.
  • ^= - Bitwise XOR and assignment
    x ^= y Performs a bitwise XOR operation between x and y and assigns the result to x.
  • >> - Bitwise right shift
    x >> 2 Shifts the bits of x two positions to the right.
  • << - Bitwise left shift
    y << 3 Shifts the bits of y three positions to the left.
  • >>= - Bitwise right shift and assignment
    x >>= 1 Shifts the bits of x one position to the right and assigns the result to x.
  • <<= - Bitwise left shift and assignment
    y <<= 2 Shifts the bits of y two positions to the left and assigns the result to y.

Special characters

  • ; Marks the end of a statement.
    print();
  • { and } mark the beginning and end of a block.
    if (someCondition) { // }
  • [ and ] mark the beginning and end of an array.
    [1, 2, 5]
  • ( and ) are used to group expressions and control the order of operations. They are also used to group arguments in a function call or a function declaration.
    Example: x + (y - z)
    y - z is evaluated first.
    Example: someFunc(argument1, argument2)
    someFunc is called with the arguments argument1 and argument2
  • : can be used not only as the ternary 'else' operator, but also to declare a type in function declarations.
    let i: int = 5; Variable i is of type int
    func add(a: int, b: int): int Function add returns a type of int and arguments a and b are of type int
  • , seperates arguments in a function call, function declaration, seperates elements in an array.

Iterators

When for prepends an iterator within array braces, it expands the iterator into the array, (1...5) would be a shorthand for iterator creation.

// Code
let list: int[] = [for (x...y)];

// Compiled
let list: int[] = new int[5];
for (let i: int; i < y - x; i++) {
  list[i] = i + x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment