VirtLang
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.
π Overview
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 (like let, while, if)
Expr β return-producing expressions (like 1 + 2, "hello")
Runtime is powered by:
- A unified
RuntimeValue type system
- Scoped environments for variable resolution
- Support for both native and user-defined functions
π§ Language Features
let and const declarations
- 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
π§ͺ Getting Started
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)
}
π Documentation
π€ Contributing
Found a bug? Want to add a feature? See CONTRIBUTING.md for guidelines.
π Analytics