Welcome to DixLang β my very own programming language built from SCRATCH using Python!
Whether youβre new to programming or a seasoned coder, DixLang offers a fun and approachable way to learn programming. Plus, itβs packed with features to help you get creative π!
- Introduction
- Getting Started
- Language Syntax Overview
- Built-In Functions
- Tutorials & Examples
- Advanced Topics
- Running Scripts and REPL
- Summary & Next Steps
DixLang is an interpreted, BASIC-inspired language that is:
- Simple: Perfect for learning basic language concepts.
- Expressive: Supports arithmetic, control flow, recursive functions, and more!
- Modular: Easily extendable with built-in functions and custom syntax.
Learn by doingβexperiment with code, explore the source, and have fun! π€©
To start using DixLang:
-
Clone the Repository:
git clone https://github.com/sedfarouk/DixLang.git cd DixLang
-
Run the REPL:
Launch the interactive shell:python shell.py
Then, try simple commands like:
5 + 3 VAR x = 10 WRITE(x)
-
Execute a Script:
Create a.dxl
file (see the Tutorials section) and run it:RUN("your_script.dxl")
-
Exit:
Simply typeexit
in the REPL.
Comments begin with a tilde ~
:
~ This is a comment! π¬
- Declare Variables: Use
VAR
- Operations:
+
,-
,*
,/
,^
Example:
VAR a = 20
VAR b = 5
~ prints 25
WRITE(a + b)
You can even do inline expressions:
~ returns 4 and assigns 2 to x!
2 + (VAR x = 2)
Single-line:
IF a < 0 THEN WRITE("Negative!") EIF a == 0 THEN WRITE("Zero!") ELSE WRITE("Positive!")
Multi-line:
IF a < 0 THEN
WRITE("Negative!")
EIF a == 0 THEN
WRITE("Zero!")
ELSE
WRITE("Positive!")
END
For Loop:
FOR i = 0 TO 5 THEN
WRITE(i)
END
Custom step:
FOR i = 10 TO 0 STEP -2 THEN
WRITE(i)
END
While Loop:
VAR count = 5
WHILE count > 0 THEN
WRITE("Countdown: " + count)
VAR count = count - 1
END
Loop controls (using CONT
for continue and BR
for break):
FOR i = 1 TO 10 THEN
IF i == 5 THEN
WRITE("Found 5, stopping loop! β")
BR
END
WRITE(i)
END
Define functions with FN
. They can be single-liners or multi-line blocks:
One-liner:
FN greet(name) -> "Hello, " + name
WRITE(greet("Alice"))
Recursive:
FN fib(n)
IF n <= 1 THEN RET n
RET fib(n - 1) + fib(n - 2)
END
WRITE("Fibonacci of 10 is: " + fib(10))
Notes:
RET
allows for early returns.- Functions are first-class citizens.
DixLang allows you to work with arrays and strings naturally.
Lists:
VAR numbers = [1, 2, 3, 4]
~ prints [1, 2, 3, 4]
WRITE(numbers)
APPEND(numbers, 5)
~ prints [1, 2, 3, 4, 5]
WRITE(numbers)
Strings:
VAR greeting = "Hello, " + "World!"
WRITE(greeting)
DixLang includes a helpful set of built-in functions:
Function | Description | Example |
---|---|---|
WRITE(value) |
Prints output to the console | WRITE("Hello!") |
WRITE_RET(value) |
Prints and returns the value as a string | WRITE_RET("Echo!") |
INPUT() |
Reads a line of user input | VAR name = INPUT() |
INPUT_INT() |
Reads an integer | VAR num = INPUT_INT() |
CLEAR() |
Clears the terminal screen | CLEAR() |
IS_NUM(value) |
Checks if the value is a number | IS_NUM(42) |
IS_STR(value) |
Checks if the value is a string | IS_STR("foo") |
IS_LIST(value) |
Checks if the value is a list | IS_LIST([1,2,3]) |
IS_FN(value) |
Checks if the value is a function | IS_FN(greet) |
APPEND(list, value) |
Appends an element to a list | APPEND(numbers, 10) |
POP(list, index) |
Removes and returns an element at a specific index | POP(numbers, 1) |
EXTEND(listA, listB) |
Merges two lists | EXTEND(list1, list2) |
LENGTH(list) |
Returns the length of a list | LENGTH(numbers) |
RUN(fn) |
Executes another DixLang script | RUN("script.dxl") |
RANDOM_INT(lo, hi) |
Returns a random integer between lo and hi | RANDOM_INT(1, 10) |
- Step 1: Open the REPL.
- Step 2: Type:
VAR x = 7 VAR y = 3 ~ Should print 10 WRITE(x + y)
- Step 3: Experiment with inline declarations:
~ returns 11 WRITE((VAR a = 5) + (VAR b = 2) * 3)
Try conditional statements:
VAR number = 4
IF number < 0 THEN
WRITE("Negative!")
ELIF number == 0 THEN
WRITE("Zero!")
ELSE
WRITE("Positive!")
END
Then test a loop:
FOR i = 1 TO 5 THEN
IF i == 3 THEN
WRITE("Skipping 3! βοΈ")
CONT
END
WRITE(i)
END
Start with a simple arithmetic function:
FN add(a, b) -> a + b
WRITE("Sum is: " + add(10, 15))
Now, try a recursive Fibonacci function:
FN fib(n)
IF n <= 1 THEN RET n
RET fib(n - 1) + fib(n - 2)
END
WRITE("Fib(7): " + fib(7))
Work with lists:
VAR myList = [1, 2, 3]
APPEND(myList, 4)
~ Expect: [1, 2, 3, 4]
WRITE(myList)
WRITE("Length: " + LENGTH(myList))
And strings:
FN exclaim(s) -> s + "!"
WRITE(exclaim("Hello"))
Here's a sample script combining multiple features:
// file: test_file_exec.dxl
WRITE("I FINALLY DID IT! MY OWN PROGRAMMING LANGUAGE! π")
~ Function to concatenate strings
FN concatenate(prefix, suffix) -> prefix + suffix
FOR i = 0 TO 5 THEN
WRITE(concatenate("Iteration ", i))
END
FN fib(n)
IF n <= 1 THEN RET n
RET fib(n - 1) + fib(n - 2)
END
WRITE("Fibonacci of 10: " + fib(10))
Run it by typing:
RUN("c:\\Users\\FaroukS\\Desktop\\DixLang\\test_file_exec.dxl")
DixLang reports errors with clear messages and visual arrows:
Invalid Syntax: Expected '+', '-', '*', '^' or '/'
File test_file_exec.dxl, line 10
^^^
This helps you pinpoint the exact location and nature of issues.
Learn how DixLang works under the hood:
- Lexing: Converts source code into tokens.
- Parsing: Builds an Abstract Syntax Tree (AST) from tokens.
- Evaluation: The interpreter processes the AST to produce results.
Developers can extend the language by adding new built-in functions or syntax rules. Explore files in src/values/functions/
and src/constants/
for ideas.
The future roadmap for DixLang includes a variety of exciting improvements and new features:
-
Improved Error Messages:
Enhance error reporting with more detailed suggestions and context-aware hints. -
Debugging Tools:
Develop built-in debugging commands to step through code and inspect variables at runtime. -
Extended Library:
Add more built-in functions, including file I/O operations, networking, and graphics modules. -
Enhanced Syntax:
Introduce additional syntactic sugar such as switch-case constructs and list comprehensions. -
Module System:
Implement an import system for better code reusability and module management. -
Performance Optimizations:
Refine the interpreter to improve execution speed and memory management. -
Community Contributions:
Encourage community-driven extensions, tutorials, and educational resources.
Stay tuned for updates and feel free to propose new ideas!
-
Interactive REPL: Run:
python shell.py
Then, try commands like:
VAR a = 100 ~ prints 50 WRITE(a / 2)
-
Batch Scripts: Save your code as a
.dxl
file and execute it:RUN("c:\\Users\\FaroukS\\Desktop\\DixLang\\your_script.dxl")
DixLang is a versatile playground:
- Arithmetic & Variables: Start with simple maths.
- Control Flow: Use conditionals and loops.
- Functions: Define, call, and even recurse.
- Lists & Strings: Manipulate collections and text.
- Built-Ins: Leverage I/O and utility functions.
Feel free to explore, modify, and extend the language by diving into the src/
folder. Happy coding and enjoy your journey with DixLang! π
For issues, suggestions, or contributions, please head over to the GitHub repository and open an issue or pull request. With love, Farouk β€οΈ