diff options
author | Kyle Gunger <kgunger12@gmail.com> | 2023-05-20 23:53:12 -0400 |
---|---|---|
committer | Kyle Gunger <kgunger12@gmail.com> | 2023-05-20 23:53:12 -0400 |
commit | bee7954037af2025fdeae9a75f0c113cd39024cb (patch) | |
tree | 775fc528161f507cf6aab4fddaa4f1ef922860c9 /tnslc | |
parent | ba0ffe00d0c62c8ac8e0f98596193b69975c9fe9 (diff) |
File copy example
Diffstat (limited to 'tnslc')
-rw-r--r-- | tnslc/c_wrap.tnsl | 34 | ||||
-rw-r--r-- | tnslc/read_file_test.tnsl | 4 | ||||
-rw-r--r-- | tnslc/tnslc_wrapped.tnsl | 90 | ||||
-rw-r--r-- | tnslc/utils.tnsl | 20 |
4 files changed, 128 insertions, 20 deletions
diff --git a/tnslc/c_wrap.tnsl b/tnslc/c_wrap.tnsl index d121967..fc60a52 100644 --- a/tnslc/c_wrap.tnsl +++ b/tnslc/c_wrap.tnsl @@ -83,9 +83,9 @@ asm "extern CloseHandle" ~void out asm "mov rcx, r8" # lpFileName - asm "mov rdx, 3" # dwDesiredAccess - asm "shl rdx, 30" # (GENERIC_READ 1 << 31 | GENERIC_WRITE 1 << 30) - asm "mov r8, 0" # dwShareMode + asm "mov rdx, 1" # dwDesiredAccess + asm "shl rdx, 28" # (GENERIC_READ 1 << 31 | GENERIC_WRITE 1 << 30) + asm "mov r8, 3" # dwShareMode asm "mov r9, 0" # lpSecurityAttributes asm "push qword 0" # hTemplateFile @@ -113,14 +113,14 @@ asm "extern CloseHandle" ~void out asm "mov rcx, r8" # lpFileName - asm "mov rdx, 3" # dwDesiredAccess - asm "shl rdx, 30" # (GENERIC_READ 1 << 31 | GENERIC_WRITE 1 << 30) - asm "mov r8, 0" # dwShareMode + asm "mov rdx, 1" # dwDesiredAccess + asm "shl rdx, 28" # (GENERIC_READ 1 << 31 | GENERIC_WRITE 1 << 30) + asm "mov r8, 3" # dwShareMode asm "mov r9, 0" # lpSecurityAttributes asm "push qword 0" # hTemplateFile asm "push qword 128" # dwFlagsAndAttributes (NORMAL_FILE = 128) - asm "push qword 2" # dwCreationDisposition (CREATE_ALWAYS = 2) + asm "push qword 3" # dwCreationDisposition (OPEN_EXISTING = 3) # Shadow space asm "push r9" @@ -133,7 +133,7 @@ asm "extern CloseHandle" # Set out to the returned value # (The compiler assignes spaces sequentially. We have a ptr in r8, and a uint in r9) - asm "mov r10, rax" + asm "mov r9, rax" return out ;/ @@ -155,13 +155,13 @@ asm "extern CloseHandle" asm "add rsp, 32" ;/ -/; _read_byte (~void handle, ~uint8 byte) [bool] - bool out +/; _read_byte (~void handle, ~uint8 byte, ~int read) [int] + int out asm "mov rcx, r8" # handle asm "mov rdx, r9" # buffer asm "mov r8, 1" # one byte - asm "mov r9, 0" + asm "mov r9, r10" # read bytes buffer asm "push qword 0" # Shadow space @@ -172,13 +172,13 @@ asm "extern CloseHandle" asm "call ReadFile" - asm "mov r10, rax" + asm "mov r11, rax" return out ;/ -/; _write_byte (~void handle, ~uint8 byte) [bool] - bool out +/; _write_byte (~void handle, ~uint8 byte) [int] + int out asm "mov rcx, r8" # handle asm "mov rdx, r9" # buffer @@ -202,9 +202,3 @@ asm "extern CloseHandle" /; print_alert _printf(~_alert{0}) ;/ - -/; cstr_len (~uint8 cstr) [int] - int i = 0 - /; loop (cstr{i} !== 0) [i++] ;/ - return i -;/ diff --git a/tnslc/read_file_test.tnsl b/tnslc/read_file_test.tnsl index 60c4c5b..cd8299b 100644 --- a/tnslc/read_file_test.tnsl +++ b/tnslc/read_file_test.tnsl @@ -1,6 +1,8 @@ :include "c_wrap.tnsl" +:include "utils.tnsl" {}uint8 usage_msg = "Usage: read_file.exe [file to read] [file to write]\n\0" +{}uint8 ok_msg = "Ok!\n\0" {}uint8 print_one = "\0\0" {}uint8 nl = "\n\0" @@ -19,6 +21,8 @@ /; if (argc < 2) _printf(~usage_msg{0}) return 1 + ;; else + _printf(~ok_msg{0}) ;/ _printf(argv{1}) diff --git a/tnslc/tnslc_wrapped.tnsl b/tnslc/tnslc_wrapped.tnsl new file mode 100644 index 0000000..77d7c13 --- /dev/null +++ b/tnslc/tnslc_wrapped.tnsl @@ -0,0 +1,90 @@ +:include "c_wrap.tnsl" +:include "vector.tnsl" +:include "utils.tnsl" + +enum TOKEN_TYPE [int] { + DEFWORD = 0 +} + +struct Token { + int line, ch, _type, + ~uint8 data +} + +/; method Token + /; init + self.data = _alloc(1) + self.data{0} = 0 + ;/ + + /; data_len [int] + return cstr_len(self.data) + ;/ + + /; append (uint8 ch) + int l = self.data_len() + self.data = _realloc(self.data, self.data_len() + 1) + self.data{l + 1} = 0 + self.data{l} = ch + ;/ + + /; pop + int l = self.data_len() + self.data = _realloc(self.data, l - 1) + self.data{l - 1} = 0 + ;/ + + /; clear + _delete(self.data) + ;/ + + /; eq (~Token t) [bool] + return cstr_eq(self.data, t`.data) + ;/ +;/ + +/; tokenize (~void file) [Vector] + Vector out + out.start(32) + + + + return out +;/ + +{}uint8 wrong_args = "Usage: copy [from] [to]" +{}uint8 write_one = "\0\n\0" + +# Proof of concept copy program +/; main (int argc, ~~uint8 argv) [int] + asm "mov r8, rcx" + asm "mov r9, rdx" + + /; if (argc < 3) + _printf(~wrong_args{0}) + return 1 + ;/ + + ~void read_handle = _open_file(argv{1}) + ~void write_handle = _create_file(argv{2}) + + _print_num(~_dec{0}, read_handle) + _print_num(~_dec{0}, write_handle) + + uint8 buf = 0 + int read_count = 0 + int tries = 0 + _read_byte(read_handle, ~buf, ~read_count) + /; loop (_read_byte(read_handle, ~buf, ~read_count)) + /; if (read_count == 0) + break + ;/ + _write_byte(write_handle, ~buf) + read_count = 0 + ;/ + + _close_file(read_handle) + _close_file(write_handle) + + return 0 +;/
\ No newline at end of file diff --git a/tnslc/utils.tnsl b/tnslc/utils.tnsl new file mode 100644 index 0000000..6fe7769 --- /dev/null +++ b/tnslc/utils.tnsl @@ -0,0 +1,20 @@ +/; cstr_len (~uint8 cstr) [int] + int i = 0 + /; loop (cstr{i} !== 0) [i++] ;/ + return i +;/ + +/; cstr_eq (~uint8 a, b) [bool] + int ln = cstr_len(a) + /; if (ln !== cstr_len(b)) + return false + ;/ + + /; loop (int i = 0; i < ln) [i++] + /; if (a{i} !== b{i}) + return false + ;/ + ;/ + + return true +;/
\ No newline at end of file |