jaba

command module
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2024 License: MIT Imports: 4 Imported by: 0

README

Golang workflow wakatime

jaba

jaba is a programming language built using golang. The language source code is transformed into tokens through lexical analysis and then tokens are transformed to an Abstract Syntax Tree using a parser. Evaluation (Interpretation) is done to give the AST meaning and returns the respective result of the source code entered.

Source Code -> Tokens -> Abstract Syntax tree (AST) -> Output

The first transformation, from source code to tokens, is called “lexical analysis”, or “lexing” for short. It’s done by a lexer (also called tokenizer or scanner – some use one word or the other to denote subtle differences in behaviour). Tokens themselves are small, easily categorizable data structures that are then fed to the parser, which does the second transformation and turns the tokens into an “Abstract Syntax Tree”. The final step is evaluation where the AST is given meaning by an interpreter which gives the respective result. The interpreter used here is called a tree walking interpreter. An object is used for the interpreter; it is not very performant, but it's easy to get started with.

Supported Features

  • C-like syntax
  • variable bindings
  • integers and booleans
  • arithmetic expressions
  • built-in functions
  • first-class and higher-order functions
  • closures

Examples

Variable binding
let age = 1;
let name = "Trent";
let result = 10 * (20 / 2);
Accessing Elements
let myArray = [1, 2, 3, 4, 5];
myArray[0]       // => 1

let thorsten = {"name": "Thorsten", "age": 28};
thorsten["name"] // => "Thorsten"
Function Binding
let add = fn(a, b) { return a + b; };
Implicit Returns
let add = fn(a, b) { a + b; };
Function Call
add(1, 2);
Complex Function
let fibonacci = fn(x) {
  if (x == 0) {
    0
  } else {
    if (x == 1) {
      1
    } else {
      fibonacci(x - 1) + fibonacci(x - 2);
    }
  }
};
Higher Order Functions
let twice = fn(f, x) { return f(f(x)); };

let addTwo = fn(x) { return x + 2; };

twice(addTwo, 2); // => 6
Closures
// newGreeter returns a new function, that greets a `name` with the given
// `greeting`.
let newGreeter = fn(greeting) {
  // `puts` is a built-in function we add to the interpreter
  return fn(name) { puts(greeting + " " + name); }
};

// `hello` is a greeter function that says "Hello"
let hello = newGreeter("Hello");

// Calling it outputs the greeting:
hello("dear, future Reader!"); // => Hello dear, future Reader!

Garbage Collection

The host language for jaba is Golang, which does the garbage collection and allows the memory not to leak when we run code like this

let counter = fn(x) {
  if (x > 100) {
    return true;
  } else {
    let foobar = 9999;
    counter(x + 1); 
  }
}; 

counter(0);

If C was used to write the interpreter, we would have to implement our own garbage collector to avoid memory leaks. An example of a garbage collector implementation is Mark and Sweep.

References

Writing An Interpreter In Go
  • Ball, Thorsten. Writing An Interpreter In Go (p. 8). Kindle Edition.
  • Ball, Thorsten. Writing An Interpreter In Go (pp. 8-9). Kindle Edition.
  • Ball, Thorsten. Writing An Interpreter In Go (p. 9). Kindle Edition.
  • Ball, Thorsten. Writing An Interpreter In Go (p. 13). Kindle Edition.
  • Ball, Thorsten. Writing An Interpreter In Go (pp. 189-190). Kindle Edition.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg
ast
evaluator
* Package evaluator uses the object system to evaluate the AST
* Package evaluator uses the object system to evaluate the AST
object
* Package object helps represent the values encountered when evaluating the jaba program as an object.
* Package object helps represent the values encountered when evaluating the jaba program as an object.
parser
* Package parser is responsible for transforming tokens into an abstract syntax tree (AST) * The parser will be a recursive descent parser, in particular the top down operator precedence parser (Pratt Parser).
* Package parser is responsible for transforming tokens into an abstract syntax tree (AST) * The parser will be a recursive descent parser, in particular the top down operator precedence parser (Pratt Parser).
repl
* Package repl (Read Eval Print Loop) or console is used to "Read" the input, * sends it to the interpreter for "Evaluation", "Prints" the output of the interpreter, and then repeats the process("Loop").
* Package repl (Read Eval Print Loop) or console is used to "Read" the input, * sends it to the interpreter for "Evaluation", "Prints" the output of the interpreter, and then repeats the process("Loop").
token
* Package token is used to represent original source code.
* Package token is used to represent original source code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL