author | Alan Dipert
<alan@dipert.org> 2023-11-26 22:42:59 UTC |
committer | Alan Dipert
<alan@dipert.org> 2023-11-26 22:42:59 UTC |
.gitignore | +4 | -0 |
Makefile | +29 | -0 |
README.md | +3 | -0 |
bbasic.l | +18 | -0 |
bbasic.y | +47 | -0 |
main.c | +10 | -0 |
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..755862a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bbasic.tab.c +bbasic.tab.h +bbasic +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e0077a0 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +CC=gcc +CFLAGS=-Wall -g +LEX=flex +YACC=bison +YFLAGS=-d + +all: bbasic + +bbasic: lex.yy.o bbasic.tab.o main.o + $(CC) $(CFLAGS) -o bbasic lex.yy.o bbasic.tab.o main.o -lfl + +main.o: main.c + $(CC) $(CFLAGS) -c main.c + +lex.yy.o: lex.yy.c bbasic.tab.h + $(CC) $(CFLAGS) -c lex.yy.c + +bbasic.tab.o: bbasic.tab.c + $(CC) $(CFLAGS) -c bbasic.tab.c + +lex.yy.c: bbasic.l + $(LEX) bbasic.l + +bbasic.tab.c bbasic.tab.h: bbasic.y + $(YACC) $(YFLAGS) bbasic.y + +clean: + rm -f bbasic lex.yy.c bbasic.tab.c bbasic.tab.h *.o + diff --git a/README.md b/README.md new file mode 100644 index 0000000..48fd101 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Buckaroo Basic + +WIP diff --git a/bbasic.l b/bbasic.l new file mode 100644 index 0000000..30e77b4 --- /dev/null +++ b/bbasic.l @@ -0,0 +1,18 @@ +%{ +#include "bbasic.tab.h" +%} + +%% + +[ \t]+ ; /* Ignore whitespaces */ +\n return EOL; +"print" return PRINT; +"if" return IF; +"then" return THEN; +"goto" return GOTO; +"=" return EQUALS; +[0-9]+ { yylval.num = atoi(yytext); return NUMBER; } +[a-zA-Z][a-zA-Z0-9]* { yylval.str = strdup(yytext); return IDENTIFIER; } +. return *yytext; + +%% diff --git a/bbasic.y b/bbasic.y new file mode 100644 index 0000000..f2350db --- /dev/null +++ b/bbasic.y @@ -0,0 +1,47 @@ +%{ +#include <stdio.h> +#include <stdlib.h> +extern int yylex(); +extern int yyparse(); +extern FILE *yyin; + +void yyerror(const char *s) { + fprintf(stderr, "Error: %s\n", s); +} +%} + +%union { + int num; + char *str; +} + +%token <num> NUMBER +%token <str> IDENTIFIER +%token EOL PRINT IF THEN GOTO EQUALS + +%left '+' '-' +%left '*' '/' + +%% +program: + | program line + ; + +line: + NUMBER statement EOL + ; + +statement: + PRINT expression + | IF expression THEN statement + ; + +expression: + IDENTIFIER + | NUMBER + | expression '+' expression + | expression '-' expression + | expression '*' expression + | expression '/' expression + ; +%% diff --git a/main.c b/main.c new file mode 100644 index 0000000..5acd07f --- /dev/null +++ b/main.c @@ -0,0 +1,10 @@ +#include <stdio.h> +#include "bbasic.tab.h" + +extern int yyparse(); + +int main() { + printf("Welcome to Buckaroo Basic!\n"); + yyparse(); // Start parsing + return 0; +}