xbintsc 0.6.13 → 0.6.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/driver/version.d.ts +1 -1
  3. package/dist/src/driver/version.js +1 -1
  4. package/dist/src/extensions/gui/features.d.ts +2 -0
  5. package/dist/src/extensions/gui/features.js +24 -0
  6. package/dist/src/extensions/gui/features.js.map +1 -1
  7. package/dist/src/extensions/gui/index.js +39 -0
  8. package/dist/src/extensions/gui/index.js.map +1 -1
  9. package/dist/tests/e2e/gui-example-pelican3d.test.d.ts +6 -4
  10. package/dist/tests/e2e/gui-example-pelican3d.test.js +103 -44
  11. package/dist/tests/e2e/gui-example-pelican3d.test.js.map +1 -1
  12. package/dist/tests/e2e/gui-example-webgpu.test.d.ts +9 -0
  13. package/dist/tests/e2e/gui-example-webgpu.test.js +59 -0
  14. package/dist/tests/e2e/gui-example-webgpu.test.js.map +1 -0
  15. package/dist/tests/e2e/gui-helpers.d.ts +1 -1
  16. package/dist/tests/e2e/gui-webgpu.test.d.ts +11 -0
  17. package/dist/tests/e2e/gui-webgpu.test.js +402 -0
  18. package/dist/tests/e2e/gui-webgpu.test.js.map +1 -0
  19. package/dist/tests/extensions/gui-features.test.js +18 -1
  20. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  21. package/doc/ai/extensions.md +7 -1
  22. package/doc/ai/zh-CN/extensions.md +6 -1
  23. package/doc/gui/webgpu.md +121 -0
  24. package/doc/gui.md +12 -1
  25. package/doc/zh-CN/gui/webgpu.md +111 -0
  26. package/doc/zh-CN/gui.md +10 -1
  27. package/llms.txt +1 -0
  28. package/package.json +7 -7
  29. package/runtime/ext_gui/browser.cpp +10 -0
  30. package/runtime/ext_gui/dom_api_canvas.cpp +21 -0
  31. package/runtime/ext_gui/dom_api_canvas_extra.cpp +61 -0
  32. package/runtime/ext_gui/gpu_feature.cpp +2 -0
  33. package/runtime/ext_gui/gpu_shader_api.h +6 -0
  34. package/runtime/ext_gui/gui_canvas.cpp +18 -0
  35. package/runtime/ext_gui/renderer_image.cpp +6 -0
  36. package/runtime/ext_gui/webgl_glsl.h +8 -0
  37. package/runtime/ext_gui/webgl_glsl_compile.cpp +11 -5
  38. package/runtime/ext_gui/webgl_msl.cpp +20 -9
  39. package/runtime/ext_gui/webgl_msl.h +4 -0
  40. package/runtime/ext_gui/webgpu.h +371 -0
  41. package/runtime/ext_gui/webgpu_api.h +42 -0
  42. package/runtime/ext_gui/webgpu_bindings.cpp +364 -0
  43. package/runtime/ext_gui/webgpu_bindings.h +59 -0
  44. package/runtime/ext_gui/webgpu_bridge.c +21 -0
  45. package/runtime/ext_gui/webgpu_buffer.cpp +293 -0
  46. package/runtime/ext_gui/webgpu_canvas.cpp +305 -0
  47. package/runtime/ext_gui/webgpu_command.cpp +510 -0
  48. package/runtime/ext_gui/webgpu_compute_pass.cpp +138 -0
  49. package/runtime/ext_gui/webgpu_convert.cpp +313 -0
  50. package/runtime/ext_gui/webgpu_device.cpp +370 -0
  51. package/runtime/ext_gui/webgpu_feature.cpp +74 -0
  52. package/runtime/ext_gui/webgpu_objects.cpp +125 -0
  53. package/runtime/ext_gui/webgpu_pass.cpp +295 -0
  54. package/runtime/ext_gui/webgpu_pass.h +33 -0
  55. package/runtime/ext_gui/webgpu_pipeline.cpp +463 -0
  56. package/runtime/ext_gui/webgpu_queue.cpp +222 -0
  57. package/runtime/ext_gui/webgpu_render_pass.cpp +417 -0
  58. package/runtime/ext_gui/webgpu_resources.cpp +315 -0
  59. package/runtime/ext_gui/webgpu_shader.cpp +246 -0
  60. package/runtime/ext_gui/wgsl.h +99 -0
  61. package/runtime/ext_gui/wgsl_api.cpp +137 -0
  62. package/runtime/ext_gui/wgsl_ast.h +233 -0
  63. package/runtime/ext_gui/wgsl_emit.cpp +452 -0
  64. package/runtime/ext_gui/wgsl_emit.h +128 -0
  65. package/runtime/ext_gui/wgsl_emit_call.cpp +281 -0
  66. package/runtime/ext_gui/wgsl_emit_expr.cpp +192 -0
  67. package/runtime/ext_gui/wgsl_emit_stmt.cpp +209 -0
  68. package/runtime/ext_gui/wgsl_entry.cpp +189 -0
  69. package/runtime/ext_gui/wgsl_infer.cpp +171 -0
  70. package/runtime/ext_gui/wgsl_lexer.cpp +179 -0
  71. package/runtime/ext_gui/wgsl_lexer.h +32 -0
  72. package/runtime/ext_gui/wgsl_parse_expr.cpp +215 -0
  73. package/runtime/ext_gui/wgsl_parse_stmt.cpp +259 -0
  74. package/runtime/ext_gui/wgsl_parser.cpp +457 -0
  75. package/runtime/ext_gui/wgsl_parser.h +77 -0
  76. package/runtime/ext_gui/wgsl_type.cpp +232 -0
  77. package/src/driver/version.ts +1 -1
  78. package/src/extensions/gui/features.ts +24 -0
  79. package/src/extensions/gui/index.ts +39 -0
@@ -0,0 +1,189 @@
1
+ /*
2
+ * xbintsc GUI engine — WGSL entry-point wrappers.
3
+ *
4
+ * A WGSL entry point is an ordinary function whose parameters and return value
5
+ * carry `@location`/`@builtin` attributes. GLSL has no equivalent, so the
6
+ * translator emits the entry point as a regular function and a `main` wrapper
7
+ * that declares the `in`/`out` globals, calls it with the builtins and copies
8
+ * the returned struct's members to `gl_Position`/`gl_FragDepth` or to the
9
+ * location-qualified outputs.
10
+ */
11
+
12
+ #include <string>
13
+ #include <vector>
14
+
15
+ #include "wgsl_emit.h"
16
+
17
+ namespace xtgui {
18
+ namespace wgsl {
19
+ namespace {
20
+
21
+ bool integer_scalar(Scalar scalar) {
22
+ return scalar == Scalar::I32 || scalar == Scalar::U32;
23
+ }
24
+
25
+ std::string location_declare(const Type &type, const std::string &name, int location, bool output,
26
+ const std::string &interpolate, bool flat) {
27
+ std::string qualifier = "layout(location = " + std::to_string(location) + ") ";
28
+ if (flat || interpolate == "flat") qualifier += "flat ";
29
+ else if (interpolate == "linear") qualifier += "noperspective ";
30
+ else if (interpolate == "perspective") qualifier += "smooth ";
31
+ qualifier += output ? "out " : "in ";
32
+ return qualifier + glsl_decl(type, name) + ";\n";
33
+ }
34
+
35
+ } // namespace
36
+
37
+ std::string Emitter::builtin_variable(const std::string &name, Stage stage) const {
38
+ if (stage == Stage::Vertex) {
39
+ /* Vulkan GLSL spells them …Index (the OpenGL …ID aliases are rejected). */
40
+ if (name == "vertex_index") return "gl_VertexIndex";
41
+ if (name == "instance_index") return "gl_InstanceIndex";
42
+ }
43
+ if (stage == Stage::Fragment) {
44
+ if (name == "position") return "gl_FragCoord";
45
+ if (name == "front_facing") return "gl_FrontFacing";
46
+ if (name == "sample_index") return "gl_SampleID";
47
+ if (name == "sample_mask") return "gl_SampleMaskIn[0]";
48
+ if (name == "primitive_index") return "gl_PrimitiveID";
49
+ }
50
+ if (stage == Stage::Compute) {
51
+ if (name == "local_invocation_id") return "gl_LocalInvocationID";
52
+ if (name == "local_invocation_index") return "gl_LocalInvocationIndex";
53
+ if (name == "global_invocation_id") return "gl_GlobalInvocationID";
54
+ if (name == "workgroup_id") return "gl_WorkGroupID";
55
+ if (name == "num_workgroups") return "gl_NumWorkGroups";
56
+ if (name == "subgroup_invocation_id") return "gl_SubgroupInvocationID";
57
+ if (name == "subgroup_size") return "gl_SubgroupSize";
58
+ }
59
+ return std::string();
60
+ }
61
+
62
+ bool Emitter::emit_entry(const FuncDecl &entry, std::string *text) {
63
+ std::string inputs;
64
+ std::string call_args;
65
+ int input_index = 0;
66
+ for (const Param &param : entry.params) {
67
+ if (!param.builtin.empty()) {
68
+ std::string value = builtin_variable(param.builtin, stage_);
69
+ if (value.empty()) {
70
+ fail("unsupported builtin '@" + param.builtin + "' in entry point '" + entry.name + "'");
71
+ return false;
72
+ }
73
+ if (param.type != nullptr && param.type->kind == Kind::Scalar &&
74
+ param.type->scalar == Scalar::U32 && param.builtin != "local_invocation_id" &&
75
+ param.builtin != "global_invocation_id" && param.builtin != "workgroup_id" &&
76
+ param.builtin != "num_workgroups" && param.builtin != "local_invocation_index") {
77
+ value = "uint(" + value + ")";
78
+ }
79
+ if (!call_args.empty()) call_args += ", ";
80
+ call_args += value;
81
+ continue;
82
+ }
83
+ if (param.type != nullptr && param.type->kind == Kind::Struct && param.type->strct != nullptr) {
84
+ std::string constructor;
85
+ for (const Member &member : param.type->strct->members) {
86
+ if (!member.builtin.empty()) {
87
+ std::string value = builtin_variable(member.builtin, stage_);
88
+ if (value.empty()) {
89
+ fail("unsupported input builtin '@" + member.builtin + "'");
90
+ return false;
91
+ }
92
+ if (!constructor.empty()) constructor += ", ";
93
+ constructor += value;
94
+ continue;
95
+ }
96
+ if (member.location < 0 || member.type == nullptr) continue;
97
+ const std::string name = "xg_loc_in_" + std::to_string(input_index++);
98
+ inputs += location_declare(*member.type, name, member.location, false, member.interpolate,
99
+ member.type->kind == Kind::Scalar && integer_scalar(member.type->scalar));
100
+ if (!constructor.empty()) constructor += ", ";
101
+ constructor += name;
102
+ }
103
+ if (!call_args.empty()) call_args += ", ";
104
+ call_args += param.type->strct->name + "(" + constructor + ")";
105
+ continue;
106
+ }
107
+ if (param.location >= 0 && param.type != nullptr) {
108
+ const std::string name = "xg_loc_in_" + std::to_string(input_index++);
109
+ inputs += location_declare(*param.type, name, param.location, false, param.interpolate,
110
+ param.type->kind == Kind::Scalar && integer_scalar(param.type->scalar));
111
+ if (!call_args.empty()) call_args += ", ";
112
+ call_args += name;
113
+ continue;
114
+ }
115
+ fail("entry point parameter '" + param.name + "' needs @location or @builtin");
116
+ return false;
117
+ }
118
+
119
+ std::string outputs;
120
+ std::string assignments;
121
+ int output_index = 0;
122
+ if (entry.ret != nullptr && entry.ret->kind != Kind::Void) {
123
+ std::string result = "xg_result";
124
+ if (entry.ret->kind == Kind::Struct && entry.ret->strct != nullptr) {
125
+ for (const Member &member : entry.ret->strct->members) {
126
+ if (member.type == nullptr) continue;
127
+ const std::string value = result + "." + member_name(member.name);
128
+ if (!member.builtin.empty()) {
129
+ if (member.builtin == "position") assignments += " gl_Position = " + value + ";\n";
130
+ else if (member.builtin == "frag_depth") assignments += " gl_FragDepth = " + value + ";\n";
131
+ else if (member.builtin == "sample_mask") assignments += " gl_SampleMask[0] = int(" + value + ");\n";
132
+ else {
133
+ fail("unsupported entry-point output builtin '@" + member.builtin + "'");
134
+ return false;
135
+ }
136
+ continue;
137
+ }
138
+ if (member.location >= 0) {
139
+ const std::string name = "xg_loc_out_" + std::to_string(output_index++);
140
+ outputs += location_declare(*member.type, name, member.location, true, member.interpolate,
141
+ member.type->kind == Kind::Scalar &&
142
+ integer_scalar(member.type->scalar));
143
+ assignments += " " + name + " = " + value + ";\n";
144
+ }
145
+ }
146
+ } else if (!entry.ret_builtin.empty()) {
147
+ if (entry.ret_builtin == "position") assignments += " gl_Position = " + result + ";\n";
148
+ else if (entry.ret_builtin == "frag_depth") assignments += " gl_FragDepth = " + result + ";\n";
149
+ else {
150
+ fail("unsupported entry-point output builtin '@" + entry.ret_builtin + "'");
151
+ return false;
152
+ }
153
+ } else if (entry.ret_location >= 0) {
154
+ const std::string name = "xg_loc_out_0";
155
+ outputs += location_declare(*entry.ret, name, entry.ret_location, true, entry.ret_interpolate,
156
+ entry.ret->kind == Kind::Scalar &&
157
+ integer_scalar(entry.ret->scalar));
158
+ assignments += " " + name + " = " + result + ";\n";
159
+ }
160
+ }
161
+
162
+ std::string head;
163
+ if (stage_ == Stage::Compute) {
164
+ head = "layout(local_size_x = " + std::to_string(entry.workgroup[0]) +
165
+ ", local_size_y = " + std::to_string(entry.workgroup[1]) +
166
+ ", local_size_z = " + std::to_string(entry.workgroup[2]) + ") in;\n";
167
+ out_->workgroup_size[0] = entry.workgroup[0];
168
+ out_->workgroup_size[1] = entry.workgroup[1];
169
+ out_->workgroup_size[2] = entry.workgroup[2];
170
+ }
171
+
172
+ std::string definition = emit_user_function(entry);
173
+ std::string wrapper = "void main() {\n";
174
+ const bool returns_value = entry.ret != nullptr && entry.ret->kind != Kind::Void;
175
+ if (returns_value) {
176
+ wrapper += " " + glsl_decl(*entry.ret, "xg_result") + " = xg_" + entry.name + "(" +
177
+ call_args + ");\n";
178
+ } else {
179
+ wrapper += " xg_" + entry.name + "(" + call_args + ");\n";
180
+ }
181
+ wrapper += assignments;
182
+ wrapper += "}\n";
183
+
184
+ *text = head + inputs + outputs + definition + wrapper;
185
+ return true;
186
+ }
187
+
188
+ } // namespace wgsl
189
+ } // namespace xtgui
@@ -0,0 +1,171 @@
1
+ /*
2
+ * xbintsc GUI engine — WGSL expression typing.
3
+ *
4
+ * GLSL needs a declared type for every `let` and differs from WGSL where an
5
+ * operator's spelling is not enough (vector comparisons become `lessThan`, a
6
+ * float `%` becomes `mod`, …), so the emitter asks this pass for the type of
7
+ * the expressions it writes.
8
+ */
9
+
10
+ #include "wgsl_emit.h"
11
+
12
+ namespace xtgui {
13
+ namespace wgsl {
14
+ namespace {
15
+
16
+ TypePtr unknown() {
17
+ auto type = std::make_shared<Type>();
18
+ type->kind = Kind::Unknown;
19
+ return type;
20
+ }
21
+
22
+ TypePtr vector_of(Scalar scalar, int width) {
23
+ return width > 1 ? make_vector(scalar, width) : make_scalar(scalar);
24
+ }
25
+
26
+ int width_of(const TypePtr &type) {
27
+ if (type == nullptr) return 1;
28
+ if (type->kind == Kind::Vector) return type->width;
29
+ return 1;
30
+ }
31
+
32
+ TypePtr common(const TypePtr &left, const TypePtr &right) {
33
+ if (left == nullptr || left->kind == Kind::Unknown) return right;
34
+ if (right == nullptr || right->kind == Kind::Unknown) return left;
35
+ if (!left->is_numeric() || !right->is_numeric()) return left;
36
+ Scalar scalar = left->scalar;
37
+ if (left->scalar == Scalar::F32 || right->scalar == Scalar::F32) scalar = Scalar::F32;
38
+ else if (left->scalar == Scalar::U32 || right->scalar == Scalar::U32) scalar = Scalar::U32;
39
+ const int width = width_of(left) > width_of(right) ? width_of(left) : width_of(right);
40
+ return vector_of(scalar, width);
41
+ }
42
+
43
+ TypePtr scalar_type(Scalar scalar) { return make_scalar(scalar); }
44
+
45
+ TypePtr texture_result(const std::string &name, const std::vector<TypePtr> &args) {
46
+ if (name.compare(0, 15, "textureSample") == 0) {
47
+ if (name.find("Compare") != std::string::npos) return scalar_type(Scalar::F32);
48
+ return make_vector(Scalar::F32, 4);
49
+ }
50
+ if (name == "textureLoad") return make_vector(Scalar::F32, 4);
51
+ if (name == "textureGather") return make_vector(Scalar::F32, 4);
52
+ if (name == "textureDimensions") {
53
+ if (name == "textureDimensions") return make_vector(Scalar::U32, 2);
54
+ }
55
+ (void)args;
56
+ return nullptr;
57
+ }
58
+
59
+ TypePtr builtin_result(const std::string &name, const std::vector<TypePtr> &args) {
60
+ const TypePtr first = args.empty() ? nullptr : args[0];
61
+ if (name == "f32" || name == "f16") return scalar_type(Scalar::F32);
62
+ if (name == "i32") return scalar_type(Scalar::I32);
63
+ if (name == "u32") return scalar_type(Scalar::U32);
64
+ if (name == "bool") return scalar_type(Scalar::Bool);
65
+ if (name == "dot" || name == "length" || name == "distance") return scalar_type(Scalar::F32);
66
+ if (name == "determinant") return scalar_type(Scalar::F32);
67
+ if (name == "all" || name == "any") return scalar_type(Scalar::Bool);
68
+ if (name == "select") return first;
69
+ if (name == "arrayLength") return scalar_type(Scalar::U32);
70
+ if (name == "cross") return make_vector(Scalar::F32, 3);
71
+ if (name == "transpose") {
72
+ if (first != nullptr && first->kind == Kind::Matrix) {
73
+ return make_matrix(first->scalar, first->rows, first->cols);
74
+ }
75
+ return first;
76
+ }
77
+ if (name == "countOneBits" || name == "reverseBits" || name == "firstLeadingBit" ||
78
+ name == "firstTrailingBit" || name == "extractBits" || name == "insertBits") {
79
+ return first;
80
+ }
81
+ if (name.size() > 5 && name.compare(0, 7, "texture") == 0) {
82
+ TypePtr result = texture_result(name, args);
83
+ if (result != nullptr) return result;
84
+ }
85
+ return first;
86
+ }
87
+
88
+ } // namespace
89
+
90
+ TypePtr Emitter::type_of(const Expr &node) {
91
+ switch (node.kind) {
92
+ case Expr::K::Int: {
93
+ if (!node.text.empty() && node.text.back() == 'u') return scalar_type(Scalar::U32);
94
+ return scalar_type(Scalar::I32);
95
+ }
96
+ case Expr::K::Float: return scalar_type(Scalar::F32);
97
+ case Expr::K::Bool: return scalar_type(Scalar::Bool);
98
+ case Expr::K::Ident: {
99
+ if (const Local *local = lookup_local(node.text)) return local->type;
100
+ if (const GlobalDecl *global = find_global(node.text)) return global->type;
101
+ return unknown();
102
+ }
103
+ case Expr::K::Unary: {
104
+ TypePtr operand = node.args.empty() ? nullptr : type_of(*node.args[0]);
105
+ if (node.text == "!") {
106
+ if (operand != nullptr && operand->kind == Kind::Vector) {
107
+ return make_vector(Scalar::Bool, operand->width);
108
+ }
109
+ return scalar_type(Scalar::Bool);
110
+ }
111
+ return operand;
112
+ }
113
+ case Expr::K::Binary: {
114
+ TypePtr left = node.args.size() > 0 ? type_of(*node.args[0]) : nullptr;
115
+ TypePtr right = node.args.size() > 1 ? type_of(*node.args[1]) : nullptr;
116
+ if (node.text == "&&" || node.text == "||") return scalar_type(Scalar::Bool);
117
+ if (node.text == "==" || node.text == "!=") return scalar_type(Scalar::Bool);
118
+ if (node.text == "<" || node.text == ">" || node.text == "<=" || node.text == ">=") {
119
+ const int width = width_of(left) > width_of(right) ? width_of(left) : width_of(right);
120
+ return vector_of(Scalar::Bool, width);
121
+ }
122
+ if (node.text == "<<" || node.text == ">>") return left;
123
+ return common(left, right);
124
+ }
125
+ case Expr::K::Index: {
126
+ TypePtr base = node.args.empty() ? nullptr : type_of(*node.args[0]);
127
+ if (base == nullptr) return unknown();
128
+ if (base->kind == Kind::Array) return base->elem;
129
+ if (base->kind == Kind::Vector) return scalar_type(base->scalar);
130
+ if (base->kind == Kind::Matrix) return make_vector(base->scalar, base->rows);
131
+ return unknown();
132
+ }
133
+ case Expr::K::Member: {
134
+ TypePtr base = node.args.empty() ? nullptr : type_of(*node.args[0]);
135
+ if (base == nullptr) return unknown();
136
+ if (base->kind == Kind::Struct && base->strct != nullptr) {
137
+ for (const Member &member : base->strct->members) {
138
+ if (member.name == node.member) return member.type;
139
+ }
140
+ return unknown();
141
+ }
142
+ if (base->kind == Kind::Vector) {
143
+ if (node.member == "length") return scalar_type(Scalar::U32);
144
+ const int width = (int)node.member.size();
145
+ return vector_of(base->scalar, width);
146
+ }
147
+ return unknown();
148
+ }
149
+ case Expr::K::ArrayCtor: return node.type;
150
+ case Expr::K::Bitcast: return node.type;
151
+ case Expr::K::Call: {
152
+ if (!node.text.empty() && node.text[0] == '.') {
153
+ if (node.text == ".length") return scalar_type(Scalar::U32);
154
+ return unknown();
155
+ }
156
+ std::vector<TypePtr> args;
157
+ args.reserve(node.args.size());
158
+ for (const ExprPtr &arg : node.args) {
159
+ if (arg != nullptr) args.push_back(type_of(*arg));
160
+ }
161
+ if (TypePtr constructed = type_by_name(module_, node.text)) return constructed;
162
+ if (const FuncDecl *function = find_function(node.text)) return function->ret;
163
+ return builtin_result(node.text, args);
164
+ }
165
+ default: break;
166
+ }
167
+ return unknown();
168
+ }
169
+
170
+ } // namespace wgsl
171
+ } // namespace xtgui
@@ -0,0 +1,179 @@
1
+ /*
2
+ * xbintsc GUI engine — WGSL tokeniser implementation.
3
+ */
4
+
5
+ #include "wgsl_lexer.h"
6
+
7
+ #include <cctype>
8
+
9
+ namespace xtgui {
10
+ namespace wgsl {
11
+ namespace {
12
+
13
+ bool is_ident_start(char c) {
14
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
15
+ }
16
+
17
+ bool is_ident_char(char c) {
18
+ return is_ident_start(c) || (c >= '0' && c <= '9');
19
+ }
20
+
21
+ bool is_digit(char c) { return c >= '0' && c <= '9'; }
22
+
23
+ void push(std::vector<Token> *out, Tok kind, std::string text, int line, int column) {
24
+ Token token;
25
+ token.kind = kind;
26
+ token.text = std::move(text);
27
+ token.line = line;
28
+ token.column = column;
29
+ out->push_back(std::move(token));
30
+ }
31
+
32
+ } // namespace
33
+
34
+ bool tokenize(const std::string &source, std::vector<Token> *out, std::string *error) {
35
+ out->clear();
36
+ size_t at = 0;
37
+ int line = 1;
38
+ int column = 1;
39
+ auto advance = [&](size_t count) {
40
+ for (size_t i = 0; i < count && at < source.size(); i++) {
41
+ if (source[at] == '\n') {
42
+ line++;
43
+ column = 1;
44
+ } else {
45
+ column++;
46
+ }
47
+ at++;
48
+ }
49
+ };
50
+
51
+ while (at < source.size()) {
52
+ const char c = source[at];
53
+ if (c == '\n' || c == ' ' || c == '\t' || c == '\r' || c == '\f' || c == '\v') {
54
+ advance(1);
55
+ continue;
56
+ }
57
+ /* Comments (WGSL allows nesting in block comments). */
58
+ if (c == '/' && at + 1 < source.size() && source[at + 1] == '/') {
59
+ while (at < source.size() && source[at] != '\n') advance(1);
60
+ continue;
61
+ }
62
+ if (c == '/' && at + 1 < source.size() && source[at + 1] == '*') {
63
+ const int start_line = line;
64
+ const int start_column = column;
65
+ advance(2);
66
+ int depth = 1;
67
+ while (at < source.size() && depth > 0) {
68
+ if (source[at] == '/' && at + 1 < source.size() && source[at + 1] == '*') {
69
+ depth++;
70
+ advance(2);
71
+ } else if (source[at] == '*' && at + 1 < source.size() && source[at + 1] == '/') {
72
+ depth--;
73
+ advance(2);
74
+ } else {
75
+ advance(1);
76
+ }
77
+ }
78
+ if (depth > 0) {
79
+ *error = std::to_string(start_line) + ":" + std::to_string(start_column) +
80
+ ": unterminated block comment";
81
+ return false;
82
+ }
83
+ continue;
84
+ }
85
+ if (is_ident_start(c)) {
86
+ const int start_line = line;
87
+ const int start_column = column;
88
+ size_t end = at;
89
+ while (end < source.size() && is_ident_char(source[end])) end++;
90
+ std::string text = source.substr(at, end - at);
91
+ advance(end - at);
92
+ push(out, Tok::Ident, std::move(text), start_line, start_column);
93
+ continue;
94
+ }
95
+ if (is_digit(c) || (c == '.' && at + 1 < source.size() && is_digit(source[at + 1]))) {
96
+ const int start_line = line;
97
+ const int start_column = column;
98
+ const size_t begin = at;
99
+ bool is_float = false;
100
+ if (c == '0' && at + 1 < source.size() && (source[at + 1] == 'x' || source[at + 1] == 'X')) {
101
+ advance(2);
102
+ while (at < source.size() && (is_digit(source[at]) || (std::tolower((unsigned char)source[at]) >= 'a' &&
103
+ std::tolower((unsigned char)source[at]) <= 'f'))) {
104
+ advance(1);
105
+ }
106
+ while (at < source.size() && (source[at] == 'i' || source[at] == 'u')) advance(1);
107
+ push(out, Tok::Int, source.substr(begin, at - begin), start_line, start_column);
108
+ continue;
109
+ }
110
+ while (at < source.size() && is_digit(source[at])) advance(1);
111
+ if (at < source.size() && source[at] == '.') {
112
+ is_float = true;
113
+ advance(1);
114
+ while (at < source.size() && is_digit(source[at])) advance(1);
115
+ }
116
+ if (at < source.size() && (source[at] == 'e' || source[at] == 'E')) {
117
+ is_float = true;
118
+ advance(1);
119
+ if (at < source.size() && (source[at] == '+' || source[at] == '-')) advance(1);
120
+ while (at < source.size() && is_digit(source[at])) advance(1);
121
+ }
122
+ if (at < source.size() &&
123
+ (source[at] == 'f' || source[at] == 'F' || source[at] == 'h' || source[at] == 'H')) {
124
+ is_float = true;
125
+ advance(1);
126
+ } else if (at < source.size() && (source[at] == 'i' || source[at] == 'u')) {
127
+ advance(1);
128
+ }
129
+ push(out, is_float ? Tok::Float : Tok::Int, source.substr(begin, at - begin), start_line,
130
+ start_column);
131
+ continue;
132
+ }
133
+ if (c == '@') {
134
+ advance(1);
135
+ const int start_line = line;
136
+ const int start_column = column;
137
+ size_t end = at;
138
+ while (end < source.size() && is_ident_char(source[end])) end++;
139
+ if (end == at) {
140
+ *error = std::to_string(start_line) + ":" + std::to_string(start_column - 1) +
141
+ ": expected an attribute name after '@'";
142
+ return false;
143
+ }
144
+ std::string text = "@" + source.substr(at, end - at);
145
+ advance(end - at);
146
+ push(out, Tok::Ident, std::move(text), start_line, start_column);
147
+ continue;
148
+ }
149
+ const int start_line = line;
150
+ const int start_column = column;
151
+ static const char *const kPunct[] = {
152
+ "<<=", ">>=", "->", "==", "!=", "<=", ">=", "&&", "||", "<<", ">>",
153
+ "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "++", "--"};
154
+ bool matched = false;
155
+ for (const char *candidate : kPunct) {
156
+ const std::string text(candidate);
157
+ if (source.compare(at, text.size(), text) == 0) {
158
+ push(out, Tok::Punct, text, start_line, start_column);
159
+ advance(text.size());
160
+ matched = true;
161
+ break;
162
+ }
163
+ }
164
+ if (matched) continue;
165
+ if (std::string("(){}[]<>=+-*/%&|^!~;:,.?").find(c) != std::string::npos) {
166
+ push(out, Tok::Punct, std::string(1, c), start_line, start_column);
167
+ advance(1);
168
+ continue;
169
+ }
170
+ *error = std::to_string(line) + ":" + std::to_string(column) + ": unexpected character '" +
171
+ std::string(1, c) + "'";
172
+ return false;
173
+ }
174
+ push(out, Tok::End, std::string(), line, column);
175
+ return true;
176
+ }
177
+
178
+ } // namespace wgsl
179
+ } // namespace xtgui
@@ -0,0 +1,32 @@
1
+ /*
2
+ * xbintsc GUI engine — WGSL tokeniser.
3
+ *
4
+ * WGSL's lexical grammar is close to C's. `>>` is kept as one token (it is a
5
+ * shift operator); the type parser splits it back into two `>` when it closes
6
+ * nested generic arguments such as `array<vec4<f32>, 4>`.
7
+ */
8
+ #ifndef XT_GUI_WGSL_LEXER_H
9
+ #define XT_GUI_WGSL_LEXER_H
10
+
11
+ #include <string>
12
+ #include <vector>
13
+
14
+ namespace xtgui {
15
+ namespace wgsl {
16
+
17
+ enum class Tok { End, Ident, Int, Float, Punct };
18
+
19
+ struct Token {
20
+ Tok kind = Tok::End;
21
+ std::string text;
22
+ int line = 1;
23
+ int column = 1;
24
+ };
25
+
26
+ /** Tokenise `source`; on an error returns false and fills `error`. */
27
+ bool tokenize(const std::string &source, std::vector<Token> *out, std::string *error);
28
+
29
+ } // namespace wgsl
30
+ } // namespace xtgui
31
+
32
+ #endif /* XT_GUI_WGSL_LEXER_H */