From 5be9f122e93bd0b2e1e0d4196b7d6a16cf362229 Mon Sep 17 00:00:00 2001 From: nub31 Date: Mon, 20 Oct 2025 18:32:27 +0200 Subject: [PATCH] ... --- bindings/generate.py | 35 +++- compiler/NubLang/Ast/TypeChecker.cs | 2 + examples/raylib/generated/raylib.nub | 279 +------------------------- examples/raylib/generated/raymath.nub | 45 ----- examples/raylib/generated/rlgl.nub | 46 ----- 5 files changed, 28 insertions(+), 379 deletions(-) diff --git a/bindings/generate.py b/bindings/generate.py index f28a94b..4e2868b 100755 --- a/bindings/generate.py +++ b/bindings/generate.py @@ -19,6 +19,11 @@ def map_type(clang_type: Type): if kind == TypeKind.POINTER: pointee = canonical.get_pointee() + if pointee.kind == TypeKind.RECORD: + decl = pointee.get_declaration() + if not decl.is_definition(): + return "^void" + if pointee.kind == TypeKind.CHAR_S or pointee.kind == TypeKind.CHAR_U: return "cstring" @@ -103,6 +108,8 @@ if tu.diagnostics: print(f'module "{os.path.basename(filename).split(".")[0]}"') print() +seen_structs = [] + for cursor in tu.cursor.walk_preorder(): if cursor.location.file and cursor.location.file.name != filename: continue @@ -122,14 +129,20 @@ for cursor in tu.cursor.walk_preorder(): print(f'export extern "{name}" func {name}({params_str}): {return_type}') elif cursor.kind == CursorKind.STRUCT_DECL: - name = cursor.spelling - print(f"export struct {name}") - print("{") - for field in cursor.get_children(): - if field.kind == CursorKind.FIELD_DECL: - field_name = field.spelling - field_type = map_type(field.type) - print(f" {field_name}: {field_type}") - else: - raise Exception(f"Unsupported child of struct: {field.spelling}") - print("}") + if cursor.get_usr() in seen_structs: + continue + + seen_structs.append(cursor.get_usr()) + + if cursor.is_definition(): + name = cursor.spelling + print(f"export struct {name}") + print("{") + for field in cursor.get_children(): + if field.kind == CursorKind.FIELD_DECL: + field_name = field.spelling + field_type = map_type(field.type) + print(f" {field_name}: {field_type}") + else: + raise Exception(f"Unsupported child of struct: {field.spelling}") + print("}") diff --git a/compiler/NubLang/Ast/TypeChecker.cs b/compiler/NubLang/Ast/TypeChecker.cs index 62e236e..23dcfea 100644 --- a/compiler/NubLang/Ast/TypeChecker.cs +++ b/compiler/NubLang/Ast/TypeChecker.cs @@ -727,6 +727,8 @@ public sealed class TypeChecker .Error($"Struct {structType.Name} does not have a field named {initializer.Key}") .At(initializer.Value) .Build()); + + continue; } initializers.Add(initializer.Key, CheckExpression(initializer.Value, typeField.Type)); diff --git a/examples/raylib/generated/raylib.nub b/examples/raylib/generated/raylib.nub index ad8908c..d043370 100644 --- a/examples/raylib/generated/raylib.nub +++ b/examples/raylib/generated/raylib.nub @@ -1,10 +1,5 @@ module "raylib" -export struct Vector2 -{ - x: f32 - y: f32 -} export struct Vector2 { x: f32 @@ -16,19 +11,6 @@ export struct Vector3 y: f32 z: f32 } -export struct Vector3 -{ - x: f32 - y: f32 - z: f32 -} -export struct Vector4 -{ - x: f32 - y: f32 - z: f32 - w: f32 -} export struct Vector4 { x: f32 @@ -55,32 +37,6 @@ export struct Matrix m11: f32 m15: f32 } -export struct Matrix -{ - m0: f32 - m4: f32 - m8: f32 - m12: f32 - m1: f32 - m5: f32 - m9: f32 - m13: f32 - m2: f32 - m6: f32 - m10: f32 - m14: f32 - m3: f32 - m7: f32 - m11: f32 - m15: f32 -} -export struct Color -{ - r: u8 - g: u8 - b: u8 - a: u8 -} export struct Color { r: u8 @@ -95,13 +51,6 @@ export struct Rectangle width: f32 height: f32 } -export struct Rectangle -{ - x: f32 - y: f32 - width: f32 - height: f32 -} export struct Image { data: ^void @@ -110,22 +59,6 @@ export struct Image mipmaps: i32 format: i32 } -export struct Image -{ - data: ^void - width: i32 - height: i32 - mipmaps: i32 - format: i32 -} -export struct Texture -{ - id: u32 - width: i32 - height: i32 - mipmaps: i32 - format: i32 -} export struct Texture { id: u32 @@ -140,12 +73,6 @@ export struct RenderTexture texture: Texture depth: Texture } -export struct RenderTexture -{ - id: u32 - texture: Texture - depth: Texture -} export struct NPatchInfo { source: Rectangle @@ -155,23 +82,6 @@ export struct NPatchInfo bottom: i32 layout: i32 } -export struct NPatchInfo -{ - source: Rectangle - left: i32 - top: i32 - right: i32 - bottom: i32 - layout: i32 -} -export struct GlyphInfo -{ - value: i32 - offsetX: i32 - offsetY: i32 - advanceX: i32 - image: Image -} export struct GlyphInfo { value: i32 @@ -189,15 +99,6 @@ export struct Font recs: ^Rectangle glyphs: ^GlyphInfo } -export struct Font -{ - baseSize: i32 - glyphCount: i32 - glyphPadding: i32 - texture: Texture - recs: ^Rectangle - glyphs: ^GlyphInfo -} export struct Camera3D { position: Vector3 @@ -206,21 +107,6 @@ export struct Camera3D fovy: f32 projection: i32 } -export struct Camera3D -{ - position: Vector3 - target: Vector3 - up: Vector3 - fovy: f32 - projection: i32 -} -export struct Camera2D -{ - offset: Vector2 - target: Vector2 - rotation: f32 - zoom: f32 -} export struct Camera2D { offset: Vector2 @@ -248,42 +134,11 @@ export struct Mesh vaoId: u32 vboId: ^u32 } -export struct Mesh -{ - vertexCount: i32 - triangleCount: i32 - vertices: ^f32 - texcoords: ^f32 - texcoords2: ^f32 - normals: ^f32 - tangents: ^f32 - colors: ^u8 - indices: ^u16 - animVertices: ^f32 - animNormals: ^f32 - boneIds: ^u8 - boneWeights: ^f32 - boneMatrices: ^Matrix - boneCount: i32 - vaoId: u32 - vboId: ^u32 -} export struct Shader { id: u32 locs: ^i32 } -export struct Shader -{ - id: u32 - locs: ^i32 -} -export struct MaterialMap -{ - texture: Texture - color: Color - value: f32 -} export struct MaterialMap { texture: Texture @@ -296,18 +151,6 @@ export struct Material maps: ^MaterialMap params: [4]f32 } -export struct Material -{ - shader: Shader - maps: ^MaterialMap - params: [4]f32 -} -export struct Transform -{ - translation: Vector3 - rotation: Vector4 - scale: Vector3 -} export struct Transform { translation: Vector3 @@ -319,23 +162,6 @@ export struct BoneInfo name: [32]i8 parent: i32 } -export struct BoneInfo -{ - name: [32]i8 - parent: i32 -} -export struct Model -{ - transform: Matrix - meshCount: i32 - materialCount: i32 - meshes: ^Mesh - materials: ^Material - meshMaterial: ^i32 - boneCount: i32 - bones: ^BoneInfo - bindPose: ^Transform -} export struct Model { transform: Matrix @@ -356,19 +182,6 @@ export struct ModelAnimation framePoses: ^^Transform name: [32]i8 } -export struct ModelAnimation -{ - boneCount: i32 - frameCount: i32 - bones: ^BoneInfo - framePoses: ^^Transform - name: [32]i8 -} -export struct Ray -{ - position: Vector3 - direction: Vector3 -} export struct Ray { position: Vector3 @@ -381,18 +194,6 @@ export struct RayCollision point: Vector3 normal: Vector3 } -export struct RayCollision -{ - hit: bool - distance: f32 - point: Vector3 - normal: Vector3 -} -export struct BoundingBox -{ - min: Vector3 - max: Vector3 -} export struct BoundingBox { min: Vector3 @@ -406,41 +207,14 @@ export struct Wave channels: u32 data: ^void } -export struct Wave -{ - frameCount: u32 - sampleRate: u32 - sampleSize: u32 - channels: u32 - data: ^void -} -export struct rAudioBuffer -{ -} -export struct rAudioProcessor -{ -} export struct AudioStream { - buffer: ^rAudioBuffer - processor: ^rAudioProcessor + buffer: ^void + processor: ^void sampleRate: u32 sampleSize: u32 channels: u32 } -export struct AudioStream -{ - buffer: ^rAudioBuffer - processor: ^rAudioProcessor - sampleRate: u32 - sampleSize: u32 - channels: u32 -} -export struct Sound -{ - stream: AudioStream - frameCount: u32 -} export struct Sound { stream: AudioStream @@ -454,26 +228,6 @@ export struct Music ctxType: i32 ctxData: ^void } -export struct Music -{ - stream: AudioStream - frameCount: u32 - looping: bool - ctxType: i32 - ctxData: ^void -} -export struct VrDeviceInfo -{ - hResolution: i32 - vResolution: i32 - hScreenSize: f32 - vScreenSize: f32 - eyeToScreenDistance: f32 - lensSeparationDistance: f32 - interpupillaryDistance: f32 - lensDistortionValues: [4]f32 - chromaAbCorrection: [4]f32 -} export struct VrDeviceInfo { hResolution: i32 @@ -497,23 +251,6 @@ export struct VrStereoConfig scale: [2]f32 scaleIn: [2]f32 } -export struct VrStereoConfig -{ - projection: [2]Matrix - viewOffset: [2]Matrix - leftLensCenter: [2]f32 - rightLensCenter: [2]f32 - leftScreenCenter: [2]f32 - rightScreenCenter: [2]f32 - scale: [2]f32 - scaleIn: [2]f32 -} -export struct FilePathList -{ - capacity: u32 - count: u32 - paths: ^cstring -} export struct FilePathList { capacity: u32 @@ -526,18 +263,6 @@ export struct AutomationEvent type: u32 params: [4]i32 } -export struct AutomationEvent -{ - frame: u32 - type: u32 - params: [4]i32 -} -export struct AutomationEventList -{ - capacity: u32 - count: u32 - events: ^AutomationEvent -} export struct AutomationEventList { capacity: u32 diff --git a/examples/raylib/generated/raymath.nub b/examples/raylib/generated/raymath.nub index c094a74..6dbce8a 100644 --- a/examples/raylib/generated/raymath.nub +++ b/examples/raylib/generated/raymath.nub @@ -1,10 +1,5 @@ module "raymath" -export struct Vector2 -{ - x: f32 - y: f32 -} export struct Vector2 { x: f32 @@ -16,19 +11,6 @@ export struct Vector3 y: f32 z: f32 } -export struct Vector3 -{ - x: f32 - y: f32 - z: f32 -} -export struct Vector4 -{ - x: f32 - y: f32 - z: f32 - w: f32 -} export struct Vector4 { x: f32 @@ -55,37 +37,10 @@ export struct Matrix m11: f32 m15: f32 } -export struct Matrix -{ - m0: f32 - m4: f32 - m8: f32 - m12: f32 - m1: f32 - m5: f32 - m9: f32 - m13: f32 - m2: f32 - m6: f32 - m10: f32 - m14: f32 - m3: f32 - m7: f32 - m11: f32 - m15: f32 -} export struct float3 { v: [3]f32 } -export struct float3 -{ - v: [3]f32 -} -export struct float16 -{ - v: [16]f32 -} export struct float16 { v: [16]f32 diff --git a/examples/raylib/generated/rlgl.nub b/examples/raylib/generated/rlgl.nub index 3a26aa2..5a0beae 100644 --- a/examples/raylib/generated/rlgl.nub +++ b/examples/raylib/generated/rlgl.nub @@ -19,36 +19,6 @@ export struct Matrix m11: f32 m15: f32 } -export struct Matrix -{ - m0: f32 - m4: f32 - m8: f32 - m12: f32 - m1: f32 - m5: f32 - m9: f32 - m13: f32 - m2: f32 - m6: f32 - m10: f32 - m14: f32 - m3: f32 - m7: f32 - m11: f32 - m15: f32 -} -export struct rlVertexBuffer -{ - elementCount: i32 - vertices: ^f32 - texcoords: ^f32 - normals: ^f32 - colors: ^u8 - indices: ^u32 - vaoId: u32 - vboId: [5]u32 -} export struct rlVertexBuffer { elementCount: i32 @@ -67,22 +37,6 @@ export struct rlDrawCall vertexAlignment: i32 textureId: u32 } -export struct rlDrawCall -{ - mode: i32 - vertexCount: i32 - vertexAlignment: i32 - textureId: u32 -} -export struct rlRenderBatch -{ - bufferCount: i32 - currentBuffer: i32 - vertexBuffer: ^rlVertexBuffer - draws: ^rlDrawCall - drawCounter: i32 - currentDepth: f32 -} export struct rlRenderBatch { bufferCount: i32