Oxygen Basic
Programming => Problems & Solutions => Topic started by: JRS on June 14, 2018, 04:59:09 PM
-
his O2 Swap macro is even better than PB Swap, bcos PB Swap statement will require the same type of primitives
for the 2 variables a and b
Does it work with numbers and strings?
a = 99
b = "beers"
SWAP a, b
PRINT "A: ",a,"\n"
PRINT "B: ",b,"\n"
jrs@jrs-laptop:~/sb/examples/test$ scriba swapit.sb
A: beers
B: 99
jrs@jrs-laptop:~/sb/examples/test$
-
It could work if O2 had support for Variants.
FreeBasic code:
DIM a AS CVAR = 99
DIM b AS CVAR = "beers"
SWAP a, b
PRINT "A:", a
PRINT "B:", b
-
It's an unintended consequence of this o2 swap macro, but you can indeed exchange values between a string and a numeric type:
macro Swap(a,b, c)
=====================
typeof(a) c=a
a=b
b=c
end macro
int a=100
string s="99"
swap a,s
print s ' result "100"
-
Cool Charles!
-
Now the problem will be to find any use for it. I don't remember having never needed to swap the contents of a string and a numeric variable.
-
It's really just a curiosity.
We can squash this impertinent behaviour with an error trap ;D
macro swap(A,B)
===============
#if typecodeof(A)<>typecodeof(B)
#error Swap type mismatch: A, B
#else
typeof(A) C
C=A
A=B
B=C
#endif
end macro
int aa
short bb
swap aa,bb
-
Now the problem will be to find any use for it. I don't remember having never needed to swap the contents of a string and a numeric variable.
One that comes to mind is reuse of the existing temporary variables in order to avoid potentially time-consuming memory allocation in speed-sensitive code. Both SB and FBSL (BASIC) interpreters are built around what you may regard as proto- and super-Variants, respectively.
In FBSL, BASIC variables have no predefined data types. They change them dynamically based on what data is actually assigned to them at any given point in time while the app runs. Such languages as Lua and Terra also use this approach as opposed to strictly typed programming languages.
Charles, O2 may be used successfully to develop interpreting languages. Please do not "squash" this "impertinent curiosity" with you "error" trap. It may very well be a useful "feature" rather than a "bug". :)
-
SB's variant approach is limited to long, real, string and array / matrix types with an undef for variables defined but not assigned a value. The goal is data is data until use.
-
SB's variant approach is limited to long, real, string and array / matrix types...
Note: Double, but not Single.
An FBSL BASIC variable can store Long, Quad, Single, Double, String, WString, UDT (both compound and simple like Byte and Short), Variant, Object, bitfield, and procedure data types and pointers, and their arrays and arrays of arrays -- both "pure" and intermixed. (that's why I called them super-Variants)
-
SB is an embeddable general purpose thread safe BASIC interpreter with unlimited expansion that runs on everything.
FBSL is the Rolls Royce of Windows scripting like interpreters. You should try and fork it and call it your own BASIC.
-
I wasn't trying to measure whose one is longer, John. I was just answering Jose's doubts if the feature is useful or not, and Charles' attempts to kill what might come in very handy when developing interpreters rather than compilers.
Note: FBSL is embeddable, and it is thread safe in its assembly and C JIT parts. It isn't multi-platform though because it is in fact 3 different programming languages in 1 environment, which is a very complicated project to maintain for a small group of developers even on one major platform let alone the three of them.
I am not going to fork or develop it any further. I have more pleasing things to enjoy in my real life. :)
-
I am not going to fork or develop it any further. I have more pleasing things to enjoy in my real life.
I think Script BASIC has a greater commercial future beyond what it's used for today. It just needs to catch some wind in its sales.
I'm excited to see a tight integration with SB and O2. What is nice is it's already working but not in a form I would call ScriptO2.