A basic programming language and compiler built in Python.
- Data Types:
- Integers
- Floating-point numbers
- Strings (with interpolation)
- Booleans
- Operators:
- Arithmetic: +, -, *, /
- Comparison: ==, !=, <, >, <=, >=
- Logical: !, &&, ||
- Control flow (if/else, while loops)
- Variables and assignments
- Print statement
- Single-line and multi-line comments
- String interpolation with
${expression}syntax
This compiler supports two execution modes:
- AST Interpretation - Directly interprets the abstract syntax tree
- Bytecode Compilation - Compiles to bytecode and runs on a virtual machine (faster)
var name = value;
name = value;
print expression;
if (condition) {
statements;
} else {
statements;
}
while (condition) {
statements;
}
{
statement1;
statement2;
...
}
// Single-line comment
/*
Multi-line
comment
*/
// Integers
var a = 42;
// Floating-point numbers
var pi = 3.14159;
// Strings
var greeting = "Hello, World!";
// Booleans
var isTrue = true;
var isFalse = false;
var name = "World";
print "Hello, ${name}!"; // Outputs: Hello, World!
var a = 10;
var b = 20;
print "The sum of ${a} and ${b} is ${a + b}"; // Outputs: The sum of 10 and 20 is 30
// Complex expressions are supported
var x = 5;
print "The square of ${x} is ${x * x}"; // Outputs: The square of 5 is 25
// Arithmetic operators
var sum = a + b;
var difference = a - b;
var product = a * b;
var quotient = a / b;
// Comparison operators
var isEqual = a == b;
var isNotEqual = a != b;
var isLess = a < b;
var isGreater = a > b;
var isLessOrEqual = a <= b;
var isGreaterOrEqual = a >= b;
// Logical operators
var not = !isTrue;
var and = isTrue && isTrue;
var or = isTrue || isFalse;
Run with bytecode compilation (default and faster):
python run.py examples/sample.txt
Run with AST interpretation:
python run.py examples/sample.txt --interpret
Debug bytecode:
python run.py examples/sample.txt --bytecode --debug
Several example programs are included in the examples/ directory to demonstrate language features:
arithmetic.txt- Basic arithmetic operationsstrings.txt- String manipulationconditionals.txt- If/else statementsloops.txt- While loopsfizzbuzz.txt- Classic FizzBuzz problemprimes.txt- Find prime numbersdata_types.txt- Demonstrate boolean and float typesstring_interpolation.txt- Examples of string interpolation
The bytecode compiler translates the AST into a sequence of stack-based instructions that can be executed by the virtual machine. This provides:
- Better performance - Bytecode execution is faster than AST interpretation
- Smaller memory footprint - Bytecode is more compact than the AST
- Potential for optimization - Bytecode can be optimized before execution
The bytecode includes:
- Operation codes (opcodes) for each instruction
- A constants pool for literals (numbers, strings)
- Variable storage indexed by name