Skip to content

Commit

Permalink
More changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed Oct 22, 2024
1 parent 8c0765d commit 9c67a6a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
8 changes: 8 additions & 0 deletions std/io.pr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Expand Down
54 changes: 30 additions & 24 deletions std/reflection.pr
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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
}

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
}

Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -141,6 +141,7 @@ export type EnumValue = struct {
}
export type EnumT = &struct {
BaseType
tpe: weak Type
signed: bool
size: size_t
align: size_t
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -327,22 +329,22 @@ 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() {
let tpe = types(id)

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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
36 changes: 17 additions & 19 deletions std/std.pr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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]
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 9c67a6a

Please sign in to comment.