Building a very basic compiler in Go. It will compile BASIC to C code. Using this experience to learn Golang.
- Language currently undergoing changes, trying to add functions
program -> functions
functions -> function functions
| main
function -> ident LPAREN param_list RPAREN LBRACE statements RETURN expression RBRACE
main -> MAIN LPAREN RPAREN LBRACE statements RETURN expression RBRACE
param_list -> expression COMMA param_list
| expression
| ε
statements -> statement statements
| statement
statement -> PRINT (expression | string)
| IF comparison THEN statements ENDIF
| WHILE comparison REPEAT statement ENDWHILE
| LABEL ident
| GOTO ident
| LET ident "=" expression
| INPUT ident
comparison -> expression (("==" | "!=" | ">" | ">=" | "<" | "<=") expression)+
expression -> term {( "-" | "+" ) term}
| function_call
term -> unary {( "/" | "*" ) unary}
unary -> ["+" | "-"] primary
primary -> number | ident | function_call
function_call -> ident LPAREN args RPAREN
args -> expression COMMA args
| expression
| ε
This compiler works by compiling your BASIC code -> C code -> Executable.
You may use compile.sh which takes an input file and will create the necessary executable.
Use it like this
bash compile.sh {sample}.teeny
Then, check the exec/ folder and you should have an executable named {sample}.
I'm currently working on adding more features to this compiler, such as functions and return types, logical operators, and more.