/BagelLangPy

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

    ██████╗  █████╗  ██████╗ ███████╗██╗     
    ██╔══██╗██╔══██╗██╔════╝ ██╔════╝██║     
    ██████╔╝███████║██║  ███╗█████╗  ██║     
    ██╔══██╗██╔══██║██║   ██║██╔══╝  ██║     
    ██████╔╝██║  ██║╚██████╔╝███████╗███████╗
    ╚═════╝ ╚═╝  ╚═╝ ╚═════╝ ╚══════╝╚══════╝
    ︵‿︵‿୨♡୧‿︵‿︵
    better syntax ocaml (subjectively)
  

License: GNU Ocaml: 4.13.1

Ocaml syntax is... weird. Let's use a better syntax !

Overview

Type Declarations

let x: int = 1;;

let y = 5;;

to

int x = 1;

// Type Inference: reducing the need for explicit type declarations
auto y = 5;

Curly Braces for Scopes

let add (a, b : int * int) {
    a + b
};;

let main () : unit = (
    ...
);;

to

int add(int a, int b) {
    return a + b;
}

unit main() {
    ...
}

Pointers

let x: int ref = ref 0;;
!x;;
x := 1;

to

int* x = &0;
x.get();
x.set(1);

Nullability

let find_value (key : int) : int option;;
let divide (numerator, denominator : int * int) : (int, error) result;;

to

Option<int> find_value(int key);
Result<int, Error> divide(int numerator, int denominator);

Ideas

// Namespaces & class definitions that resemble C++
namespace Math {
    int add(int a, int b) {
        return a + b;
    }
}

class Point {
public:
    int x;
    int y;

    Point(int x, int y) {
        this->x = x;
        this->y = y;
    }

    void move(int dx, int dy) {
        x += dx;
        y += dy;
    }
}

// Recursive functions
#[recursive, debug]
int fibo(int n) {
    if (n <= 1) {
        return n;
    }
    return fibo(n - 1) + fibo(n - 2);
}


// Anonymous functions
auto inc = [](int x) {
    return x + 1;
};

// Maps
auto squares = map([1, 2, 3], [](int x) { return x * 2; });

Notes

While loops are faster than for, local and blocks loops