Gladlang
GladLang is a dynamic, interpreted, object-oriented programming language with a full interpreter built in Python. Features closures, classes, inheritance, and robust error handling.
Install / Use
/learn @gladw-in/GladlangREADME
GladLang
GladLang is a dynamic, interpreted, object-oriented programming language. This is a full interpreter built from scratch in Python, complete with a lexer, parser, and runtime environment. It supports modern programming features like closures, classes, inheritance, and robust error handling.
GladLang source files use the .glad file extension.
This is the full overview of the GladLang language, its features, and how to run the interpreter.
Table of Contents
- About The Language
- Key Features
- Getting Started
- Language Tour (Syntax Reference)
- Error Handling
- Running Tests
- License
About The Language
GladLang is an interpreter for a custom scripting language. It was built as a complete system, demonstrating the core components of a programming language:
- Lexer: A tokenizer that scans source code and converts it into a stream of tokens (e.g.,
NUMBER,STRING,IDENTIFIER,KEYWORD,PLUS). - Parser: A parser that takes the token stream and builds an Abstract Syntax Tree (AST), representing the code's structure.
- AST Nodes: A comprehensive set of nodes that define every syntactic structure in the language (e.g.,
BinOpNode,IfNode,FunDefNode,ClassNode). - Runtime: Defines the
ContextandSymbolTablefor managing variable scope, context (for tracebacks), and closures. - Values: Defines the language's internal data types (
Number,String,List,Dict,Function,Class,Instance). - Interpreter: The core engine that walks the AST. It uses a "Zero-Copy" architecture with Dependency Injection for high-performance execution and low memory overhead.
- Entry Point: The main file that ties everything together. It handles command-line arguments, runs files, and starts the interactive shell.
Key Features
GladLang supports a rich, modern feature set:
- Data Types: Numbers (int/float, plus Hex/Octal/Binary literals), Strings, Lists, Dictionaries, Booleans, and Null.
- Variables: Dynamic variable assignment with
LET. - Advanced Assignments:
- Destructuring: Unpack lists in assignments (
LET [x, y] = [1, 2]) and loops (FOR [x, y] IN points). - Slicing: Access sub-lists or substrings easily (
list[0:3]).
- Destructuring: Unpack lists in assignments (
- String Manipulation:
- Interpolation: JavaScript-style template strings (
`Hello ${name}`). - Multi-line Strings: Triple-quoted strings (
"""...""") for large text blocks.
- Interpolation: JavaScript-style template strings (
- Comprehensions:
- List Comprehensions: Supports nesting (
[x+y FOR x IN A FOR y IN B]). - Dictionary Comprehensions: Create dicts programmatically (
{k: v FOR k IN list}).
- List Comprehensions: Supports nesting (
- Dictionaries: Key-value data structures (
{'key': 'value'}). - Control Flow:
- Full support for
IF/ELSE IF,SWITCH/CASE. - Universal Iteration:
FORloops over Lists, Strings (chars), and Dictionaries (keys). - C-Style Iteration: Traditional
FORloops (FOR (LET I = 0; I < 10; I++)) featuring strict block scoping and safe closure capturing.
- Full support for
- Functions: First-class citizens, Closures, Recursion, Named/Anonymous support, and Overloading (by argument count).
- Object-Oriented: Full OOP support with
CLASS,INHERITS, Access Modifiers, and Method/Constructor Overloading. Object instantiation is $O(1)$ due to constructor caching. - Advanced Inheritance: Support for Multiple and Hybrid inheritance with strict C3-style Method Resolution Order (MRO).
- Parent Delegation: Full support for
SUPERin both constructors and overridden methods, plus explicit parent targeting. - Static Members: Java-style
STATICfields, methods, and constants (STATIC FINAL). - Operators: Ternary Operator (
condition ? true : false) for concise conditional logic. - Enums: Fully encapsulated, immutable
ENUMtypes with auto-incrementing values and explicit assignments. - OOP Safety: Runtime checks for circular inheritance, LSP violations, strict unbound method type-checking, and secure encapsulation.
- Error Management: Gracefully handle errors with
TRY,CATCH, andFINALLY. - Constants: Declare immutable values using
FINAL. These use Atomic Locking (set_if_absent) to prevent race conditions and are fully protected from shadowing or modification. - Memory Safety: Built-in protection against Denial-of-Service attacks. List repetition is capped at 1,000,000 elements, and a global Instruction Budget prevents infinite loop lockups.
- Logical Accuracy: Full Short-Circuit Evaluation for
AND/ORoperators and corrected math logic for compound assignments like+=. - Built-ins:
PRINTLN,PRINT,INPUT,STR,INT,FLOAT,BOOL,LEN. - Error Handling: Robust, user-friendly runtime error reporting with full tracebacks.
- Advanced Math: Compound assignments (
+=,*=), Power (**), Modulo (%), and automatic float division. - Rich Comparisons: Chained comparisons (
1 < x < 10), Identity checks (IS), and runtime type-checking (INSTANCEOF). - Boolean Logic: Strict support for
AND/OR/NOT.
Getting Started
There are several ways to install and run GladLang.
1. Installation
Option A: Install via Pip (Recommended)
If you just want to use the language, install it via pip:
pip install gladlang
Option B: Install from Source (For Developers)
If you want to modify the codebase, clone the repository and install it in editable mode:
git clone --depth 1 https://github.com/gladw-in/gladlang.git
cd gladlang
pip install -e .
2. Usage
Once installed, you can use the global gladlang command.
Interactive Shell (REPL)
Run the interpreter without arguments to start the shell:
gladlang
Running a Script
Pass a file path to execute a script:
gladlang "tests/test.glad"
Inline Execution
Run code directly from your terminal string:
gladlang "PRINTLN 10 + 5"
3. Running Without Installation (Source)
You can run the interpreter directly from the source code without installing it via pip:
python run.py "tests/test.glad"
4. Building the Executable
You can build a standalone executable (no Python required) using PyInstaller:
pip install pyinstaller
pyinstaller run.py --paths src -F --name gladlang --icon=favicon.ico
This will create a single-file executable at dist/gladlang (or gladlang.exe on Windows).
Adding to PATH (Optional): To run the standalone executable from anywhere:
- Windows: Move it to a folder and add that folder to your System PATH variables.
- Mac/Linux: Move it to
/usr/local/bin:sudo mv dist/gladlang /usr/local/bin/
Language Tour (Syntax Reference)
Here is a guide to the GladLang syntax, with examples from the tests/ directory.
1. Comments
Comments start with # and last for the entire line.
# This is a comment.
LET a = 10 # This is an inline comment
2. Variables and Data Types
Variables
Variables are assigned using the LET keyword. You can also unpack lists directly into variables using Destructuring.
# Immutable Constants
FINAL PI = 3.14159
# Variable Assignment
LET a = 10
LET b = "Hello"
LET my_list = [a, b, 123]
# Destructuring Assignment
LET point = [10, 20]
LET [x, y] = point
PRINTLN x # 10
PRINTLN y # 20
Numbers
Numbers can be integers or floats. You can also use Hexadecimal, Octal, and Binary literals.
LET math_
