VirtLang is a custom-built programming language interpreter written in Go, designed for extensibility, clarity, and hackability. It powers a full-language runtime with native support for variables, functions, control flow, error handling, and complex types.
VirtLang features a clean three-stage architecture:
- Lexing (
lexer.Tokenize()) — converts source code into tokens - Parsing (
parser.ProduceAST()) — builds an Abstract Syntax Tree (AST) - Evaluation (
evaluator.Evaluate()) — runs the AST and produces results
The AST separates logic into:
Stmt→ non-returning statements (likelet,while,if)Expr→ return-producing expressions (like1 + 2,"hello")
Runtime is powered by:
- A unified
RuntimeValuetype system - Scoped environments for variable resolution
- Support for both native and user-defined functions
letandconstdeclarations- Functions (closures, parameters, call support)
- Control flow:
if,else,while - Structured error handling:
try,catch - Rich type system: numbers, strings, booleans, arrays, objects
- Member access (
obj.key) and array indexing (arr[i]) - Binary, logical, and comparison operators
Here's a minimal example showing how to evaluate VirtLang code in Go:
package main
import (
"fmt"
"github.com/dev-kas/virtlang-go/v4/environment"
"github.com/dev-kas/virtlang-go/v4/evaluator"
"github.com/dev-kas/virtlang-go/v4/parser"
)
func main() {
code := `let n = 1
while (n < 10) {
n = n + 1
}
n // output: 10`
// Set up parser and global environment
p := parser.New("demo")
env := environment.NewEnvironment(nil)
// Parse source code into AST
program, err := p.ProduceAST(code)
if err != nil {
fmt.Printf("Syntax error: %v\n", err)
return
}
// Evaluate AST
result, runErr := evaluator.Evaluate(program, env, nil)
if runErr != nil {
fmt.Printf("Runtime error: %v\n", runErr)
return
}
// Display result
fmt.Printf("Result: %v (Type: %v)\n", result.Value, result.Type)
}- Auto-generated Go package docs:
DOCS.md - Full design write-up and architecture: VirtLang Wiki
Found a bug? Want to add a feature? See CONTRIBUTING.md for guidelines.