Skip to content

Latest commit

 

History

History
25 lines (15 loc) · 2.24 KB

compiler.md

File metadata and controls

25 lines (15 loc) · 2.24 KB

The SimpleFast compiler, which we'll call sfastc, needs to process .sfast files and generate executable binaries. When you run a SimpleFast file, the compiler interprets the source code, translates it into an intermediate representation (IR), optimizes this IR, and finally generates machine code.

Since SimpleFast is designed to be similar to Python but with performance close to C++, one way to implement sfastc could be to use a Python interpreter to parse the SimpleFast code and a C++ compiler to generate the final executable. However, this requires a translation layer that converts the Python-like SimpleFast syntax into equivalent C++ code.

Here's a simplified view of how this might work:

  1. Lexing and Parsing: The lexer breaks the SimpleFast code into tokens (words, numbers, symbols, etc.), and the parser checks if these tokens follow the grammar rules of SimpleFast. If the code is syntactically correct, the parser generates an abstract syntax tree (AST). You can use existing Python tools like PLY (Python Lex-Yacc) for this step, as the syntax of SimpleFast is similar to Python.

  2. Translation: The translator walks through the AST and generates equivalent C++ code. This is a crucial step as it involves mapping the high-level constructs of SimpleFast to equivalent C++ constructs. For instance, a SimpleFast function would be translated into a C++ function, a SimpleFast class into a C++ class, and so on.

  3. Compilation: The generated C++ code is then compiled using a C++ compiler (like g++ or clang) into an executable binary. The compiler also optimizes the code during this step for better runtime performance.

To make it even simpler for users, you can provide a command-line tool (also named sfastc or just sfast) that encapsulates all these steps. This tool would take a .sfast file as input and produce an executable as output:

sfastc my_program.sfast

This command would compile the my_program.sfast file and generate an executable (say, my_program.exe on Windows or my_program on Linux/Mac). You can then run this executable to run your SimpleFast program:

./my_program

This process makes it straightforward for users to compile and run SimpleFast programs while benefiting from the performance of C++.