summaryrefslogtreecommitdiffhomepage
path: root/misc/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-06-11 14:03:16 +0200
committerTyge Løvset <[email protected]>2023-06-11 14:59:06 +0200
commitb564ef6bdfcd2437f1b4997f42054c45ccdedbb1 (patch)
treeb6c5458b8bc47279d18408f25e79eb6118808d78 /misc/examples
parentf3529a2600141dc7f84c734ea3bf5db8f7090e56 (diff)
downloadSTC-modified-b564ef6bdfcd2437f1b4997f42054c45ccdedbb1.tar.gz
STC-modified-b564ef6bdfcd2437f1b4997f42054c45ccdedbb1.zip
Added priv/linkage.h and renamed priv/template2.h => priv/template_undef.h.
Make all examples c++ compatible, except those using cspan.h Removed: crange_obj() Renamed: crange_make() => crange_init() Renamed: cspan_make() => cspan_init() Renamed: cstr_NULL => cstr_null Renamed: csview_NULL => csview_null
Diffstat (limited to 'misc/examples')
-rw-r--r--misc/examples/astar.c2
-rw-r--r--misc/examples/box.c2
-rw-r--r--misc/examples/box2.c6
-rw-r--r--misc/examples/csmap_find.c12
-rw-r--r--misc/examples/csmap_insert.c12
-rw-r--r--misc/examples/dining_philosophers.c5
-rw-r--r--misc/examples/forfilter.c5
-rwxr-xr-xmisc/examples/make.sh14
-rw-r--r--misc/examples/music_arc.c2
-rw-r--r--misc/examples/new_list.c2
-rw-r--r--misc/examples/new_sptr.c3
-rw-r--r--misc/examples/new_vec.c8
-rw-r--r--misc/examples/person_arc.c3
-rw-r--r--misc/examples/prime.c4
-rw-r--r--misc/examples/printspan.c8
-rw-r--r--misc/examples/read.c2
-rw-r--r--misc/examples/shape.c2
-rw-r--r--misc/examples/vikings.c10
18 files changed, 54 insertions, 48 deletions
diff --git a/misc/examples/astar.c b/misc/examples/astar.c
index db6bbd70..1b3876aa 100644
--- a/misc/examples/astar.c
+++ b/misc/examples/astar.c
@@ -21,7 +21,7 @@ point;
point
point_init(int x, int y, int width)
{
- return (point) { x, y, 0, width };
+ return c_LITERAL(point){ x, y, 0, width };
}
int
diff --git a/misc/examples/box.c b/misc/examples/box.c
index e352aa2b..a9131afa 100644
--- a/misc/examples/box.c
+++ b/misc/examples/box.c
@@ -5,7 +5,7 @@
typedef struct { cstr name, last; } Person;
Person Person_make(const char* name, const char* last) {
- return (Person){.name = cstr_from(name), .last = cstr_from(last)};
+ return c_LITERAL(Person){.name = cstr_from(name), .last = cstr_from(last)};
}
uint64_t Person_hash(const Person* a) {
diff --git a/misc/examples/box2.c b/misc/examples/box2.c
index 33212ef4..963a3815 100644
--- a/misc/examples/box2.c
+++ b/misc/examples/box2.c
@@ -29,12 +29,12 @@ typedef struct {
#include <stc/cbox.h> // BoxBoxPoint
Point origin(void) {
- return (Point){ .x=1.0, .y=2.0 };
+ return c_LITERAL(Point){ .x=1.0, .y=2.0 };
}
cbox_Point boxed_origin(void) {
// Allocate this point on the heap, and return a pointer to it
- return cbox_Point_make((Point){ .x=1.0, .y=2.0 });
+ return cbox_Point_make(c_LITERAL(Point){ .x=1.0, .y=2.0 });
}
@@ -47,7 +47,7 @@ int main(void) {
};
// Heap allocated rectangle
- cbox_Rectangle boxed_rectangle = cbox_Rectangle_make((Rectangle){
+ cbox_Rectangle boxed_rectangle = cbox_Rectangle_make(c_LITERAL(Rectangle){
.top_left = origin(),
.bottom_right = { .x=3.0, .y=-4.0 }
});
diff --git a/misc/examples/csmap_find.c b/misc/examples/csmap_find.c
index a8928410..c123e398 100644
--- a/misc/examples/csmap_find.c
+++ b/misc/examples/csmap_find.c
@@ -50,12 +50,12 @@ int main()
print_collection_csmap_istr(&m1);
typedef cvec_istr_value pair;
- cvec_istr_push(&v, (pair){43, "Tc"});
- cvec_istr_push(&v, (pair){41, "Nb"});
- cvec_istr_push(&v, (pair){46, "Pd"});
- cvec_istr_push(&v, (pair){42, "Mo"});
- cvec_istr_push(&v, (pair){44, "Ru"});
- cvec_istr_push(&v, (pair){44, "Ru"}); // attempt a duplicate
+ cvec_istr_push(&v, c_LITERAL(pair){43, "Tc"});
+ cvec_istr_push(&v, c_LITERAL(pair){41, "Nb"});
+ cvec_istr_push(&v, c_LITERAL(pair){46, "Pd"});
+ cvec_istr_push(&v, c_LITERAL(pair){42, "Mo"});
+ cvec_istr_push(&v, c_LITERAL(pair){44, "Ru"});
+ cvec_istr_push(&v, c_LITERAL(pair){44, "Ru"}); // attempt a duplicate
puts("Inserting the following vector data into m1:");
print_collection_cvec_istr(&v);
diff --git a/misc/examples/csmap_insert.c b/misc/examples/csmap_insert.c
index f96cc08f..18a88ec3 100644
--- a/misc/examples/csmap_insert.c
+++ b/misc/examples/csmap_insert.c
@@ -34,7 +34,7 @@ int main()
// insert single values
csmap_ii m1 = {0};
csmap_ii_insert(&m1, 1, 10);
- csmap_ii_push(&m1, (csmap_ii_value){2, 20});
+ csmap_ii_push(&m1, c_LITERAL(csmap_ii_value){2, 20});
puts("The original key and mapped values of m1 are:");
print_ii(m1);
@@ -61,11 +61,11 @@ int main()
csmap_ii m2 = {0};
cvec_ii v = {0};
typedef cvec_ii_value ipair;
- cvec_ii_push(&v, (ipair){43, 294});
- cvec_ii_push(&v, (ipair){41, 262});
- cvec_ii_push(&v, (ipair){45, 330});
- cvec_ii_push(&v, (ipair){42, 277});
- cvec_ii_push(&v, (ipair){44, 311});
+ cvec_ii_push(&v, c_LITERAL(ipair){43, 294});
+ cvec_ii_push(&v, c_LITERAL(ipair){41, 262});
+ cvec_ii_push(&v, c_LITERAL(ipair){45, 330});
+ cvec_ii_push(&v, c_LITERAL(ipair){42, 277});
+ cvec_ii_push(&v, c_LITERAL(ipair){44, 311});
puts("Inserting the following vector data into m2:");
c_foreach (e, cvec_ii, v)
diff --git a/misc/examples/dining_philosophers.c b/misc/examples/dining_philosophers.c
index f9c05e71..e13eb055 100644
--- a/misc/examples/dining_philosophers.c
+++ b/misc/examples/dining_philosophers.c
@@ -29,9 +29,10 @@ struct Dining {
// Philosopher coroutine
void philosopher(struct Philosopher* p)
{
+ double duration;
cco_routine(p) {
while (1) {
- double duration = 1.0 + crandf()*2.0;
+ duration = 1.0 + crandf()*2.0;
printf("Philosopher %d is thinking for %.0f minutes...\n", p->id, duration*10);
cco_timer_await(&p->tm, duration);
@@ -46,7 +47,7 @@ void philosopher(struct Philosopher* p)
cco_sem_release(p->left_fork);
cco_sem_release(p->right_fork);
}
-
+
cco_final:
printf("Philosopher %d finished\n", p->id);
}
diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c
index 94a84065..d39693b5 100644
--- a/misc/examples/forfilter.c
+++ b/misc/examples/forfilter.c
@@ -55,7 +55,8 @@ fn main() {
void demo2(void)
{
IVec vector = {0};
- c_forfilter (x, crange, crange_obj(INT64_MAX),
+ crange r = crange_init(INT64_MAX);
+ c_forfilter (x, crange, r,
c_flt_skipwhile(x, *x.ref != 11) &&
(*x.ref % 2) != 0 &&
c_flt_take(x, 5)
@@ -124,7 +125,7 @@ void demo5(void)
{
#define flt_even(i) ((*i.ref & 1) == 0)
#define flt_mid_decade(i) ((*i.ref % 10) != 0)
- crange R = crange_make(1963, INT32_MAX);
+ crange R = crange_init(1963, INT32_MAX);
c_forfilter (i, crange, R,
c_flt_skip(i,15) &&
diff --git a/misc/examples/make.sh b/misc/examples/make.sh
index 61d9f879..cf224950 100755
--- a/misc/examples/make.sh
+++ b/misc/examples/make.sh
@@ -7,13 +7,13 @@ if [ "$(uname)" = 'Linux' ]; then
fi
cc=gcc; cflags="-std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-maybe-uninitialized -Wno-implicit-fallthrough -Wno-missing-field-initializers"
-#cc=gcc; cflags="-DSTC_STATIC -std=c99 -g -Werror -Wfatal-errors -Wpedantic -Wall $sanitize"
-#cc=tcc; cflags="-DSTC_STATIC -std=c99 -Wall"
-#cc=clang; cflags="-DSTC_STATIC -std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough -Wno-missing-field-initializers"
-#cc=gcc; cflags="-DSTC_STATIC -x c++ -std=c++20 -O2 -s -Wall"
-#cc=cl; cflags="-DSTC_STATIC -nologo -O2 -MD -W3 -wd4003"
-#cc=cl; cflags="-DSTC_STATIC -nologo -TP -wd4003"
-#cc=cl; cflags="-DSTC_STATIC -nologo -std:c11 -wd4003"
+#cc=gcc; cflags="-std=c99 -g -Werror -Wfatal-errors -Wpedantic -Wall $sanitize"
+#cc=tcc; cflags="-std=c99 -Wall"
+#cc=clang; cflags="-std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough -Wno-missing-field-initializers"
+#cc=gcc; cflags="-x c++ -std=c++20 -O2 -s -Wall"
+#cc=cl; cflags="-nologo -O2 -MD -W3 -wd4003"
+#cc=cl; cflags="-nologo -TP -std:c++20 -wd4003"
+#cc=cl; cflags="-nologo -std:c11 -wd4003"
if [ "$cc" = "cl" ]; then
oflag='/Fe:'
diff --git a/misc/examples/music_arc.c b/misc/examples/music_arc.c
index 9c7173ef..18ea30c0 100644
--- a/misc/examples/music_arc.c
+++ b/misc/examples/music_arc.c
@@ -13,7 +13,7 @@ int Song_cmp(const Song* x, const Song* y)
{ return cstr_cmp(&x->title, &y->title); }
Song Song_make(const char* artist, const char* title)
- { return (Song){cstr_from(artist), cstr_from(title)}; }
+ { return c_LITERAL(Song){cstr_from(artist), cstr_from(title)}; }
void Song_drop(Song* s) {
printf("drop: %s\n", cstr_str(&s->title));
diff --git a/misc/examples/new_list.c b/misc/examples/new_list.c
index 993f1aac..382943bb 100644
--- a/misc/examples/new_list.c
+++ b/misc/examples/new_list.c
@@ -45,7 +45,7 @@ int main()
{
MyStruct my = {0};
clist_i32_push_back(&my.intlst, 123);
- clist_pnt_push_back(&my.pntlst, (Point){123, 456});
+ clist_pnt_push_back(&my.pntlst, c_LITERAL(Point){123, 456});
MyStruct_drop(&my);
clist_pnt plst = c_init(clist_pnt, {{42, 14}, {32, 94}, {62, 81}});
diff --git a/misc/examples/new_sptr.c b/misc/examples/new_sptr.c
index aa8dd175..36a61f9c 100644
--- a/misc/examples/new_sptr.c
+++ b/misc/examples/new_sptr.c
@@ -30,7 +30,8 @@ uint64_t Person_hash(const Person* p);
Person Person_make(const char* name, const char* last) {
- return (Person){.name = cstr_from(name), .last = cstr_from(last)};
+ Person p = {.name = cstr_from(name), .last = cstr_from(last)};
+ return p;
}
int Person_cmp(const Person* a, const Person* b) {
diff --git a/misc/examples/new_vec.c b/misc/examples/new_vec.c
index d4b66883..e10910d9 100644
--- a/misc/examples/new_vec.c
+++ b/misc/examples/new_vec.c
@@ -26,10 +26,10 @@ int main()
{
MyStruct my = {0};
- cvec_pnt_push(&my.pntvec, (Point){42, 14});
- cvec_pnt_push(&my.pntvec, (Point){32, 94});
- cvec_pnt_push(&my.pntvec, (Point){62, 81});
- cvec_pnt_push(&my.pntvec, (Point){32, 91});
+ cvec_pnt_push(&my.pntvec, c_LITERAL(Point){42, 14});
+ cvec_pnt_push(&my.pntvec, c_LITERAL(Point){32, 94});
+ cvec_pnt_push(&my.pntvec, c_LITERAL(Point){62, 81});
+ cvec_pnt_push(&my.pntvec, c_LITERAL(Point){32, 91});
cvec_pnt_sort(&my.pntvec);
diff --git a/misc/examples/person_arc.c b/misc/examples/person_arc.c
index b4b926da..3614c02d 100644
--- a/misc/examples/person_arc.c
+++ b/misc/examples/person_arc.c
@@ -6,7 +6,8 @@
typedef struct { cstr name, last; } Person;
Person Person_make(const char* name, const char* last) {
- return (Person){.name = cstr_from(name), .last = cstr_from(last)};
+ Person p = {.name = cstr_from(name), .last = cstr_from(last)};
+ return p;
}
int Person_cmp(const Person* a, const Person* b) {
diff --git a/misc/examples/prime.c b/misc/examples/prime.c
index cb0f8926..34fa144c 100644
--- a/misc/examples/prime.c
+++ b/misc/examples/prime.c
@@ -41,7 +41,9 @@ int main(void)
puts("\n");
puts("Show the last 50 primes using a temporary crange generator:");
- c_forfilter (i, crange, crange_obj(n - 1, 0, -2),
+ crange range = crange_init(n - 1, 0, -2);
+
+ c_forfilter (i, crange, range,
cbits_test(&primes, *i.ref/2) &&
c_flt_take(i, 50)
){
diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c
index b5099ed5..7564bd88 100644
--- a/misc/examples/printspan.c
+++ b/misc/examples/printspan.c
@@ -22,22 +22,22 @@ void printMe(intspan container) {
int main()
{
- intspan sp1 = cspan_make(intspan, {1, 2});
+ intspan sp1 = cspan_init(intspan, {1, 2});
printMe( sp1 );
printMe( c_init(intspan, {1, 2, 3}) );
int arr[] = {1, 2, 3, 4, 5, 6};
intspan sp2 = cspan_from_array(arr);
- printMe( (intspan)cspan_subspan(&sp2, 1, 4) );
+ printMe( c_LITERAL(intspan)cspan_subspan(&sp2, 1, 4) );
cvec_int vec = c_init(cvec_int, {1, 2, 3, 4, 5});
- printMe( (intspan)cspan_from(&vec) );
+ printMe( c_LITERAL(intspan)cspan_from(&vec) );
printMe( sp2 );
cstack_int stk = c_init(cstack_int, {1, 2, 3, 4, 5, 6, 7});
- printMe( (intspan)cspan_from(&stk) );
+ printMe( c_LITERAL(intspan)cspan_from(&stk) );
csset_str set = c_init(csset_str, {"5", "7", "4", "3", "8", "2", "1", "9", "6"});
printf("%d:", (int)csset_str_size(&set));
diff --git a/misc/examples/read.c b/misc/examples/read.c
index 545d706a..3c1cadf6 100644
--- a/misc/examples/read.c
+++ b/misc/examples/read.c
@@ -9,7 +9,7 @@ cvec_str read_file(const char* name)
{
cvec_str vec = cvec_str_init();
c_with (FILE* f = fopen(name, "r"), fclose(f))
- c_with (cstr line = cstr_NULL, cstr_drop(&line))
+ c_with (cstr line = cstr_null, cstr_drop(&line))
while (cstr_getline(&line, f))
cvec_str_push(&vec, cstr_clone(line));
return vec;
diff --git a/misc/examples/shape.c b/misc/examples/shape.c
index 22e993db..1d9fe5c5 100644
--- a/misc/examples/shape.c
+++ b/misc/examples/shape.c
@@ -137,7 +137,7 @@ int main(void)
{
Shapes shapes = {0};
- Triangle* tri1 = c_new(Triangle, Triangle_from((Point){5, 7}, (Point){12, 7}, (Point){12, 20}));
+ Triangle* tri1 = c_new(Triangle, Triangle_from(c_LITERAL(Point){5, 7}, c_LITERAL(Point){12, 7}, c_LITERAL(Point){12, 20}));
Polygon* pol1 = c_new(Polygon, Polygon_init());
Polygon* pol2 = c_new(Polygon, Polygon_init());
diff --git a/misc/examples/vikings.c b/misc/examples/vikings.c
index cf087119..d9024052 100644
--- a/misc/examples/vikings.c
+++ b/misc/examples/vikings.c
@@ -44,12 +44,12 @@ static inline RViking Viking_toraw(const Viking* vp) {
int main()
{
Vikings vikings = {0};
- Vikings_emplace(&vikings, (RViking){"Einar", "Norway"}, 20);
- Vikings_emplace(&vikings, (RViking){"Olaf", "Denmark"}, 24);
- Vikings_emplace(&vikings, (RViking){"Harald", "Iceland"}, 12);
- Vikings_emplace(&vikings, (RViking){"Björn", "Sweden"}, 10);
+ Vikings_emplace(&vikings, c_LITERAL(RViking){"Einar", "Norway"}, 20);
+ Vikings_emplace(&vikings, c_LITERAL(RViking){"Olaf", "Denmark"}, 24);
+ Vikings_emplace(&vikings, c_LITERAL(RViking){"Harald", "Iceland"}, 12);
+ Vikings_emplace(&vikings, c_LITERAL(RViking){"Björn", "Sweden"}, 10);
- Vikings_value* v = Vikings_get_mut(&vikings, (RViking){"Einar", "Norway"});
+ Vikings_value* v = Vikings_get_mut(&vikings, c_LITERAL(RViking){"Einar", "Norway"});
v->second += 3; // add 3 hp points to Einar
c_forpair (vk, hp, Vikings, vikings) {