Skip to content

huyhapitk12/LP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

173 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

πŸš€ LP Language

LP Logo

A lightweight programming language with Python-like syntax that compiles to native machine code

⚑ No GCC/LLVM required! ⚑

Version License Platform Language

Quick Start β€’ Features β€’ Documentation β€’ Comparison β€’ Contributing


✨ Why LP?

Feature LP Python C Go Rust
Compilation Native ASM Interpreted Native Native Native
Dependencies ~5MB (binutils) ~100MB+ ~500MB (GCC) ~200MB ~1GB+
Startup Time ⚑ Instant 🐒 Slow ⚑ Instant ⚑ Instant ⚑ Instant
Performance πŸš€ Native 🐌 Slow πŸš€ Native πŸš€ Native πŸš€ Native
Syntax 🐍 Python-like 🐍 Python 😰 Verbose 🎯 Clean 😰 Complex
Learning Curve βœ… Easy βœ… Easy ❌ Hard ⚠️ Medium ❌ Hard

🎯 Highlights

  • πŸ”₯ Native Compilation - Direct LP β†’ Assembly β†’ Machine Code (no GCC/LLVM needed!)
  • πŸͺΆ Ultra-lightweight - Only requires binutils (as + ld, ~5MB)
  • 🐍 Python-like Syntax - Familiar def, class, if, for, while, import
  • ⚑ Modern Features - F-Strings, Pattern Matching, Decorators, Async/Await, Generics, Type Unions
  • 🧡 Built-in Modules - math, random, time, os, sys, http, json, sqlite, thread, memory, dsa
  • πŸ“š DSA Module - Data Structures & Algorithms for competitive programming (Stack, Queue, DSU, Heap, Segment Tree, Graph algorithms, Geometry)
  • πŸš€ Native OpenMP - Automatic parallel execution with parallel for
  • βš™οΈ @settings Decorator - Fine-grained control over parallel threads, GPU, scheduling
  • πŸ”’ @security Decorator - Built-in rate limiting, authentication, input validation
  • 🎯 Optimizations - Constant folding, dead code elimination, loop unrolling

πŸ“‹ Requirements

  • Linux x86-64: binutils (as, ld) - usually pre-installed
  • Windows: MSYS2 UCRT64 or MinGW
  • macOS: Xcode Command Line Tools

πŸš€ Quick Start

Build The Compiler

# POSIX shell
./compile.sh

# Or using make
make

Hello World

Create hello.lp:

def main():
    print("Hello from LP!")

main()

Run it with native compilation:

# Native compilation (default - no GCC needed!)
./build/lp hello.lp

# Or with GCC backend (optional)
./build/lp hello.lp --gcc

πŸ“– Features

🎨 Modern Syntax

# Variables with type inference
name = "LP"
version = "0.1.0"
active = True

# Explicit types
count: int = 100
ratio: float = 0.5
label: str = "text"

# F-Strings
greeting = f"Hello from {name} v{version}!"
print(greeting)  # Output: Hello from LP v0.1.0!

🧩 Pattern Matching

match value:
    case 1:
        print("one")
    case 2:
        print("two")
    case n if n > 10:
        print("big")
    case _:
        print("other")

🎭 Decorators

# @settings - Configure parallel/GPU execution
@settings(threads=8, schedule="dynamic", chunk=100)
def parallel_task(data: list) -> list:
    results = []
    parallel for item in data:
        results.append(process(item))
    return results

# @settings with GPU acceleration
@settings(device="gpu", gpu_id=0)
def gpu_compute(n: int) -> int:
    result = 0
    parallel for i in range(n):
        result += i * i
    return result

# @security - Built-in security features
@security(level=3, auth=True, rate_limit=100)
def secure_api_endpoint(data: dict) -> dict:
    return {"status": "ok"}

⚑ Async/Await

async def fetch_data(url: str) -> str:
    return "data from " + url

async def main():
    data = await fetch_data("https://example.com")
    print(data)

πŸ“¦ Generic Types

class Box[T]:
    value: T
    
    def get(self) -> T:
        return self.value

# Usage
int_box: Box[int] = Box()
int_box.set(42)

πŸš€ Parallel Computing

# OpenMP-style parallel for (auto-enabled!)
parallel for i in range(1000000):
    process_item(i)

# With @settings decorator for fine-grained control
@settings(threads=4, schedule="dynamic", chunk=1000)
def process_large_dataset(data: list) -> int:
    total = 0
    parallel for item in data:
        total += item
    return total

# GPU acceleration
@settings(device="gpu", unified=True)
def gpu_matrix_multiply(a: list, b: list) -> list:
    # Automatically runs on GPU if available
    parallel for i in range(len(a)):
        compute(a[i], b[i])

πŸ“š Built-in Modules

import math
import time
import http
import json
import sqlite
import thread
import memory

# Math
pi: float = math.pi
result = math.sqrt(16)  # 4.0

# HTTP
response: str = http.get("https://api.example.com")

# JSON
data = json.loads('{"name": "LP"}')
text = json.dumps(data)

# SQLite
db = sqlite.connect("mydb.db")
results = sqlite.query(db, "SELECT * FROM users")

# Threading
# Worker must be a named function with 0 args, returning int or void
def worker_function() -> int:
    return 42

t = thread.spawn(worker_function)
result = thread.join(t)

# Memory Management
arena = memory.arena_new(1024)
ptr = memory.arena_alloc(arena, 64)

πŸ“š DSA Module (Data Structures & Algorithms)

LP provides a comprehensive DSA module optimized for competitive programming:

import dsa

# Fast I/O (critical for competitive programming)
n = dsa.read_int()
dsa.write_int_ln(n)

# Number Theory
primes = dsa.sieve(1000000)
is_prime = dsa.is_prime(17)
result = dsa.mod_pow(2, 100, 1000000007)

# Data Structures
stack = dsa.stack_new(100)
dsa.stack_push(stack, 10)
dsa.stack_pop(stack)

queue = dsa.queue_new(100)
dsa.queue_push(queue, 1)
dsa.queue_pop(queue)

# DSU (Union-Find)
dsu = dsa.dsu_new(n)
dsa.dsu_union(dsu, 0, 1)
same = dsa.dsu_same(dsu, 0, 1)

# Heap / Priority Queue
heap = dsa.heap_new(100)
dsa.heap_push(heap, value, priority)

# Graph Algorithms
g = dsa.graph_new(n, True)
dsa.graph_add_edge(g, 0, 1, 10)
dist = dsa.graph_dijkstra(g, 0)

# Segment Tree with Lazy Propagation
seg = dsa.segtree_new(arr, n, "sum")
dsa.segtree_build(seg)
dsa.segtree_update(seg, 1, 4, 10)
result = dsa.segtree_query(seg, 0, 5)

# Geometry
hull = dsa.convex_hull(points, n)
area = dsa.polygon_area(hull.points, hull.count)

πŸ› οΈ CLI Commands

lp file.lp                    # Run with native backend (default)
lp file.lp --gcc              # Run with GCC backend (optional)
lp file.lp -o out.c           # Generate C code
lp file.lp -c out.exe         # Compile to executable
lp file.lp -asm out.s         # Generate assembly
lp test examples              # Run tests
lp profile file.lp            # Profile execution
lp watch file.lp              # Hot reload mode
lp build file.lp --release    # Build optimized executable
lp package file.lp            # Package for distribution

πŸ“‚ Project Structure

LP/
β”œβ”€β”€ compiler/          # Compiler source code (C)
β”œβ”€β”€ runtime/           # Runtime library
β”œβ”€β”€ examples/          # Example programs
β”œβ”€β”€ docs/              # Documentation
β”œβ”€β”€ vscode-lp/         # VSCode extension
β”œβ”€β”€ skills/            # AI assistant skills
β”œβ”€β”€ Makefile           # Build configuration
β”œβ”€β”€ compile.sh         # Build script (POSIX)
└── build.bat          # Build script (Windows)

πŸ“š Documentation

See the LP Language Wiki for comprehensive documentation:

Document Description
Feature Status Comprehensive feature status
Language Reference Complete language reference
Runtime Modules Built-in modules (math, http, json, dsa, etc.)
Parallel Computing OpenMP integration
Vietnamese Guide HΖ°α»›ng dαΊ«n tiαΊΏng Việt

πŸ“Š Comparison

Feature LP Python C Go
Compilation Native ASM Interpreted Native Native
Dependencies ~5MB (binutils) ~100MB+ ~500MB (GCC) ~200MB
Startup Time Instant Slow Instant Instant
Performance Native Slow Native Native
Memory Safe Partial Yes No Yes

🀝 Contributing

We welcome contributions! See CONTRIBUTING.md for guidelines.

Ways to Contribute

  • πŸ› Report bugs
  • πŸ’‘ Suggest features
  • πŸ“ Improve documentation
  • πŸ”§ Submit pull requests
  • ⭐ Star the repository!

πŸ“œ License

MIT License - See LICENSE


Made with ❀️ by Ho Quang Huy

About

πŸš€ LP - A lightweight programming language with Python-like syntax that compiles to native machine code. No GCC/LLVM required!

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages