Skip to content

Commit b29f47a

Browse files
committed
Implemented distinct BOOLEAN type for BASIC
1 parent 231e60c commit b29f47a

File tree

7 files changed

+55
-12
lines changed

7 files changed

+55
-12
lines changed

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Version 6.6.0
22
- Implemented proper boolean type for C
33
- Fixed -- operator for floats
4+
- Made the BASIC boolean type a distinct type
45

56
Version 6.5.4
67
- Minor optimization improvement (and major documentation improvements) courtesy of Ada

Test/Expect/basexec03.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ closed handle
1717
0.8000
1818
0.9000
1919
1.0000
20+
0 : a, b = 0 -1
21+
1 : a, b = -1 0
22+
2 : a, b = -1 -1

Test/basexec03.bas

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ for i# = 0.3 to 1.0001 step 0.1
4545
print i#
4646
next i#
4747

48+
const two = 2
49+
sub trybool(x as single)
50+
var a = two <> 2
51+
dim b as boolean
52+
53+
b = x
54+
print "0 : a, b = ", a, b
55+
a -= 2
56+
b += 1
57+
print "1 : a, b = ", a, b
58+
a = two
59+
b += 1
60+
print "2 : a, b = ", a, b
61+
62+
end sub
63+
64+
trybool(1.0)
65+
66+
4867
''
4968
'' send the magic propload status code
5069
''

doc/basic.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ Returns the bits of the 32 bit unsigned integer `y` in reverse order. For exampl
13561356

13571357
### BOOLEAN
13581358

1359-
Represents a true or false result. Represented as a 32 bit integer, with TRUE set to all 1's and FALSE set to all 0's. In practice any non-zero value will be accepted as being "true".
1359+
Represents a true or false result. Represented as a 32 bit integer, with TRUE set to all 1's and FALSE set to all 0's. The compiler tries to normalize boolean values to be either TRUE or FALSE, but this may not succeed in all cases. In such cases any non-zero value will be accepted as being "true".
13601360

13611361
### __BUILTIN_ALLOCA
13621362

expr.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,7 +2856,8 @@ AST *
28562856
ExprTypeRelative(SymbolTable *table, AST *expr, Module *P)
28572857
{
28582858
AST *sub;
2859-
2859+
int lang = GetCurrentLang();
2860+
28602861
if (!expr) return NULL;
28612862
if (!P) {
28622863
P = current;
@@ -2865,7 +2866,7 @@ ExprTypeRelative(SymbolTable *table, AST *expr, Module *P)
28652866
case AST_TYPEOF:
28662867
return ExprTypeRelative(table, expr->left, P);
28672868
case AST_INTEGER:
2868-
if ( IsCLang(P->curLanguage) && expr->d.ival == 0) {
2869+
if ( IsCLang(lang) && expr->d.ival == 0) {
28692870
return ast_type_generic;
28702871
}
28712872
if (expr->left) { /* explicit cast */
@@ -2904,12 +2905,12 @@ ExprTypeRelative(SymbolTable *table, AST *expr, Module *P)
29042905
// in Spin, a string is always dereferenced
29052906
// so "abc" is the same as "a" is the same as 0x65
29062907
// (actually no -- "abc" is the same as "a", "b", "c")
2907-
if (curfunc && IsSpinLang(curfunc->language)) {
2908+
if (IsSpinLang(lang)) {
29082909
return ast_type_long;
29092910
}
29102911
/* otherwise fall through */
29112912
case AST_STRINGPTR:
2912-
if ( curfunc && curfunc->language == LANG_CFAMILY_C && !(curfunc->warn_flags & WARN_C_CONST_STRING) ) {
2913+
if ( lang == LANG_CFAMILY_C && curfunc && !(curfunc->warn_flags & WARN_C_CONST_STRING) ) {
29132914
return ast_type_ptr_byte;
29142915
}
29152916
return ast_type_string;
@@ -2952,7 +2953,7 @@ ExprTypeRelative(SymbolTable *table, AST *expr, Module *P)
29522953
case SYM_LABEL:
29532954
lab = (Label *)sym->v.ptr;
29542955
typ = lab->type;
2955-
if (curfunc && IsSpinLang(curfunc->language) && typ && typ->kind != AST_ARRAYTYPE) {
2956+
if (IsSpinLang(lang) && typ && typ->kind != AST_ARRAYTYPE) {
29562957
if (typ == ast_type_void) {
29572958
return typ;
29582959
}
@@ -3028,11 +3029,11 @@ ExprTypeRelative(SymbolTable *table, AST *expr, Module *P)
30283029
if (typexpr && typexpr->kind == AST_FUNCTYPE) {
30293030
return typexpr->left;
30303031
}
3031-
if ( (IsArrayType(old_typexpr) || IsPointerType(old_typexpr)) && curfunc && IsBasicLang(curfunc->language)) {
3032+
if ( (IsArrayType(old_typexpr) || IsPointerType(old_typexpr)) && IsBasicLang(lang)) {
30323033
// in BASIC we may not have converted x(i) into an array reference yet
30333034
return BaseType(old_typexpr);
30343035
}
3035-
if (curfunc && IsSpinLang(curfunc->language)) {
3036+
if (IsSpinLang(lang)) {
30363037
if (old_typexpr == NULL) {
30373038
return NULL;
30383039
}
@@ -3144,7 +3145,7 @@ ExprTypeRelative(SymbolTable *table, AST *expr, Module *P)
31443145
return ltype;
31453146
}
31463147
}
3147-
if (curfunc && IsSpinLang(curfunc->language)) {
3148+
if (IsSpinLang(lang)) {
31483149
if (ltype) {
31493150
//if (IsIntOrGenericType(ltype)) return ltype;
31503151
if (IsPointerType(ltype)) {
@@ -3193,7 +3194,7 @@ ExprTypeRelative(SymbolTable *table, AST *expr, Module *P)
31933194
case '&':
31943195
ltype = ExprTypeRelative(table, expr->left, P);
31953196
rtype = ExprTypeRelative(table, expr->right, P);
3196-
if (IsBasicLang(GetCurrentLang())) {
3197+
if (IsBasicLang(lang)) {
31973198
if (IsStringType(ltype)) {
31983199
return ltype;
31993200
} else if (IsStringType(rtype)) {
@@ -3206,7 +3207,25 @@ ExprTypeRelative(SymbolTable *table, AST *expr, Module *P)
32063207
if ( TypeSize(rtype) > LONG_SIZE ) {
32073208
return ast_type_long64;
32083209
}
3209-
return ast_type_long;
3210+
return ast_type_long;
3211+
case '<':
3212+
case '>':
3213+
case K_EQ:
3214+
case K_NE:
3215+
case K_GE:
3216+
case K_LE:
3217+
case K_GEU:
3218+
case K_LEU:
3219+
case K_GTU:
3220+
case K_LTU:
3221+
case K_BOOL_AND:
3222+
case K_BOOL_OR:
3223+
case K_BOOL_NOT:
3224+
{
3225+
if (IsBasicLang(lang)) {
3226+
return ast_type_basic_boolean;
3227+
}
3228+
}
32103229
default:
32113230
ltype = ExprTypeRelative(table, expr->left, P);
32123231
rtype = ExprTypeRelative(table, expr->right, P);

frontends/basic/basic.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2415,7 +2415,7 @@ basetypename:
24152415
| BAS_INTEGER_KW
24162416
{ $$ = ast_type_long; }
24172417
| BAS_BOOLEAN
2418-
{ $$ = ast_type_long; }
2418+
{ $$ = ast_type_basic_boolean; }
24192419
| BAS_UINTEGER
24202420
{ $$ = ast_type_unsigned_long; }
24212421
| BAS_SINGLE

testlex.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ AST *ast_type_ptr_byte;
3232
AST *ast_type_unsigned_long;
3333
AST *ast_type_void;
3434
AST *ast_type_long64, *ast_type_unsigned_long64, *ast_type_float64;
35+
AST *ast_type_c_boolean, *ast_type_basic_boolean;
3536

3637
int gl_p2 = 0;
3738
int gl_errors = 0;

0 commit comments

Comments
 (0)