From 24f516e0c3c7351e4b2a663856e22495791fa795 Mon Sep 17 00:00:00 2001 From: Vic Nightfall Date: Fri, 17 May 2024 16:10:43 +0200 Subject: [PATCH] Get it to compile --- std/reflection.pr | 65 +++++++++++++++++++++++++---------------------- std/strings.pr | 4 +++ 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/std/reflection.pr b/std/reflection.pr index 76704f9a..33fcb7e3 100644 --- a/std/reflection.pr +++ b/std/reflection.pr @@ -1,12 +1,12 @@ export type Type = &interface { - let name: string - let module: string + let name: StringSlice + let module: StringSlice let id: uint64 let size: size_t let align: size_t } -export def implements(a: Type, b: StructuralT) -> bool { +export def implements(a: Type, b: InterfaceT) -> bool { return false } @@ -22,8 +22,8 @@ export def != (a: Type, b: Type) -> bool { } type BaseType = struct { - name: string - module: string + name: StringSlice + module: StringSlice id: uint64 } @@ -57,7 +57,7 @@ export type CharT = &struct { export type BoxType = struct { BaseType - ref: weak Type + tpe: weak Type } export type PointerT = &struct { @@ -72,8 +72,8 @@ export type ReferenceT = &struct { } export type WeakReferenceT = &struct { BoxType - const size: size_t = size_of weak_ref - const align: size_t = align_of weak_ref + const size: size_t = size_of weak & + const align: size_t = align_of weak & } export type ArrayT = &struct { BoxType @@ -102,7 +102,7 @@ type RecordT = struct { members: &[Field] } export type Field = struct { - name: string + name: StringSlice offset: size_t tpe: weak Type } @@ -114,7 +114,7 @@ export type UnionT = &struct { } export type EnumValue = struct { - name: string + name: StringSlice value: int64 } export type EnumT = &struct { @@ -126,9 +126,9 @@ export type EnumT = &struct { } export type Function = struct { - name: string + name: StringSlice exported: bool - module: string + module: StringSlice tpe: FunctionT } @@ -174,6 +174,7 @@ type TypeKind = enum { import io import map +import std var types: &Map(size_t, Type) @@ -189,8 +190,8 @@ def load_types(data: [uint8], num: size_t, strings: *char) { var index = 0 while index < num { let kind = fp.read(TypeKind) - let name = make_string(strings ++ fp.read(int)) - let module = make_string(strings ++ fp.read(int)) + let name = make_slice(strings, fp.read(int)) + let module = make_slice(strings, fp.read(int)) let id = fp.read(size_t) switch kind { @@ -212,7 +213,7 @@ def load_types(data: [uint8], num: size_t, strings: *char) { let align = fp.read(int) let members = zero_allocate(Field, fp.read(int)) for var i in 0..members.size { - members(i) = [ name = make_string(strings ++ fp.read(int)), offset = fp.read(int) ] !Field + members(i) = [ name = make_slice(strings, fp.read(int)), offset = fp.read(int) ] !Field } if kind == TypeKind::STRUCT { @@ -249,7 +250,7 @@ def load_types(data: [uint8], num: size_t, strings: *char) { let values = allocate_ref(type EnumValue, fp.read(int)) for var i in 0..values.size { values(i) = [ - name = make_string(strings ++ fp.read(int)), + name = make_slice(strings, fp.read(int)), value = fp.read(int64) ] !EnumValue } @@ -261,9 +262,9 @@ def load_types(data: [uint8], num: size_t, strings: *char) { let members = allocate_ref(type Function, fp.read(int)) for var i in 0..members.size { members(i) = [ - name = make_string(strings ++ fp.read(int)), + name = make_slice(strings, fp.read(int)), exported = fp.read(bool), - module = make_string(strings ++ fp.read(int)) + module = make_slice(strings, fp.read(int)) ] !Function } types(index) = [ name = name, module = module, id = id, members = members ] !InterfaceT @@ -281,35 +282,37 @@ def load_types(data: [uint8], num: size_t, strings: *char) { } // Resolve type references - for var tpe in types { + for var id in @types.keys() { + let tpe = types(id) + if tpe.type == StructT { let rec = tpe !StructT - for var i in 0..rec.fields.size { - let f = *rec.fields(i) + for var i in 0..rec.members.size { + let f = *rec.members(i) f.tpe = type_id(fp.read(int)) } } else if tpe.type == UnionT { let rec = tpe !UnionT - for var i in 0..rec.fields.size { - let f = *rec.fields(i) + for var i in 0..rec.members.size { + let f = *rec.members(i) f.tpe = type_id(fp.read(int)) } } else if tpe.type == InterfaceT { let intf = tpe !InterfaceT for var i in 0..intf.members.size { - let m = *inf.members(i) + let m = *intf.members(i) m.tpe = type_id(fp.read(int)) !FunctionT } } else if tpe.type == PointerT { - (box !PointerT).tpe = type_id(fp.read(int)) + (tpe !PointerT).tpe = type_id(fp.read(int)) } else if tpe.type == ReferenceT { - (box !ReferenceT).tpe = type_id(fp.read(int)) + (tpe !ReferenceT).tpe = type_id(fp.read(int)) } else if tpe.type == WeakReferenceT { - (box !WeakReferenceT).tpe = type_id(fp.read(int)) + (tpe !WeakReferenceT).tpe = type_id(fp.read(int)) } else if tpe.type == ArrayT { - (box !ArrayT).tpe = type_id(fp.read(int)) + (tpe !ArrayT).tpe = type_id(fp.read(int)) } else if tpe.type == StaticArrayT { - (box !StaticArrayT).tpe = type_id(fp.read(int)) + (tpe !StaticArrayT).tpe = type_id(fp.read(int)) } else if (tpe.type == VariantT) { let vnt = tpe !VariantT for var i in 0..vnt.variants.size { @@ -317,8 +320,8 @@ def load_types(data: [uint8], num: size_t, strings: *char) { } } else if (tpe.type == TupleT) { let tuple = tpe !TupleT - for var i in 0..vnt.elements.size { - vnt.elements(i) = type_id(fp.read(int)) + for var i in 0..tuple.elements.size { + tuple.elements(i) = type_id(fp.read(int)) } } else if (tpe.type == FunctionT) { let fun = tpe !FunctionT diff --git a/std/strings.pr b/std/strings.pr index 4e3c4d70..5e2cd807 100644 --- a/std/strings.pr +++ b/std/strings.pr @@ -400,6 +400,10 @@ export def >= (s1: IString, s2: IString) -> bool { return cmp(s1, s2) >= 0 } +export def make_slice(s: *char, offset: size_t) -> StringSlice { + return [ parent = null, count = cstd::strlen(s), data = s, offset = offset ] !StringSlice +} + export implicit def to_slice(s: [char]) -> StringSlice { return slice(s, 0, s.length()) }