Skip to content

blank-manash/glang

Repository files navigation

GLANG

Introduction

The above project is a complete interpreter for glang. It leverages a custom build lexer and parser implementing the TOP-DOWN Recursive Descent Algorithm for semantic checking.

Syntax

glang borrows a lot of it’s syntax from javascript, along with it’s support for functional programming. You might find some reference to python and haskell as well, but overall it’s what you make it.

An example code is provided below

let x = 51 + 21;

let add = func(a, b) {
  a + b 
}

let add5 = func(x) {
  return add(5,x); 
}

You can find more examples in the examples/ directory.

Rules

glang is an dynamically typed language. And supports flexible assignments like javascript or python. Except statements like let or return, everything is and expression, and everything can produce a value.

This means that the above is completely valid (Implicit Returns).

let fib = func(n) {
    if (n == 0) {
        0
    } elif (n == 1) {
        1
    } else {
        fib(n - 1) + fib(n - 2)
    }
}

As you can see return statements are optional, supporting implicit returns.

Like javascript and (not so) python, ‘;’ are also optional.

NOTE: Comments are not supported yet.

Variable Scopes

The variables in glang follow a blocked scope. As such,

let z = 0;

let q = func() {
    let o = 1;
}

puts(z) // Accesible
puts(o) // Unknown Identifier Error

Objects

Objects are like hashes, which have expressions, mapped to expressions. You can use objects as such.

let two = "two";
let o = {
     "one": 10 - 9,
     two: 1 + 1,
     "thr" + "ee": 6 / 2,
     4: 4,
     true: 5,
     false: 6
 }
return [o['one'], o[two], o['three'], o[4], o[true], o[false]]; // [1, 2, 3, 4, 5, 6]

Installation and Usage

You can download the binary, and directly use it as follows.

./<your-binary-name> <source-file>

Or to start the REPL, run it without any arguments.

If you decide to download the source code, run npm install in the directory, then you can do the following.

tsc --build
node dist/index.js <source-file>

Or if you have ts-node installed, you can simply do

npx ts-node index.ts

Contributing

Please raise bugs and feature requests in Github Issues Section.

LICENSE

All files and folders are under MIT.

About

A short and sweet compiler

Resources

Stars

Watchers

Forks

Packages

No packages published