AxScript is a simple programming language implemented in C++ that supports basic programming constructs. This project demonstrates the implementation of a lexer, parser, and interpreter for a custom programming language.
Current features include:
- Variable declarations and initialization
- Basic arithmetic operations (+, -, *, /)
- Print statements
- Number and string literals
- User input
- Control flow
- Loops
- Error handling and recovery
- REPL (Read-Eval-Print Loop) interface
Example code:
var x = 42;
print x;
var result = x + 8;
print result;
var num;
print "Enter a number : ";
input num;
print num;
var num = 7;
compeq (num, 5) {
print "num is equal to 5";
}
compneq (num, 5) {
print "num is not equal to 5";
}
compge (num, 5) {
print "num is greater than or equal to 5";
}
comple (num, 5) {
print "num is less than or equal to 5";
}
print "end";
loop i = 1 to 5 {
print i;
}
loop i = 5 to 1 down {
print i;
}
.
├── src/
│ ├── ast.h # Abstract Syntax Tree definitions
│ ├── environment.h # Variable environment management
│ ├── interpreter.h # Code interpretation logic
│ ├── lexer.cpp # Lexical analysis implementation
│ ├── lexer.h # Lexer header
│ ├── main.cpp # Entry point
│ ├── parser.h # Parser implementation
│ ├── tokens.cpp # Token utilities
│ ├── tokens.h # Token definitions
│ └── visitor.h # Visitor pattern implementation
- C++ compiler with C++11 support or later
- CMake (version 3.0 or higher)
- Clone the repository:
git clone https://github.com/sthaarwin/axscript.git
cd axscript
- Generate build files and compile:
make all
- Run the executable:
./src/output/axscript
./axscript script.axp
./axscript
>> print "hello world!";
hello world!
var name = value;
print expression;
input variableName;
compeq (left, right) { ... }
: Executes the block ifleft
is equal toright
.compneq (left, right) { ... }
: Executes the block ifleft
is not equal toright
.compge (left, right) { ... }
: Executes the block ifleft
is greater than or equal toright
.comple (left, right) { ... }
: Executes the block ifleft
is less than or equal toright
.
Example:
compeq (x, 10) {
print "x is 10";
}
compneq (x, 10) {
print "x is not 10";
}
compge (x, 5) {
print "x is greater than or equal to 5";
}
comple (x, 15) {
print "x is less than or equal to 15";
}
loop var=initial-value to max-range{}
loop var=initial-value to min-range down{}
- Arithmetic:
+
,-
,*
,/
- Numbers: Integer or floating-point
- Strings: In double quotes
- Variables: Reference by name
- Loop:
loop i = 1 to 5 {}
,loop i = 5 to 1 down {}
The interpreter provides error messages for:
- Syntax errors
- Undefined variables
- Type mismatches
- Missing semicolons
- Invalid expressions
- Fork the repository
- Create a feature branch (
git checkout -b feature/YourFeature
) - Commit your changes (
git commit -m 'Add some feature'
) - Push to the branch (
git push origin feature/YourFeature
) - Open a Pull Request
- Control flow (comparison statements)
- Loops
- Functions
- More data types (boolean, arrays)
- Standard library
- Better error messages
- Code optimization
- Inspired by various programming language implementation tutorials and books
- Built using modern C++ features and best practices
Arwin Shrestha
Feel free to open issues or contribute to the project!