BASIC compiler into C

AbderFaiz 87d9d62b6a update README пре 11 месеци
examples 6b464dfa7b update makefile and add an example пре 11 месеци
src 4284817373 First version of Basic Compiler пре 11 месеци
.gitignore 5dee9fbce4 Add README and examples пре 11 месеци
Makefile 5dee9fbce4 Add README and examples пре 11 месеци
README.md 87d9d62b6a update README пре 11 месеци

README.md

BASIC-compiler

BASIC compiler into C

Overview

This project is a compiler that translates BASIC source code into C.

Note:
This repository includes an implementation in C++ of the "Teeny Tiny Compiler" tutorial by Austin Henley.
The code follows the steps and concepts from the tutorial, providing a minimal compiler example for educational purposes.

Features

  • Parses a subset of the BASIC language
  • Generates readable and compilable C code
  • Command-line interface for compiling BASIC files

Usage

  1. Clone the repository:

    git clone https://github.com/yourusername/BASIC-compiler.git
    cd BASIC-compiler
    
  2. Build the compiler (example using make):

    make
    
  3. Compile a BASIC file:

    ./basic-compiler source.bas
    
  4. Compile the generated C code:

    gcc out.c -o program
    
  5. Run your program:

    ./program
    

Example

Given a BASIC file examples/factorial:

LABEL START
PRINT "Enter a number to compute its factorial:"
INPUT n
PRINT ""

LET result = 1

WHILE n > 1 REPEAT
    LET result = result * n
    LET n = n - 1
ENDWHILE
PRINT "The factorial is:"
PRINT result
PRINT ""
GOTO START

Compile and run:

./basic-compiler examples/factorial
gcc out.c -o factorial
./factorial

Produced c code is:

#include <stdio.h>
int main(void){
float n;
float result;
START: printf("Enter a number to compute its factorial:\n");
if (0 == scanf("%f", &n)){
n = 0;
scanf("%*s");
}
printf("\n");
result = 1;
while(n>1){
result = result*n;
n = n-1;
}
printf("The factorial is:\n");
printf("%.2f\n", (float)(result));
printf("\n");
goto START;
return 0;
}

TODO

  • Parentheses for expressions
  • Logical operators (and, or, not)
  • ELSE IF and ELSE
  • FOR loop
  • Number literals written in binary, hex, and octal
  • Better compiler errors (e.g., what line the error occurred)
  • Allow multiple code files
  • Functions with parameters and return values
  • Lexical scope (see scope)
  • Standard library (e.g., file operations)
  • Abstract syntax tree representation
  • More primitive types (e.g., integer, strings, boolean)
  • Arrays
  • Record types (i.e., structs or tuples)
  • Type checking (see type systems)
  • Compiler optimizations (e.g., constant folding)
  • Test cases for the compiler (see unit testing and test-driven development)