This is Cf, an in-progress compiler that forms a minimal programming language meant to be written in French and with C-like syntax. In the future, the intention is to integrate some nicer, Python-like features. The compiler generates x86_64 assembly code for macOS.
Cf currently supports:
- Basic arithmetic operations (+, -, *, /)
- Comparison operations (==, !=, <, <=, >, >=)
- Integer literals
- Expression grouping with parentheses
- Variable declarations and assignments with
soit
(roughly semantically equivalent tolet
) - Control flow for if-else statements with
si
andsinon
(semantically equivalent toif
andelse
) - Return statements with
rendre
(semantically equivalent toreturn
) - Variable references in expressions
- Scoping with
{
and}
- Error handling:
- Syntax error detection with line and column numbers
- Variable redeclaration checks
- Undefined variable usage detection
- Missing semicolon detection
- Invalid operator usage detection
- Stack overflow protection for complex expressions
- Variables are declared using the
soit
keyword - Return statements use the
rendre
keyword - If-else statements use the
si
andsinon
keywords - Statements must end with semicolons
- Expressions follow standard operator precedence rules
- Parentheses
()
- Multiplication and division
*
,/
- Addition and subtraction
+
,-
- Comparison operators
==
,!=
,<
,<=
,>
,>=
rendre (((6 + 6) / 2) * 3) / 9 - 2; // returns 0
This example demonstrates nested arithmetic expressions and operator precedence.
soit x1 = 4;
soit y1 = 8;
soit x2 = 7;
soit y2 = 11;
soit dx = x2 - x1;
soit dy = y2 - y1;
soit distanceCarre = dx * dx + dy * dy;
rendre distanceCarre;
This example calculates the squared distance between two points.
soit valeur1 = 2;
soit valeur2 = 3;
soit resultatSuperieurOuEgal1 = valeur1*4 >= valeur2;
soit resultatSuperieurOuEgal2 = valeur1*3 >= valeur2*2;
soit resultatInferieurOuEgal1 = valeur1 <= valeur2;
soit resultatInferieurOuEgal2 = valeur1*3 <= valeur2*2;
soit resultatEgal1 = valeur1*3 == valeur2*2;
soit resultatEgal2 = valeur1 == valeur2;
soit resultatDifferent1 = valeur1 != valeur2;
soit resultatDifferent2 = valeur1*3 != valeur2*2;
soit resultatSuperieur1 = valeur1*4 > valeur2;
soit resultatSuperieur2 = valeur1*3 > valeur2*2;
soit resultatInferieur1 = valeur1 < valeur2;
soit resultatInferieur2 = valeur1*3 < valeur2*2;
rendre resultatSuperieurOuEgal1 + resultatSuperieurOuEgal2 +
resultatInferieurOuEgal1 + resultatInferieurOuEgal2 +
resultatEgal1 + resultatEgal2 + resultatDifferent1 + resultatDifferent2 +
resultatSuperieur1 + resultatSuperieur2 +
resultatInferieur1 + resultatInferieur2; // expected return value of 8
This example demonstrates all comparison operators.
soit x = 2;
si (x < 3) {
rendre 1;
} sinon {
rendre 0;
}
This example demonstrates simple control flow with a si-sinon statement.
soit valeur1 = 7;
soit valeur2 = 9;
soit valeur3 = 15;
si (valeur1 + valeur2 < valeur3) {
si (valeur1 - valeur2 > 0) {
rendre (valeur3 - valeur1) + valeur2;
} sinon {
rendre valeur2 + valeur3;
}
} sinon {
si (valeur1 + valeur3 > valeur2 * 4) {
rendre (valeur1 + valeur2) + valeur3;
} sinon {
si (valeur3 + 1 == valeur1 + valeur2) {
si (valeur2 - valeur1 - 2 >= 0) {
rendre ((valeur1 * valeur2) + valeur3) / 2; // expected to return 39
} sinon {
rendre (valeur1 + valeur3) + valeur2;
}
} sinon {
rendre (valeur1 + valeur2 + valeur3) * 2 - 4;
}
}
}
This example demonstrates a more complex control flow with nested si-sinon statements.
Within the sampleAsm
and sampleCfFiles
directories, there are a collection of .asm
and .cf
files, respectively tests1-6.asm
and v1-6CfSample.cf
which contain both Cf code snippets and their associated compiled assembly files. These should serve as a reference for what Cf code might look like.
If you wish to build and run in the Makefile with make full
(see more immediately below), the reference files test6.asm
and v6CfSample.cf
will be targeted, so you may want to modify v6CfSample.cf
with your own Cf code. Alternatively, you may also adjust the default targets in the Makefile yourself or manually compile with your own specified args, as below.
- GCC/Clang: Apple Clang version 16.0.0
- NASM: >= 2.16.03
- Make: GNU Make 3.81
- macOS
make
./cf yourInput.cf yourPreferredOutputFileName.asm
This will generate an assembly file named yourPreferredOutputFileName.asm
.
make full CF_FILE=yourInput.cf ASM_FILE=yourPreferredOutputFileName.asm
or just
make full
to run with default targets defined in Makefile.
This will:
- Compile your Cf program to assembly
- Assemble it to an object file
- Link it into an executable
- Run the executable and display its return value
make clean
The compiler follows a traditional compilation pipeline:
- Lexical analysis (tokenizer)
- Parsing (recursive descent parser)
- AST generation
- Code generation (x86_64 assembly)