Browse Source

change the IO APIs a bit

master
Martin Dørum 9 months ago
parent
commit
fa662b6868
4 changed files with 16 additions and 16 deletions
  1. 2
    2
      cmd/main.c
  2. 8
    8
      include/lang2/io.h
  3. 5
    5
      lib/io.c
  4. 1
    1
      lib/parse/parse.c

+ 2
- 2
cmd/main.c View File

@@ -99,7 +99,7 @@ int main(int argc, char **argv) {
fclose(inf);

if (do_print_bytecode) {
l2_vm_print_bytecode((l2_word *)w.mem, w.len / sizeof(l2_word));
l2_vm_print_bytecode(w.mem, w.len / sizeof(l2_word));
}

if (do_print_bytecode || do_print_tokens) {
@@ -108,7 +108,7 @@ int main(int argc, char **argv) {
}

struct l2_vm vm;
l2_vm_init(&vm, (void *)w.mem, w.len / sizeof(l2_word));
l2_vm_init(&vm, w.mem, w.len / sizeof(l2_word));

if (do_step) {
step_through(&vm);

+ 8
- 8
include/lang2/io.h View File

@@ -7,11 +7,11 @@
#define L2_IO_BUFSIZ 1024

struct l2_io_reader {
size_t (*read)(struct l2_io_reader *self, char *buf, size_t len);
size_t (*read)(struct l2_io_reader *self, void *buf, size_t len);
};

struct l2_io_writer {
void (*write)(struct l2_io_writer *self, const char *buf, size_t len);
void (*write)(struct l2_io_writer *self, const void *buf, size_t len);
};

struct l2_bufio_reader {
@@ -49,28 +49,28 @@ struct l2_io_mem_reader {
struct l2_io_reader r;
size_t idx;
size_t len;
const char *mem;
const void *mem;
};
size_t l2_io_mem_read(struct l2_io_reader *self, char *buf, size_t len);
size_t l2_io_mem_read(struct l2_io_reader *self, void *buf, size_t len);

struct l2_io_file_reader {
struct l2_io_reader r;
FILE *f;
};
size_t l2_io_file_read(struct l2_io_reader *self, char *buf, size_t len);
size_t l2_io_file_read(struct l2_io_reader *self, void *buf, size_t len);

struct l2_io_mem_writer {
struct l2_io_writer w;
size_t len;
char *mem;
void *mem;
};
void l2_io_mem_write(struct l2_io_writer *self, const char *buf, size_t len);
void l2_io_mem_write(struct l2_io_writer *self, const void *buf, size_t len);

struct l2_io_file_writer {
struct l2_io_writer w;
FILE *f;
};
void l2_io_file_write(struct l2_io_writer *self, const char *buf, size_t len);
void l2_io_file_write(struct l2_io_writer *self, const void *buf, size_t len);

/*
* Defined in the header to let the compiler inline

+ 5
- 5
lib/io.c View File

@@ -84,7 +84,7 @@ void l2_bufio_flush(struct l2_bufio_writer *b) {
b->idx = 0;
}

size_t l2_io_mem_read(struct l2_io_reader *self, char *buf, size_t len) {
size_t l2_io_mem_read(struct l2_io_reader *self, void *buf, size_t len) {
struct l2_io_mem_reader *r = (struct l2_io_mem_reader *)self;
if (len >= r->len - r->idx) {
len = r->len - r->idx;
@@ -95,20 +95,20 @@ size_t l2_io_mem_read(struct l2_io_reader *self, char *buf, size_t len) {
return len;
}

size_t l2_io_file_read(struct l2_io_reader *self, char *buf, size_t len) {
size_t l2_io_file_read(struct l2_io_reader *self, void *buf, size_t len) {
struct l2_io_file_reader *r = (struct l2_io_file_reader *)self;
return fread(buf, 1, len, r->f);
}

void l2_io_mem_write(struct l2_io_writer *self, const char *buf, size_t len) {
void l2_io_mem_write(struct l2_io_writer *self, const void *buf, size_t len) {
struct l2_io_mem_writer *w = (struct l2_io_mem_writer *)self;
size_t idx = w->len;
w->len += len;
w->mem = realloc(w->mem, w->len);
memcpy(w->mem + idx, buf, len);
memcpy((char *)w->mem + idx, buf, len);
}

void l2_io_file_write(struct l2_io_writer *self, const char *buf, size_t len) {
void l2_io_file_write(struct l2_io_writer *self, const void *buf, size_t len) {
struct l2_io_file_writer *w = (struct l2_io_file_writer *)self;
fwrite(buf, 1, len, w->f);
}

+ 1
- 1
lib/parse/parse.c View File

@@ -133,7 +133,7 @@ static int parse_function_literal(
return -1;
}

l2_word *ops = (l2_word *)w.mem;
l2_word *ops = w.mem;
l2_word opcount = w.len / sizeof(l2_word);

// Due to the earlier gen_rjmp, the second word will be the argument to RJMP.

Loading…
Cancel
Save