Skip to content
/ Cf Public

Cf (C-french): French-flavored programming language compiler

Notifications You must be signed in to change notification settings

b3nkang/Cf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cf (C-french) Compiler

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.

Language Features

Cf currently supports:

  • Basic arithmetic operations (+, -, *, /)
  • Comparison operations (==, !=, <, <=, >, >=)
  • Integer literals
  • Expression grouping with parentheses
  • Variable declarations and assignments with soit (roughly semantically equivalent to let)
  • Control flow for if-else statements with si and sinon (semantically equivalent to if and else)
  • Return statements with rendre (semantically equivalent to return)
  • 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

Syntax

  • Variables are declared using the soit keyword
  • Return statements use the rendre keyword
  • If-else statements use the si and sinon keywords
  • Statements must end with semicolons
  • Expressions follow standard operator precedence rules

Operator Precedence (highest to lowest)

  1. Parentheses ()
  2. Multiplication and division *, /
  3. Addition and subtraction +, -
  4. Comparison operators ==, !=, <, <=, >, >=

Examples

Basic Arithmetic

rendre (((6 + 6) / 2) * 3) / 9 - 2; // returns 0

This example demonstrates nested arithmetic expressions and operator precedence.

Variable Declaration

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.

Comparison Operations

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.

Simple Control Flow

soit x = 2;
si (x < 3) {
    rendre 1;
} sinon {
    rendre 0;
}

This example demonstrates simple control flow with a si-sinon statement.

Complex Nested Control Flow

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.

Reference Files

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.

Building and Running

Prerequisites

  • GCC/Clang: Apple Clang version 16.0.0
  • NASM: >= 2.16.03
  • Make: GNU Make 3.81
  • macOS

Building the Compiler

make

Compiling a Cf Program

./cf yourInput.cf yourPreferredOutputFileName.asm

This will generate an assembly file named yourPreferredOutputFileName.asm.

Running a Complete Build Pipeline

make full CF_FILE=yourInput.cf ASM_FILE=yourPreferredOutputFileName.asm

or just

make full

to run with default targets defined in Makefile.

This will:

  1. Compile your Cf program to assembly
  2. Assemble it to an object file
  3. Link it into an executable
  4. Run the executable and display its return value

Cleaning Build Files

make clean

Implementation Details

The compiler follows a traditional compilation pipeline:

  1. Lexical analysis (tokenizer)
  2. Parsing (recursive descent parser)
  3. AST generation
  4. Code generation (x86_64 assembly)

Grammar

grammar_latex_cogs

About

Cf (C-french): French-flavored programming language compiler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published