Skip to content

Interpreter implementation for Tableng. Learning project

Notifications You must be signed in to change notification settings

ShaJaPas/Tableng

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Tableng

Overview

This project contains of:

  • Parser, for parsing languge constructions into AST
  • Bytecode compiler, which compiles AST to bytecode
  • Register-based VM for executing bytecode

Code example

// Function defined in rust interpreter
extern fn print(x: any)

// Recursive fibonacci
fn fib(n: int) -> int {
    if n < 2 {
        return n
    } else {
        return fib(n - 2) + fib(n - 1)
    }
}

// Function to calculate Collatz conjecture sequence length
fn calc(index: int) -> int {
    var step = 0
    while index > 1 {
        if index % 2 == 0 {
            index = index / 2
            step = step + 1
        } else {
            index = (3 * index + 1) / 2
            step = step + 2
        }
    }

    return step
}

var result = 0

// Find the longest sequence in [1, 1000000]
for x in 1..1000000 {
    var e = calc(x)
    if e > result {
        result = e
    }
}

print(result)

// Calc 30 fibonacci number
print(fib(30))

// All complex data types are tables
var t = {1, 2, 3}

t[0] = {2: "2"}

t[1] = fib

print(t)

About

Interpreter implementation for Tableng. Learning project

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages