Skip to content

SethGK/Inscript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inscript

A stack-based bytecode compiled programming language implemented in Go.

Features

  • Stack-based bytecode compilation
  • Dynamic typing with optional type annotations
  • Functions with default parameters
  • Control flow constructs (if/else, while, for-in loops)
  • Built-in data structures (lists and tables)
  • Basic module system

Example Programs

Basic Syntax

# Variables and types
name = "Alice"
age = 25
scores = [95, 87, 92]
person = { name = "Bob", age = 30 }

# Functions
function greet(name, title = "Hello") {
    return title + ", " + name + "!"
}

print(greet("Alice"))

Control Flow

function check_grade(score) {
    if score >= 90 {
        return "A"
    } else if score >= 80 {
        return "B"
    } else {
        return "C"
    }
}

# While loop with break/continue
count = 0
while count < 10 {
    count += 1
    if count % 2 == 0 {
        continue
    }
    print("Odd:", count)
}

Data Structures

# Process a list of students
students = [
    { name = "Alice", grade = 85 },
    { name = "Bob", grade = 92 },
    { name = "Charlie", grade = 78 }
]

function calculate_class_average(student_list) {
    total = 0
    for student in student_list {
        total += student.grade
    }
    return total / len(student_list)
}

average = calculate_class_average(students)
print("Class average:", average)

Recursive Functions

function factorial(n) {
    if n <= 1 {
        return 1
    }
    return n * factorial(n - 1)
}

function fibonacci(n) {
    if n <= 1 {
        return n
    }
    return fibonacci(n - 1) + fibonacci(n - 2)
}

print("5! =", factorial(5))
print("fib(8) =", fibonacci(8))

Type Annotations (Optional)

function calculate_area(radius: number) -> number {
    return 3.14159 * radius ^^ 2
}

function format_name(first: string, last: string) -> string {
    return first + " " + last
}

Language Overview

Data Types

  • Numbers (integers and floats)
  • Strings (single, double, or triple-quoted)
  • Booleans (true, false)
  • Lists ([1, 2, 3])
  • Tables/Maps ({ key = value })
  • nil (null value)

Operators

  • Arithmetic: +, -, *, /, // (integer division), %, ^^ (power)
  • Comparison: ==, !=, <, <=, >, >=
  • Logical: and, or, not
  • Assignment: =, +=, -=, *=, /=, ^^=

Control Structures

  • if/else statements
  • while loops
  • for/in loops
  • break and continue
  • return statements

Implementation

The compiler pipeline is:

  1. Lexing with ANTLR4
  2. Parsing to AST
  3. Symbol table construction for scoping and parameters
  4. Bytecode generation for a stack-based opcodesz
  5. Execution on a custom virtual machine

Usage

# Run a program
./inscript program.ins

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published