feat: Begin of proprocessor; including files now works

This commit is contained in:
overflowerror 2024-05-17 20:19:03 +02:00
parent 2d41031965
commit af20b29691
6 changed files with 68 additions and 0 deletions

View file

@ -16,6 +16,8 @@ in "in"
char "'"([^\\]|[\\]n|[\\]t|[\\]{2})"'"
id [a-zA-Z_][a-zA-Z0-9_]*
include "#include"
%option noyywrap
%option nodefault
%option nounput
@ -29,6 +31,7 @@ id [a-zA-Z_][a-zA-Z0-9_]*
%{
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
@ -37,6 +40,11 @@ id [a-zA-Z_][a-zA-Z0-9_]*
#include "y.tab.h"
extern void push_file_to_yy_stack(FILE* file) {
YY_BUFFER_STATE state = yy_create_buffer(file, YY_BUF_SIZE);
yypush_buffer_state(state);
}
char char_escape(const char* yytext) {
if (yytext[1] == '\\') {
if (yytext[2] == '\\') return '\\';
@ -66,6 +74,8 @@ strbuf_t strbuf = NULL;
<INITIAL>"//"[^\n]* { /* single line comment */ }
<INITIAL>{include} { return PRE_INCLUDE; }
<INITIAL>";" { return SEMICOLON; }
<INITIAL>\" { BEGIN STRING; strbuf_clear(strbuf); }
@ -127,4 +137,11 @@ strbuf_t strbuf = NULL;
<INITIAL>{id} { yylval.id = strdup(yytext); return ID; }
<INITIAL>{whitespace}+ { }
<<EOF>> {
yypop_buffer_state();
if (!YY_CURRENT_BUFFER) {
yyterminate();
}
}
<INITIAL>. { lex_panic("unknown token: %s\n", yytext); }

View file

@ -12,7 +12,9 @@
#include "ast.h"
#define yylex preproc_lex
int yylex(void);
void yyerror(const char*);
extern struct block* program;
@ -76,6 +78,8 @@ extern struct block* program;
%token RMAP
%token IN
%token PRE_INCLUDE
%start file
%%

View file

@ -0,0 +1,41 @@
#include <stdio.h>
#include "y.tab.h"
#include <error.h>
void push_file_to_yy_stack(FILE*);
int yylex(void);
extern char* yytext;
extern YYSTYPE yylval;
static void handle_include(void) {
int token = yylex();
if (token != STR) {
fprintf(stderr, "preprocessor: include: file name expected, got: %s\n", yytext);
panic("processor syntax error");
}
const char* filename = yylval.str;
FILE* file = fopen(filename, "r");
if (file == NULL) {
panic("file not found");
}
push_file_to_yy_stack(file);
}
extern int preproc_lex(void) {
int token = yylex();
switch(token) {
case PRE_INCLUDE:
handle_include();
token = yylex();
break;
}
return token;
}

View file

@ -0,0 +1,3 @@
#include "025a-preproc-include.include"
print(" World!\n");

View file

@ -0,0 +1 @@
print("Hello,");

View file

@ -0,0 +1,2 @@
[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++<<<<<.>.>.>.>.>.
<<<<<[-]++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<<<.>.>.>.>.>.>.>.