Files
corelang/build/examples/_polymorphism.core
Krzosa Karol 41d2baa56b We are working! POLYMORPHS There was an error where we have to reconstruct exact
typespec from type. This means we will probably have problems with namespaces!!
2023-04-02 15:07:47 +02:00

103 lines
2.1 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
Triple :: struct($A: Type, $B: Type, $C: Type)
a: A
b: B
c: C
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: int, b: int = 10, c: int = 20)
pass
main :: (argc: int, argv: **char): int
buff: *int
array: Array(int) = {len = 10, cap = 10, data = buff}
second_array: Array(int)
third_array: Array(int)
fourth: Array(F32)
fifth: Array(F32)
sixth: Array(Array(F32))
seventh: Variant(int, F32, S64)
test_a := int
test := *int
Assert(test_a != test)
// c := MakeArray(buff, GetCount(GetCount(32)))
a := MultipleArgs()
Test(10)
Test(10, 20)
Test(10, 20, 30)
Test(10, b = 10)
Test(10, b = 10, c = 20)
Test(a = 10, b = 10, c = 20)
// 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