goblin

command module
v0.0.0-...-8302c90 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 14 Imported by: 0

README

goblin

logo

Toy language built for fun.

Read the Goblin Book for installation, language features, standard-library APIs, and examples.

Installation

$ go install github.com/aisk/goblin@latest

Hello world

$ cat hello.goblin
print("Hello, world!")

$ goblin run hello.goblin
Hello, world!

To compile a source file to a native executable:

$ goblin build-exe hello.goblin
$ ./hello
Hello, world!

Learn Goblin in 5 Minutes

Goblin is a dynamically-typed language. The CLI can interpret a source file directly with goblin run, or transpile and compile it with goblin build-exe.

# Comments start with #

# Variables: int, float, string, bool, nil
var name = "Goblin"
var age = 1
var pi = 3.14
var cool = true
var nothing = nil

# Reassignment
age = 2

# Arithmetic (supports mixed int/float)
print(1 + 2 * 3)          # 7
print("ha" * 3)           # hahaha
print("hello" + " world") # hello world

# Comparisons return booleans; && and || short-circuit to an operand
print(1 < 2 && !false)    # true
print(0 || "fallback")    # fallback

# Control flow
if age > 1 {
    print(name, "is growing!")
} else if age == 1 {
    print("just born")
} else {
    print("not yet")
}

var i = 0
while i < 3 {
    i = i + 1
}

for x in [1, 2, 3] {
    print(x)
}

for i in range(0, 5) {
    print(i)
}

# Strings
var s = "hello"
print(s.size)   # 5
print(s.upper())  # HELLO
print(s.contains("ell"))

# Lists
var list = [1, 2, 3]
print(list[0])    # 1
list.push(4)
print(list.pop()) # 4
print(list.size)  # 3

# Dictionaries
var d = {"name": "Alice", "age": 30}
print(d["name"])  # Alice
d["city"] = "Paris"
print(d.size)   # 3

# Functions are first-class
func add(a, b) {
    return a + b
}
print(add(1, 2))  # 3

func apply(f, a, b) {
    return f(a, b)
}
print(apply(add, 3, 4))  # 7

# Function calls support positional, keyword, *args, and **kwargs
print(add(5, 6))       # 11
print(add(a=5, b=6))   # 11

func collect(prefix, *args, **kwargs) {
    print(prefix, args.size, kwargs.size)
}
collect("n=", 1, 2, 3)
collect("n=", *range(0, 2))
collect(prefix="n=", **{"flag": true})

# Custom Types
type User(name, age=18) {
    func hello(self) {
        print(self.name)
    }
}

var alice = User("alice")
print(alice.name)   # "alice"
print(alice.age)    # 18
alice.hello()

var bob = User(name="bob", age=20)
print(bob.age)      # 20

# Error handling: raise an Error, recover it with try/catch
func checked_div(a, b) {
    if b == 0 {
        raise ZeroDivisionError.wrap("checked_div")
    }
    return a / b
}
try {
    checked_div(1, 0)
} catch e {
    print(e.message)               # checked_div: ZeroDivisionError
    print(e.is(ZeroDivisionError)) # true
}

# Errors are values built with Error(); wrap adds context, unwrap/is inspect the chain
var not_found = Error("not found")
var err = not_found.wrap("loading config")
print(err.message)          # loading config: not found
print(err.unwrap().message) # not found
print(err.is(not_found))    # true

# Predefined kinds are hierarchical: IndexError is a LookupError,
# ZeroDivisionError is an ArithmeticError, ParseError is a ValueError,
# and OS/network failures are IOError subclasses.
try {
    var x = [1, 2, 3][9]
} catch e {
    print(e.is(IndexError))  # true
    print(e.is(LookupError)) # true
}

# Built-ins include print, eprint, range, max, min, spawn, Error and typed constructors
print(max(1, 2, 3))  # 3
print(min(1, 2.5))   # 1
print(Int("42"))     # 42
print(Str(nil))      # nil

# Concurrency uses Chan plus spawn()
var ch = Chan(0)
spawn(func() {
    ch.send("done")
})
print(ch.recv())     # done
ch.close()

# Standard modules must be imported before use
import "os"
import "json"

os.getenv("HOME")
os.getpid()
print(json.unmarshal("42"))

# Export
export name
export add

More examples are in the examples/ directory. They are executable tests, so they are the best source for exact output. For local module imports, see examples/module_import.goblin.

Grammar

Take a look at goblin.bnf.

About the Project

Goblin is © 2023-2026 by AN Long.

License

Goblin is distributed by a MIT license.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
bench
callbacks command
Functions as values: closures, composition, and list helpers that call back into a function for every element.
Functions as values: closures, composition, and list helpers that call back into a function for every element.
fib command
Naive recursive Fibonacci: measures function call overhead.
Naive recursive Fibonacci: measures function call overhead.
hanoi command
Towers of Hanoi: deep recursion with three arguments, moves are counted instead of printed so the output stays small.
Towers of Hanoi: deep recursion with three arguments, moves are counted instead of printed so the output stays small.
logparse command
Log line parsing: string building, trimming, splitting, prefix tests and integer conversion, with the results counted in a map.
Log line parsing: string building, trimming, splitting, prefix tests and integer conversion, with the results counted in a map.
mandelbrot command
Mandelbrot set rendered as ASCII: a float-heavy inner loop.
Mandelbrot set rendered as ASCII: a float-heavy inner loop.
matmul command
Dense matrix multiplication: nested loops over indexed 2-D slices.
Dense matrix multiplication: nested loops over indexed 2-D slices.
nqueens command
N-Queens: recursive backtracking with an O(row) safety check.
N-Queens: recursive backtracking with an O(row) safety check.
objects command
User-defined types: construction, method calls, field reads and writes, and an Add method standing in for Goblin's overloaded operator.
User-defined types: construction, method calls, field reads and writes, and an Add method standing in for Goblin's overloaded operator.
sieve command
Sieve of Eratosthenes: tight loops over a large flat array.
Sieve of Eratosthenes: tight loops over a large flat array.
wordfreq command
Word frequency counting: string split, map updates, and a sort with a key.
Word frequency counting: string split, map updates, and a sort with a key.
csv
fs
internal/archive
Package archive holds the plumbing shared by the x/archive modules: the ordered entry list, argument parsing, and the buffer-or-dest sink.
Package archive holds the plumbing shared by the x/archive modules: the ordered entry list, argument parsing, and the buffer-or-dest sink.
internal/compress
Package compress holds the shared plumbing of the whole-value compression modules under x/compress: argument parsing, the common module member set, and the buffer-or-dest write path.
Package compress holds the shared plumbing of the whole-value compression modules under x/compress: argument parsing, the common module member set, and the buffer-or-dest write path.
internal/digest
Package digest holds the argument plumbing shared by the digest and checksum stdlib modules (x/crypto/* and x/hash/*).
Package digest holds the argument plumbing shared by the digest and checksum stdlib modules (x/crypto/* and x/hash/*).
internal/modtest
Package modtest holds the helpers shared by the stdlib module package tests.
Package modtest holds the helpers shared by the stdlib module package tests.
os
regexp
Package regexp exposes Go's RE2-based regular expression engine to Goblin.
Package regexp exposes Go's RE2-based regular expression engine to Goblin.
url
Package url adapts Go's net/url package to Goblin.
Package url adapts Go's net/url package to Goblin.
x/net/netip
Package netip adapts Go's net/netip value types to Goblin.
Package netip adapts Go's net/netip value types to Goblin.
Package interpreter provides a tree-walking interpreter for Goblin.
Package interpreter provides a tree-walking interpreter for Goblin.
Package source wraps the generated lexer constructors so every way of loading Goblin code goes through the same normalization.
Package source wraps the generated lexer constructors so every way of loading Goblin code goes through the same normalization.

Jump to

Keyboard shortcuts

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