gladlang 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gladlang-0.1.1/LICENSE +7 -0
- gladlang-0.1.1/PKG-INFO +594 -0
- gladlang-0.1.1/README.md +568 -0
- gladlang-0.1.1/pyproject.toml +34 -0
- gladlang-0.1.1/setup.cfg +4 -0
- gladlang-0.1.1/src/gladlang/__init__.py +0 -0
- gladlang-0.1.1/src/gladlang/__main__.py +145 -0
- gladlang-0.1.1/src/gladlang/constants.py +63 -0
- gladlang-0.1.1/src/gladlang/errors.py +69 -0
- gladlang-0.1.1/src/gladlang/interpreter.py +769 -0
- gladlang-0.1.1/src/gladlang/lexer.py +363 -0
- gladlang-0.1.1/src/gladlang/nodes.py +261 -0
- gladlang-0.1.1/src/gladlang/parser.py +1153 -0
- gladlang-0.1.1/src/gladlang/runtime.py +70 -0
- gladlang-0.1.1/src/gladlang/values.py +790 -0
- gladlang-0.1.1/src/gladlang.egg-info/PKG-INFO +594 -0
- gladlang-0.1.1/src/gladlang.egg-info/SOURCES.txt +18 -0
- gladlang-0.1.1/src/gladlang.egg-info/dependency_links.txt +1 -0
- gladlang-0.1.1/src/gladlang.egg-info/entry_points.txt +2 -0
- gladlang-0.1.1/src/gladlang.egg-info/top_level.txt +1 -0
gladlang-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2025 - present GLAD432
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
gladlang-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gladlang
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: The GladLang Interpreter
|
|
5
|
+
Author: Glad432
|
|
6
|
+
License: Copyright (c) 2025 - present GLAD432
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
13
|
+
Project-URL: Homepage, https://github.com/gladw-in/gladlang
|
|
14
|
+
Keywords: interpreter,language,compiler,educational
|
|
15
|
+
Classifier: Development Status :: 4 - Beta
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
21
|
+
Classifier: Operating System :: OS Independent
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# GladLang
|
|
28
|
+
|
|
29
|
+
**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.
|
|
30
|
+
|
|
31
|
+

|
|
32
|
+
|
|
33
|
+
This is the full overview of the GladLang language, its features, and how to run the interpreter.
|
|
34
|
+
|
|
35
|
+
## Table of Contents
|
|
36
|
+
|
|
37
|
+
- [About The Language](#about-the-language)
|
|
38
|
+
- [Key Features](#key-features)
|
|
39
|
+
- [Getting Started](#getting-started)
|
|
40
|
+
- [Running a Script](#running-a-script)
|
|
41
|
+
- [Using the Interactive Shell](#using-the-interactive-shell)
|
|
42
|
+
- [Building the Executable](#building-the-executable)
|
|
43
|
+
- [Language Tour (Syntax Reference)](#language-tour-syntax-reference)
|
|
44
|
+
- [1. Comments](#1-comments)
|
|
45
|
+
- [2. Variables and Data Types](#2-variables-and-data-types)
|
|
46
|
+
- [Variables](#variables)
|
|
47
|
+
- [Numbers](#numbers)
|
|
48
|
+
- [Strings](#strings)
|
|
49
|
+
- [Lists](#lists)
|
|
50
|
+
- [Booleans](#booleans)
|
|
51
|
+
- [Null](#null)
|
|
52
|
+
- [3. Operators](#3-operators)
|
|
53
|
+
- [Arithmetic & Precedence](#arithmetic--precedence)
|
|
54
|
+
- [Comparison & Logical](#comparison--logical)
|
|
55
|
+
- [Increment / Decrement](#increment--decrement)
|
|
56
|
+
- [4. Control Flow](#4-control-flow)
|
|
57
|
+
- [IF Statements](#if-statements)
|
|
58
|
+
- [WHILE Loops](#while-loops)
|
|
59
|
+
- [FOR Loops](#for-loops)
|
|
60
|
+
- [5. Functions](#5-functions)
|
|
61
|
+
- [Named Functions](#named-functions)
|
|
62
|
+
- [Anonymous Functions](#anonymous-functions)
|
|
63
|
+
- [Closures](#closures)
|
|
64
|
+
- [Recursion](#recursion)
|
|
65
|
+
- [6. Object-Oriented Programming (OOP)](#6-object-oriented-programming-oop)
|
|
66
|
+
- [Classes and Instantiation](#classes-and-instantiation)
|
|
67
|
+
- [The `SELF` Keyword](#the-self-keyword)
|
|
68
|
+
- [Inheritance](#inheritance)
|
|
69
|
+
- [Polymorphism](#polymorphism)
|
|
70
|
+
- [7. Built-in Functions](#7-built-in-functions)
|
|
71
|
+
- [Error Handling](#error-handling)
|
|
72
|
+
- [Running Tests](#running-tests)
|
|
73
|
+
- [License](#license)
|
|
74
|
+
|
|
75
|
+
-----
|
|
76
|
+
|
|
77
|
+
## About The Language
|
|
78
|
+
|
|
79
|
+
GladLang is an interpreter for a custom scripting language. It was built as a complete system, demonstrating the core components of a programming language:
|
|
80
|
+
|
|
81
|
+
* **Lexer (`lexer.py`):** A tokenizer that scans source code and converts it into a stream of tokens (e.g., `NUMBER`, `STRING`, `IDENTIFIER`, `KEYWORD`, `PLUS`).
|
|
82
|
+
* **Parser (`parser.py`):** A parser that takes the token stream and builds an Abstract Syntax Tree (AST), representing the code's structure.
|
|
83
|
+
* **AST Nodes (`nodes.py`):** A comprehensive set of nodes that define every syntactic structure in the language (e.g., `BinOpNode`, `IfNode`, `FunDefNode`, `ClassNode`).
|
|
84
|
+
* **Runtime (`runtime.py`):** Defines the `Context` and `SymbolTable` for managing variable scope, context (for tracebacks), and closures.
|
|
85
|
+
* **Values (`values.py`):** Defines the language's internal data types (`Number`, `String`, `List`, `Function`, `Class`, `Instance`).
|
|
86
|
+
* **Interpreter (`interpreter.py`):** The core engine that walks the AST and executes the program by visiting each node.
|
|
87
|
+
* **Entry Point (`gladlang.py`):** The main file that ties everything together. It handles command-line arguments, runs files, and starts the interactive shell.
|
|
88
|
+
|
|
89
|
+
-----
|
|
90
|
+
|
|
91
|
+
## Key Features
|
|
92
|
+
|
|
93
|
+
GladLang supports a rich, modern feature set:
|
|
94
|
+
|
|
95
|
+
* **Data Types:** Numbers (int/float), Strings, Lists, Booleans, and Null.
|
|
96
|
+
* **Variables:** Dynamic variable assignment with the `LET` keyword.
|
|
97
|
+
* **Operators:** Full set of arithmetic (`+`, `-`, `*`, `/`, `^`), comparison (`==`, `!=`, `<`, `>`), and logical (`AND`, `OR`, `NOT`) operators.
|
|
98
|
+
* **Control Flow:** `IF...THEN...ENDIF`, `WHILE...ENDWHILE`, and `FOR...IN...ENDFOR` loops.
|
|
99
|
+
* **Flow Control:** `BREAK` and `CONTINUE` keywords for loops.
|
|
100
|
+
* **Functions:**
|
|
101
|
+
* First-class citizens (can be passed as arguments and stored in variables).
|
|
102
|
+
* Support for named and anonymous functions.
|
|
103
|
+
* **Closures:** Functions capture their parent's scope.
|
|
104
|
+
* Recursive function calls.
|
|
105
|
+
* **Object-Oriented Programming:**
|
|
106
|
+
* `CLASS...ENDCLASS` syntax.
|
|
107
|
+
* `NEW` keyword for instantiation.
|
|
108
|
+
* `SELF` keyword for instance context.
|
|
109
|
+
* `INHERITS` keyword for class inheritance.
|
|
110
|
+
* Method overriding and polymorphism.
|
|
111
|
+
* **Advanced Operators:** Pre- and post-increment/decrement (`++i`, `i--`).
|
|
112
|
+
* **List Manipulation:** Index access (`my_list[0]`), index assignment (`my_list[0] = 10`), and concatenation (`list1 + list2`).
|
|
113
|
+
* **Built-ins:** `PRINT`, `INPUT`, `STR`, `INT`, `FLOAT`, and `BOOL`.
|
|
114
|
+
* **Error Handling:** Robust, user-friendly runtime error reporting with full tracebacks.
|
|
115
|
+
|
|
116
|
+
-----
|
|
117
|
+
|
|
118
|
+
## Getting Started
|
|
119
|
+
|
|
120
|
+
### Running a Script
|
|
121
|
+
|
|
122
|
+
You can execute any `.glad` file by passing it as an argument to `gladlang.py`.
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
python gladlang.py "test.glad"
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**Output:**
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
--- OOP & Inheritance Tester ---
|
|
132
|
+
--- Creating a Pet ---
|
|
133
|
+
A new pet is born!
|
|
134
|
+
Sammy makes a generic pet sound.
|
|
135
|
+
--- Creating a Dog ---
|
|
136
|
+
A new pet is born!
|
|
137
|
+
Buddy says: Woof!
|
|
138
|
+
Buddy wags its tail.
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Using the Interactive Shell
|
|
142
|
+
|
|
143
|
+
Run the interpreter without any arguments to start the interactive Read-Eval-Print Loop (REPL) shell.
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
python gladlang.py
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**Shell:**
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
Welcome to GladLang (v0.1.0)
|
|
153
|
+
Type 'exit' or 'quit' to close the shell.
|
|
154
|
+
--------------------------------------------------
|
|
155
|
+
GladLang > PRINT "Hello, " + "World!"
|
|
156
|
+
Hello, World!
|
|
157
|
+
0
|
|
158
|
+
GladLang > LET a = 10 * (2 + 3)
|
|
159
|
+
0
|
|
160
|
+
GladLang > PRINT a
|
|
161
|
+
50
|
|
162
|
+
0
|
|
163
|
+
GladLang > exit
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Building the Executable
|
|
167
|
+
|
|
168
|
+
You can build a **standalone executable** using **PyInstaller**:
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
pyinstaller run.py --paths src -F --name gladlang --icon=favicon.ico
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
This will create a single-file executable at `dist/gladlang`.
|
|
175
|
+
|
|
176
|
+
-----
|
|
177
|
+
|
|
178
|
+
### Adding to System PATH for Global Use
|
|
179
|
+
|
|
180
|
+
You can then add it to your system **PATH** so it can be run globally as "**gladlang**" from anywhere.
|
|
181
|
+
|
|
182
|
+
#### Windows:
|
|
183
|
+
|
|
184
|
+
1. **Move or copy** the executable to a folder of your choice, for example:
|
|
185
|
+
```bash
|
|
186
|
+
move dist\gladlang.exe "C:\Programs\GladLang"
|
|
187
|
+
```
|
|
188
|
+
2. **Add that folder to your system PATH:**
|
|
189
|
+
* Press **Win + R**, type `sysdm.cpl`, press **Enter**.
|
|
190
|
+
* Go to **Advanced** $\rightarrow$ **Environment Variables**.
|
|
191
|
+
* Under **System variables**, select **Path** $\rightarrow$ **Edit** $\rightarrow$ **New**.
|
|
192
|
+
* Add the full path (e.g., `C:\Programs\GladLang`).
|
|
193
|
+
3. **Open a new terminal and test:**
|
|
194
|
+
```bash
|
|
195
|
+
gladlang "test_recursion.glad"
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### macOS / Linux:
|
|
199
|
+
|
|
200
|
+
1. **Move** the executable to `/usr/local/bin` (or another directory in your PATH):
|
|
201
|
+
```bash
|
|
202
|
+
sudo mv dist/gladlang /usr/local/bin/
|
|
203
|
+
```
|
|
204
|
+
2. **Make sure it's executable:**
|
|
205
|
+
```bash
|
|
206
|
+
sudo chmod +x /usr/local/bin/gladlang
|
|
207
|
+
```
|
|
208
|
+
3. **Test it anywhere:**
|
|
209
|
+
```bash
|
|
210
|
+
gladlang "test_recursion.glad"
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Now you can run GladLang from any folder without prefixing `./dist/`.
|
|
214
|
+
|
|
215
|
+
-----
|
|
216
|
+
|
|
217
|
+
## Language Tour (Syntax Reference)
|
|
218
|
+
|
|
219
|
+
Here is a guide to the GladLang syntax, with examples from the `tests/` directory.
|
|
220
|
+
|
|
221
|
+
### 1\. Comments
|
|
222
|
+
|
|
223
|
+
Comments start with `#` and last for the entire line.
|
|
224
|
+
|
|
225
|
+
```glad
|
|
226
|
+
# This is a comment.
|
|
227
|
+
LET a = 10 # This is an inline comment
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### 2\. Variables and Data Types
|
|
231
|
+
|
|
232
|
+
#### Variables
|
|
233
|
+
|
|
234
|
+
Variables are assigned using the `LET` keyword.
|
|
235
|
+
|
|
236
|
+
```glad
|
|
237
|
+
LET a = 10
|
|
238
|
+
LET b = "Hello"
|
|
239
|
+
LET my_list = [a, b, 123]
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
#### Numbers
|
|
243
|
+
|
|
244
|
+
Numbers can be integers or floats. All standard arithmetic operations are supported.
|
|
245
|
+
|
|
246
|
+
```glad
|
|
247
|
+
LET math_result = (1 + 2) * 3 # 9
|
|
248
|
+
LET float_result = 10 / 4 # 2.5
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
#### Strings
|
|
252
|
+
|
|
253
|
+
Strings are defined with double quotes and support escape characters. They can be concatenated with `+`.
|
|
254
|
+
|
|
255
|
+
```glad
|
|
256
|
+
LET newline = "Hello\nWorld"
|
|
257
|
+
LET quoted = "She said, \"This is cool!\""
|
|
258
|
+
PRINT "Hello, " + "GladLang"
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
#### Lists
|
|
262
|
+
|
|
263
|
+
Lists are ordered collections of any type. They support indexing, assignment, and concatenation.
|
|
264
|
+
|
|
265
|
+
```glad
|
|
266
|
+
LET my_list = [1, "hello", 2 * 3, TRUE]
|
|
267
|
+
|
|
268
|
+
PRINT my_list[1] # Access: "hello"
|
|
269
|
+
|
|
270
|
+
LET my_list[1] = "world" # Assign
|
|
271
|
+
PRINT my_list[1] # "world"
|
|
272
|
+
|
|
273
|
+
LET other_list = my_list + [FALSE, 100]
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
#### Booleans
|
|
277
|
+
|
|
278
|
+
Booleans are `TRUE` and `FALSE`. They are the result of comparisons and logical operations.
|
|
279
|
+
|
|
280
|
+
```glad
|
|
281
|
+
LET t = TRUE
|
|
282
|
+
LET f = FALSE
|
|
283
|
+
PRINT t AND f # 0 (False)
|
|
284
|
+
PRINT t OR f # 1 (True)
|
|
285
|
+
PRINT NOT t # 0 (False)
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
**Truthiness:** `0`, `0.0`, `""`, `NULL`, and `FALSE` are "falsy." All other values (including non-empty strings, non-zero numbers, lists, functions, and classes) are "truthy."
|
|
289
|
+
|
|
290
|
+
#### Null
|
|
291
|
+
|
|
292
|
+
The `NULL` keyword represents a null or "nothing" value. It is falsy and prints as `0`. Functions with no `RETURN` statement implicitly return `NULL`.
|
|
293
|
+
|
|
294
|
+
-----
|
|
295
|
+
|
|
296
|
+
### 3\. Operators
|
|
297
|
+
|
|
298
|
+
#### Arithmetic & Precedence
|
|
299
|
+
|
|
300
|
+
`^` (power), `*`, `/` (division), `+`, `-`. Standard operator precedence is respected.
|
|
301
|
+
|
|
302
|
+
```glad
|
|
303
|
+
PRINT 1 + 2 * 3 # 7
|
|
304
|
+
PRINT (1 + 2) * 3 # 9
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
#### Comparison & Logical
|
|
308
|
+
|
|
309
|
+
* **Comparison:** `==`, `!=`, `<`, `>`, `<=`, `>=`
|
|
310
|
+
* **Logical:** `AND`, `OR`, `NOT`
|
|
311
|
+
|
|
312
|
+
<!-- end list -->
|
|
313
|
+
|
|
314
|
+
```glad
|
|
315
|
+
PRINT (10 < 20) AND (10 != 5) # 1 (True)
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
#### Increment / Decrement
|
|
319
|
+
|
|
320
|
+
Supports C-style pre- and post-increment/decrement operators on variables and list elements.
|
|
321
|
+
|
|
322
|
+
```glad
|
|
323
|
+
LET i = 5
|
|
324
|
+
PRINT i++ # 5
|
|
325
|
+
PRINT i # 6
|
|
326
|
+
PRINT ++i # 7
|
|
327
|
+
PRINT i # 7
|
|
328
|
+
|
|
329
|
+
LET my_list = [10, 20]
|
|
330
|
+
PRINT my_list[1]++ # 20
|
|
331
|
+
PRINT my_list[1] # 21
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
-----
|
|
335
|
+
|
|
336
|
+
### 4\. Control Flow
|
|
337
|
+
|
|
338
|
+
#### IF Statements
|
|
339
|
+
|
|
340
|
+
Uses `IF...THEN...ENDIF` syntax.
|
|
341
|
+
|
|
342
|
+
```glad
|
|
343
|
+
LET num = -5
|
|
344
|
+
IF num < 0 THEN
|
|
345
|
+
PRINT "It is negative."
|
|
346
|
+
ENDIF
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
#### WHILE Loops
|
|
350
|
+
|
|
351
|
+
Loops while a condition is `TRUE`.
|
|
352
|
+
|
|
353
|
+
```glad
|
|
354
|
+
LET i = 3
|
|
355
|
+
WHILE i > 0
|
|
356
|
+
PRINT "i = " + i
|
|
357
|
+
LET i = i - 1
|
|
358
|
+
ENDWHILE
|
|
359
|
+
|
|
360
|
+
# Prints:
|
|
361
|
+
# i = 3
|
|
362
|
+
# i = 2
|
|
363
|
+
# i = 1
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
#### FOR Loops
|
|
367
|
+
|
|
368
|
+
Iterates over the elements of a list.
|
|
369
|
+
|
|
370
|
+
```glad
|
|
371
|
+
LET my_list = ["apple", "banana", "cherry"]
|
|
372
|
+
FOR item IN my_list
|
|
373
|
+
PRINT "Item: " + item
|
|
374
|
+
ENDFOR
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
**`BREAK` and `CONTINUE`** are supported in both `WHILE` and `FOR` loops.
|
|
378
|
+
|
|
379
|
+
-----
|
|
380
|
+
|
|
381
|
+
### 5\. Functions
|
|
382
|
+
|
|
383
|
+
#### Named Functions
|
|
384
|
+
|
|
385
|
+
Defined with `DEF...ENDEF`. Arguments are passed by value. `RETURN` sends a value back.
|
|
386
|
+
|
|
387
|
+
```glad
|
|
388
|
+
DEF add(a, b)
|
|
389
|
+
RETURN a + b
|
|
390
|
+
ENDEF
|
|
391
|
+
|
|
392
|
+
LET sum = add(10, 5)
|
|
393
|
+
PRINT sum # 15
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
#### Anonymous Functions
|
|
397
|
+
|
|
398
|
+
Functions can be defined without a name, perfect for assigning to variables.
|
|
399
|
+
|
|
400
|
+
```glad
|
|
401
|
+
LET double = DEF(x)
|
|
402
|
+
RETURN x * 2
|
|
403
|
+
ENDEF
|
|
404
|
+
|
|
405
|
+
PRINT double(5) # 10
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
#### Closures
|
|
409
|
+
|
|
410
|
+
Functions capture variables from their parent scope.
|
|
411
|
+
|
|
412
|
+
```glad
|
|
413
|
+
DEF create_greeter(greeting)
|
|
414
|
+
DEF greeter_func(name)
|
|
415
|
+
# 'greeting' is "closed over" from the parent
|
|
416
|
+
RETURN greeting + ", " + name + "!"
|
|
417
|
+
ENDEF
|
|
418
|
+
RETURN greeter_func
|
|
419
|
+
ENDEF
|
|
420
|
+
|
|
421
|
+
LET say_hello = create_greeter("Hello")
|
|
422
|
+
PRINT say_hello("Alex") # "Hello, Alex!"
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
#### Recursion
|
|
426
|
+
|
|
427
|
+
Functions can call themselves.
|
|
428
|
+
|
|
429
|
+
```glad
|
|
430
|
+
DEF fib(n)
|
|
431
|
+
IF n <= 1 THEN
|
|
432
|
+
RETURN n
|
|
433
|
+
ENDIF
|
|
434
|
+
RETURN fib(n - 1) + fib(n - 2)
|
|
435
|
+
ENDEF
|
|
436
|
+
|
|
437
|
+
PRINT fib(7) # 13
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
-----
|
|
441
|
+
|
|
442
|
+
### 6\. Object-Oriented Programming (OOP)
|
|
443
|
+
|
|
444
|
+
#### Classes and Instantiation
|
|
445
|
+
|
|
446
|
+
Use `CLASS...ENDCLASS` to define classes and `NEW` to create instances. The constructor is `init`.
|
|
447
|
+
|
|
448
|
+
```glad
|
|
449
|
+
CLASS Counter
|
|
450
|
+
DEF init(self)
|
|
451
|
+
SELF.count = 0 # 'SELF' is the instance
|
|
452
|
+
ENDEF
|
|
453
|
+
|
|
454
|
+
DEF increment(self)
|
|
455
|
+
SELF.count = SELF.count + 1
|
|
456
|
+
ENDEF
|
|
457
|
+
|
|
458
|
+
DEF get_count(self)
|
|
459
|
+
RETURN SELF.count
|
|
460
|
+
ENDEF
|
|
461
|
+
ENDCLASS
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
#### The `SELF` Keyword
|
|
465
|
+
|
|
466
|
+
`SELF` is the mandatory first argument for all methods and is used to access instance attributes and methods.
|
|
467
|
+
|
|
468
|
+
```glad
|
|
469
|
+
LET c = NEW Counter()
|
|
470
|
+
c.increment()
|
|
471
|
+
PRINT c.get_count() # 1
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
#### Inheritance
|
|
475
|
+
|
|
476
|
+
Use the `INHERITS` keyword. Methods can be overridden by the child class.
|
|
477
|
+
|
|
478
|
+
```glad
|
|
479
|
+
CLASS Pet
|
|
480
|
+
DEF init(self, name)
|
|
481
|
+
SELF.name = name
|
|
482
|
+
ENDEF
|
|
483
|
+
|
|
484
|
+
DEF speak(self)
|
|
485
|
+
PRINT SELF.name + " makes a generic pet sound."
|
|
486
|
+
ENDEF
|
|
487
|
+
ENDCLASS
|
|
488
|
+
|
|
489
|
+
CLASS Dog INHERITS Pet
|
|
490
|
+
# Override the 'speak' method
|
|
491
|
+
DEF speak(self)
|
|
492
|
+
PRINT SELF.name + " says: Woof!"
|
|
493
|
+
ENDEF
|
|
494
|
+
ENDCLASS
|
|
495
|
+
|
|
496
|
+
LET my_dog = NEW Dog("Buddy")
|
|
497
|
+
my_dog.speak() # "Buddy says: Woof!"
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
#### Polymorphism
|
|
501
|
+
|
|
502
|
+
When a base class method calls another method on `SELF`, it will correctly use the **child's overridden version**.
|
|
503
|
+
|
|
504
|
+
```glad
|
|
505
|
+
CLASS Pet
|
|
506
|
+
DEF introduce(self)
|
|
507
|
+
PRINT "I am a pet and I say:"
|
|
508
|
+
SELF.speak() # This will call the child's 'speak'
|
|
509
|
+
ENDEF
|
|
510
|
+
|
|
511
|
+
DEF speak(self)
|
|
512
|
+
PRINT "(Generic pet sound)"
|
|
513
|
+
ENDEF
|
|
514
|
+
ENDCLASS
|
|
515
|
+
|
|
516
|
+
CLASS Cat INHERITS Pet
|
|
517
|
+
DEF speak(self)
|
|
518
|
+
PRINT "Meow!"
|
|
519
|
+
ENDEF
|
|
520
|
+
ENDCLASS
|
|
521
|
+
|
|
522
|
+
LET my_cat = NEW Cat("Whiskers")
|
|
523
|
+
my_cat.introduce()
|
|
524
|
+
# Prints:
|
|
525
|
+
# I am a pet and I say:
|
|
526
|
+
# Meow!
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
-----
|
|
530
|
+
|
|
531
|
+
### 7\. Built-in Functions
|
|
532
|
+
|
|
533
|
+
* `PRINT(value)`: Prints a value to the console.
|
|
534
|
+
* `INPUT()`: Reads a line of text from the user as a String.
|
|
535
|
+
* `STR(value)`: Casts a value to a String.
|
|
536
|
+
* `INT(value)`: Casts a String or Float to an Integer.
|
|
537
|
+
* `FLOAT(value)`: Casts a String or Integer to a Float.
|
|
538
|
+
* `BOOL(value)`: Casts a value to its Boolean representation (`TRUE` or `FALSE`).
|
|
539
|
+
|
|
540
|
+
-----
|
|
541
|
+
|
|
542
|
+
## Error Handling
|
|
543
|
+
|
|
544
|
+
GladLang features detailed error handling and prints full tracebacks for runtime errors, making debugging easy.
|
|
545
|
+
|
|
546
|
+
**Example: Name Error** (`test_name_error.glad`)
|
|
547
|
+
|
|
548
|
+
```
|
|
549
|
+
Traceback (most recent call last):
|
|
550
|
+
File test_name_error.glad, line 6, in <program>
|
|
551
|
+
Runtime Error: 'b' is not defined
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
**Example: Type Error** (`test_type_error.glad` with input "5")
|
|
555
|
+
|
|
556
|
+
```
|
|
557
|
+
Traceback (most recent call last):
|
|
558
|
+
File test_type_error.glad, line 6, in <program>
|
|
559
|
+
Runtime Error: Illegal operation
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
**Example: Argument Error** (`test_arg_error.glad`)
|
|
563
|
+
|
|
564
|
+
```
|
|
565
|
+
Traceback (most recent call last):
|
|
566
|
+
File test_arg_error.glad, line 7, in <program>
|
|
567
|
+
File test_arg_error.glad, line 4, in add
|
|
568
|
+
Runtime Error: Incorrect argument count for 'add'. Expected 2, got 3
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
-----
|
|
572
|
+
|
|
573
|
+
## Running Tests
|
|
574
|
+
|
|
575
|
+
The `tests/` directory contains a comprehensive suite of `.glad` files to test every feature of the language. You can run any test by executing it with the interpreter:
|
|
576
|
+
|
|
577
|
+
```bash
|
|
578
|
+
python gladlang.py "test_closures.glad"
|
|
579
|
+
python gladlang.py "test_lists.glad"
|
|
580
|
+
python gladlang.py "test_polymorphism.glad"
|
|
581
|
+
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
or
|
|
585
|
+
|
|
586
|
+
```bash
|
|
587
|
+
gladlang "test_closures.glad"
|
|
588
|
+
gladlang "test_lists.glad"
|
|
589
|
+
gladlang "test_polymorphism.glad"
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
## License
|
|
593
|
+
|
|
594
|
+
You can use this under the MIT License. See [LICENSE](LICENSE) for more details.
|