function pointers and callbacks
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
native :: import "../include/native.h"
|
||||
|
||||
Manual :: c_struct {
|
||||
value c_int
|
||||
}
|
||||
|
||||
mirror_manual :: c_func(value Manual) Manual {
|
||||
return value
|
||||
}
|
||||
|
||||
mirror_large :: c_func(value native.Large) native.Large {
|
||||
return value
|
||||
}
|
||||
|
||||
native_pair :: func(value native.Pair) native.Pair {
|
||||
return value
|
||||
}
|
||||
|
||||
global_pair native.Pair :: native.Pair { left = 1, right = 2 }
|
||||
|
||||
main :: func() i32 {
|
||||
pair native.Pair = native_pair(global_pair)
|
||||
pair.left = 10
|
||||
pair = native.echo_pair(pair)
|
||||
pairs [1]native.Pair :: [pair]
|
||||
_ = native_pair(pairs[0])
|
||||
|
||||
triple native.Triple :: native.echo_triple(native.Triple { first = 3, second = 4, third = 5 })
|
||||
floats native.Floats :: native.echo_floats(native.Floats { x = 6.0, y = 7.0 })
|
||||
large native.Large :: mirror_large(native.echo_large(native.Large { values = [8, 9, 10] }))
|
||||
nested native.Nested :: native.echo_nested(native.Nested {
|
||||
pair = native.Pair { left = 11, right = 12 },
|
||||
tail = 13,
|
||||
})
|
||||
arrays native.Arrays :: native.echo_arrays(native.Arrays { values = [14, 15, 16] })
|
||||
choice native.Choice = native.Choice { decimal = 1.0 }
|
||||
choice.integer = 17
|
||||
choice = native.echo_choice(choice)
|
||||
forward native.Forward :: native.echo_forward(native.Forward { value = 19 })
|
||||
manual Manual :: mirror_manual(Manual { value = 18 })
|
||||
_ = manual.value
|
||||
_ = forward.value
|
||||
|
||||
_ = native.verify_records(pair, triple, floats, large, nested, arrays, choice)
|
||||
return 1
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef BROLANG_RECORDS_H
|
||||
#define BROLANG_RECORDS_H
|
||||
|
||||
typedef struct Pair {
|
||||
int left;
|
||||
int right;
|
||||
} Pair;
|
||||
|
||||
typedef struct Triple {
|
||||
int first;
|
||||
int second;
|
||||
int third;
|
||||
} Triple;
|
||||
|
||||
typedef struct Floats {
|
||||
float x;
|
||||
float y;
|
||||
} Floats;
|
||||
|
||||
typedef struct Large {
|
||||
long values[3];
|
||||
} Large;
|
||||
|
||||
typedef struct Nested {
|
||||
Pair pair;
|
||||
int tail;
|
||||
} Nested;
|
||||
|
||||
typedef struct Arrays {
|
||||
int values[3];
|
||||
} Arrays;
|
||||
|
||||
typedef union Choice {
|
||||
long integer;
|
||||
double decimal;
|
||||
} Choice;
|
||||
|
||||
typedef struct Forward Forward;
|
||||
struct Forward {
|
||||
int value;
|
||||
};
|
||||
|
||||
Pair echo_pair(Pair value);
|
||||
Triple echo_triple(Triple value);
|
||||
Floats echo_floats(Floats value);
|
||||
Large echo_large(Large value);
|
||||
Nested echo_nested(Nested value);
|
||||
Arrays echo_arrays(Arrays value);
|
||||
Choice echo_choice(Choice value);
|
||||
Forward echo_forward(Forward value);
|
||||
int verify_records(Pair pair, Triple triple, Floats floats, Large large, Nested nested, Arrays arrays, Choice choice);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "include/native.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
Pair echo_pair(Pair value) { return value; }
|
||||
Triple echo_triple(Triple value) { return value; }
|
||||
Floats echo_floats(Floats value) { return value; }
|
||||
Large echo_large(Large value) { return value; }
|
||||
Nested echo_nested(Nested value) { return value; }
|
||||
Arrays echo_arrays(Arrays value) { return value; }
|
||||
Choice echo_choice(Choice value) { return value; }
|
||||
Forward echo_forward(Forward value) { return value; }
|
||||
|
||||
int verify_records(Pair pair, Triple triple, Floats floats, Large large, Nested nested, Arrays arrays, Choice choice) {
|
||||
if (!(pair.left == 10 && pair.right == 2 &&
|
||||
triple.first == 3 && triple.second == 4 && triple.third == 5 &&
|
||||
floats.x == 6.0f && floats.y == 7.0f &&
|
||||
large.values[0] == 8 && large.values[1] == 9 && large.values[2] == 10 &&
|
||||
nested.pair.left == 11 && nested.pair.right == 12 && nested.tail == 13 &&
|
||||
arrays.values[0] == 14 && arrays.values[1] == 15 && arrays.values[2] == 16 &&
|
||||
choice.integer == 17)) {
|
||||
abort();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user