gofu aims to provide a flexible toolkit for creating custom scripting languages in Go.

gofu

a scripting language toolkit in Go

intro

gofu aims to provide a flexible toolkit for creating custom scripting languages in Go.

functions

Functions have a name, an argument list, a result list and a body.

p := gofu.Pos("Test", -1, -1)

add := gofu.Func("+", []gofu.Type{types.Int(), types.Int()}, []gofu.Type{types.Int()},
	func(pos gofu.TPos, thread *gofu.TThread, _func *gofu.TFunc, pc *int) error {
		stack := thread.Stack()
		stack.Push(types.Int(), stack.Pop().Value().(int) + stack.Pop().Value().(int))
		return nil
	})

scope := gofu.Scope()	
scope.BindSlot("+", types.Func(), add)

block := gofu.Block()
c := forms.Call(p, forms.Id(p, "+"), forms.Literal(p, types.Int(), 35), forms.Literal(p, types.Int(), 7))
c.Compile(scope, block)
block.Emit(ops.Stop())

thread := gofu.Thread(scope)
block.Run(thread, 0)

The same thing could be accomplished by manually emitting operations.

block.Emit(ops.Push(types.Int(), 35))
block.Emit(ops.Push(types.Int(), 7))
block.Emit(ops.Call(p, add))
block.Emit(ops.Stop())

fimps.Compile may be used to compile function bodies.

fimp, err := fimps.Compile(forms.Literal(p, []gofu.Type{types.Int()}, 42), block)
fortyTwo := gofu.Func("fortyTwo", nil, []gofu.Type{types.Int()}, fimp)
scope.BindSlot("fortyTwo", types.Func(), f)

multiple dispatch

The following example will dispatch to the right function based on the argument and push "Bool!" on the stack.

f1 := gofu.Func("foo", []gofu.Type{types.Bool()}, []gofu.Type{types.Int()},
    func(pos gofu.TPos, thread *gofu.TThread, _func *gofu.TFunc, pc *int) error {
	    stack := thread.Stack()
	    stack.Pop()
	    stack.Push(types.String(), "Bool!")
	    return nil
    })

f2 := gofu.Func("foo", []gofu.Type{types.Int()}, []gofu.Type{types.Int()},
    func(pos gofu.TPos, thread *gofu.TThread, _func *gofu.TFunc, pc *int) error {
	    stack := thread.Stack()
	    stack.Pop()
	    stack.Push(types.String(), "Int!")
	    return nil
    })

m := gofu.Multi("foo", 1, f1, f2)
block.Emit(ops.Push(types.Bool(), true))
block.Emit(ops.Call(p, m))
block.Emit(ops.Stop())	

macros

Macros are called at compile time and may emit different code depending on arguments.

scope.BindSlot("reset",
	types.Macro(),
	gofu.Macro("reset", 0,
		func(pos gofu.TPos, args []gofu.Form, scope *gofu.TScope, block *gofu.TBlock) error {
			block.Emit(ops.Reset())
			return nil
		}))

types

The following list of types are provided but optional, anything implementing gofu.Type may be used as a type.

  • Any: Any - Anything
  • Bool: Any - t/f
  • Char: Any - Characters
  • Func: Any Target - Functions
  • Int: Any Num - Integers
  • Maybe[T]: Any - Contains T or Nil
  • Meta: Any - The type of types
  • Multi: Any Target - Multimethods
  • Nil - Nothing, it's only value being _
  • Num: Any - Parent of all numbers
  • Seq[T]: Any - Parent of all sequences
  • Stack[T]: Any Seq[T] - Stacks of values
  • String: Any Seq[Char] - Strings
  • Target: Any - Callable values

repl

A primitive REPL is provided, it reads one form at a time and prints the stack after each evaluation.

$ cd bin
$ ./mk
$ ./repl
gofu v1
  +(35 7)
[42]

Parens may be used to group forms.

  (1 2 3)
[1 2 3]

The stack may be directly modified using d and reset.

  (1 2 3)
[1 2 3]
  d
[1 2]
  reset
[]

Code may be executed conditionally using if.

  if(t 1 2)
[1]
  if(f 3 4)
[1 4]

Functions may be called by suffixing names with argument lists.

  +(35 7)
[42]

New functions may be defined using func.

  func(foo () (Int) 42)
[]
  foo
[42]

Values may be bound to identifiers using bind.

  bind(foo 42)
[]
  foo
[42]

The REPL is heavily parameterized and assumes very little about the actual language.

scope := gofu.Scope()
block := gofu.Block()
thread := gofu.Thread(scope)
utils.Repl(scope, parsers.Any(), block, thread)
Owner
Andreas Nilsson
I am by nature a dealer in words, and words are the most powerful drug known to humanity.
Andreas Nilsson
Similar Resources

The project aims to provide the utility for Kunlun-Cluster aka KTS

Kunlun Tool Set Description This project aims to provide the utility for Kunlun-Cluster aka 'KTS' Including but not limited to backup/restore tools...

Jul 1, 2022

Dokvs - This project aims to provide document-store capabilities, built over key-value persisted backends

This project aims to provide document-store capabilities, built over key-value persisted backends. It is built in Go using the new generics features to expose a friendly, type-safe API.

Apr 25, 2022

Arche - Smart Hybrid Workforce Manager: A system that aims to provide companies an easy to use platform for managing company resources by allowing employees to book company spaces and resources.

Arche - Smart Hybrid Workforce Manager: A system that aims to provide companies an easy to use platform for managing company resources by allowing employees to book company spaces and resources.

Description Smart Hybrid Workforce Manager is a system that aims to provide companies an easy to use system for managing company resources by allowing

Dec 8, 2022

Go network programming framework, supports multiplexing, synchronous and asynchronous IO mode, modular design, and provides flexible custom interfaces

Go network programming framework, supports multiplexing, synchronous and asynchronous IO mode, modular design, and provides flexible custom interfaces

Go network programming framework, supports multiplexing, synchronous and asynchronous IO mode, modular design, and provides flexible custom interfaces。The key is the transport layer, application layer protocol has nothing to do

Nov 7, 2022

Tpf2-tpnetmap-toolkit - A toolkit to create svg map images from TransportFever2 world data

Tpf2-tpnetmap-toolkit - A toolkit to create svg map images from TransportFever2 world data

tpf2-tpnetmap-toolkit TransportFever2 のワールドデータから svg のマップ画像を作成するツールキットです。 1. 導入方

Feb 17, 2022

A curated Golang toolkit for creating Bitcoin SV powered apps

A curated Golang toolkit for creating Bitcoin SV powered apps

bsv A curated Golang toolkit for creating Bitcoin SV powered apps Table of Contents Installation Maintainers License Installation bsv requires a suppo

May 10, 2022

A bytecode-based virtual machine to implement scripting/filtering support in your golang project.

eval-filter Implementation Scripting Facilities Types Built-In Functions Conditionals Loops Functions Case/Switch Use Cases Security Denial of service

Dec 30, 2022

Golem is a general purpose, interpreted scripting language.

Golem is a general purpose, interpreted scripting language.

The Golem Programming Language Golem is a general purpose, interpreted scripting language, that brings together ideas from many other languages, inclu

Sep 28, 2022

Scripting language for Go.

Minima Minima is an experimental interpreter written in Go (the language is called the same). We needed a way to massage our JSON data with a scriptin

Feb 11, 2022

do-nothing scripting framework

donothing donothing is a Go framework for do-nothing scripting. Do-nothing scripting is an approach to writing procedures. It allows you to start with

Dec 19, 2022

A bytecode-based virtual machine to implement scripting/filtering support in your golang project.

eval-filter Implementation Scripting Facilities Types Built-In Functions Conditionals Loops Functions Case/Switch Use Cases Security Denial of service

Jan 8, 2023

miscellaneous useful commands, including 'gosh' the Go scripting tool

utilities Miscellaneous useful commands. gosh This is a tool for running Go code from the command line. See here. findCmpRm This finds files with copi

Oct 31, 2022

Gola is a Golang tool for automated scripting purpose

Gola Gola is a Golang tool for automated scripting purpose. How To Install You can find the install script here. Example Configuration commands: - n

Aug 12, 2022

Go specs implemented as a scripting language in Rust.

Goscript A script language like Python or Lua written in Rust, with exactly the same syntax as Go's. The Goal Runs most pure Go code, probably add som

Jan 8, 2023

Kakoune syntax highlighting for the Godot Engine / Godot Scripting Language gdscript

Kakoune syntax highlighting for the Godot Engine / Godot Scripting Language gdscript

gdscript-kak Kakoune syntax highlighting for the Godot Engine / Godot Scripting Language gdscript. Adds basic syntax highlighting to your .gd files fo

Mar 2, 2021

Toy scripting language with a syntax similar to Rust.

Dust - toy scripting language Toy scripting language with a syntax similar to Rust. 👍 Syntax similar to Rust 👍 Loose JSON parsing 👍 Calling host fu

Sep 28, 2022

Gopherscript is a secure and minimal scripting language written in Go.

Gopherscript is a secure and minimal scripting language written in Go.

Gopherscript Gopherscript is a secure scripting/configuration language written in Go. It features a fined-grain permission system and enforces a stron

Oct 2, 2022

Source code editor written in Go using go-gtk bindings. It aims to handle navigation effectively among large number of files.

Source code editor written in Go using go-gtk bindings. It aims to handle navigation effectively among large number of files.

tabby Source code editor written in Go using go-gtk bindings. It aims to handle navigation effectively among large number of files. screenshot: depend

Nov 16, 2022

This is a Golang wrapper for working with TMDb API. It aims to support version 3.

This is a Golang wrapper for working with TMDb API. It aims to support version 3.

This is a Golang wrapper for working with TMDb API. It aims to support version 3. An API Key is required. To register for one, head over to themoviedb

Dec 27, 2022
Related tags
go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

Jan 2, 2023
go-sundheit:A library built to provide support for defining service health for golang services
go-sundheit:A library built to provide support for defining service health for golang services

A library built to provide support for defining service health for golang services. It allows you to register async health checks for your dependencies and the service itself, and provides a health endpoint that exposes their status.

Dec 27, 2022
utf8 - provide unicode information on runes

utf8 utf8 provides unicode code point values for input runes and the unicode rune (if printable) for a given unicode code point. With no arguments, pr

Jan 8, 2022
Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite
Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Jan 5, 2023
Magma: Gives network operators an open, flexible and extendable mobile core network solution
Magma: Gives network operators an open, flexible and extendable mobile core network solution

Connecting the Next Billion People Magma is an open-source software platform tha

Dec 24, 2021
LogAnalyzer - Analyze logs with custom regex patterns.Can search for particular patterns on multiple files in a directory.
LogAnalyzer - Analyze logs with custom regex patterns.Can search for particular patterns on multiple files in a directory.

LogAnalyzer Analyze logs with custom regex patterns.Can search for particular patterns on multiple files in a directory

May 31, 2022
a tool for creating exploited media files for discord

Discord-Exploits A program for creating exploited media files for discord written in Go. Usage discord-exploits is a command line utility, meaning you

Dec 29, 2021
Simple utilities for creating ascii text in Go

Simple utilities for creating ascii text in Go

Oct 30, 2021
Show Languages In Code. A fast and lightweight CLI to generate stats on the languages inside your project
Show Languages In Code. A fast and lightweight CLI to generate stats on the languages inside your project

slic Show Languages In Code. Usage Run it with an -h flag to list all commands. -d flag can be used to specify the directory of search -i flag can be

Dec 25, 2021
hego aims to provide a consistent API for several metaheuristics

hego hego aims to provide a consistent API for several metaheuristics (black box optimization algorithms) while being performant. Even though most of

Dec 24, 2022