From 4c92b22e6d18e07e872812cb85ab5e2798b17dac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Tue, 18 Jun 2024 19:04:23 +0500 Subject: [PATCH] Kind of works --- Makefile | 15 +++++++++++++++ close_file.lb | 5 +++++ eof_file.lb | 5 +++++ getc_file.lb | 5 +++++ native/fclose.c | 11 +++++++++++ native/feof.c | 11 +++++++++++ native/fgetc.c | 11 +++++++++++ native/fopen.c | 20 ++++++++++++++++++++ open_file.lb | 23 +++++++++++++++++++++++ test.lb | 26 ++++++++++++++++++++++++++ 10 files changed, 132 insertions(+) create mode 100644 Makefile create mode 100644 close_file.lb create mode 100644 eof_file.lb create mode 100644 getc_file.lb create mode 100644 native/fclose.c create mode 100644 native/feof.c create mode 100644 native/fgetc.c create mode 100644 native/fopen.c create mode 100644 open_file.lb create mode 100644 test.lb diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e1ccceb --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CFLAGS ?= + +ifeq ($(OS),Windows_NT) + LIBSUFFIX = dll +else + LIBSUFFIX = so +endif + +%.$(LIBSUFFIX): %.c + $(CC) -shared -fPIC $(CFLAGS) $< -o $@ + +all: native/fopen.$(LIBSUFFIX) native/fgetc.$(LIBSUFFIX) native/feof.$(LIBSUFFIX) + +clean: + rm native/*.$(LIBSUFFIX) diff --git a/close_file.lb b/close_file.lb new file mode 100644 index 0000000..4108cfd --- /dev/null +++ b/close_file.lb @@ -0,0 +1,5 @@ +@ 0 + args 4 + size + #exec std/rev.lb + #native liblabafile/native/fclose diff --git a/eof_file.lb b/eof_file.lb new file mode 100644 index 0000000..efdd14d --- /dev/null +++ b/eof_file.lb @@ -0,0 +1,5 @@ +@ 0 + args 4 + size + #exec std/rev.lb + #native liblabafile/native/feof diff --git a/getc_file.lb b/getc_file.lb new file mode 100644 index 0000000..e0172fb --- /dev/null +++ b/getc_file.lb @@ -0,0 +1,5 @@ +@ 0 + args 4 + size + #exec std/rev.lb + #native liblabafile/native/fgetc diff --git a/native/fclose.c b/native/fclose.c new file mode 100644 index 0000000..984688d --- /dev/null +++ b/native/fclose.c @@ -0,0 +1,11 @@ +#include +#include + +void labashka(unsigned short (*pop)(void), void (*push)(unsigned short), size_t (*len)(void), size_t max_size ) { + FILE *file = NULL; + file = (void*)((size_t)(pop()) << (16*0) | (size_t)file); + file = (void*)((size_t)(pop()) << (16*1) | (size_t)file); + file = (void*)((size_t)(pop()) << (16*2) | (size_t)file); + file = (void*)((size_t)(pop()) << (16*3) | (size_t)file); + push(fclose(file)); +} diff --git a/native/feof.c b/native/feof.c new file mode 100644 index 0000000..e6b5701 --- /dev/null +++ b/native/feof.c @@ -0,0 +1,11 @@ +#include +#include + +void labashka(unsigned short (*pop)(void), void (*push)(unsigned short), size_t (*len)(void), size_t max_size ) { + FILE *file = NULL; + file = (void*)((size_t)(pop()) << (16*0) | (size_t)file); + file = (void*)((size_t)(pop()) << (16*1) | (size_t)file); + file = (void*)((size_t)(pop()) << (16*2) | (size_t)file); + file = (void*)((size_t)(pop()) << (16*3) | (size_t)file); + push(feof(file)); +} diff --git a/native/fgetc.c b/native/fgetc.c new file mode 100644 index 0000000..9b14746 --- /dev/null +++ b/native/fgetc.c @@ -0,0 +1,11 @@ +#include +#include + +void labashka(unsigned short (*pop)(void), void (*push)(unsigned short), size_t (*len)(void), size_t max_size ) { + FILE *file = NULL; + file = (void*)((size_t)(pop()) << (16*0) | (size_t)file); + file = (void*)((size_t)(pop()) << (16*1) | (size_t)file); + file = (void*)((size_t)(pop()) << (16*2) | (size_t)file); + file = (void*)((size_t)(pop()) << (16*3) | (size_t)file); + push(fgetc(file)); +} diff --git a/native/fopen.c b/native/fopen.c new file mode 100644 index 0000000..73b4054 --- /dev/null +++ b/native/fopen.c @@ -0,0 +1,20 @@ +#include +#include + +void labashka(unsigned short (*pop)(void), void (*push)(unsigned short), size_t (*len)(void), size_t max_size ) { + unsigned short modelen = pop(); + char *mode = calloc(modelen, sizeof(char)); + for (short i = modelen - 1; i != -1; i--) + mode[i] = pop(); + + unsigned short filenamelen = pop(); + char *filename = calloc(filenamelen, sizeof(char)); + for (short i = filenamelen - 1; i != -1; i--) + filename[i] = pop(); + + FILE *file = fopen(filename, mode); + push((short)((0xFFFF000000000000 & (long)file) >> (16*3) )); + push((short)((0x0000FFFF00000000 & (long)file) >> (16*2) )); + push((short)((0x00000000FFFF0000 & (long)file) >> (16*1) )); + push((short)((0x000000000000FFFF & (long)file) >> (16*0) )); +} diff --git a/open_file.lb b/open_file.lb new file mode 100644 index 0000000..57da514 --- /dev/null +++ b/open_file.lb @@ -0,0 +1,23 @@ +@ 0 + args 0 + size + #exec std/string/array/rev.lb + + args 1 + dup + ; |str_size str_size...> + +@ 1 + + kjz 2 + #expr - + args 1 + #exec std/rot.lb + #exec std/rot.lb + jmp 1 + +@ 2 + pop + #exec std/string/array/rev.lb + #exec std/string/array/swap.lb + #native liblabafile/native/fopen diff --git a/test.lb b/test.lb new file mode 100644 index 0000000..4427228 --- /dev/null +++ b/test.lb @@ -0,0 +1,26 @@ +; Чё-то кириллицей. +@ 0 + $string liblabafile/test.lb + $string r + #exec liblabafile/open_file.lb + +@ 1 + #exec std/dup4.lb + #exec liblabafile/getc_file.lb + ; |char ptr[4]> + push 4 + pick + push 4 + pick + push 4 + pick + push 4 + pick + ; |ptr[4] char ptr[4]> + #exec liblabafile/eof_file.lb + jnz 2 + putc + jmp 1 + +@ 2 + quit