AlgoLang is a custom programming language featuring both a tree-walk interpreter and a compiler targeting a custom MIPS32-based architecture with Floating Point Unit (FPU) and basic Quantum Computing (QC) extensions.
AlgoLang supports a variety of programming paradigms and features:
- Typing: Dynamically typed when interpreted, with runtime type checking. Statically (lexically) scoped when compiled.
- Operators: Standard arithmetic (
+,-,*,/,//ordiv,%ormod,^), logical (and,or,not), and comparison (<,>,==,<=,>=) operators. - Data Structures:
- Lists: Dynamically sized lists when run with interpreter and fixed size when compiled, except with concatenation available. Can be created empty with
list <size>;or initialized like(1, 2, 3). Lists are concatenated with+. - Vectors: Similar to NumPy arrays when interpreted, treated as lists when compiled. Created with
vec size;,vec (1, 2, 3), orvec <shape_tuple>where<shape_tuple>is another vector. Supports element-wise operations. - Dictionaries: Hash tables initialized using
dict var;ordict list[index];Not available when compiled, yet.
- Lists: Dynamically sized lists when run with interpreter and fixed size when compiled, except with concatenation available. Can be created empty with
- Control Flow:
if/elif/elseconditional statements.whileloops.- Loop control:
breakloop(breaks loop) andskipit(continues to next iteration). elsecan be used afterwhileto execute if thewhilewas not broken, just like in Python.
- Functions:
- First-class functions defined using the
algkeyword:myFunc = alg(arg1, arg2) { ... return result; };. - Functions can be values, passed around, and returned.
- Closures are supported both when interpreted and compiled, except with different behaviours of closed up-values.
- First-class functions defined using the
- Variables: Declared and optionally initialized using
let var = value;orlet var;. Assignment uses=. - Getting Output: Basic printing using the
printstatement. - Extensibility: Interpreter functionality can be extended via user-defined
SysCalls. Built-in syscalls includelen,int,float,int2str,input,str,split,push,pop,keys,read,write,drawQC,import, the first 5 of which run when compiled. Others are yet to be added to the compiler. - Quantum Computing (Experimental):
- Interpreter uses Qiskit for simulation (
QG,measure,clearQ,drawQC). - Compiler targets custom "co-processor 2" instructions for basic Quantum Computing operations (
QG,measure,clearQ).
- Interpreter uses Qiskit for simulation (
- Plotting: Basic terminal plotting via
termplotlibusing theplotstatement when interpreted. Can be useful when running in shell environment. - AST Visualization: Abstract Syntax Tree can be visualized using
graphviz.
The AlgoLang project includes several components:
- Interpreter (
SYNTAX.py,RUN.py,SHELL.py):- Executes AlgoLang code directly using a tree-walking approach.
SHELL.pyprovides an interactive REPL (Read-Eval-Print Loop) with features like syntax highlighting, command history, and basic keyword completion usingprompt_toolkit. PressCtrl+Jto evaluate a command andCtrl+Cto exit.
* `SYNTAX.py` has an argument `vis` which, if set to "True" will create a PNG image for the AST of the program. For example:
- Compiler (
COM.py):- Compiles AlgoLang source files (
.algl) into MIPS assembly code (.s). - Supports output format compatible with both SPIM (
spim), or the customEXE.csimulator (VM).
- Compiles AlgoLang source files (
- Assembler (
ASS.py):- Assembles the generated MIPS
.sfile into.hexmachine code suitable for theEXE.csimulator and prints the conversion to the terminal with colored and interpretable binary values for each instruction.
- Supports a MIPS I instruction set plus FPU (COP1) and custom Quantum (COP2) instructions.
- Handles common MIPS pseudo-instructions like
move,li,la,sgtby expanding them into base instructions.
- Assembles the generated MIPS
- Disassembler (
DAS.c) There is also a disassembler that can convert the.hexfile back to.scode. The disassembler output can be cleaned usingclean.pyand then run on SPIM. All this is largely for debugging of the tools and is not intended to be used finally. - Simulator (
EXE.c):- A custom MIPS Virtual Machine that executes
.hexfiles generated byASS.py. This has 3 parts. - Core: Executes MIPS I integer instructions.
- COP1: Executes single-precision floating-point operations (
add.s,sub.s,mul.s,div.s,cvt.s.w,cvt.w.s,mov.s), loads/stores (l.s,s.s), and data movement (mfc1,mtc1). - COP2: Simulates custom quantum instructions (
h,x,cnot,measure,reset,cp) acting on a simulated 5-qubit state vector. - Memory Model: Implements a segmented memory space (Code, Data, Stack, Heap, PPT) with dynamic memory allocation and resizing for the Heap and Parent Pointer Tree (PPT) segments.
- Syscalls: Handles standard SPIM-like syscalls for I/O (printing/reading integers, floats, strings) and exiting.
- Debugging (
-d/--debug): Enables instruction-by-instruction execution tracing, showing the PC, instruction hex, and register states. - Profiling (
-p/--profile): Counts the number of executed instructions categorized by type (R, I, J, COP1, COP2, Syscall, Other) and prints a summary upon completion.
- A custom MIPS Virtual Machine that executes
- Helper Scripts:
run.sh: Compiles an AlgoLang file usingCOM.pyand executes the resulting.sfile using the SPIM simulator.chain.sh: Compiles (COM.py), assembles (ASS.py), and runs (EXE.c) an AlgoLang file.
Running Interpreter
- Run a file directly:
python3 RUN.py your_script.algl - Start the interactive shell:
Inside the shell, type code, press Enter for new lines, and Ctrl+J to evaluate. Type
python3 SHELL.pyexitorquitto leave.
Compiling and Running with VM
-
With SPIM:
./run.sh your_script.algl
This uses
COM.pyto generateyour_script.sand runs it withspim. -
With Custom Simulator (
EXE.c):./chain.sh your_script.algl
This uses
COM.py-->ASS.py-->EXE.cto compile, assemble, and run the code.EXE.cis compiled automatically to the executable (gcc EXE.c -o EXE -lm -std=c99 -Wall)
Debugging and Profiling (with EXE.c)
- Debug Mode: See register states at each step.
# First compile and assemble python3 COM.py your_script.algl --format VM python3 ASS.py your_script.s # Then run EXE with -d ./EXE -d your_script.hex
- Profile Mode: Get instruction execution statistics.
Note: Debug (
# First compile and assemble python3 COM.py your_script.algl --format VM python3 ASS.py your_script.s # Then run EXE with -p ./EXE -p your_script.hex
-d) and Profile (-p) flags can be used together.
- Terminal: The interpreter (
RUN.py) and interactive shell (SHELL.py) provide syntax highlighting in terminals supporting ANSI escape codes. - VS Code: A VS Code extension named
AlgSynis available for syntax highlighting.alglfiles.