Skip to content

Commit 730726a

Browse files
committed
Accept FreeBasic style TYPE definitions for BASIC classes
1 parent 6d539d0 commit 730726a

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Version 7.6.8
22
- Add OFFSETOF to BASIC
3+
- Accept FreeBasic style TYPE definitions for BASIC classes
34

45
Version 7.6.7
56
- Add OFFSETOF implementation

Test/basexec04.bas

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,24 @@ interface floatable
8686
implements function asFloat() as single
8787
end interface
8888

89-
class wrapFloat
89+
type wrapFloat
9090
dim f as single
9191
function asFloat() as single
9292
return f
9393
end function
9494
sub set(a as single)
9595
f = a
9696
end sub
97-
end class
98-
class wrapInt
97+
end type
98+
type wrapInt
9999
dim f as integer
100100
sub set(a as integer)
101101
f = a
102102
end sub
103103
function asFloat() as single
104104
return f
105105
end function
106-
end class
106+
end type
107107

108108
sub testit(x as floatable)
109109
print x.asFloat() mod 2.1

doc/basic.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,10 @@ Closes a subroutine definition.
20702070

20712071
Closes a `try`/`catch` error handling block.
20722072

2073+
#### END TYPE
2074+
2075+
Closes any kind of type definition (`class`, `type`, or `union`).
2076+
20732077
#### END WHILE
20742078

20752079
Marks the end of a `while` loop; this may be used in place of `wend`.
@@ -3642,6 +3646,13 @@ Creates an alias for a type. For example,
36423646
```
36433647
creates a new type name `uptr` which is a pointer to a `ubyte`. You may use the new type name anywhere a type is required.
36443648

3649+
The `type` keyword may also be used instead of `class` for defining a class. For example:
3650+
```
3651+
type mypoint
3652+
dim as integer x, y
3653+
end type
3654+
```
3655+
36453656
### UBYTE
36463657

36473658
An unsigned 8 bit integer, occupying one byte of computer memory. The signed version of this is `byte`. The difference arises with the treatment of the upper bit. Both `byte` and `ubyte` treat 0-127 the same, but for `byte` 128 to 255 are considered equivalent to -128 to -1 respectively (that is, when a `byte` is copied to a larger sized integer the upper bit is repeated into all the other bits; for `ubyte` the new bytes are filled with 0 instead).

frontends/basic/basic.y

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,6 +2207,25 @@ classheader:
22072207
current = P;
22082208
$$ = NULL;
22092209
}
2210+
| BAS_TYPE BAS_IDENTIFIER eoln
2211+
{
2212+
AST *ident = $2;
2213+
const char *classname = ident->d.string;
2214+
Module *P = NewModule(classname, current->curLanguage);
2215+
AST *newobj = NewAbstractObject( ident, NULL, 0 );
2216+
newobj->d.ptr = P;
2217+
AddSymbol(currentTypes, classname, SYM_TYPEDEF, newobj, NULL);
2218+
if (P != current) {
2219+
P->Lptr = current->Lptr;
2220+
P->subclasses = current->subclasses;
2221+
current->subclasses = P;
2222+
P->superclass = current;
2223+
P->fullname = current->fullname; // for finding "class using"
2224+
}
2225+
PushCurrentModule();
2226+
current = P;
2227+
$$ = NULL;
2228+
}
22102229
| BAS_UNION BAS_IDENTIFIER eoln
22112230
{
22122231
AST *ident = $2;
@@ -2279,6 +2298,10 @@ classend:
22792298
PopCurrentModule();
22802299
}
22812300
}
2301+
| BAS_END BAS_TYPE
2302+
{
2303+
PopCurrentModule();
2304+
}
22822305
;
22832306

22842307
classdeclitem:

0 commit comments

Comments
 (0)