Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libs/ast/expr.b
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion libs/ast/parser.b
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
48 changes: 36 additions & 12 deletions packages/clib/clib/types.b
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ 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.
* 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
Expand All @@ -215,16 +215,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
Expand Down
2 changes: 1 addition & 1 deletion scripts/ast.b
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down