Files
corelang/build/examples/_polymorphism.core
2023-04-03 16:15:50 +02:00

114 lines
2.3 KiB
Core

/*
PushStruct :: (a: *MA.Arena, $T: Type): *$T
size := size_of(Type)
result := PushSize(a, size)
return result
Array(int)
QueueAddSLL(list: $List, node: $Node, first: #Identifier = first, last: #Identifier = last, next: #Identifier = next)
ArrayAdd(array: $Array, item: $Item, data: #Identifier = data, len: #Identifier = len, len: #Identifier = cap)
QueueAddSLL(list: $List, node: $Node, $first = first, $last = last, $next = next)
if list.first == 0
list.first = list.last = node
else
list.last = list.last.next = node
*/
Array :: struct($T: Type)
data: *T
len: int
cap: int
Tuple :: struct($A: Type, $B: Type)
a: A
b: B
Variant :: union($A: Type, $B: Type, $C: Type)
a: A
b: B
c: C
MakeArray :: (a: *int, count: int): Array(int)
result := Array(int) {
data = a,
len = count,
cap = count
}
return result
// :Multiple arguments
// @todo: maybe disallow multiple arguments in current form
// and use polimorphism. Then we could make var unpacking,
// unpack structs making it more powerful
// a,b := MultipleArgs() // @todo var unpacking
MultipleArgs :: (): Tuple(int, F32)
return {32, 32}
PolyLambda :: ($T: Type = *int): T
return 0
PolyType :: (a: $T): T
return a
GetCount :: (a: int): int
return a
// @todo: this is allowed, shouldn't be
// Test :: (a: int, b: int = 10, c: int???)
Test :: (a: C.Triple(int, int, int))
pass
// @todo:
// Add :: (arr: *Array($T), item: T)
// return
C :: #import "LibC.core"
Add :: (arr: *Array($T), val: T)
if arr.cap == 0
arr.cap = 16
arr.data = C.malloc(SizeOf(T)->U64 * arr.cap->U64)
arr.data[arr.len++] = val
main :: (argc: int, argv: **char): int
buff: *int
array: Array(S64)
second_array: Array(int)
third_array: Array(int)
fourth: Array(F32)
fifth: Array(F32)
sixth: Array(Array(F32))
seventh: Variant(int, F32, S64)
Test({1,2,3})
test_a := int
test := *int
Assert(test_a != test)
// c := MakeArray(buff, GetCount(GetCount(32)))
a := MultipleArgs()
Add(&array, 32)
Add(&second_array, 32)
Add(&third_array, 32)
Add(&fourth, 32)
Add(&fifth, 32)
Add(&sixth, {32})
// Add(&seventh, {32, 32, 32})
// value := PolyLambda(**int)
// PolyType_r1 := PolyType(10)
// PolyType_r2 := PolyType(int)
PolyType_r5 := PolyType(seventh)
// PolyType_r3 := PolyType(test)
// PolyType_r4 := PolyType(test_a)
// PolyType_r5 := PolyType(sixth)
return 0