read-file and write-file worked

This commit is contained in:
Koichi Nakamura 2021-01-06 03:22:01 +09:00
parent 11f8e6fb9f
commit 56eb6c7586
3 changed files with 64 additions and 8 deletions

View file

@ -2011,12 +2011,12 @@ need-defined (read-file)
tuck file>name !
['] (read-file) over file>read-file !
['] (write-file) over file>write-file !
success
;
: close-file ( file -- e )
file>obj (close-file) throw
success
;
s" bootstrap.fs" R/O open-file
." Ready" cr

View file

@ -157,18 +157,42 @@ defbinary("|", or, |, uintptr_t)
defbinary("^", xor, ^, uintptr_t)
defbinary("<", lt, <, intptr_t)
defbinary("=", eq, ==, intptr_t)
/* File IO */
#define SUCCESS 0
#define CLOSE_FILE_ERROR -62
#define OPEN_FILE_ERROR -69
#define READ_FILE_ERROR -70
#define WRITE_FILE_ERROR -75
defcode("(open-file)", openfile) {
int flags = pop();
char *name = (char*) pop();
int fd = open(name, flags);
push(fd);
push(fd >= 0);
push((fd >= 0) ? SUCCESS : OPEN_FILE_ERROR);
next();
}
defcode("(close-file)", closefile) {
int fd = pop();
int r = close(fd);
push(r >= 0);
push((r >= 0) ? SUCCESS : CLOSE_FILE_ERROR);
next();
}
defcode("(read-file)", readfile) {
int fd = pop();
int size = pop();
char *buf = (char*) pop();
int r = read(fd, buf, size);
push(r);
push((r >= 0) ? SUCCESS : READ_FILE_ERROR);
next();
}
defcode("(write-file)", writefile) {
int fd = pop();
int size = pop();
char *buf = (char*) pop();
int r = write(fd, buf, size);
push((r == size) ? SUCCESS : WRITE_FILE_ERROR);
next();
}

View file

@ -38,14 +38,12 @@ def comma(v):
write(HERE_CELL, here + 4)
def read_byte(addr):
#return memory[addr]
i = addr>>2
m = (addr&0x3)*8
v = memory[i]
return (v >> m) & 0xff
def write_byte(addr, c):
#memory[addr] = v
i = addr>>2
m = (addr&0x3)*8
v = memory[i]
@ -61,6 +59,11 @@ def comma_string(s):
comma_byte(ord(c))
comma_byte(0)
def write_string(addr, s):
for c in s:
write_byte(addr, c)
addr += 1
def read_string(addr):
s = ""
while True:
@ -70,6 +73,13 @@ def read_string(addr):
addr += 1
return s
def read_bytes(addr, n):
data = []
for i in range(n):
data.append(read_byte(addr))
addr += 1
return bytes(data)
def find(c):
it = read(LATEST_CELL)
while it != 0:
@ -207,18 +217,40 @@ def argv():
push(ARGV_ADDR)
push(len(sys.argv))
add_simple_operator('v', argv)
SUCCESS = 0
CLOSE_FILE_ERROR = -62
OPEN_FILE_ERROR = -69
READ_FILE_ERROR = -70
WRITE_FILE_ERROR = -75
def openfile():
flag = pop()
name = read_string(pop())
fd = os.open(name, flag)
push(fd)
push(fd >= 0)
push(SUCCESS if (fd >= 0) else OPEN_FILE_ERROR)
def closefile():
fd = pop()
os.close(fd)
push(fd >= 0)
push(SUCCESS if (fd >= 0) else CLOSE_FILE_ERROR)
def readfile():
fd = pop()
size = pop()
addr = pop()
s = os.read(fd, size)
write_string(addr, s)
push(len(s))
push(SUCCESS if (len(s) > 0) else READ_FILE_ERROR)
def writefile():
fd = pop()
size = pop()
addr = pop()
n = os.write(fd, read_bytes(addr, size))
push(SUCCESS if (n == size) else WRITE_FILE_ERROR)
add_simple_operator('(open-file)', openfile)
add_simple_operator('(close-file)', closefile)
add_simple_operator('(write-file)', writefile)
add_simple_operator('(read-file)', readfile)
start = read(HERE_CELL)
comma(find('k'))