Files
kraken/tests/test_adt.krak

164 lines
4.6 KiB
Plaintext
Raw Normal View History

import io:*
adt options {
option0,
option1
}
adt maybe_int {
no_int,
an_int: int
}
2015-11-19 16:27:36 -05:00
fun TestObj(num: int): TestObj {
2015-11-25 15:35:06 -05:00
print("gonna make object in function ")
2015-11-19 16:27:36 -05:00
println(num)
var toRet.construct(num): TestObj
return toRet
}
obj TestObj (Object) {
var obj_num: int
2015-11-25 15:35:06 -05:00
var ref_num: int
2015-11-19 16:27:36 -05:00
fun construct(num:int): *TestObj {
obj_num = num
2015-11-25 15:35:06 -05:00
ref_num = num
2015-11-19 16:27:36 -05:00
print("constructed object ")
2015-11-25 15:35:06 -05:00
print(obj_num);print(" : ");println(ref_num)
2015-11-19 16:27:36 -05:00
}
fun copy_construct(old: *TestObj) {
2015-11-25 15:35:06 -05:00
obj_num = old->obj_num
ref_num = old->ref_num + 100
2015-11-19 16:27:36 -05:00
print("copy constructed object ")
2015-11-25 15:35:06 -05:00
print(obj_num);print(" : ");print(ref_num)
2015-11-19 16:27:36 -05:00
print(" from ")
2015-11-25 15:35:06 -05:00
print(old->obj_num);print(" : ");println(old->ref_num)
2015-11-19 16:27:36 -05:00
}
fun destruct() {
print("destructed object ")
2015-11-25 15:35:06 -05:00
print(obj_num);print(" : ");println(ref_num)
2015-11-19 16:27:36 -05:00
}
2015-11-25 15:35:06 -05:00
/*fun operator==(other: ref TestObj): bool {*/
fun operator==(other: TestObj): bool {
2015-11-19 16:27:36 -05:00
return obj_num == other.obj_num;
}
}
adt maybe_object {
no_obj,
an_obj: TestObj,
an_int: int
}
fun handle_possibility(it: maybe_int) {
2015-11-09 13:26:02 -05:00
if (it == maybe_int::no_int()) {
println("no int")
2015-11-25 15:35:06 -05:00
} else {
print("an int: ")
println(it.an_int)
}
}
fun give_maybe(give_it: bool): maybe_int {
if (give_it)
return maybe_int::an_int(7)
return maybe_int::no_int()
}
fun can_pass(it: options): options {
return it
}
fun main():int {
2015-11-25 15:35:06 -05:00
var it: options = can_pass(options::option1())
2015-11-09 13:26:02 -05:00
if (it == options::option0()) {
println("nope")
2015-11-09 13:26:02 -05:00
}
if (it == options::option1()) {
println("option1")
2015-11-09 13:26:02 -05:00
}
var possibility = give_maybe(false)
handle_possibility(possibility)
possibility = give_maybe(true)
handle_possibility(possibility)
if ( maybe_int::an_int(7) == maybe_int::an_int(7) )
println("equality true works!")
else
println("equality true fails!")
if ( maybe_int::an_int(7) != maybe_int::an_int(8) )
println("equality false works!")
else
println("equality false fails!")
match (maybe_int::an_int(11)) {
maybe_int::an_int(the_int) {
print("matched an int:")
print(the_int)
println(" correctly!")
}
maybe_int::no_int() {
println("matched no int incorrectly!")
}
}
match (maybe_int::no_int()) {
maybe_int::an_int(the_int) println("matched an int incorrectly!")
maybe_int::no_int() println("matched no int correctly!")
}
2015-11-19 16:27:36 -05:00
var obj_item = maybe_object::no_obj()
match (obj_item) {
maybe_object::no_obj() println("matched no_obj correctly")
maybe_object::an_obj(obj_instance) {
print("matched an_obj incorrectly ")
println(obj_instance.obj_num)
}
maybe_object::an_int(int_thiny) {
print("matched an_intj incorrectly ")
println(int_thiny)
}
}
2015-11-25 15:35:06 -05:00
println("assignment to old variable")
2015-11-19 16:27:36 -05:00
obj_item = maybe_object::an_obj(TestObj(100))
2015-11-25 15:35:06 -05:00
println("done assignment to old variable")
2015-11-19 16:27:36 -05:00
match (obj_item) {
maybe_object::no_obj() println("matched no_obj incorrectly")
maybe_object::an_obj(obj_instance) {
print("matched an_obj correctly ")
2015-11-25 15:35:06 -05:00
print(obj_instance.obj_num);print(" : ");println(obj_instance.ref_num)
2015-11-19 16:27:36 -05:00
}
maybe_object::an_int(int_thiny) {
print("matched an_intj incorrectly ")
println(int_thiny)
}
}
2015-11-25 15:35:06 -05:00
println("int assignment to old var")
2015-11-19 16:27:36 -05:00
obj_item = maybe_object::an_int(1337)
2015-11-25 15:35:06 -05:00
println("done int assignment to old var")
/*println("test copying thing");*/
/*var obj_item_new = maybe_object::an_obj(TestObj(1000))*/
/*println("new object assingment")*/
/*var obj_item_new_copy = obj_item_new;*/
/*println("done new object assingment")*/
/*println("done test copying thing");*/
2015-11-19 16:27:36 -05:00
match (obj_item) {
maybe_object::no_obj() println("matched no_obj incorrectly")
maybe_object::an_obj(obj_instance) {
print("matched an_obj incorrectly ")
2015-11-25 15:35:06 -05:00
print(obj_instance.obj_num);print(" : ");println(obj_instance.ref_num)
2015-11-19 16:27:36 -05:00
}
maybe_object::an_int(int_thiny) {
print("matched an_int correctly ")
println(int_thiny)
}
}
2015-11-25 15:35:06 -05:00
println("test copy_construct for non ref equality");
if (maybe_object::an_obj(TestObj(110)) == maybe_object::an_obj(TestObj(110)))
println("equality an_obj correctly ")
else
println("equality an_obj incorrectly ")
println("done test copy_construct for non ref equality");
println(maybe_object::an_int(10).an_int)
return 0
}