io.Buffer, os.Timeval
This commit is contained in:
@@ -5,7 +5,7 @@ A very cool language
|
|||||||
## Features
|
## Features
|
||||||
* Clean indentation-based syntax
|
* Clean indentation-based syntax
|
||||||
* Compiles to x86_64 Assembly
|
* Compiles to x86_64 Assembly
|
||||||
* Pretty big [standard library](https://github.com/antpiasecki/zern/tree/main/src/std)
|
* Growing [standard library](https://github.com/antpiasecki/zern/tree/main/src/std)
|
||||||
* Produces tiny static executables (~30KB with musl)
|
* Produces tiny static executables (~30KB with musl)
|
||||||
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
|
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
|
||||||
* Sometimes works
|
* Sometimes works
|
||||||
|
|||||||
@@ -389,14 +389,13 @@ func main[argc: i64, argv: ptr] : i64
|
|||||||
|
|
||||||
let c: CHIP8 = chip8_create()
|
let c: CHIP8 = chip8_create()
|
||||||
|
|
||||||
let bytes_size: ptr = 0
|
let buffer: io.Buffer = io.read_binary_file(path)
|
||||||
let bytes: ptr = io.read_binary_file(path, ^bytes_size)
|
|
||||||
|
|
||||||
for i in 0..bytes_size
|
for i in 0..buffer->size
|
||||||
c->memory[0x200 + i] = bytes[i]
|
c->memory[0x200 + i] = buffer->data[i]
|
||||||
|
|
||||||
if disassemble
|
if disassemble
|
||||||
chip8_disassemble(c, bytes_size / 2)
|
chip8_disassemble(c, buffer->size / 2)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
InitWindow(640, 320, "CHIP-8")
|
InitWindow(640, 320, "CHIP-8")
|
||||||
|
|||||||
@@ -101,18 +101,22 @@ func io.read_file[path: str] : str
|
|||||||
buffer[n] = 0
|
buffer[n] = 0
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.read_binary_file[path: str, len_ptr: ptr] : ptr
|
struct io.Buffer
|
||||||
|
data: ptr
|
||||||
|
size: i64
|
||||||
|
|
||||||
|
func io.read_binary_file[path: str] : io.Buffer
|
||||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||||
if fd < 0
|
if fd < 0
|
||||||
panic("failed to open file")
|
panic("failed to open file")
|
||||||
|
|
||||||
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
let buffer: io.Buffer = new io.Buffer
|
||||||
|
buffer->size = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||||
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||||
|
|
||||||
let buffer: ptr = mem.alloc(size)
|
buffer->data = mem.alloc(buffer->size)
|
||||||
_builtin_syscall(SYS_read, fd, buffer, size)
|
_builtin_syscall(SYS_read, fd, buffer->data, buffer->size)
|
||||||
_builtin_syscall(SYS_close, fd)
|
_builtin_syscall(SYS_close, fd)
|
||||||
mem.write64(len_ptr, size)
|
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.write_file[path: str, content: str] : void
|
func io.write_file[path: str, content: str] : void
|
||||||
@@ -560,13 +564,15 @@ func os.urandom_i64[]: i64
|
|||||||
mem.free(buffer)
|
mem.free(buffer)
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
struct os.Timeval
|
||||||
|
tv_sec: i64
|
||||||
|
tv_usec: i64
|
||||||
|
|
||||||
func os.time[] : i64
|
func os.time[] : i64
|
||||||
let tv: ptr = mem.alloc(16)
|
let tv: os.Timeval = new os.Timeval
|
||||||
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
||||||
let seconds: i64 = mem.read64(tv)
|
|
||||||
let microseconds: i64 = mem.read64(tv + 8)
|
|
||||||
mem.free(tv)
|
mem.free(tv)
|
||||||
return seconds * 1000 + microseconds / 1000
|
return tv->tv_sec * 1000 + tv->tv_usec / 1000
|
||||||
|
|
||||||
// voodoo magic
|
// voodoo magic
|
||||||
func os.shell[command: str] : i64
|
func os.shell[command: str] : i64
|
||||||
@@ -574,7 +580,7 @@ func os.shell[command: str] : i64
|
|||||||
if pid == 0
|
if pid == 0
|
||||||
// leaky but not sure where can i free it
|
// leaky but not sure where can i free it
|
||||||
let argv: Array = ["sh", "-c", command, 0]
|
let argv: Array = ["sh", "-c", command, 0]
|
||||||
_builtin_syscall(SYS_execve, "/bin/sh", mem.read64(argv), _builtin_environ())
|
_builtin_syscall(SYS_execve, "/bin/sh", argv->data, _builtin_environ())
|
||||||
_builtin_syscall(SYS_exit, 1)
|
_builtin_syscall(SYS_exit, 1)
|
||||||
else
|
else
|
||||||
let status = 0
|
let status = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user