From 2ce3fb0be5efe675d6910ba7a4a8165d0272f731 Mon Sep 17 00:00:00 2001 From: Martin Taibr Date: Sun, 17 Nov 2019 12:47:04 +0100 Subject: [PATCH] qc weirdness examples --- tests/_accumulate.qc | 39 +++++++++ tests/_from_wiki_typedef.qc | 17 ++++ tests/_fuck_retarded_sigs.qc | 63 ++++++++++++++ tests/_listing_fieldref.qc | 16 ++++ tests/_my_test.qc | 128 +++++++++++++++++++++++++++ tests/_string_nil.qc | 24 ++++++ tests/_tmp.qc | 18 ++++ tests/_verify_field_syntax.qc | 54 ++++++++++++ tests/_verify_fn_syntax_builtins.qc | 34 ++++++++ tests/_verify_fn_syntax_fields.qc | 129 ++++++++++++++++++++++++++++ tests/_verify_local_arrays.qc | 40 +++++++++ tests/_verify_new_qc.qc | 48 +++++++++++ 12 files changed, 610 insertions(+) create mode 100644 tests/_accumulate.qc create mode 100644 tests/_from_wiki_typedef.qc create mode 100644 tests/_fuck_retarded_sigs.qc create mode 100644 tests/_listing_fieldref.qc create mode 100644 tests/_my_test.qc create mode 100644 tests/_string_nil.qc create mode 100644 tests/_tmp.qc create mode 100644 tests/_verify_field_syntax.qc create mode 100644 tests/_verify_fn_syntax_builtins.qc create mode 100644 tests/_verify_fn_syntax_fields.qc create mode 100644 tests/_verify_local_arrays.qc create mode 100644 tests/_verify_new_qc.qc diff --git a/tests/_accumulate.qc b/tests/_accumulate.qc new file mode 100644 index 0000000..57e376c --- /dev/null +++ b/tests/_accumulate.qc @@ -0,0 +1,39 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +[[accumulate]] float f() { + print("a"); + return = 1; +} + +[[accumulate]] float f() { + print("b"); + return; +} + +[[accumulate]] float f() { + print("c"); + return 3; +} + +[[accumulate]] float f() { + print("d"); +} + +void main() { + print(ftos(f())); +} diff --git a/tests/_from_wiki_typedef.qc b/tests/_from_wiki_typedef.qc new file mode 100644 index 0000000..754edf9 --- /dev/null +++ b/tests/_from_wiki_typedef.qc @@ -0,0 +1,17 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; + +float sum3(float a, float b, float c) +{ + return a + b + c; +} + +typedef float(float, float, float) op3func_t; +var float(float a, float b, float c) f; +op3func_t g; + +void main() { + f = sum3; + g = f; + print(ftos(g(1, 2, 3)), "\n"); // prints 6 +} diff --git a/tests/_fuck_retarded_sigs.qc b/tests/_fuck_retarded_sigs.qc new file mode 100644 index 0000000..8993267 --- /dev/null +++ b/tests/_fuck_retarded_sigs.qc @@ -0,0 +1,63 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +void p() { + print("\n"); +} + +void pn(float n) = { + print(ftos(n), "\n"); +} + +// ====================================== + +void v2v() { print(ftos(5)); } +float v2f() { return 6; } +void f2v(float x) { print(ftos(x)); } +float f2f(float x) { return x; } + +void ret_v2v()() { return v2v; } +void ret_v2f()() { return v2f; } +void ret_f2v(float)() { return f2v; } +void ret_f2f(float)() { return f2f; } + +float ret()() { return v2f; } +float f(float)();// { return f2f; } + +//float()() xxx { return ret; } + +void() ret_v2f_2() { return v2f; } + +void main() { + void() local_v2v_1 = v2v; + void() local_v2v_2 = ret_v2v(); + + float() local_v2f_1 = v2f; + //float() local_v2f_2 = ret_v2f(); + + void(float) local_f2v_1 = f2v; + void(float) local_f2v_2 = ret_f2v(); + + float(float) local_f2f_1 = f2f; + //float(float) local_f2f_2 = ret_f2f(); + + pn(ret()()); + pn(f()(666)); + pn(f()(666)); + + ret_v2f_2()(); +} diff --git a/tests/_listing_fieldref.qc b/tests/_listing_fieldref.qc new file mode 100644 index 0000000..5a4868e --- /dev/null +++ b/tests/_listing_fieldref.qc @@ -0,0 +1,16 @@ +entity () spawn = #3; + +float TICK_LENGTH = 0.016; + +.float health; +.float energy; + +void regenerate_resource(entity player, .float res, float dt) { + player.(res) += 0.5 * dt; +} + +void main() { + entity player = spawn(); + regenerate_resource(player, health, TICK_LENGTH); + regenerate_resource(player, energy, TICK_LENGTH); +} diff --git a/tests/_my_test.qc b/tests/_my_test.qc new file mode 100644 index 0000000..d8a9994 --- /dev/null +++ b/tests/_my_test.qc @@ -0,0 +1,128 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +void p() { + print("\n"); +} + +void pn(float n) { + print(ftos(n), "\n"); +} + +// ====================================== + +float global_f; +.float field_f; + +float global_af[5]; +//float[5] global_af_2; NOPE + +float(float) fn_f2f; +float fn_f2f_2(float); +float fn_f2f(float) { return 0; } +float fn_f2f_2(float a) { return a; } + +.float() field_v2f_1; +.float(void) field_v2f_2; +.float(float) field_f2f; +.float fn_f2pf(float); +.float fn_f2pf(float) { return field_f; } + +float fn_f_f2f_2_f(float a, float(float wat) f2f) { + return f2f(a); +} + +float fn_f_f2f_2_f_2(float a, float f2f(float wat)) { + return f2f(a); +} + +typedef .float(float) Fn_f2pf; +var Fn_f2pf var_fn_f2pf; +//var .float(float) var_fn_f2pf_2; +//.Fn_v2pf idk; +//..float() idk2; +//Fn_v2pf fn_v2pf() = { return field_f; } +//.Fn_v2pf field_v2pf; + +typedef float(float) Fn_f2f; +typedef float ty_f; +typedef float ty_f5[5]; + +var float(float) var_fn_f2f; + +float idk() { return 5; }; +float idk2()() { return idk; } +float wtf(float, void) { return 6; } +float wtf2()(float, void) { return wtf; } + +.float sanity; +//.float(float) madness { return sanity; } +void aaaaaa() {} +.float(.float(float a) a()()()) cthulhu(.float a)(float, void, ...)(.float a(float))()()()() { return aaaaaa; } + +float unused; + +void main() { + //cthulhu(madness); + + unused = 1; + + global_f = 666; + global_af[4] = 10; + entity e = spawn(); + e.field_f = 667; + //var_fn_f2pf_2 = fn_f2pf; + //var_fn_f2pf = fn_f2pf; + //e.field_v2pf = fn_v2pf; + //e.idk(); + //idk = 6; + //idk2 = 7; + + pn(global_f); + pn(e.field_f); + p(); + + pn(fn_f2f(1)); + pn(fn_f2f_2(1)); + p(); + + e.field_f2f = fn_f2f; + pn(e.field_f2f(2)); + e.field_f2f = fn_f2f_2; + pn(e.field_f2f(2)); + p(); + + pn(e.(fn_f2pf(3))); + p(); + + .float loc_field_f = fn_f2pf(3); + pn(e.loc_field_f); + pn(e.(loc_field_f)); + p(); + + var_fn_f2f = fn_f2f_2; + pn(var_fn_f2f(4)); + p(); + + pn(fn_f_f2f_2_f(5, fn_f2f_2)); + pn(fn_f_f2f_2_f_2(5, fn_f2f_2)); + p(); + + float x, y = 1; + //pn(x); + pn(y); +} diff --git a/tests/_string_nil.qc b/tests/_string_nil.qc new file mode 100644 index 0000000..2c09d5b --- /dev/null +++ b/tests/_string_nil.qc @@ -0,0 +1,24 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +void main() { + string x; + string n = nil; + print(ftos(x == n)); + print(ftos(x == "")); + print(ftos("" == n)); +} diff --git a/tests/_tmp.qc b/tests/_tmp.qc new file mode 100644 index 0000000..52937a5 --- /dev/null +++ b/tests/_tmp.qc @@ -0,0 +1,18 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +void main() {} diff --git a/tests/_verify_field_syntax.qc b/tests/_verify_field_syntax.qc new file mode 100644 index 0000000..93ea14e --- /dev/null +++ b/tests/_verify_field_syntax.qc @@ -0,0 +1,54 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +void p() { + print("\n"); +} + +void pn(float n) { + print(ftos(n), "\n"); +} + +// ====================================== + +.float x, y; + +const .float mine, his = mine; + +void pe(entity e) { + print(ftos(e.x), ftos(e.y), ftos(e.mine), ftos(e.his), "\n"); +} + +void main() { + entity e = spawn(); + + pe(e); + .float x_bak = x; + e.x = 1; + e.y = 2; + e.mine = 3; + pn(e.x_bak); + pe(e); + x = y; + .float pf = mine; + pn(e.pf); + pe(e); + e.his = 4; + x = x_bak; + pn(e.pf); + pe(e); +} diff --git a/tests/_verify_fn_syntax_builtins.qc b/tests/_verify_fn_syntax_builtins.qc new file mode 100644 index 0000000..56578c6 --- /dev/null +++ b/tests/_verify_fn_syntax_builtins.qc @@ -0,0 +1,34 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +void p() { + print("\n"); +} + +void pn(float n) { + print(ftos(n), "\n"); +} + +// ====================================== + +float(float) sqrt1 = #13; +float sqrt2(float) = #13; + +void main() { + pn(sqrt1(4)); + pn(sqrt2(4)); +} diff --git a/tests/_verify_fn_syntax_fields.qc b/tests/_verify_fn_syntax_fields.qc new file mode 100644 index 0000000..6b4dd2e --- /dev/null +++ b/tests/_verify_fn_syntax_fields.qc @@ -0,0 +1,129 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +void p() { + print("\n"); +} + +void pn(float n) { + print(ftos(n), "\n"); +} + +// ====================================== + +// . always declares a field for all entities +// if you want to assign something to it, it's done on a per-entity basis +// .type fn_name(params) is an alias for .type(params) fn_name +// if you want a field that is a function returning a field you want something like `.(.type(...)(params) name` +// er, `.(.type(...))(params) name` I mean + +// actually: +// `.type fn_name(params)` declares functions that return fields +// `.type(params) fn_name` declare fields that are functions that return `type` + +float global_of_float; + +.float field_of_float; + +float(float f) fn_float_to_float_1; +float(float f) fn_float_to_float_1 { return f; } +float fn_float_to_float_2(float f); +float fn_float_to_float_2(float f) { return f; } + +.float fn_float_to_field_of_float(float f); +.float fn_float_to_field_of_float(float f) { return field_of_float; } +//.float(float f) fn_float_to_field_of_float_bad { return field_of_float; } +.float(float f) field_of_fn_float_to_float; + +.float(float f1) fn_float_to_field_of_fn_float_to_float(float f2); +.float(float f1) fn_float_to_field_of_fn_float_to_float(float f2) { return field_of_fn_float_to_float; } + +var .float global_of_field_of_float; + +var float(float f) global_of_fn_float_to_float_1; +var float global_of_fn_float_to_float_2(float f); + +var .float global_of_fn_float_to_field_of_float(float f); + +var .float(float f) todo1; + +var .float(float f1) todo2(float f2); + +void main() { + + entity e1 = spawn(); + + // globals + + pn(fn_float_to_float_1(10)); + + e1.(fn_float_to_field_of_float(5)) = 100; + pn(e1.field_of_float); + + e1.field_of_fn_float_to_float = fn_float_to_float_1; + pn(e1.field_of_fn_float_to_float(30)); + e1.field_of_fn_float_to_float = fn_float_to_float_2; + pn(e1.field_of_fn_float_to_float(30)); + + //pn(e1.fn_float_to_field_of_fn_float_to_float(6)(66)); + pn(e1.(fn_float_to_field_of_fn_float_to_float(6))(66)); + + global_of_field_of_float = field_of_float; + pn(e1.(global_of_field_of_float)); + + global_of_fn_float_to_float_1 = fn_float_to_float_2; + pn(global_of_fn_float_to_float_1(-10)); + + global_of_fn_float_to_float_2 = fn_float_to_float_1; + pn(global_of_fn_float_to_float_2(-10)); + + global_of_fn_float_to_field_of_float = fn_float_to_field_of_float; + pn(e1.(global_of_fn_float_to_field_of_float(-5))); + + p(); + + // locals + + .float local_of_field_of_float = field_of_float; + pn(e1.(local_of_field_of_float)); + + float(float f) local_of_fn_float_to_float_1; + local_of_fn_float_to_float_1 = fn_float_to_float_1; + pn(local_of_fn_float_to_float_1(11)); + local_of_fn_float_to_float_1 = fn_float_to_float_2; + pn(local_of_fn_float_to_float_1(11)); + + float local_of_fn_float_to_float_2(float f); + local_of_fn_float_to_float_2 = fn_float_to_float_1; + pn(local_of_fn_float_to_float_2(12)); + local_of_fn_float_to_float_2 = local_of_fn_float_to_float_1; + pn(local_of_fn_float_to_float_2(12)); + + .float local_of_fn_float_to_field_of_float(float f); + //local_of_fn_float_to_field_of_float = field_of_fn_float_to_float; // cannot assign .float(float) to .float(float) + local_of_fn_float_to_field_of_float = fn_float_to_field_of_float; + pn(e1.(local_of_fn_float_to_field_of_float(7))); + + .float(float f) local_of_field_of_fn_float_to_float; + local_of_field_of_fn_float_to_float = field_of_fn_float_to_float; + //local_of_field_of_fn_float_to_float = fn_float_to_field_of_float; // cannot assign .float(float) to .float(float) + pn(e1.local_of_field_of_fn_float_to_float(13)); + + .float(float f1) local_of_fn_float_to_field_of_fn_float_to_float(float f2); + local_of_fn_float_to_field_of_fn_float_to_float = fn_float_to_field_of_fn_float_to_float; + pn(e1.(local_of_fn_float_to_field_of_fn_float_to_float(8))(14)); +} diff --git a/tests/_verify_local_arrays.qc b/tests/_verify_local_arrays.qc new file mode 100644 index 0000000..c670b25 --- /dev/null +++ b/tests/_verify_local_arrays.qc @@ -0,0 +1,40 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +void p() { + print("\n"); +} + +void pn(float n) { + print(ftos(n), "\n"); +} + +// ====================================== + +void f(float x) { + if (x == 0) return; + + float xs[2]; + xs[0] = x; + pn(xs[0]); + f(x - 1); + pn(xs[0]); +} + +void main() { + f(3); +} diff --git a/tests/_verify_new_qc.qc b/tests/_verify_new_qc.qc new file mode 100644 index 0000000..97e87a4 --- /dev/null +++ b/tests/_verify_new_qc.qc @@ -0,0 +1,48 @@ +void (string str, ...) print = #1; +string (float val) ftos = #2; +entity () spawn = #3; +void (entity ent) kill = #4; +string (vector vec) vtos = #5; +void (string str) error = #6; +float (vector vec) vlen = #7; +string (entity ent) etos = #8; +float (string str) stof = #9; +string (...) strcat = #10; +float (string str1, string str2) strcmp = #11; +vector (vector vec) normalize = #12; +float (float val) sqrt = #13; +float (float val) floor = #14; +float (float val1, float val2) pow = #15; +vector (string str) stov = #16; + +void p() { + print("\n"); +} + +void pn(float n) { + print(ftos(n), "\n"); +} + +// ====================================== + +// note: the comments are from wiki, they're wrong +// https://gitlab.com/xonotic/xonotic/wikis/NewQC + +float global; // global variable +//float .foo; // entity field + +.float field; // fieldpointer +//.float .foo; // entity field of type fieldpointer + +float function_void_to_float(void) { return 5; } // function +//float foo*(void); // function pointer + +.float foo(void) { return field; } // function returning a fieldpointer .float +//.float foo*(void); // function pointer, returning a fieldpointer .float + +//float .foo(void); // entity field of type function returning float +//.float .foo(void); // entity field of type function returning fieldpointer + +void main() { + foo(); +} -- 2.39.2