Getting Started - ThornLang/JavaThorn GitHub Wiki

Getting Started with ThornLang

This guide will help you install ThornLang and write your first program.

Table of Contents

Prerequisites

ThornLang requires:

  • Java 11 or higher
  • Git (for cloning the repository)
  • A text editor or IDE

Installation

1. Clone the Repository

git clone https://github.com/brodycritchlow/ThornLang.git
cd ThornLang

2. Build ThornLang

./scripts/build.sh

This creates the compiled Java classes in the appropriate directories.

3. Verify Installation

java com.thorn.Thorn --version

You should see the ThornLang version information.

Hello World

Create a file named hello.thorn:

// My first Thorn program
print("Hello, World!");

Run it:

java com.thorn.Thorn hello.thorn

Output:

Hello, World!

Running Programs

Basic Execution

java com.thorn.Thorn yourscript.thorn

Using the VM for Better Performance

java com.thorn.Thorn yourscript.thorn --vm

The --vm flag uses the bytecode virtual machine instead of the tree-walking interpreter, which is faster for compute-intensive programs.

Viewing the AST (for debugging)

java com.thorn.Thorn yourscript.thorn --ast

This displays the Abstract Syntax Tree, useful for understanding how your code is parsed.

REPL Usage

ThornLang includes an interactive REPL (Read-Eval-Print Loop) for experimentation.

Starting the REPL

java com.thorn.Thorn

You'll see:

ThornLang REPL - Type 'exit' to quit
thorn>

REPL Examples

thorn> x = 42
42
thorn> y = x * 2
84
thorn> print("x = " + x + ", y = " + y)
x = 42, y = 84
nil
thorn> $ double(n) => n * 2
<function double>
thorn> double(21)
42
thorn> exit

REPL Features

  • Multi-line input: The REPL automatically detects incomplete statements
  • Expression evaluation: Type any expression to see its value
  • Function definitions: Define functions interactively
  • Variable persistence: Variables remain available throughout the session

Command Line Options

Option Description Example
(none) Start REPL java com.thorn.Thorn
filename Run a script java com.thorn.Thorn script.thorn
--vm Use bytecode VM java com.thorn.Thorn script.thorn --vm
--ast Show AST java com.thorn.Thorn script.thorn --ast
--version Show version java com.thorn.Thorn --version

Your First Program

Let's write a more complex program to explore ThornLang features:

// greet.thorn - A simple greeting program

// Define a greeting function
$ greet(name: string, time: string): string {
    greeting = "";
    
    // Pattern matching on time of day
    greeting = match time {
        "morning" => "Good morning",
        "afternoon" => "Good afternoon",
        "evening" => "Good evening",
        _ => "Hello"
    };
    
    return greeting + ", " + name + "!";
}

// Get current hour (in a real program, you'd get actual time)
hour = 14; // 2 PM

// Determine time of day
timeOfDay = "afternoon";
if (hour < 12) {
    timeOfDay = "morning";
} else if (hour < 18) {
    timeOfDay = "afternoon";
} else {
    timeOfDay = "evening";
}

// Create an array of names
names = ["Alice", "Bob", "Charlie"];

// Greet everyone
for (name in names) {
    message = greet(name, timeOfDay);
    print(message);
}

// Using a lambda function
names.forEach($(name) => {
    print("Welcome, " + name + "!");
});

Save this as greet.thorn and run:

java com.thorn.Thorn greet.thorn

Output:

Good afternoon, Alice!
Good afternoon, Bob!
Good afternoon, Charlie!
Welcome, Alice!
Welcome, Bob!
Welcome, Charlie!

Directory Structure

After installation, your ThornLang directory contains:

ThornLang/
├── src/           # Java source code
├── examples/      # Example Thorn programs
├── benchmarks/    # Performance benchmarks
├── tests/         # Test files
├── scripts/       # Build and utility scripts
└── wiki/          # Documentation

Troubleshooting

Common Issues

  1. "Class not found" error

    • Make sure you've run ./scripts/build.sh
    • Check that you're in the ThornLang directory
  2. "Java not found" error

    • Verify Java 11+ is installed: java -version
    • Check your PATH includes Java
  3. Script permission denied

    • Make build script executable: chmod +x scripts/build.sh

Next Steps

Now that you have ThornLang running:

  1. Explore the Language: Read the Language Reference for comprehensive syntax documentation
  2. Learn Types: Check the Type System guide for type annotations
  3. See Examples: Browse the examples/ directory for more code samples
  4. Performance: Read the Performance Guide to optimize your programs
  5. Contribute: Check our GitHub repository to contribute

Quick Reference Card

// Variables
name = "Thorn";
age: number = 25;
@immut PI = 3.14159;

// Functions  
$ add(a: number, b: number): number {
    return a + b;
}

// Lambdas
double = $(x) => x * 2;

// Arrays
nums = [1, 2, 3];
nums.push(4);

// Objects
person = {
    "name": "Alice",
    "age": 30
};

// Classes
class Point {
    $ init(x: number, y: number) {
        x = x;
        y = y;
    }
}

// Control Flow
if (condition) {
    // code
}

for (i = 0; i < 10; i += 1) {
    // code
}

// Pattern Matching
result = match value {
    0 => "zero",
    n if n > 0 => "positive",
    _ => "negative"
};

Happy coding with ThornLang! 🚀