aboutsummaryrefslogtreecommitdiff
path: root/code
diff options
context:
space:
mode:
authorGinger Bill <github@gingerbill.org>2016-09-22 13:34:14 +0100
committerGinger Bill <github@gingerbill.org>2016-09-22 13:34:14 +0100
commit6907951f1eebcc9053ccfbe32a30dc704e790747 (patch)
treefb202a7f52e7d3f7d70362aa0a2436ba82080194 /code
parent664c2cd7a587feb18f02378506bdade2503343d3 (diff)
Fix type info generation
The problem: entry's index != entry->value in info_type_map But I was assuming this
Diffstat (limited to 'code')
-rw-r--r--code/demo.odin70
1 files changed, 49 insertions, 21 deletions
diff --git a/code/demo.odin b/code/demo.odin
index 19d997c33..4311ec213 100644
--- a/code/demo.odin
+++ b/code/demo.odin
@@ -8,7 +8,7 @@ main :: proc() {
// bounds_checking()
// type_introspection()
// any_type()
- crazy_introspection()
+ // crazy_introspection()
// namespaces_and_files()
// miscellany()
}
@@ -143,25 +143,51 @@ bounds_checking :: proc() {
}
type_introspection :: proc() {
+ {
+ info: ^Type_Info
+ x: int
+
+ info = type_info(int) // by type
+ info = type_info_of_val(x) // by value
+ // See: runtime.odin
+
+ match type i : info {
+ case Type_Info.Integer:
+ fmt.println("integer!")
+ case Type_Info.Float:
+ fmt.println("float!")
+ default:
+ fmt.println("potato!")
+ }
- info: ^Type_Info
- x: int
+ // Unsafe cast
+ integer_info := info as ^Type_Info.Integer
+ }
- info = type_info(int) // by type
- info = type_info(x) // by value
- // See: runtime.odin
+ {
+ Vector2 :: struct { x, y: f32 }
+ Vector3 :: struct { x, y, z: f32 }
- match type i : info {
- case Type_Info.Integer:
- fmt.println("integer!")
- case Type_Info.Float:
- fmt.println("float!")
- default:
- fmt.println("potato!")
- }
+ v1: Vector2
+ v2: Vector3
+ v3: Vector3
- // Unsafe cast
- integer_info := info as ^Type_Info.Integer
+ t1 := type_info_of_val(v1)
+ t2 := type_info_of_val(v2)
+ t3 := type_info_of_val(v3)
+
+ fmt.println()
+ fmt.print("Type of v1 is:\n\t", t1)
+ // fmt.fprint_type(os.stdout, t1)
+
+ fmt.println()
+ fmt.print("Type of v2 is:\n\t")
+ fmt.fprint_type(os.stdout, t2)
+
+ fmt.println("\n")
+ fmt.println("t1 == t2:", t1 == t2)
+ fmt.println("t2 == t3:", t2 == t3)
+ }
}
any_type :: proc() {
@@ -174,14 +200,17 @@ any_type :: proc() {
a = x
a = y
a = z
- a = a
+ a = a // This the "identity" type, it doesn't get converted
+
+ a = 123 // Literals are copied onto the stack first
// any has two members
// data - rawptr to the data
// type_info - pointer to the type info
fmt.println(x, y, z)
- // See: Implementation
+ // See: fmt.odin
+ // For variadic any procedures in action
}
crazy_introspection :: proc() {
@@ -204,6 +233,7 @@ crazy_introspection :: proc() {
fmt.println(s)
fmt.println(f)
+ // See: runtime.odin
}
@@ -225,9 +255,7 @@ crazy_introspection :: proc() {
name := (fruit_ti as ^Type_Info.Named).name // Unsafe casts
info := type_info_base(fruit_ti) as ^Type_Info.Enum // Unsafe casts
- fmt.printf("% :: enum ", name);
- fmt.fprint_type(os.stdout, info.base)
- fmt.printf(" {\n")
+ fmt.printf("% :: enum % {", name, info.base);
for i := 0; i < info.values.count; i++ {
fmt.printf("\t%\t= %,\n", info.names[i], info.values[i])
}