From 4d47c07fd2a768819c68b467640d4b669091225d Mon Sep 17 00:00:00 2001 From: anon Date: Thu, 22 Feb 2024 20:46:05 +0100 Subject: [PATCH] init --- .gitignore | 3 ++ Makefile | 14 ++++++++++ farmsay.l | 19 +++++++++++++ farmsay.y | 43 ++++++++++++++++++++++++++++ main.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ object/.gitkeep | 0 6 files changed, 153 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 farmsay.l create mode 100644 farmsay.y create mode 100644 main.cpp create mode 100644 object/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3baed8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.gdb_history +farmsay +object/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..660a705 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +object/%.yy.cpp: %.l + flex --header-file=object/$(basename $(notdir $<)).yy.h -o $@ $< + +object/%.tab.cpp: %.y + bison --header=object/$(basename $(notdir $<)).tab.h -o $@ $< + +farmsay: main.cpp object/farmsay.tab.cpp object/farmsay.yy.cpp + g++ -Iobject/ $+ -o farmsay -ggdb + +clean: + -rm object/* + +test: + -./farmsay < <(printf "I'me! Megmutatom ne'ktek az utolso' embert. Mi az, hogy szerelem? Mi az, hogy teremte's? Mi az, hogy va'gy? Mi az, hogy csillag? - i'gy ke'rdezo''sko:dik az utolso' ember e's vaksin pislog.") diff --git a/farmsay.l b/farmsay.l new file mode 100644 index 0000000..0223358 --- /dev/null +++ b/farmsay.l @@ -0,0 +1,19 @@ +%{ + #include "farmsay.tab.h" + extern void yyerror(...); +%} + +%option noyywrap +%option nodefault + +%% + +{ +\.[[:space:]]* { return DOT; } +\![[:space:]]* { return BANG; } +\?[[:space:]]* { return QUESTIONMARK; } +. { yylval.strval = yytext; return STRING; } +\n { ; } +} + +%% diff --git a/farmsay.y b/farmsay.y new file mode 100644 index 0000000..ee5bdd6 --- /dev/null +++ b/farmsay.y @@ -0,0 +1,43 @@ +%code{ + extern void yyerror(...); +} +%{ + #include "farmsay.yy.h" + + #include + #include + + extern void bison_say(const char * const s); + extern void snake_say(const char * const s); + extern void sheep_say(const char * const s); + + using namespace std; +%} + +%union { + char * strval; +} + +%token DOT BANG QUESTIONMARK +%token STRING +%type sentence snake sheep bison + +%% + +speech: %empty + | speech snake + | speech bison + | speech sheep + ; + +snake: sentence DOT { snake_say($$); }; +bison: sentence BANG { bison_say($$); }; +sheep: sentence QUESTIONMARK { sheep_say($$); }; + +sentence: STRING + | sentence STRING + ; + +%% + +void yyerror(...) { puts("\033[31m\033[7m!!!\033[0m"); } diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..1606fdc --- /dev/null +++ b/main.cpp @@ -0,0 +1,74 @@ +#include +#include + +#include "farmsay.tab.h" + +inline +void line(const char c, const int n) { + putc(' ', stdout); + for (int i = 0; i < n-2; i++) { + putc(c, stdout); + } + putc('\n', stdout); +} + +void frame(const char f, const int b, const char * s) { + const int WIDTH = 24; + + line(f, WIDTH); + + const int l = strlen(s); + for (int i = 0; i < (l / (WIDTH-4)) + 1; i++) { + printf("%c %-20.20s %c\n", f, s + (i*(WIDTH-4)), f); + } + + line(f, WIDTH); + + for (int i = 0; i < b; i++) { + putc(' ', stdout); + } + + fputs("|/\n", stdout); +} + +void bison_say(const char * const s) { + const char * const BISON = + " .xxx###;=>\n" + ":xxxx####> \n" + " L p \n" + ; + fputs("\033[31m", stdout); + frame('*', 12, s); + puts(BISON); + fputs("\033[0m", stdout); +} + +void snake_say(const char * const s) { + const char * const SNAKE = + " .~-\n" + " '. \n" + " :..;\n" + ; + fputs("\033[32m", stdout); + frame(';', 7, s); + puts(SNAKE); + fputs("\033[0m", stdout); +} + +void sheep_say(const char * const s) { + const char * const SHEEP = + " ____ o=\n" + ",######[ \n" + " l l \n" + ; + fputs("\033[33m", stdout); + frame('o', 10, s); + puts(SHEEP); + fputs("\033[0m", stdout); +} + + +signed main(int argc, char * * argv) { + yyparse(); + return 0; +} diff --git a/object/.gitkeep b/object/.gitkeep new file mode 100644 index 0000000..e69de29