Skip to content

Commit

Permalink
add notes for Polymorphic Procedures, parts 1 & 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemenno committed Jul 31, 2019
1 parent beaa532 commit 5e1d425
Show file tree
Hide file tree
Showing 19 changed files with 663 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/_data/videos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ demo_20150401:
“Polymorphic Procedures, part 1” _YouTube_, uploaded by Jonathan Blow, Apr 1, 2015
url: https://youtu.be/BwqeFrlSpuI

demo_20150401_2:
mla: |-
“Polymorphic Procedures, part 2” _YouTube_, uploaded by Jonathan Blow, Apr 1, 2015
url: https://youtu.be/7Fsy2WaxLOY

reboot_2017:
mla: |-
“Reboot Develop 2017 - Jonathan Blow, Thekla Inc. / Making Game Programming Less Terrible.” _YouTube_, uploaded by Reboot Develop, May 22, 2017
Expand Down
5 changes: 5 additions & 0 deletions docs/_includes/code/do_three_times.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// from: https://youtu.be/BwqeFrlSpuI?t=1200

do_three_times :: (func: ($T) -> void, arg: T) {
for 1..3 func(arg);
}
6 changes: 6 additions & 0 deletions docs/_includes/code/find.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// from: https://youtu.be/BwqeFrlSpuI?t=868

find :: (array: [] $T, item: T) -> s64 {
for array if it == item return it_index;
return -1; // (not found)
}
13 changes: 13 additions & 0 deletions docs/_includes/code/internal_bake_test.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// from: https://youtu.be/BwqeFrlSpuI?t=2444

internal :: (x: u16) {
y : $SomeType = x;

printf("internal: y is %d\n", y);
}

baked := #bake internal(SomeType = u64);
baked (6000);

// won't work to call it directly:
// internal(5);
54 changes: 54 additions & 0 deletions docs/_includes/code/length_body_text.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// from: https://youtu.be/7Fsy2WaxLOY?t=2456

concat :: (s: string, t: string) -> string {
return case(mprintf("%s%s", s, t), string);
}

length :: (using v: $T) -> float

#body_text (T: ^Type_Info) -> string {

if T.type != Type_Info_Tag.STRUCT {
printf("Error: 'length' wants a Vector type, but you gave it something that wasn't a struct!\n");
return null;
}

// this leaks; in real code, you would use a string builder here.

squares := "x*x";
if has_field(T, "y") squares = concat(squares, " + y*y");
if has_field(T, "z") squares = concat(squares, " + z*z");
if has_field(T, "w") squares = concat(squares, " + w*w");

s := mprintf("return sqrtf(%s);", squares);

printf("** Returning generated body text: %s\n", s);

return cast(s, string); // dumb cast!
}

Vector2 :: struct {
x : float = 1;
y : float = 4;
}

Vector3 :: struct {
x : float = 1;
y : float = 4;
z : float = 9;
}

Vector4 :: struct {
x : float = 1;
y : float = 4;
z : float = 9;
w : float = 16;
}

v2: Vector2;
v3: Vector3;
v4: Vector4;

printf("Length of v2 is: %f\n", length(v2));
printf("Length of v3 is: %f\n", length(v3));
printf("Length of v4 is: %f\n", length(v4));
39 changes: 39 additions & 0 deletions docs/_includes/code/map_test.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// from: https://youtu.be/7Fsy2WaxLOY?t=3540

map_test :: () {

/*

An experiment in programming in a more Lisp-y style.

*/

N :: 5;

a: [N] int;
for 0..N-1 a[it] = it+1;

for a printf("a[%d] = %d\n", it_index, it);

// map: apply f to each element of array, returning the resulting array.

map :: (array: [] $T, f: (T) -> $R) -> [] R {

result: [] R;
result.data = malloc(array.count * size_of(R));
result.count = array.count;

for array result[it_index] = f(it);

return result;

}

square :: (n: int) -> int { return n*n; }

b := map(a, square);
for b printf("b[%d] = %d\n", it_index, it);

free(b.data);

}
14 changes: 14 additions & 0 deletions docs/_includes/code/named_bake_test.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// from: https://youtu.be/BwqeFrlSpuI?t=2325

named_bake_test :: (a1: $A, a2: A, b1: $B, b2: B) -> A,B {
a_sum := a1 + a2;
b_product := b1 * b2;

return a_sum, b_product;
}

nbt :: #bake named_bake_test(A = float, B = int);
// specify the names in any order:
// nbt :: #bake named_bake_test(B = int, A = float);

sum, product := nbt(1, 2, 3, 4); // 3.000000, 12
17 changes: 17 additions & 0 deletions docs/_includes/code/pointers.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// from: https://youtu.be/-UPFH0eWHEI?t=769

N :: 10;

Entity :: struct {
serial_number : int;
}

value : Entity; // type declared as Entity
values : [N] float; // type declared as N-long array of floats

pointer : ^Entity; // type declared as address of an Entity
pointer = ^value; // assignment as address of Entity named 'value'
pointers : [N] ^Entity // type declared as N-long array of addresses of Entities

pointer := ^value; // get a pointer to a value's memory address
value := *pointer; // get a value by dereferencing a pointer
45 changes: 45 additions & 0 deletions docs/_includes/code/print_names_and_values_autobake.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// from: https://youtu.be/7Fsy2WaxLOY?t=3110

Spacing_Mode :: enum {
NONE,
INDENT,
EXDENT
}

using Spacing_Mode.members;

print_names_and_values :: (names: [] string, values: [] int, $$ mode: Spacing_Mode.strict, use_dots: bool) {
if mode == EXDENT printf(" ");
printf("print_names_and_values says:\n");

for 0..names.count-1 {
if mode == INDENT printf(" ");

printf("%8s:", names[it]);

if use_dots printf("......");
else printf(" ")

printf("%d\n", values[it]);
}
}

N :: 5;

names : [N] string;
values : [N] int;

names[0] = "Cow";
names[1] = "Horse";
names[2] = "Pig";
names[3] = "Mule";
names[4] = "Llama";

values[0] = 1;
values[1] = 4;
values[2] = 6;
values[3] = 4;
values[4] = 1;

print_names_and_values(names, values, NONE, false);
print_names_and_values(names, values, INDENT, true);
33 changes: 33 additions & 0 deletions docs/_includes/code/satisfies_interface.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// from: https://youtu.be/7Fsy2WaxLOY?t=1276

get_field :: (info: ^Type_Info_Struct, name: string) -> ^Type_Info_Struct_Member {
for ^ info.members if it.name == name return it;
return null;
}

satisfies_struct_interface :: (info: ^Type_Info_Struct, target: ^Type_Info_Struct) -> bool {
for target.members {
field := get_field(info, cast(it.name, string)); // dumb cast
if !field return false;

if field.type != it.type return false;
}

return true;
}

satisfies_interface :: (info: ^Type_Info, target: ^Type_Info) -> bool {
if info.type != Type_Info_Tag.STRUCT return false;
if target.type != Type_Info_Tag.STRUCT return false;

return satisfies_struct_interface(cast(info, ^Type_Info_Struct), cast(target, ^Type_Info_Struct));
}


Ints :: struct { x, y: int; }
Vector2 :: struct { x, y: float; }
Vector4 :: struct { x, y, z, w: float; }

printf("Does Ints satisfy Vector2? : %d\n", satisfies_interface(type_info(Ints), type_info(Vector2))); // 0 (false)
printf("Does Vector4 satisfy Vector2? : %d\n", satisfies_interface(type_info(Vector4), type_info(Vector2))); // 1 (true)
printf("Does Vector2 satisfy Vector4? : %d\n", satisfies_interface(type_info(Vector2), type_info(Vector4))); // 0 (false)
38 changes: 38 additions & 0 deletions docs/_includes/code/sum_modify.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// from: https://youtu.be/BwqeFrlSpuI?t=2871
// and: https://youtu.be/7Fsy2WaxLOY?t=109
// and: https://youtu.be/7Fsy2WaxLOY?t=355

sum :: (a : [] $T) -> $R // note that return type is different from T
#modify ensure_R_is_big_enough
{
result : R = 0;
for a result += it;

return result;
}

fill_and_sum :: (a : [] $T) -> $R
#modify ensure_R_is_big_enough // using same modify proc
{
for 0..a.count-1 a[it] = cast(it*10, T);

return sum(a);
}

ensure_R_is_big_enough :: (T : ^Type_Info, R : ^Type_Info) -> ^Type_Info, ^Type_Info {

if T.type == Type_Info_Tag.FLOAT return T, T; // handle float32 and 64 at their own precisions

if T.type != Type_Info_Tag.INTEGER {
printf("#modify proc ensure_R_is_big_enough only takes int or float types.\n");
return null, null;
}

// for integer types, ensure return type won't overflow
info := cast(T, ^Type_Info_Integer);

if info.size_in_bits >= 32 return T, T; // big enough

if info.signed return T, type_info(s32);
return T, type_info(u32);
}
7 changes: 7 additions & 0 deletions docs/_includes/code/swap.jai
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// from: https://youtu.be/BwqeFrlSpuI?t=501

swap :: (a: ^$T, b: ^T) {
tmp := *a;
*a = *b;
*b = tmp;
}
Loading

0 comments on commit 5e1d425

Please sign in to comment.