Skip to content

hamer1818/TulparLang

Repository files navigation

TulparLang

Version Build License Platform

A statically-typed programming language with LLVM backend, UTF-8 support, and native JSON syntax.


Overview

TulparLang is a modern programming language built in C with an LLVM-18 backend for native code generation. It combines the simplicity of dynamic languages with the performance of compiled languages.

Key characteristics:

  • Static typing with type inference
  • First-class JSON support
  • UTF-8 native strings
  • LLVM AOT compilation
  • Cross-platform (Linux, macOS; Windows via WSL)

Performance

Benchmark results comparing total execution time across 11 algorithmic tests (lower is better):

Rank Language Total Time (ms) Relative to C Relative to Tulpar
🥇 C (gcc -O3) 4.05 ms 1.0x 2.26x faster
🥈 Tulpar (AOT) 9.17 ms 2.26x slower Reference
🥉 Rust (1.80) 9.27 ms 2.29x slower 1.01x slower
4 Go (1.23) 17.33 ms 4.28x slower 1.89x slower
5 JavaScript (Node) 22.16 ms 5.47x slower 2.42x slower
6 PHP 8.3 197.04 ms 48.6x slower 21.48x slower
7 Python 3.12 365.67 ms 90.2x slower 39.87x slower

Note: Tulpar's AOT compiler (LLVM-backed) performs neck-and-neck with Rust, demonstrating high efficiency for algorithmic tasks.

Tulpar achieves near-C performance while being significantly faster than interpreted languages like Python and PHP.

Installation

Prerequisites

  • GCC or Clang
  • LLVM 18+
  • CMake 3.14+

Build from Source

git clone https://github.com/hamer1818/TulparLang.git
cd TulparLang
mkdir build && cd build
cmake ..
make

Quick Install (Linux/macOS)

./build.sh

Windows (via WSL)

Windows users should use WSL (Windows Subsystem for Linux) to build and run TulparLang:

# Inside WSL (Ubuntu):
sudo apt-get install build-essential cmake llvm-18-dev
cd /mnt/c/path/to/TulparLang
./build.sh

Quick Start

Create hello.tpr:

str message = "Hello, World!";
print(message);

func square(int n) {
    return n * n;
}

print(square(5));  // Output: 25

Run:

./tulpar hello.tpr

Command Line Usage

Running Scripts (Default: Native AOT Speed)

tulpar script.tpr           # Fastest: AOT compile & run (native speed)
tulpar --vm script.tpr      # VM mode: instant start, good for development

By default, tulpar transparently compiles your code to a native binary via LLVM and runs it. This gives you C-like speed with Python-like simplicity - no extra flags needed.

Building Standalone Executables

Compile your Tulpar code into a standalone native binary for distribution:

tulpar build script.tpr         # Creates 'script' binary
tulpar build script.tpr myapp   # Custom output name

Interactive REPL

tulpar --repl

Language Features

Data Types

int x = 42;
float pi = 3.14159;
str name = "Tulpar";
bool active = true;

array mixed = [1, "text", 3.14];
arrayInt numbers = [1, 2, 3, 4, 5];
json config = {"host": "localhost", "port": 8080};

Control Flow

if (x > 0) {
    print("positive");
} else {
    print("non-positive");
}

for (int i = 0; i < 10; i++) {
    print(i);
}

while (condition) {
    // loop body
}

Functions

func fibonacci(int n) {
    if (n <= 1) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}

JSON Objects

json user = {
    "name": "Alice",
    "age": 30,
    "address": {
        "city": "Istanbul",
        "country": "Turkey"
    }
};

print(user["address"]["city"]);  // Istanbul
print(user.address.city);        // Istanbul (dot notation)

Custom Types

type Person {
    str name;
    int age;
    str city = "Istanbul";  // default value
}

Person p = Person("Ali", 25);
print(p.name, p.age, p.city);

Error Handling

try {
    int result = riskyOperation();
} catch (e) {
    print("Error:", e);
}

Multi-Threading

thread_create("worker_function", arg);

mutex lock = mutex_create();
mutex_lock(lock);
// critical section
mutex_unlock(lock);

Networking

int server = socket_server("127.0.0.1", 8080);
int client = socket_accept(server);
str data = socket_receive(client, 1024);
socket_send(client, "HTTP/1.1 200 OK\r\n\r\nHello");
socket_close(client);

Database (SQLite)

int db = db_open("app.db");
db_execute(db, "CREATE TABLE users (id INTEGER, name TEXT)");
json rows = db_query(db, "SELECT * FROM users");
db_close(db);

Standard Library

Tulpar Wings (Web Framework)

import "wings";

func home() {
    return {"message": "Welcome to Tulpar Wings!"};
}

func users() {
    return [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}];
}

get("/", "home");
get("/users", "users");
listen(3000);

Built-in Functions

Category Functions
I/O print, input, inputInt, inputFloat
Type Conversion toInt, toFloat, toString, toBool
Math abs, sqrt, pow, sin, cos, tan, log, exp, floor, ceil, round, random, randint, min, max
String length, upper, lower, trim, split, join, replace, substring, contains, startsWith, endsWith, indexOf
Array push, pop, length, range
JSON toJson, fromJson
Time timestamp, time_ms, clock_ms, sleep
File file_read, file_write, file_exists, file_delete

Project Structure

TulparLang/
├── src/
│   ├── lexer/          # Tokenization
│   ├── parser/         # AST generation
│   ├── interpreter/    # Runtime execution
│   ├── vm/             # Virtual machine
│   └── aot/            # LLVM backend
├── lib/                # Standard libraries
├── examples/           # Example programs
├── cmake/              # CMake modules
└── runtime/            # Runtime support

Examples

Example programs are available in the examples/ directory:

File Description
01_hello_world.tpr Basic syntax
02_basics.tpr Variables, loops, functions
03_interactive.tpr User input/output
04_math_logic.tpr Mathematical operations
05_strings.tpr String manipulation
06_data_structures.tpr JSON and arrays
07_modules.tpr Import system
08_file_io.tpr File operations
09_socket_server.tpr Network server
10_try_catch.tpr Error handling
11_router_app.tpr Web application
12_threaded_server.tpr Multi-threaded HTTP
13_database.tpr SQLite integration
api_wings.tpr REST API with Wings

Documentation

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/name)
  3. Commit changes (git commit -m 'Add feature')
  4. Push to branch (git push origin feature/name)
  5. Open a Pull Request

License

MIT License - see LICENSE for details.

Author

Hamza Ortatepe
GitHub: @hamer1818


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors