This commit is contained in:
nub31
2025-09-21 21:56:59 +02:00
parent 58b1a5c017
commit f5ee47a396
7 changed files with 284 additions and 164 deletions

View File

@@ -3,8 +3,8 @@ NUBC = ../compiler/NubLang.CLI/bin/Debug/net9.0/nubc
out: .build/out.o
gcc -nostartfiles -o out x86_64.s .build/out.o
.build/out.o: $(NUBC) src/main.nub
$(NUBC) src/main.nub
.build/out.o: $(NUBC) src/main.nub src/ref.nub
$(NUBC) src/main.nub src/ref.nub
.PHONY: $(NUBC)
$(NUBC):

View File

@@ -1,8 +1,6 @@
module "main"
import "core"
extern "puts" func puts(text: cstring)
extern "malloc" func malloc(size: u64): ^void
extern "free" func free(address: ^void)
module "main"
struct Human
{
@@ -11,47 +9,11 @@ struct Human
extern "main" func main(args: []cstring): i64
{
let x: ref<Human> = {}
let x: core::ref<Human> = {}
test(x)
return 0
}
func test(x: ref<Human>)
func test(x: core::ref<Human>)
{
}
struct ref<T>
{
value: ^T
count: ^u64
@oncreate
func on_create()
{
puts("on_create")
this.value = @interpret(^T, malloc(@size(T)))
this.count = @interpret(^u64, malloc(@size(u64)))
this.count^ = 1
}
@oncopy
func on_copy()
{
puts("on_copy")
this.count^ = this.count^ + 1
}
@ondestroy
func on_destroy()
{
puts("on_destroy")
this.count^ = this.count^ - 1
if this.count^ <= 0
{
puts("free")
free(@interpret(^void, this.value))
free(@interpret(^void, this.count))
}
}
}

40
example/src/ref.nub Normal file
View File

@@ -0,0 +1,40 @@
module "core"
extern "puts" func puts(text: cstring)
extern "malloc" func malloc(size: u64): ^void
extern "free" func free(address: ^void)
export struct ref<T>
{
value: ^T
count: ^u64
@oncreate
func on_create()
{
puts("on_create")
this.value = @interpret(^T, malloc(@size(T)))
this.count = @interpret(^u64, malloc(@size(u64)))
this.count^ = 1
}
@oncopy
func on_copy()
{
puts("on_copy")
this.count^ = this.count^ + 1
}
@ondestroy
func on_destroy()
{
puts("on_destroy")
this.count^ = this.count^ - 1
if this.count^ <= 0
{
puts("free")
free(@interpret(^void, this.value))
free(@interpret(^void, this.count))
}
}
}