Skip to content

dev-kas/virtlang-go

Repository files navigation

Project Tests Passing   Project License   GitHub Repo stars   GitHub Release

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:

  1. Lexing (lexer.Tokenize()) — converts source code into tokens
  2. Parsing (parser.ProduceAST()) — builds an Abstract Syntax Tree (AST)
  3. 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

Repobeats analytics image for virtlang-go

About

Golang implementation of @dev-kas/VirtLang, originally written in TypeScript.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages