From 0603cfa86dc82f5c77f3d2bcfafbe385a66dda52 Mon Sep 17 00:00:00 2001 From: benstigsen Date: Mon, 2 Jun 2025 00:05:39 +0200 Subject: [PATCH 1/3] Add clib.named_struct() documentation example --- packages/clib/clib/types.b | 50 +++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/packages/clib/clib/types.b b/packages/clib/clib/types.b index ad806863..fd7dfed0 100644 --- a/packages/clib/clib/types.b +++ b/packages/clib/clib/types.b @@ -191,12 +191,14 @@ var function = _clib.closure /** - * Returns a type that can be used to declare structs. - * To create or read value for the struct you need to use the `new()` + * Returns a type that can be used to declare structs. + * To create or read value for the struct you need to use the `new()` + * Returns a type that can be used to declare structs. + * To create or read value for the struct you need to use the `new()` * and `get()` functions respectively. - * Alternatively, you may use the `pack()` and `unpack()` + * Alternatively, you may use the `pack()` and `unpack()` * function in the `struct` module respectively. - * + * * @note This function can also be used to define a C union or array. * @param any... type * @returns type @@ -215,16 +217,40 @@ def struct(...) { } /** - * Returns a type that can be used to declare structs based on the named - * types. The function works well with the `get()` function because it - * automatically assigns the name of the struct elements when getting the - * value. - * - * To create or read value for the struct you need to use the `new()` + * Returns a struct type with named fields. The function works well with the `get()` + * function because it automatically assigns the name of the struct elements when + * getting the value. + * + * To create or read value for the struct you need to use the `new()` * and `get()` functions respectively. - * Alternatively, you may use the `pack()` and `unpack()` + * Alternatively, you may use the `pack()` and `unpack()` * function in the `struct` module respectively. - * + * + * For example, let's say you have the following C struct: + * ```c + * typedef struct { + * char* message; + * int status; + * } custom_error; + * ``` + * + * This is how you'd create a named struct for it: + * ```blade + * import clib + * + * var lib = clib.load('./custom-library.so') + * + * var custom_error = clib.named_struct({ + * 'message': clib.char_ptr, + * 'status': clib.int + * }) + * + * var myfunction = lib.define('custom_error_function', custom_error) + * echo myfunction() # {message: oh no!, status: 1} + * + * lib.close() + * ``` + * * @note This function can also be used to define a C union or array. * @param dictionary types * @returns type From 48f1f4c9ec1402705790c8467daa44babd7d13bd Mon Sep 17 00:00:00 2001 From: benstigsen Date: Tue, 3 Jun 2025 20:30:01 +0200 Subject: [PATCH 2/3] Add lhs AST index expressions --- libs/ast/expr.b | 5 ++++- libs/ast/parser.b | 2 +- scripts/ast.b | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/libs/ast/expr.b b/libs/ast/expr.b index f96dd3f1..ad090fce 100644 --- a/libs/ast/expr.b +++ b/libs/ast/expr.b @@ -247,16 +247,19 @@ class SetExpr < Expr { class IndexExpr < Expr { /** + * @param {Expr|any|nil} expr * @param {Expr|any|nil} args * @constructor */ - IndexExpr(args) { + IndexExpr(expr, args) { + self.expr = expr self.args = args } @to_json() { return { type: 'IndexExpr', + expr: self.expr, args: self.args, } } diff --git a/libs/ast/parser.b b/libs/ast/parser.b index 9295dbff..4fbe026d 100644 --- a/libs/ast/parser.b +++ b/libs/ast/parser.b @@ -238,7 +238,7 @@ class Parser { self._ignore_newline() self._consume(TokenType.RBRACKET, "']' expected at end of indexer") - return IndexExpr(args) + return IndexExpr(callee, args) } /** diff --git a/scripts/ast.b b/scripts/ast.b index 3d73688c..a7c390c4 100644 --- a/scripts/ast.b +++ b/scripts/ast.b @@ -30,7 +30,7 @@ var asts = { Call: ['callee', 'args'], Get: ['expr', 'name'], Set: ['expr', 'name', 'value'], - Index: ['args'], + Index: ['expr', 'args'], List: ['items'], Dict: ['keys', 'values'], Interpolation: ['data'] From 0173b94e34e6764c13d9f016993a63436be7116a Mon Sep 17 00:00:00 2001 From: benstigsen Date: Tue, 3 Jun 2025 20:52:37 +0200 Subject: [PATCH 3/3] Remove duplicate documentation comment --- packages/clib/clib/types.b | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/clib/clib/types.b b/packages/clib/clib/types.b index fd7dfed0..83c928c9 100644 --- a/packages/clib/clib/types.b +++ b/packages/clib/clib/types.b @@ -191,8 +191,6 @@ var function = _clib.closure /** - * Returns a type that can be used to declare structs. - * To create or read value for the struct you need to use the `new()` * Returns a type that can be used to declare structs. * To create or read value for the struct you need to use the `new()` * and `get()` functions respectively.