Skip to content

kirochk4/goloxx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goloxx

goloxx is a Go implementation of the Lox programming language interpreter based on clox from Crafting Interpreters.

goloxx features

  • REPL and file execution
  • Non global Scanner, Parser, Compiler and VM
  • Loxx language
  • Embeddable

Loxx

Loxx is a superset of the Lox language (like C++ and C).

Loxx features

  • Better print statement
  • break and continue statements in loops
  • Ternary expression cond ? then : else
  • Import modules import myModule
  • Classes for all types "abc".reverse()
  • 0x, 0o and 0b number literals
  • Underscores in number literals
  • Auto semicolons
  • Escape sequences in strings
  • Arrays [0, "1", nil]
  • Maps {"key": "value"}
  • Box type (userdata)
  • Thread type functions (coroutine)
  • var (mutable) and const (immutable) declarations

Thread function

fun inc(i) {
    if (i > 5) return false
    else yield true

    while (i < 5) i = i + yield i
    if (i >= 5) return i
}

const seq = []
if (inc(1))
    while (inc.status() != "done")
        seq.append(resume inc with 1)

print seq // <array 1, 2, 3, 4, 5>

Embedding

Go

import "github.com/kirochk4/goloxx/loxx"

events := make(map[string]*loxx.Closure)
clicks := 0

// create vm instance
var vm *loxx.VM = loxx.New(".")

// define native function
vm.Globals["addEvent"] = loxx.NewNativeFunction(
    2, func(this loxx.Value, args []loxx.Value) (loxx.Value, error) {
        name := args[0].(loxx.String)
        fun := args[1].(*loxx.Closure)
        events[string(name)] = fun
        return loxx.Nil{}, nil
    },
)

// define outer reference
vm.Globals["outer"] = loxx.Box{
    "clicks": &clicks,
}

// load and interpret your script
source, _ := os.OpenFile("./your_script.loxx")
vm.Interpret(source)

// call added event function
vm.Call(events["onclick"])

Loxx

fun onClick() {
    print "click"

    // this will change outer value
    outer.clicks = outer.clicks + 1
}

addEvent("onclick", onClick)

Scripts

Use this script to build loxx.

Use this script to run tests.

Use this script to run benchmarks.

About

Bytecode interpreter for the Loxx (Lox superset) programming language.

Resources

License

Stars

Watchers

Forks