Fluent is a high-level, statically-typed programming language designed for simplicity, safety, and expressiveness. This repository contains a basic compiler for Fluent, written in C. The compiler currently translates Fluent code into C code, which can then be compiled with a C compiler like GCC.
Fluent was written as an experiment in creating a programming language and is neither complete nor 100% bug-free. It is intended for educational purposes and as a learning experience for the author.
The Fluent compiler currently supports:
- Variables and Assignments: Immutable (
let
) and mutable (var
) variable declarations. - Binary Operations: Arithmetic operations with correct operator precedence.
- Function Declarations: Definition of functions without parameters.
- Control Flow Statements:
if
statements withelse
clauses,while
loops. - Indentation-Based Blocks: Uses indentation to define code blocks, similar to Python.
- C Compiler: GCC or Clang compatible with C99 or later.
- Make: For using the provided Makefile.
Clone the repository and navigate to the project directory:
git clone https://github.com/yourusername/fluent_compiler.git
cd fluent_compiler
Compile the compiler using the provided Makefile:
make
This will generate the fluentc
executable in the project root.
To compile a Fluent source file (.flu
extension) into C code:
./fluentc path/to/your_program.flu > output.c
Compile the generated C code:
gcc -o output output.c
Run the executable:
./output
- Immutable Variable Declaration:
let x = 10
- Mutable Variable Declaration:
var y = 20
- Assignment:
x = x + y
-
Function Declaration:
func main(): # Function body
-
Note: Currently, functions cannot have parameters or return values.
-
If Statement:
if condition: # Then block else: # Else block
-
While Loop:
while condition: # Loop body
- Indentation is significant and used to define code blocks.
- Use consistent spaces (e.g., 4 spaces) for indentation.
func main():
let x = 10
var y = 20
if x < y:
print("x is less than y")
else:
print("x is greater than or equal to y")
x = x + y
print(x)
-
Compile the Fluent Program to C Code:
./fluentc test.flu > output.c
-
Compile the Generated C Code:
gcc -o output output.c
-
Run the Executable:
./output
Expected Output:
x is less than y
30
- Function Calls and Parameters: Function calls and parameter passing are not yet implemented.
- Standard Library Functions: The
print
function is not implemented in the code generator. You'll need to modify the generated C code to includeprintf
statements. - Data Types: Only integer variables are supported. No support for floats, strings (except as literals in
print
), or other data types. - Error Handling: Limited error messages and handling in the lexer and parser.
- Semantic Analysis: No type checking or scope management beyond basic parsing.
- Standard Library: No standard library functions are available.
- Function Parameters and Calls: Implement parsing and code generation for function parameters and function calls.
- Type System: Develop a type system with type inference and support for multiple data types.
- Standard Library: Create a standard library with common functions like
print
,input
, etc. - Enhanced Error Handling: Improve error reporting with detailed messages and recovery mechanisms.
- Semantic Analysis: Implement a semantic analysis phase for type checking and scope resolution.
- Optimizations: Add optimization passes to improve generated code performance.
- Platform Support: Ensure compatibility across different operating systems and architectures.
Contributions are welcome! Feel free to submit issues, fork the repository, and make pull requests.
This project is licensed under the MIT License. See the LICENSE file for details.
Note: This compiler is a work in progress and is intended for educational purposes. It provides a foundation for further development and learning about compiler design.