Skip to content

Commit

Permalink
Get it to compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed May 17, 2024
1 parent e59a2c7 commit 24f516e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
65 changes: 34 additions & 31 deletions std/reflection.pr
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -22,8 +22,8 @@ export def != (a: Type, b: Type) -> bool {
}

type BaseType = struct {
name: string
module: string
name: StringSlice
module: StringSlice
id: uint64
}

Expand Down Expand Up @@ -57,7 +57,7 @@ export type CharT = &struct {

export type BoxType = struct {
BaseType
ref: weak Type
tpe: weak Type
}

export type PointerT = &struct {
Expand All @@ -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
Expand Down Expand Up @@ -102,7 +102,7 @@ type RecordT = struct {
members: &[Field]
}
export type Field = struct {
name: string
name: StringSlice
offset: size_t
tpe: weak Type
}
Expand All @@ -114,7 +114,7 @@ export type UnionT = &struct {
}

export type EnumValue = struct {
name: string
name: StringSlice
value: int64
}
export type EnumT = &struct {
Expand All @@ -126,9 +126,9 @@ export type EnumT = &struct {
}

export type Function = struct {
name: string
name: StringSlice
exported: bool
module: string
module: StringSlice
tpe: FunctionT
}

Expand Down Expand Up @@ -174,6 +174,7 @@ type TypeKind = enum {

import io
import map
import std

var types: &Map(size_t, Type)

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -281,44 +282,46 @@ 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 {
vnt.variants(i) = type_id(fp.read(int))
}
} 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
Expand Down
4 changes: 4 additions & 0 deletions std/strings.pr
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit 24f516e

Please sign in to comment.