From 9c67a6a6a9384d28696e8da33ed474f5cd213300 Mon Sep 17 00:00:00 2001 From: Vic Nightfall Date: Sun, 20 Oct 2024 13:42:46 +0200 Subject: [PATCH] More changes --- std/io.pr | 8 +++++++ std/reflection.pr | 54 ++++++++++++++++++++++++++--------------------- std/std.pr | 36 +++++++++++++++---------------- 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/std/io.pr b/std/io.pr index d074ca3..f1747e6 100644 --- a/std/io.pr +++ b/std/io.pr @@ -95,6 +95,10 @@ export const NO_BLOCKING = 1 return null } + export def open_memory_as_file(arr: [uint8], mode: String) -> File { + return null + } + } else { import linux @@ -156,6 +160,10 @@ export const NO_BLOCKING = 1 return linux::isatty(cstd::fileno(file)) != 0 } + export def open_memory_as_file(arr: [uint8]) -> File { + return cstd::fmemopen(arr.value, arr.size, "r".value) + } + export def open_memory_as_file(arr: [uint8], mode: String) -> File { return cstd::fmemopen(arr.value, arr.size, mode.to_array().value) } diff --git a/std/reflection.pr b/std/reflection.pr index 0f4dd16..1f50902 100644 --- a/std/reflection.pr +++ b/std/reflection.pr @@ -9,7 +9,7 @@ export type Type = &interface { let type_members: &[Function] } -export def implements(a: Type, b: InterfaceT) -> bool { +export def implements(a: Type, b: Type) -> bool { return false // TODO } @@ -17,7 +17,7 @@ export def assignable(a: Type, b: Type) -> bool { return false // TODO } -export def contains(a: StructT, b: Type) -> bool { +export def contains(a: Type, b: Type) -> bool { return false // TODO } @@ -101,7 +101,7 @@ export type StaticArrayT = &struct { length: size_t } -type FunctionBase = struct { +export type FunctionBase = struct { BaseType arguments: &[weak Type] returns: &[weak Type] @@ -117,11 +117,11 @@ export type ClosureT = &struct { const align: size_t = align_of -> } -type RecordT = struct { +export type RecordT = struct { BaseType size: size_t align: size_t - members: &[Field] + fields: &[Field] } export type Field = struct { name: StringSlice @@ -141,6 +141,7 @@ export type EnumValue = struct { } export type EnumT = &struct { BaseType + tpe: weak Type signed: bool size: size_t align: size_t @@ -211,7 +212,7 @@ def load_types(input: *uint8, size: size_t, num: size_t, strings: *char) { var data: [uint8] data.value = input data.size = size - let fp = io::open_memory_as_file(data, "r") + let fp = io::open_memory_as_file(data) defer close(fp) @@ -225,8 +226,9 @@ def load_types(input: *uint8, size: size_t, num: size_t, strings: *char) { let name = make_slice(strings, fp.read(int)) let module = make_slice(strings, fp.read(int)) let id = fp.read(uint64) - - print(kind, " ", name, " ", module, " ", id, "\n") + + //NOTE: print doesn't work in here + printf("%d %s %s %zu\n".value, kind, name.data ++ name.offset, module.data ++ module.offset, id) switch kind { case TypeKind::BOOL @@ -245,22 +247,22 @@ def load_types(input: *uint8, size: size_t, num: size_t, strings: *char) { case TypeKind::STRUCT, TypeKind::UNION let size = fp.read(int) let align = fp.read(int) - let members = zero_allocate(Field, fp.read(int)) + let fields = zero_allocate(Field, fp.read(int)) - print("\tsize: ", size, " align: ", align, " members: ", members.size, "\n") + printf("\tsize: %zu align: %zu fields: %zu\n".value, size, align, fields.size) - for var i in 0..members.size { + for var i in 0..fields.size { let field = [ name = make_slice(strings, fp.read(int)), offset = fp.read(int) ] !Field - members(i) = field - print("\tname: ", field.name, " offset: ", field.offset, "\n") + fields(i) = field + printf("\tname: %s offset: %d\n".value, field.name.data ++ field.name.offset, field.offset) } if kind == TypeKind::STRUCT { types(id) = [ name = name, module = module, id = id, - size = size, align = align, members = members ] !StructT + size = size, align = align, fields = fields ] !StructT } else { types(id) = [ name = name, module = module, id = id, - size = size, align = align, members = members ] !UnionT + size = size, align = align, fields = fields ] !UnionT } case TypeKind::ARRAY types(id) = [ name = name, module = module, id = id ] !ArrayT @@ -327,7 +329,7 @@ def load_types(input: *uint8, size: size_t, num: size_t, strings: *char) { index += 1 } - print("==============================================\n") + printf("==============================================\n".value) // Resolve type references for var id in @types.keys() { @@ -335,14 +337,14 @@ def load_types(input: *uint8, size: size_t, num: size_t, strings: *char) { let base = *(tpe !BaseType) let nmembers = fp.read(int) - print(tpe.name, " members: ", nmembers, "\n") + printf("%s members: %d\n".value, tpe.name.data ++ tpe.name.offset, nmembers) base.type_members = allocate_ref(type Function, nmembers) for var i in 0..nmembers { let name = make_slice(strings, fp.read(int)) let exported = fp.read(bool) let module = make_slice(strings, fp.read(int)) - print("\t", name, " ", exported, " ", module, "\n") + printf("\t%s %d %s\n".value, name.data ++ name.offset, exported, module.data ++ module.offset) let member = [ name = name, @@ -366,19 +368,19 @@ def load_types(input: *uint8, size: size_t, num: size_t, strings: *char) { if tpe.type == StructT { let rec = tpe !StructT - for var i in 0..rec.members.size { - let f = *rec.members(i) + for var i in 0..rec.fields.size { + let f = *rec.fields(i) f.tpe = type_id(fp.read(uint64)) } } else if tpe.type == UnionT { let rec = tpe !UnionT - for var i in 0..rec.members.size { - let f = *rec.members(i) + for var i in 0..rec.fields.size { + let f = *rec.fields(i) f.tpe = type_id(fp.read(uint64)) } } else if tpe.type == InterfaceT { let intf = tpe !InterfaceT - print(intf.type.name, " ", intf.name, "\n") + //print(intf.type.name, " ", intf.name, "\n") for var i in 0..intf.members.size { let m = *intf.members(i) for var i in 0..m.arguments.size { @@ -424,11 +426,15 @@ def load_types(input: *uint8, size: size_t, num: size_t, strings: *char) { for var i in 0..fun.returns.size { fun.returns(i) = type_id(fp.read(uint64)) } - } + } else if tpe.type == EnumT { + let enm = tpe !EnumT + enm.tpe = type_id(fp.read(uint64)) + } } } export def type_id(id: uint64) -> Type { if id == 0 { return null } + if not types.contains(id) { abort() } return types(id) } \ No newline at end of file diff --git a/std/std.pr b/std/std.pr index 8efc540..adb80d1 100644 --- a/std/std.pr +++ b/std/std.pr @@ -345,10 +345,10 @@ def print_val(file: File, value: *, tpe: reflection::Type) -> int { return cstd::fprintf(file, "false".value) } } else if reflection::implements(tpe, ToString) { - let str = (value !&ToString).to_string() - return print_val(file, str, type_of str) + let str = (@(value !*&ToString)).to_string() + return print_val(file, *str, type_of str) } else if reflection::implements(tpe, IString) { - let str = (value !String) + let str = (@(value !*String)) for var c in str.chars() { cstd::putc(c, file) } @@ -411,15 +411,13 @@ def print_val(file: File, value: *, tpe: reflection::Type) -> int { sum += cstd::fprintf(file, "[".value) for var i in 0..fields.size { let field = fields(i) - sum += cstd::fprintf(file, "%s = ".value, field.name.value) - + sum += print(field.name, " = ") sum += print_val(file, value ++ field.offset, field.tpe) - delete(ref_tpe) if i < fields.size - 1 { sum += cstd::fprintf(file, ", ".value) } } - sum += cstd::fprintf(file, "] !%s".value) + sum += cstd::fprintf(file, "] !".value) sum += print(tpe.name) return sum } @@ -428,10 +426,10 @@ def print_val(file: File, value: *, tpe: reflection::Type) -> int { export def fprint(file: File, args: &...) -> int { var sum = 0 for var i in 0..args.size { - let arg = *args(i) - let ref_tpe = args(i).type + let arg = args(i) + let ref_tpe = arg.type !ReferenceT if ref_tpe { - sum += print_val(file, arg !*, ref_tpe) + sum += print_val(file, arg !*, ref_tpe.tpe) } else { sum += cstd::fprintf(file, "null".value) } @@ -485,13 +483,13 @@ export def allocate(size: size_t) -> * { } export def allocate(type T) -> *T { - return cstd::malloc(T.size) !*T + return cstd::malloc(size_of T) !*T } export def allocate(type T, size: size_t) -> [T] { var arr: [T] arr.size = size - arr.value = cstd::malloc(T.size * size) !*T + arr.value = cstd::malloc(size_of T * size) !*T return arr } @@ -500,20 +498,20 @@ export def zero_allocate(size: size_t) -> * { } export def zero_allocate(type T) -> *T { - return cstd::calloc(1, T.size) !*T + return cstd::calloc(1, size_of T) !*T } export def zero_allocate(type T, size: size_t) -> [T] { var arr: [T] arr.size = size - arr.value = cstd::calloc(size, T.size) !*T + arr.value = cstd::calloc(size, size_of T) !*T return arr } export def allocate_ref(type T, size: size_t) -> &[T] { var arr: [T] arr.size = size - arr.value = cstd::calloc(size, T.size) !*T + arr.value = cstd::calloc(size, size_of T) !*T defer cstd::free(arr.value) return arr !&[T] } @@ -544,20 +542,20 @@ export def close(file: File) -> int { // Reads buffer.size * T.size bytes from file export def read(file: File, buffer: type [T]) -> size_t { - return cstd::fread(buffer.value, T.size, buffer.size, file) + return cstd::fread(buffer.value, size_of T, buffer.size, file) } export def read(file: File, buffer: type [T], size: size_t) -> size_t { - return cstd::fread(buffer.value, T.size, size, file) + return cstd::fread(buffer.value, size_of T, size, file) } export def read(file: File, ptr: type *T) -> size_t { - return cstd::fread(ptr, T.size, 1, file) + return cstd::fread(ptr, size_of T, 1, file) } export def read(file: File, type T) -> T { var data: T - cstd::fread(*data, T.size, 1, file) + cstd::fread(*data, size_of T, 1, file) return data }