This is an implementation of custom programming language. It's implemented with bison and flex but I plan in the future to do a fork that uses prolog instead of bison just to experiment a bit more on programming languages.
Clone the repository:
git clone https://github.com/le0nAg/yapl.gitOnce that you are inside run:
makeThis will generate an interpreter that you can use to run programs.
You can create your own source file or use one of the examples in the /prog folder after that run:
./interpreter <path/to/src.prog>
yapl follows a simple compilation pipeline:
- Lexer (Flex) - Tokenizes source code
- Parser (Bison) - Builds Abstract Syntax Tree
- Interpreter - Walks the AST and executes
The language uses a symbol table for variables and functions with lexical scoping.
yapl uses mostly C-style syntax:
// Comments
int x = 10;
float y = 3.14;
str message = "Hello";
bool flag = true;
if (x > 5) {
print("yes");
} else {
print("no");
}
while (x < 20) {
x++;
}int, float, str, bool, void, matrix
- Arithmetic:
+-*/% - Comparison:
<><=>===!= - Logical:
&&||! - Assignment:
=+=-=*=/= - Increment/Decrement:
++-- - Matrix multiplication:
@ - Pattern matching:
~=
fn name(type param1, type param2) return_type {
return value;
}Example:
fn add(int a, int b) int {
return a + b;
}
fn main() void {
int result = add(5, 3);
print(result);
}Built-in functions:
print(...)- Print valuesprintm(matrix)- Print matrix formattedread()- Read input (auto-detects type)
Ranges provide concise iteration syntax:
for (i : 0..10) { // inclusive: 0,1,2,3,4,5,6,7,8,9,10
print(i);
}The parser recognizes .. as a range operator and creates a NODE_RANGE AST node. The interpreter evaluates start and end expressions, then iterates accordingly.
// AST structure
struct range {
ASTNode *start;
ASTNode *end;
ASTNode *step;
}Pattern matching for strings using the ~= operator:
str email = "user@example.com";
if (email ~= ".*@.*\\.com") {
print("Valid email");
}Uses POSIX regex library (<regex.h>):
case NODE_PATTERN_MATCH:
regcomp(®ex, pattern, REG_EXTENDED);
match = regexec(®ex, string, 0, NULL, 0);
regfree(®ex);
return create_bool_value(match == 0);Native matrix support for linear algebra:
matrix A = [[1, 2, 3],
[4, 5, 6]];
matrix B = [[7, 8],
[9, 10],
[11, 12]];
matrix C = A @ B; // Matrix multiplication
printm(C);Matrices are stored as:
typedef struct {
int rows;
int cols;
double **data;
} Matrix;Array literals [[1,2],[3,4]] are converted to matrices. The @ operator performs standard matrix multiplication without dimension checking.
See /prog folder for examples.