mirror of
https://github.com/sigmasternchen/macrofuck
synced 2025-03-15 07:08:56 +00:00
feat: Begin of proprocessor; including files now works
This commit is contained in:
parent
2d41031965
commit
af20b29691
6 changed files with 68 additions and 0 deletions
|
@ -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); }
|
||||
|
|
|
@ -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
|
||||
|
||||
%%
|
||||
|
|
41
compiler/src/preprocessor.c
Normal file
41
compiler/src/preprocessor.c
Normal 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;
|
||||
}
|
||||
|
3
compiler/test/cases/025a-preproc-include.in
Normal file
3
compiler/test/cases/025a-preproc-include.in
Normal file
|
@ -0,0 +1,3 @@
|
|||
#include "025a-preproc-include.include"
|
||||
|
||||
print(" World!\n");
|
1
compiler/test/cases/025a-preproc-include.include
Normal file
1
compiler/test/cases/025a-preproc-include.include
Normal file
|
@ -0,0 +1 @@
|
|||
print("Hello,");
|
2
compiler/test/cases/025a-preproc-include.out
Normal file
2
compiler/test/cases/025a-preproc-include.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++<<<<<.>.>.>.>.>.
|
||||
<<<<<[-]++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++>[-]++++++++++<<<<<<<.>.>.>.>.>.>.>.
|
Loading…
Reference in a new issue