diff --git a/examples/shell/shell.q b/examples/shell/shell.q index 509b53e..f484486 100644 --- a/examples/shell/shell.q +++ b/examples/shell/shell.q @@ -13,7 +13,6 @@ const state { main() { length := 256 command := mem.alloc(length) - siginfo := mem.alloc(128) loop { io.out("λ ") @@ -31,6 +30,6 @@ main() { return } - sys.waitid(idtype.pid, pid, siginfo, state.exited) + sys.waitid(idtype.pid, pid, 0, state.exited) } } \ No newline at end of file diff --git a/examples/winapi/winapi.q b/examples/winapi/winapi.q index c7a9c8e..be63231 100644 --- a/examples/winapi/winapi.q +++ b/examples/winapi/winapi.q @@ -1,5 +1,5 @@ extern user32 { - MessageBoxA(window *any, text *int8, title *int8, flags uint) + MessageBoxA(window *any, text *int8, title *int8, flags uint) -> int } main() { diff --git a/lib/core/core_windows.q b/lib/core/core_windows.q index b0acd58..575b41e 100644 --- a/lib/core/core_windows.q +++ b/lib/core/core_windows.q @@ -1,13 +1,3 @@ -extern kernel32 { - SetConsoleCP(cp uint) - SetConsoleOutputCP(cp uint) - ExitProcess(code uint) -} - -const cp { - utf8 65001 -} - init() { kernel32.SetConsoleCP(cp.utf8) kernel32.SetConsoleOutputCP(cp.utf8) @@ -17,4 +7,14 @@ init() { exit() { kernel32.ExitProcess(0) +} + +const cp { + utf8 65001 +} + +extern kernel32 { + SetConsoleCP(cp uint) + SetConsoleOutputCP(cp uint) + ExitProcess(code uint) } \ No newline at end of file diff --git a/lib/mem/alloc_linux.q b/lib/mem/alloc_linux.q index 78e403c..f7178a2 100644 --- a/lib/mem/alloc_linux.q +++ b/lib/mem/alloc_linux.q @@ -1,15 +1,5 @@ import sys -const prot { - read 0x1 - write 0x2 -} - -const map { - private 0x02 - anonymous 0x20 -} - alloc(length int) -> []int8 { x := sys.mmap(0, length+8, prot.read|prot.write, map.private|map.anonymous) @@ -19,4 +9,14 @@ alloc(length int) -> []int8 { store(x, 8, length) return x + 8 +} + +const prot { + read 0x1 + write 0x2 +} + +const map { + private 0x02 + anonymous 0x20 } \ No newline at end of file diff --git a/lib/mem/alloc_mac.q b/lib/mem/alloc_mac.q index e45eaf4..fe0f500 100644 --- a/lib/mem/alloc_mac.q +++ b/lib/mem/alloc_mac.q @@ -1,15 +1,5 @@ import sys -const prot { - read 0x1 - write 0x2 -} - -const map { - private 0x02 - anonymous 0x1000 -} - alloc(length int) -> []int8 { x := sys.mmap(0, length+8, prot.read|prot.write, map.private|map.anonymous) @@ -19,4 +9,14 @@ alloc(length int) -> []int8 { store(x, 8, length) return x + 8 +} + +const prot { + read 0x1 + write 0x2 +} + +const map { + private 0x02 + anonymous 0x1000 } \ No newline at end of file diff --git a/lib/mem/alloc_windows.q b/lib/mem/alloc_windows.q index 3a4b0a4..c5b74de 100644 --- a/lib/mem/alloc_windows.q +++ b/lib/mem/alloc_windows.q @@ -1,14 +1,5 @@ import sys -const page { - readwrite 0x0004 -} - -const mem { - commit 0x1000 - reserve 0x2000 -} - alloc(length int) -> []int8 { x := sys.mmap(0, length+8, page.readwrite, mem.commit|mem.reserve) @@ -18,4 +9,13 @@ alloc(length int) -> []int8 { store(x, 8, length) return x + 8 +} + +const page { + readwrite 0x0004 +} + +const mem { + commit 0x1000 + reserve 0x2000 } \ No newline at end of file diff --git a/lib/net/htons.q b/lib/net/htons.q index 8e414c6..fe2c047 100644 --- a/lib/net/htons.q +++ b/lib/net/htons.q @@ -1,3 +1,3 @@ -htons(num int) -> int { +htons(num uint16) -> uint16 { return ((num & 0xFF) << 8) | (num >> 8) } \ No newline at end of file diff --git a/lib/sys/fs_linux.q b/lib/sys/fs_linux.q deleted file mode 100644 index 2eda9ff..0000000 --- a/lib/sys/fs_linux.q +++ /dev/null @@ -1,23 +0,0 @@ -getcwd(buffer *any, length int) -> int { - return syscall(79, buffer, length) -} - -chdir(path *any) -> int { - return syscall(80, path) -} - -rename(old *any, new *any) -> int { - return syscall(82, old, new) -} - -mkdir(path *any, mode int) -> int { - return syscall(83, path, mode) -} - -rmdir(path *any) -> int { - return syscall(84, path) -} - -unlink(file *any) -> int { - return syscall(87, file) -} \ No newline at end of file diff --git a/lib/sys/io_linux.q b/lib/sys/io_linux.q deleted file mode 100644 index d70e220..0000000 --- a/lib/sys/io_linux.q +++ /dev/null @@ -1,15 +0,0 @@ -read(fd int, buffer *any, length int) -> int { - return syscall(0, fd, buffer, length) -} - -write(fd int, buffer *any, length int) -> int { - return syscall(1, fd, buffer, length) -} - -open(path *any, flags int, mode int) -> int { - return syscall(2, path, flags, mode) -} - -close(fd int) -> int { - return syscall(3, fd) -} \ No newline at end of file diff --git a/lib/sys/io_mac.q b/lib/sys/io_mac.q deleted file mode 100644 index 87f7adc..0000000 --- a/lib/sys/io_mac.q +++ /dev/null @@ -1,15 +0,0 @@ -read(fd int, buffer *any, length int) -> int { - return syscall(0x2000003, fd, buffer, length) -} - -write(fd int, buffer *any, length int) -> int { - return syscall(0x2000004, fd, buffer, length) -} - -open(path *any, flags int, mode int) -> int { - return syscall(0x2000005, path, flags, mode) -} - -close(fd int) -> int { - return syscall(0x2000006, fd) -} \ No newline at end of file diff --git a/lib/sys/mem_linux.q b/lib/sys/mem_linux.q deleted file mode 100644 index 14dca51..0000000 --- a/lib/sys/mem_linux.q +++ /dev/null @@ -1,7 +0,0 @@ -mmap(address int, length int, protection int, flags int) -> *any { - return syscall(9, address, length, protection, flags) -} - -munmap(address *any, length int) -> int { - return syscall(11, address, length) -} \ No newline at end of file diff --git a/lib/sys/mem_mac.q b/lib/sys/mem_mac.q deleted file mode 100644 index 2b0d33b..0000000 --- a/lib/sys/mem_mac.q +++ /dev/null @@ -1,7 +0,0 @@ -mmap(address int, length int, protection int, flags int) -> *any { - return syscall(0x20000C5, address, length, protection, flags) -} - -munmap(address *any, length int) -> int { - return syscall(0x2000049, address, length) -} \ No newline at end of file diff --git a/lib/sys/mem_windows.q b/lib/sys/mem_windows.q deleted file mode 100644 index cdcc65e..0000000 --- a/lib/sys/mem_windows.q +++ /dev/null @@ -1,16 +0,0 @@ -extern kernel32 { - VirtualAlloc(address int, length int, flags int, protection int) - VirtualFree(address *any, length int, type int) -> bool -} - -const mem { - decommit 0x4000 -} - -mmap(address int, length int, protection int, flags int) -> *any { - return kernel32.VirtualAlloc(address, length, flags, protection) -} - -munmap(address *any, length int) -> int { - return kernel32.VirtualFree(address, length, mem.decommit) -} \ No newline at end of file diff --git a/lib/sys/net_linux.q b/lib/sys/net_linux.q deleted file mode 100644 index d4977bc..0000000 --- a/lib/sys/net_linux.q +++ /dev/null @@ -1,26 +0,0 @@ -struct sockaddr_in { - sin_family int16 - sin_port int16 - sin_addr int64 - sin_zero int64 -} - -socket(family int, type int, protocol int) -> int { - return syscall(41, family, type, protocol) -} - -accept(fd int, address *any, length int) -> int { - return syscall(43, fd, address, length) -} - -bind(fd int, address *sockaddr_in, length int) -> int { - return syscall(49, fd, address, length) -} - -listen(fd int, backlog int) -> int { - return syscall(50, fd, backlog) -} - -setsockopt(fd int, level int, optname int, optval *any, optlen int) -> int { - return syscall(54, fd, level, optname, optval, optlen) -} \ No newline at end of file diff --git a/lib/sys/net_mac.q b/lib/sys/net_mac.q deleted file mode 100644 index f143b1e..0000000 --- a/lib/sys/net_mac.q +++ /dev/null @@ -1,23 +0,0 @@ -struct sockaddr_in_bsd { - sin_len int8 - sin_family int8 - sin_port int16 - sin_addr int64 - sin_zero int64 -} - -socket(family int, type int, protocol int) -> int { - return syscall(0x2000061, family, type, protocol) -} - -accept(fd int, address *any, length int) -> int { - return syscall(0x200001E, fd, address, length) -} - -bind(fd int, address *sockaddr_in_bsd, length int) -> int { - return syscall(0x2000068, fd, address, length) -} - -listen(fd int, backlog int) -> int { - return syscall(0x200006A, fd, backlog) -} \ No newline at end of file diff --git a/lib/sys/proc_linux.q b/lib/sys/proc_linux.q deleted file mode 100644 index 6a6491b..0000000 --- a/lib/sys/proc_linux.q +++ /dev/null @@ -1,19 +0,0 @@ -clone(flags int, stack *any) -> int { - return syscall(56, flags, stack) -} - -fork() -> int { - return syscall(57) -} - -execve(path *any, argv *any, envp *any) -> int { - return syscall(59, path, argv, envp) -} - -exit(status int) { - syscall(60, status) -} - -waitid(type int, id int, info *any, options int) -> int { - return syscall(247, type, id, info, options) -} \ No newline at end of file diff --git a/lib/sys/proc_mac.q b/lib/sys/proc_mac.q deleted file mode 100644 index a3dc367..0000000 --- a/lib/sys/proc_mac.q +++ /dev/null @@ -1,15 +0,0 @@ -exit(status int) { - syscall(0x2000001, status) -} - -fork() -> int { - return syscall(0x2000002) -} - -execve(path *any, argv *any, envp *any) -> int { - return syscall(0x200003B, path, argv, envp) -} - -waitid(type int, id int, info *any, options int) -> int { - return syscall(0x20000AD, type, id, info, options) -} \ No newline at end of file diff --git a/lib/sys/proc_windows.q b/lib/sys/proc_windows.q deleted file mode 100644 index b846e5c..0000000 --- a/lib/sys/proc_windows.q +++ /dev/null @@ -1,7 +0,0 @@ -extern kernel32 { - ExitProcess(code uint) -} - -exit(code int) { - kernel32.ExitProcess(code) -} \ No newline at end of file diff --git a/lib/sys/struct_linux.q b/lib/sys/struct_linux.q new file mode 100644 index 0000000..8a22345 --- /dev/null +++ b/lib/sys/struct_linux.q @@ -0,0 +1,11 @@ +struct sockaddr_in { + sin_family int16 + sin_port int16 + sin_addr int64 + sin_zero int64 +} + +struct timespec { + seconds int64 + nanoseconds int64 +} \ No newline at end of file diff --git a/lib/sys/struct_mac.q b/lib/sys/struct_mac.q new file mode 100644 index 0000000..480199a --- /dev/null +++ b/lib/sys/struct_mac.q @@ -0,0 +1,7 @@ +struct sockaddr_in_bsd { + sin_len int8 + sin_family int8 + sin_port int16 + sin_addr int64 + sin_zero int64 +} \ No newline at end of file diff --git a/lib/sys/sys_linux.q b/lib/sys/sys_linux.q new file mode 100644 index 0000000..f35439b --- /dev/null +++ b/lib/sys/sys_linux.q @@ -0,0 +1,91 @@ +read(fd int, buffer *any, length int) -> int { + return syscall(0, fd, buffer, length) +} + +write(fd int, buffer *any, length int) -> int { + return syscall(1, fd, buffer, length) +} + +open(path *any, flags int, mode int) -> int { + return syscall(2, path, flags, mode) +} + +close(fd int) -> int { + return syscall(3, fd) +} + +mmap(address int, length int, protection int, flags int) -> *any { + return syscall(9, address, length, protection, flags) +} + +munmap(address *any, length int) -> int { + return syscall(11, address, length) +} + +clone(flags int, stack *any) -> int { + return syscall(56, flags, stack) +} + +fork() -> int { + return syscall(57) +} + +execve(path *any, argv *any, envp *any) -> int { + return syscall(59, path, argv, envp) +} + +exit(status int) { + syscall(60, status) +} + +waitid(type int, id int, info *any, options int) -> int { + return syscall(247, type, id, info, options) +} + +socket(family int, type int, protocol int) -> int { + return syscall(41, family, type, protocol) +} + +accept(fd int, address *any, length int) -> int { + return syscall(43, fd, address, length) +} + +bind(fd int, address *sockaddr_in, length int) -> int { + return syscall(49, fd, address, length) +} + +listen(fd int, backlog int) -> int { + return syscall(50, fd, backlog) +} + +setsockopt(fd int, level int, optname int, optval *any, optlen int) -> int { + return syscall(54, fd, level, optname, optval, optlen) +} + +getcwd(buffer *any, length int) -> int { + return syscall(79, buffer, length) +} + +chdir(path *any) -> int { + return syscall(80, path) +} + +rename(old *any, new *any) -> int { + return syscall(82, old, new) +} + +mkdir(path *any, mode int) -> int { + return syscall(83, path, mode) +} + +rmdir(path *any) -> int { + return syscall(84, path) +} + +unlink(file *any) -> int { + return syscall(87, file) +} + +nanosleep(duration *timespec) -> int { + return syscall(35, duration, 0) +} \ No newline at end of file diff --git a/lib/sys/sys_mac.q b/lib/sys/sys_mac.q new file mode 100644 index 0000000..6c31517 --- /dev/null +++ b/lib/sys/sys_mac.q @@ -0,0 +1,55 @@ +read(fd int, buffer *any, length int) -> int { + return syscall(0x2000003, fd, buffer, length) +} + +write(fd int, buffer *any, length int) -> int { + return syscall(0x2000004, fd, buffer, length) +} + +open(path *any, flags int, mode int) -> int { + return syscall(0x2000005, path, flags, mode) +} + +close(fd int) -> int { + return syscall(0x2000006, fd) +} + +mmap(address int, length int, protection int, flags int) -> *any { + return syscall(0x20000C5, address, length, protection, flags) +} + +munmap(address *any, length int) -> int { + return syscall(0x2000049, address, length) +} + +exit(status int) { + syscall(0x2000001, status) +} + +fork() -> int { + return syscall(0x2000002) +} + +execve(path *any, argv *any, envp *any) -> int { + return syscall(0x200003B, path, argv, envp) +} + +waitid(type int, id int, info *any, options int) -> int { + return syscall(0x20000AD, type, id, info, options) +} + +socket(family int, type int, protocol int) -> int { + return syscall(0x2000061, family, type, protocol) +} + +accept(fd int, address *any, length int) -> int { + return syscall(0x200001E, fd, address, length) +} + +bind(fd int, address *sockaddr_in_bsd, length int) -> int { + return syscall(0x2000068, fd, address, length) +} + +listen(fd int, backlog int) -> int { + return syscall(0x200006A, fd, backlog) +} \ No newline at end of file diff --git a/lib/sys/io_windows.q b/lib/sys/sys_windows.q similarity index 56% rename from lib/sys/io_windows.q rename to lib/sys/sys_windows.q index 29fc51a..1d67740 100644 --- a/lib/sys/io_windows.q +++ b/lib/sys/sys_windows.q @@ -1,9 +1,3 @@ -extern kernel32 { - GetStdHandle(handle int64) -> int64 - WriteConsoleA(fd int64, buffer *any, length uint32, written *uint32) -> bool - ReadConsole(fd int64, buffer *any, length uint32, written *uint32) -> bool -} - read(fd int64, buffer *any, length int64) -> int64 { fd = kernel32.GetStdHandle(-10 - fd) kernel32.ReadConsole(fd, buffer, length, 0) @@ -14,4 +8,24 @@ write(fd int64, buffer *any, length int64) -> int64 { fd = kernel32.GetStdHandle(-10 - fd) kernel32.WriteConsoleA(fd, buffer, length, 0) return length +} + +mmap(address int, length int, protection int, flags int) -> *any { + return kernel32.VirtualAlloc(address, length, flags, protection) +} + +munmap(address *any, length int) -> int { + return kernel32.VirtualFree(address, length, mem.decommit) +} + +const mem { + decommit 0x4000 +} + +extern kernel32 { + GetStdHandle(handle int64) -> int64 + ReadConsole(fd int64, buffer *any, length uint32, written *uint32) -> bool + VirtualAlloc(address int, length int, flags int, protection int) + VirtualFree(address *any, length int, type int) -> bool + WriteConsoleA(fd int64, buffer *any, length uint32, written *uint32) -> bool } \ No newline at end of file diff --git a/lib/sys/time_linux.q b/lib/sys/time_linux.q deleted file mode 100644 index 3099f60..0000000 --- a/lib/sys/time_linux.q +++ /dev/null @@ -1,8 +0,0 @@ -struct timespec { - seconds int64 - nanoseconds int64 -} - -nanosleep(duration *timespec) -> int { - return syscall(35, duration, 0) -} \ No newline at end of file diff --git a/src/core/ToNumber.go b/src/core/ToNumber.go index 95d65a2..da73904 100644 --- a/src/core/ToNumber.go +++ b/src/core/ToNumber.go @@ -31,7 +31,12 @@ func (f *Function) ToNumber(t token.Token) (int, error) { } number, err := strconv.Atoi(digits) - return number, err + + if err != nil { + return 0, errors.New(err, f.File, t.Position) + } + + return number, nil case token.Rune: r := t.Bytes(f.File.Bytes)