Skip to content

Commit 25e972b

Browse files
committed
Add OFFSETOF keyword for BASIC
1 parent 75faa43 commit 25e972b

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

Changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Version 7.6.8
2+
- Add OFFSETOF to BASIC
3+
14
Version 7.6.7
25
- Add OFFSETOF implementation
36
- Fix a Windows crash

doc/basic.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ next
301301
new
302302
nil
303303
not
304+
offsetof
304305
open
305306
option
306307
or
@@ -2900,6 +2901,24 @@ Convert `x` into a string with `d` digits in base `base`. If `x` is too big to f
29002901
```
29012902
Returns a string representing the unsigned integer `x` in base 8. Only the lowest `n` digits of the representation are included. If `n` is omitted or is 0 then the returned string is the minimum length needed to represent the unsigned value.
29022903

2904+
### OFFSETOF
2905+
2906+
```
2907+
n = offsetof(typename, fieldname)
2908+
```
2909+
Returns the byte offset of the field `fieldname` within the class, struct, or union type `typename`. `typename` must name a type (not a variable), and `fieldname` must be a plain identifier naming a member of that type. The result is a compile-time integer constant suitable for use anywhere an integer is expected.
2910+
2911+
For example:
2912+
```
2913+
class point
2914+
dim as integer x
2915+
dim as integer y
2916+
end class
2917+
2918+
print offsetof(point, y)
2919+
```
2920+
will print 4.
2921+
29032922
### ON X GOTO
29042923

29052924
For compatibility only, FlexBASIC accepts statements like:

frontends/basic/basic.y

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ AdjustParamForByVal(AST *param)
476476
%token BAS_NEXT "next"
477477
%token BAS_NIL "nil"
478478
%token BAS_NOT "not"
479+
%token BAS_OFFSETOF "offsetof"
479480
%token BAS_ON "on"
480481
%token BAS_OPEN "open"
481482
%token BAS_OPTION "option"
@@ -1681,6 +1682,10 @@ pseudofunc_expr:
16811682
AST *ident = $5;
16821683
$$ = NewAST(AST_HASMETHOD, typnam, ident);
16831684
}
1685+
| BAS_OFFSETOF '(' typename ',' BAS_IDENTIFIER ')'
1686+
{
1687+
$$ = NewAST(AST_OFFSETOF, $3, $5);
1688+
}
16841689
| BAS_SAMETYPES '(' typename ',' typename ')'
16851690
{
16861691
AST *expr = $3;

frontends/lexer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,7 @@ struct reservedword basic_keywords[] = {
26652665
{ "next", BAS_NEXT },
26662666
{ "not", BAS_NOT },
26672667
{ "nil", BAS_NIL },
2668+
{ "offsetof", BAS_OFFSETOF },
26682669
{ "on", BAS_ON },
26692670
{ "open", BAS_OPEN },
26702671
{ "option", BAS_OPTION },

0 commit comments

Comments
 (0)