Oxygen Basic

Programming => Example Code => General => Topic started by: Charles Pegge on January 24, 2014, 03:54:15 AM

Title: Boolean Statements
Post by: Charles Pegge on January 24, 2014, 03:54:15 AM
These are useful for collecting state information for later action, or for breaking down complex logic into simpler expressions.

Code: [Select]
'BOOLEAN STATEMENTS
sys a
a = (1.0+1.0==2.0) '-1
a = (1.0+1.0>=2.0) '-1
print a
a = (1.0+1.0>2.0)  '0
a = (1.0+1.0<>2.0) '0
print a
a = ("Hello"=="Hello") '-1
a = ("Hello"=="hello") '0
a = ("Hello"<>"hello") '-1
print a
Title: Re: Boolean Statements
Post by: Peter on January 24, 2014, 04:59:59 AM
very simple, but confusing for Aurel!  :D
Title: Re: Boolean Statements
Post by: Peter on January 24, 2014, 05:36:21 AM
Code: [Select]
sys a
iF a=1.0+1.0 = 2.0
   print "TRUE"
End iF   

iF a=1.0+1.0 >= 2.0
   print "TRUE"
End iF   

iF (a=2.0) > 2.0 
   print "FALSE"
End iF

iF (a=2.0) <> 2.0 
   print "FALSE"
End iF

iF (a=2.0) < 2.0  'does not work 
   print "FALSE"
End iF

Print "okay"
Title: Re: Boolean Statements
Post by: Charles Pegge on January 24, 2014, 07:39:49 AM
They all work as expected here. Only the third case bypasses:

iF (a=2.0) > 2.0 
   print "FALSE 3"
End iF
Title: Re: Boolean Statements
Post by: Frankolinox on January 28, 2014, 01:59:09 AM
short question, I've tested the interesting "boolean" example, the last "false" result (third one) from peter isn't correct as result, but why? do you have fixed the problem charles in new oxygen dll or I am confused too? (must grinning) ;)

servus, frank
Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 28, 2014, 11:09:33 PM
Gentlemen,

I don't quite get what all of you are talking about here. Peter's test script should work completely different if Oxygen's parentheses () are supposed to fulfill their intended purpose.

For example, the following conditional

if ((a = 2.0) < 2.0) // ML: C syntax proper

or its equivalent Oxygen conditional

if (a = 2.0) < 2.0 ' ML: O2 syntax assumed hereinbelow for simplicity

would be analyzed in the C language as follows.

The () operator has the highest scope precedence in a statement. The sub-expression within the innermost parentheses (luckily there's only one nesting level in our case) is evaluated or executed the first. Here = is an assignment that makes a equal to 2.0 and this assignment is the very first thing the language does when evaluating the entire expression.

Further, C's parentheses also return their own temporary value which is equal to a result of the sub-expression that they immediately embrace. The result may be either arithmetical (as in our case above) or stringified (for example, a string produced as a result of concatenating several strings into one). Our sub-expression evaluates to 2.0, math-wise. This temporary result is valid for the purposes of boolean evaluation in the given if-block.

Whereby the initial expression may be reduced to simple

if 2.0 < 2.0 ' ML: ditto about syntax

which obviously evaluates to false. That is exactly why we shouldn't see the entire if-block print anything at all apart from doing at least one more useful thing that the programmer evidently wanted it to do, which was to assign 2.0 to the variable a.

From this standpoint, the following code:

Code: [Select]
sys a
iF a=1.0+1.0 = 2.0
   print "1.0+1.0 = 2.0 (TRUE ==> should print)"
End iF   

iF a=1.0+1.0 >= 2.0
   print "1.0+1.0 >= 2.0 (TRUE ==> should print)"
End iF   

iF (a=2.0) > 2.0 
   print "2.0 > 2.0 (FALSE ==> should NOT print at all!)"
End iF

iF (a=2.0) <> 2.0 
   print "2.0 <> 2.0 (FALSE ==> should NOT print at all!)"
End iF

iF (a=2.0) < 2.0
   print "2.0 < 2.0 (FALSE ==> should NOT print at all!)"
End iF

Print "okay"

does really strange things which I am inclined to regard as undefined behavior of Oxygen's parentheses.

It seems like we should ask Charles to revise his design decision again if he really wants Oxygen's parentheses to behave conventionally, because the above is very likely an outstanding bug.  :-\

Regards,
Title: Re: Boolean Statements
Post by: Charles Pegge on January 29, 2014, 01:52:44 AM

There is a difference between the C '=' and Basic '=' in a conditional statement. You will find that in all of these statements, 'a' remains unaltered. However, these examples can be improved by using the unambiguous form '=='.

Another difference is that a boolean true produces an integer value of '-1', not '1'.

float a=0.0

if (a==2.0) > 2.0  'if 0 > 2
   print "3" 'never
end if

if (a==0) > 2.0 'if -1 > 2
  print "3a" 'never
end if

But I can't think of any practical situation where you would want to use such constructs :)

PS:

There is also a specific assignment operator for use in conditionals: ':='. Normally this would be used with a function returning a handle or error code
Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 02:45:49 AM
Charles,

There is a difference between the C '=' and Basic '=' in a conditional statement.
Shouldn't it read Oxygen Basic given everything else I'm reading in this message?

You will find that in all of these statements, 'a' remains unaltered.
I am afraid, this contradicts (in fact, denies and ignores) all of the following:
1. scoping function of parentheses in an expression;
2. conventional BASIC usage of parentheses as a formal attribute of a function, i.e. an expression that returns a value, in this case, the value of 2.0; and finally
3. your own implementation of a = b = c ... = n.

There is absolutely nothing ambiguous, BASIC-wise, in such a parenthesized = which is unambiguously an assignment. Standard BASIC would decompose such an expression into:

a = 2.0
if a < 2.0 then ' in fact, "if 2.0 < 2.0 then" which means "never!"
...


However, these examples can be improved by using the unambiguous form '=='.
This operator is redundant if you follow standard BASIC conventions. In FBSL, == has a special meaning of "value and data type equality".

Another difference is that a boolean true produces an integer value of '-1', not '1'.
Again, this is irrelevant if you will follow standard BASIC conventions.

float a=0.0

if (a==2.0) > 2.0  'if 0 > 2
   print "3" 'never
end if

if (a==0) > 2.0 'if -1 > 2
  print "3a" 'never
end if

But I can't think of any practical situation where you would want to use such constructs :)
Neither can I. :)

There is also a specific assignment operator for use in conditionals: ':='. Normally this would be used with a function returning a handle or error code
As a language creator, you are free to use any alien operators however you see fit for your creation. But conventional BASIC operators should remain conventional in Oxygen Basic too IMHO.

Regards,
Title: Re: Boolean Statements
Post by: Charles Pegge on January 29, 2014, 04:11:47 AM
In Oxygen, the behaviour of '=' as a comparator, is governed by being in an if or while statement. (There is actually a switch available to override it #assign on)

Otherwise '=' is always an assignment operator. Hence the need to use '==' in a plain boolean statement.

sys a,b,c
c=(a==b) 'result c=-1

I have further mods in the pipeline, which will improve syntactic consistency:  for instance there are no comparator here

sys a,b,c
a=(b=c+1)+1
'results a=2, b=1, c=0

Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 04:21:39 AM
Charles,

Be warned that you seem to be deliberately making your Basic code incompatible with every other BASIC in the world.

(http://i1240.photobucket.com/albums/gg490/FbslGeek/StandardBASIC.png)

Why should you, really? :)

Regards,
Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 04:32:52 AM
sys a,b,c
c=(a==b) 'result c=-1
...............................................
sys a,b,c
a=(b=c+1)+1
'results a=2, b=1, c=0

Please don't confuse these with what is there in Peter's code. These are a pretty different pair of shoes. There "shall not" (= mustn't, in Standard BASIC Specs' terms) be any special cases anywhere except those that are governed by the parentheses. And I repeat, from this standpoint, your == is a tautology, a redundancy, and an abuse. 3 in 1, as Nescafe goes.  :)

Your

c=(a==b)

is in fact

if a = b then c = -1 ' or c = true, for that matter

while

c = (a = b)

is unambiguously

let c, a = b ' in dialects where multi-let is allowed

or yet simpler

c = a = b ' in your own O2

Moreover, your

a=(b=c+1)+1

is in fact

b = c + 1
a = b + 1


which is one character shorter than your one-liner! :)

Again, there's also a ternary iif(..., ..., ...). Isn't that supposed to be a Basic, after all? If yes then why bother, may I ask? What's in there in your semantics that can't be expressed in the existing clear and concise BASIC terms?  :)

P.S. OK OK, that will be all for today... :)
Title: Re: Boolean Statements
Post by: Charles Pegge on January 29, 2014, 06:03:46 AM
I thought BASIC was the preferred  language of rebels and renegades. :)

The only public ECMA BASIC I can see was published in 1978, but you have a copy of the 1986 standard there?
http://www.ecma-international.org/publications/files/ECMA-ST-WITHDRAWN/ECMA-55%20SCOPE.pdf

Oxygen borrows quite a few concepts from C and '==' is one of them. It will accept most C operators to aid porting, and to interpret C headers.

But what we are discussing here are new 'fringe' features, though a natural consequence of how the compiler works. I think they might lead somewhere interesting. I quite like the Multi-Let you mentioned, Mike, so maybe we could incorporate that as well.

Let a,b,c=42

Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 06:20:04 AM
Aha, so both of us are not asleep yet. My premature desertion would be unfair to Oxygen Basic and its Creator. :)

O2 has a very, very strong point. It is both an extremely efficient interpreter because of its underlying JIT compiler nature and it is also a static compiler with EXE and DLL capability. Plus, of course, its very well-developed inline assembly feature for the ultimate gurus into the bargain.

You shouldn't decorate all this treasure with C-ish extras that add no real good to the existing BASIC armory. That would only erode its consistency and predictability for a wide variety of potential Oxygen Basic users. There's still a lot to be done to extend O2 to modern realities and non-native platforms too. This is where I'd like to see O2 heading for. In this respect, I do support what John is usually advocating though I don't normally do it in his uncompromisingly straight-forward manner. :)

P.S. 1. No, only Freestyle BASIC was. :D

2. Yup, multi-let is nice. FBSL has historically formulated it as a function call though in fact it is not:

Let(a, b, c) = 123

In FBSL, () are a mandatory formal attribute of a fully qualified (executable) function call. Function names used without the parentheses are what we call volatiles (hehe, in fact non-volatiles) - they preserve the most recent fully qualified call's return value until  a subsequent fully qualified call to the same function is made. This helps avoid unnecessary temp vars and assignments. Sort of code optimization, speed-wise.

3. I'm sending you my copy of Standard BASIC Specs in a PM. There wasn't any newer Specs issued after 1986 to the best of my knowledge.

Kind regards,
Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 06:30:02 AM
Sorry Charles, the zip (7z) is over 50MB in size; the forum won't accept such an attachment. I must go out to my bank before it closes down for today. I'll send you a link as soon as I'm back which will be in about 2 hours from now.

Sorry again for the inconvenience even if it's beyond my direct control, and see you soon.
Title: Re: Boolean Statements
Post by: JRS on January 29, 2014, 08:52:27 AM
Quote
I thought BASIC was the preferred  language of rebels and renegades.

Mike,

Have a look at Nimrod (http://nimrod-lang.org/) is you want to see a language built from the best syntax of many different language concepts. BASIC is going on 60 years old and it only seems logical to modernize the language for todays needs. I make sure I'm not idle with SB in any direction for too long as I don't want it labeled as one thing or another. I think Charles feels the same way about O2. IMHO

John
Title: Re: Boolean Statements
Post by: Charles Pegge on January 29, 2014, 09:03:30 AM
As I recall, the Bible compresses to about 1 meg, so the BASIC specifications, at 50 megs, must be truly vast and comprehensive  ;D

I think I've cracked the multi-let

Because LET is also used for creating objects, potentially involving memory allocation, the only safe way is for the compiler to split it down into separate LET statements for individual processing.

let dracula, frankenstein=monster()
--->
let dracula=monster()
let frankenstein=monster()

Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 09:16:44 AM
Have a look at Nimrod (http://nimrod-lang.org/) is you want to see a language built from the best syntax of many different language concepts.

Tastes differ. For me, there's nothing compares to ANSI C - laconic, expressive, crisp.

You have one indisputable advantage over me, John, which is your being a native American (~English, more or less) speaker. I can't compete with you in flattering anybody without a dictionary in hand and a deep breath beforehand. But that one can hardly pass for flattering. You risk ending up in the esoteric trashbin if you don't signal unambiguously what lear you really are. IMHO
Title: Re: Boolean Statements
Post by: JRS on January 29, 2014, 09:22:11 AM
Okay.

ScriptBasic is a ANSI C thread safe scripting language API that uses BASIC for it's syntax.

SCRIBA & SBHTTPD were just examples how to use the SB API. They took on life's of their own. SB can be anything you want to morph it into.
Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 09:22:20 AM
Charles,

Here's the link to SBS.7z (http://wikisend.com/download/485346/SBS.7z) as promised. It contains two .PDF's that's why it's so huge and resistant to brute-force compression.

The link will be valid for 7 days only but it won't require registering with the site.
Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 09:40:58 AM
John,

"You" in my sentence has had a somewhat broader sense encompassing both you personally and Charles. I just thought "you two" or "the two of you" would sound somewhat crude so I've restricted myself to this generalized pronoun.

Yet my assertion stays. FBSL was designed to inter-operate with 3rd-party libraries in the most efficient way imaginable under Windows. Its .DLL version can be wrapped up into any ActiveX control you like to mimic the wildest syntaxes or semantics. Yet it will always stay essentially distinct, predictable and expectable BASIC, C, and Asm to those who prefer to use it in its core, aborigenal form - again, I'm too lazy to look up a better English connotation. :)
Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 09:49:25 AM
Charles,

Set is a de-facto standard to denote allocation of objects in modern BASIC dialects. Maybe it is not yet too late to roll O2 back to comply with it and make room for its own multi-Let? Is Oxygen really that burdened by backwards compatibility issues?
Title: Re: Boolean Statements
Post by: JRS on January 29, 2014, 09:50:01 AM
I just finished a project where SB is embedded in firmware (a commercial product) on a POSIX based controller that takes serial data from 3 solar power inverters and converts it to values on a BMC BACNet network. Charles pulled off a miracle and wrote a MD5 routine in SB to unlock the power inverters so we could communicate with them.

The ScriptBasic SDL extension module is opening a new chapter in SB's ability to stay relevant.

Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 09:59:27 AM
I can only wish you good luck with projects like this and I am happy you have found each other for mutual support. Being a homo sentient as I am, I'm stepping out of your way.

Naturally, no personal insults implied. It's just my humble non-British origin that keeps on telling again. :D
Title: Re: Boolean Statements
Post by: Charles Pegge on January 29, 2014, 10:47:32 AM

Thanks for the ECMA specs, Mike. I have just downloaded them. I see they are entirely in image form, unfortunately I am unable to interrogate the pixels, or do text-to-speech, so I will have to study them with my magnifying glass :)

I have never been a VB user, but I presume the use of SET for objects originated there. I based Oxygen's Object model on C++ and discarded as much noisy syntax as was practicable. Class is very closely related to Type in O2, and it may well be possible to merge the two concepts.

I like the idea of finding extended meanings for old concepts and keywords. set and get remain available for user definition, though normally one would expect them to be used for indirectly accessing an object's properties. Let is similar to Dim but also allow one variable or object to be cloned from another, or created from a class factory. But this is still experimental.
Title: Re: Boolean Statements
Post by: JRS on January 29, 2014, 11:09:06 AM
Quote from: Charles
I like the idea of finding extended meanings for old concepts and keywords.

Then you should have a blast with the BBC BASIC graphics syntax I added to SB in the SDL extension module.

Title: Re: Boolean Statements
Post by: Charles Pegge on January 29, 2014, 11:20:08 AM
BBC Basic was my first programming experience. My inspiration for removing unnecessary brackets. BBC basic even had an inline assembler for the 6502 cpu.
Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 11:23:17 AM
unfortunately I am unable to interrogate the pixels, or do text-to-speech, so I will have to study them with my magnifying glass :)
Oh. What a misfortune... I could've helped but my OCR is Russian only and these are the only copies I have. Should I google for potential (though unlikely) alternatives for you?

I presume the use of SET for objects originated there.
That's correct.

I like the idea of finding extended meanings for old concepts and keywords.
I assure you, these "machine" words are but mere labels for myriads of PC-aware human beings. They are devoid of any particular human meaning because 25 per cent of programmers are not native to English and the other 75 per cent coders do not know any human language at all including their own mother tongue.

set and get remain available for user definition, though normally one would expect them to be used for indirectly accessing an object's properties.
Not exactly. Modern BASIC would reserve Set for accessing properties that accept instantiated objects only, Let for those that accept all other, simpler data types, and Get for the returns of the both.

Let is similar to Dim but also allow one variable or object to be cloned from another, or created from a class factory. But this is still experimental.
That seems logical. Looking forward to final results.

Kind regards,
Title: Re: Boolean Statements
Post by: Mike Lobanovsky on January 29, 2014, 11:34:52 AM
I've just checked out the Holy Bible. It compresses to 1.06MB with uber-Zip, and to just under 868KB, with 7zip.

That's an update FYI.

;D
Title: Re: Boolean Statements
Post by: Charles Pegge on January 29, 2014, 03:28:44 PM
That is a remarkable level of compression: about 1 byte per word of text!  :o
Title: Re: Boolean Statements
Post by: Emil_halim on January 30, 2014, 08:29:15 AM
Hi Charles.

I see that Oxybasic will catch c sooner.

very nice approach. 
Title: Re: Boolean Statements
Post by: Kuron on January 30, 2014, 09:24:56 AM
I can't comment on FBSL, as I have never used it. 

However, the idea that there is a BASIC standard is a pipe dream.  Every variant seems to implement their own unique syntax changes, which is part of what makes each variant special in its own way.  There would be no need for so many variants if they were all identical.  Even in the heyday of BASIC, in the 70s-80s, each system had a unique BASIC and you often had to tweak code to get it to run on a different system.  Much like Microsoft's bastardization of C++ in VisualC++/VisualStudio.  If you use it, you will have issues working with other C++ compilers.  At least with C++, there is a semblance of a standard.  BASIC, has always been no-holds barred when it comes to syntax.  Even Bob Zale destroyed the legacy of PowerBASIC by introducing OOP/COM and trying to make PB a C++ wannabe language in an effort to retain users who were leaving for compilers that better supported modern versions of Windows.  Bob was not alone as there were many BASIC/C/C++ Hybrid languages.  Some good, some bad.  Few got the hybridization "right".  Bob missed it by a mile.

Coding standards have changed since the 70s and 80s.  Even leaving out OOP, what may have been acceptable as a standard even in the 90s is not acceptable now.  There is no right or wrong answer, each language needs to use what works best for them and their demographic.  If you are targeting legacy systems, legacy standards from the 70s and 80s would be ideal.  If you are targeting the 21st century, then it is best to forget the "perceived" standards of the old days.
 
If you don't like what Oxygen offers, don't use it.  Problem solved.
Title: Re: Boolean Statements
Post by: JRS on January 30, 2014, 10:57:10 AM
To All:

If you are trying O2 and taking notes as how things work, please post them here so others can benefit from what you learned. It's a small step toward a formal documentation effort. No one is asking you to pull out your credit card but open source means freedom to view and modify the source, not a FREE lunch. O2 is only as good as the effort you're willing to invest to make it so.

There is suppose to be a PowerBASIC announcement on the future of the company on the first. It would be great if PB past users would join us and help give the OxygenBasic project a bright future.

Title: Re: Boolean Statements
Post by: Charles Pegge on January 30, 2014, 11:21:55 AM
I'm hedging my bets with Oxygen (re: BASIC standards) , and have kept some of the ancient habits, such as line numbers, gosub, and goto in case someone  finds contemporary use for them. At the other end, OxygenBasic supports multi-inheritance and polymorphic OOP, which of course, one is not obliged to use.

But APIs are so huge and diverse these days, that an IDE to keep them all under control seems to be more important than the language itself.

PS
Emil, the #preprocess switch is now implemented to support C-style preprocessing of #define, #ifdef ..  (and %)
Title: Re: Boolean Statements
Post by: Emil_halim on January 30, 2014, 11:19:00 PM

PS
Emil, the #preprocess switch is now implemented to support C-style preprocessing of #define, #ifdef ..  (and %)

thanks Charles , i will test it.