|
|
il y a 5 mois | |
|---|---|---|
| examples | il y a 5 mois | |
| src | il y a 5 mois | |
| .gitignore | il y a 5 mois | |
| Makefile | il y a 5 mois | |
| README.md | il y a 5 mois |
BASIC compiler into C
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.
Clone the repository:
git clone https://github.com/yourusername/BASIC-compiler.git
cd BASIC-compiler
Build the compiler (example using make):
make
Compile a BASIC file:
./basic-compiler source.bas
Compile the generated C code:
gcc out.c -o program
Run your program:
./program
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;
}