A magical programming language built with ANTLR4, where syntax is based on wizard vocabulary.
- Numbers (tome):
42,3.14 - Strings (scroll):
"hello world" - Booleans:
truth,falsehood
| Feature | Syntax | Example |
|---|---|---|
| Variables | brew name = value; |
brew power = 42; |
| Functions | cast name(params) { ... } |
cast amplify(n) { reveal n * 2; } |
| If/Else | divine (cond) { ... } otherwise { ... } |
divine (x > 0) { speak "positive"; } |
| While Loop | chant (cond) { ... } |
chant (i < 5) { ... } |
| For Loop | enchant (init; cond; step) { ... } |
enchant (i = 0; i < 5; i = i + 1) { ... } |
| Output | speak expr; |
speak "hello"; |
| Return | reveal expr; |
reveal x * 2; |
- Arithmetic:
+-*/ - Logical:
andornot - Comparison:
<<=>>===!=
brew power = 42;
cast amplify(n) {
reveal n * 2;
}
divine (power > 10) {
speak "The spell is mighty!";
} otherwise {
speak "The spell is weak.";
}
brew i = 0;
chant (i < 5) {
speak amplify(i);
brew i = i + 1;
}
Output:
The spell is mighty!
0
2
4
6
8
cast factorial(n) {
divine (n <= 1) {
reveal 1;
} otherwise {
reveal n * factorial(n - 1);
}
}
speak factorial(5);
Output: 120
brew x = 10;
brew y = 20;
speak x + y;
speak "hello" + " " + "world";
brew flag = truth;
divine (flag) {
speak "flag is true";
}
Output:
30
hello world
flag is true
SpellScript/
├── grammar/
│ └── SpellScript.g4 # ANTLR4 grammar definition
├── src/
│ ├── SpellScript.java # Main interpreter
│ ├── SpellScriptLexer.java # Generated lexer
│ ├── SpellScriptParser.java # Generated parser
│ ├── SpellScriptVisitor.java # Generated visitor
│ └── SpellScriptBaseVisitor.java # Generated base
├── samples/
│ ├── example.spell # Main demo
│ ├── basic.spell # Basic operations
│ └── functions.spell # Recursion example
├── build.sh # Build script
└── README.md # This file
- Java JDK 11+ (for compilation)
- Java Runtime (for execution)
- ANTLR4 JAR (included in
.local/lib/)
./build.sh./build.sh samples/example.spellThe ANTLR4 grammar (grammar/SpellScript.g4) defines:
- Lexer: Keywords, operators, identifiers, numbers, strings, comments
- Parser: Program structure, statements, expressions with proper precedence
- Visitor: Tree-walking interpreter for execution
Key rules:
program: Sequence of statementsstatement: Variable declaration, function definition, conditionals, loops, expressionsexpr: Binary/unary operations with correct operator precedenceprimary: Literals, identifiers, function calls, parenthesized expressions
The interpreter uses the visitor pattern to traverse the ANTLR parse tree:
- Lexer tokenizes the input (SpellScriptLexer.java)
- Parser builds parse tree (SpellScriptParser.java)
- Interpreter visits nodes and evaluates (SpellScript.java)
- Maintains variable and function symbol tables
- Handles function calls with local scope
- Implements return values with flag-based control flow
The interpreter provides meaningful error messages for:
- Undefined variables:
error: variable 'x' not brewed - Undefined functions:
error: function 'foo' not cast - Type errors:
error: cannot convert to number - Division by zero:
error: divide by zero - Syntax errors from ANTLR parser
Run sample programs to verify all features:
./build.sh samples/example.spell # All features
./build.sh samples/basic.spell # Variables, operators
./build.sh samples/functions.spell # RecursionCreated for Programming Languages course assignment