Simplified standard library
This commit is contained in:
parent
b8e37fafae
commit
b7685fd7ec
@ -13,7 +13,6 @@ const state {
|
|||||||
main() {
|
main() {
|
||||||
length := 256
|
length := 256
|
||||||
command := mem.alloc(length)
|
command := mem.alloc(length)
|
||||||
siginfo := mem.alloc(128)
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
io.out("λ ")
|
io.out("λ ")
|
||||||
@ -31,6 +30,6 @@ main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sys.waitid(idtype.pid, pid, siginfo, state.exited)
|
sys.waitid(idtype.pid, pid, 0, state.exited)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
extern user32 {
|
extern user32 {
|
||||||
MessageBoxA(window *any, text *int8, title *int8, flags uint)
|
MessageBoxA(window *any, text *int8, title *int8, flags uint) -> int
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
|
@ -1,13 +1,3 @@
|
|||||||
extern kernel32 {
|
|
||||||
SetConsoleCP(cp uint)
|
|
||||||
SetConsoleOutputCP(cp uint)
|
|
||||||
ExitProcess(code uint)
|
|
||||||
}
|
|
||||||
|
|
||||||
const cp {
|
|
||||||
utf8 65001
|
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
kernel32.SetConsoleCP(cp.utf8)
|
kernel32.SetConsoleCP(cp.utf8)
|
||||||
kernel32.SetConsoleOutputCP(cp.utf8)
|
kernel32.SetConsoleOutputCP(cp.utf8)
|
||||||
@ -18,3 +8,13 @@ init() {
|
|||||||
exit() {
|
exit() {
|
||||||
kernel32.ExitProcess(0)
|
kernel32.ExitProcess(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cp {
|
||||||
|
utf8 65001
|
||||||
|
}
|
||||||
|
|
||||||
|
extern kernel32 {
|
||||||
|
SetConsoleCP(cp uint)
|
||||||
|
SetConsoleOutputCP(cp uint)
|
||||||
|
ExitProcess(code uint)
|
||||||
|
}
|
@ -1,15 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
const prot {
|
|
||||||
read 0x1
|
|
||||||
write 0x2
|
|
||||||
}
|
|
||||||
|
|
||||||
const map {
|
|
||||||
private 0x02
|
|
||||||
anonymous 0x20
|
|
||||||
}
|
|
||||||
|
|
||||||
alloc(length int) -> []int8 {
|
alloc(length int) -> []int8 {
|
||||||
x := sys.mmap(0, length+8, prot.read|prot.write, map.private|map.anonymous)
|
x := sys.mmap(0, length+8, prot.read|prot.write, map.private|map.anonymous)
|
||||||
|
|
||||||
@ -20,3 +10,13 @@ alloc(length int) -> []int8 {
|
|||||||
store(x, 8, length)
|
store(x, 8, length)
|
||||||
return x + 8
|
return x + 8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const prot {
|
||||||
|
read 0x1
|
||||||
|
write 0x2
|
||||||
|
}
|
||||||
|
|
||||||
|
const map {
|
||||||
|
private 0x02
|
||||||
|
anonymous 0x20
|
||||||
|
}
|
@ -1,15 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
const prot {
|
|
||||||
read 0x1
|
|
||||||
write 0x2
|
|
||||||
}
|
|
||||||
|
|
||||||
const map {
|
|
||||||
private 0x02
|
|
||||||
anonymous 0x1000
|
|
||||||
}
|
|
||||||
|
|
||||||
alloc(length int) -> []int8 {
|
alloc(length int) -> []int8 {
|
||||||
x := sys.mmap(0, length+8, prot.read|prot.write, map.private|map.anonymous)
|
x := sys.mmap(0, length+8, prot.read|prot.write, map.private|map.anonymous)
|
||||||
|
|
||||||
@ -20,3 +10,13 @@ alloc(length int) -> []int8 {
|
|||||||
store(x, 8, length)
|
store(x, 8, length)
|
||||||
return x + 8
|
return x + 8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const prot {
|
||||||
|
read 0x1
|
||||||
|
write 0x2
|
||||||
|
}
|
||||||
|
|
||||||
|
const map {
|
||||||
|
private 0x02
|
||||||
|
anonymous 0x1000
|
||||||
|
}
|
@ -1,14 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
const page {
|
|
||||||
readwrite 0x0004
|
|
||||||
}
|
|
||||||
|
|
||||||
const mem {
|
|
||||||
commit 0x1000
|
|
||||||
reserve 0x2000
|
|
||||||
}
|
|
||||||
|
|
||||||
alloc(length int) -> []int8 {
|
alloc(length int) -> []int8 {
|
||||||
x := sys.mmap(0, length+8, page.readwrite, mem.commit|mem.reserve)
|
x := sys.mmap(0, length+8, page.readwrite, mem.commit|mem.reserve)
|
||||||
|
|
||||||
@ -19,3 +10,12 @@ alloc(length int) -> []int8 {
|
|||||||
store(x, 8, length)
|
store(x, 8, length)
|
||||||
return x + 8
|
return x + 8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const page {
|
||||||
|
readwrite 0x0004
|
||||||
|
}
|
||||||
|
|
||||||
|
const mem {
|
||||||
|
commit 0x1000
|
||||||
|
reserve 0x2000
|
||||||
|
}
|
@ -1,3 +1,3 @@
|
|||||||
htons(num int) -> int {
|
htons(num uint16) -> uint16 {
|
||||||
return ((num & 0xFF) << 8) | (num >> 8)
|
return ((num & 0xFF) << 8) | (num >> 8)
|
||||||
}
|
}
|
@ -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)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
extern kernel32 {
|
|
||||||
ExitProcess(code uint)
|
|
||||||
}
|
|
||||||
|
|
||||||
exit(code int) {
|
|
||||||
kernel32.ExitProcess(code)
|
|
||||||
}
|
|
11
lib/sys/struct_linux.q
Normal file
11
lib/sys/struct_linux.q
Normal file
@ -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
|
||||||
|
}
|
7
lib/sys/struct_mac.q
Normal file
7
lib/sys/struct_mac.q
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
struct sockaddr_in_bsd {
|
||||||
|
sin_len int8
|
||||||
|
sin_family int8
|
||||||
|
sin_port int16
|
||||||
|
sin_addr int64
|
||||||
|
sin_zero int64
|
||||||
|
}
|
91
lib/sys/sys_linux.q
Normal file
91
lib/sys/sys_linux.q
Normal file
@ -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)
|
||||||
|
}
|
55
lib/sys/sys_mac.q
Normal file
55
lib/sys/sys_mac.q
Normal file
@ -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)
|
||||||
|
}
|
@ -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 {
|
read(fd int64, buffer *any, length int64) -> int64 {
|
||||||
fd = kernel32.GetStdHandle(-10 - fd)
|
fd = kernel32.GetStdHandle(-10 - fd)
|
||||||
kernel32.ReadConsole(fd, buffer, length, 0)
|
kernel32.ReadConsole(fd, buffer, length, 0)
|
||||||
@ -15,3 +9,23 @@ write(fd int64, buffer *any, length int64) -> int64 {
|
|||||||
kernel32.WriteConsoleA(fd, buffer, length, 0)
|
kernel32.WriteConsoleA(fd, buffer, length, 0)
|
||||||
return length
|
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
|
||||||
|
}
|
@ -1,8 +0,0 @@
|
|||||||
struct timespec {
|
|
||||||
seconds int64
|
|
||||||
nanoseconds int64
|
|
||||||
}
|
|
||||||
|
|
||||||
nanosleep(duration *timespec) -> int {
|
|
||||||
return syscall(35, duration, 0)
|
|
||||||
}
|
|
@ -31,7 +31,12 @@ func (f *Function) ToNumber(t token.Token) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
number, err := strconv.Atoi(digits)
|
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:
|
case token.Rune:
|
||||||
r := t.Bytes(f.File.Bytes)
|
r := t.Bytes(f.File.Bytes)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user