Author Topic: SWAP  (Read 548 times)

0 Members and 1 Guest are viewing this topic.

John

  • Hero Member
  • *****
  • Posts: 3280
SWAP
« on: June 14, 2018, 04:59:09 PM »
Quote from: CC@JRS
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?

Code: Script BASIC
  1. a = 99
  2. b = "beers"
  3. SWAP a, b
  4. PRINT "A: ",a,"\n"
  5. PRINT "B: ",b,"\n"
  6.  


jrs@jrs-laptop:~/sb/examples/test$ scriba swapit.sb
A: beers
B: 99
jrs@jrs-laptop:~/sb/examples/test$


Josť Roca

  • Jr. Member
  • **
  • Posts: 87
Re: SWAP
« Reply #1 on: June 14, 2018, 05:41:42 PM »
It could work if O2 had support for Variants.

FreeBasic code:

Code: [Select]
DIM a AS CVAR = 99
DIM b AS CVAR = "beers"
SWAP a, b
PRINT "A:", a
PRINT "B:", b

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3751
    • Oxygen Basic
Re: SWAP
« Reply #2 on: June 14, 2018, 06:34:10 PM »
It's an unintended consequence of this o2 swap macro, but you can indeed exchange values between a string and a numeric type:

Code: [Select]
  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"

John

  • Hero Member
  • *****
  • Posts: 3280
Re: SWAP
« Reply #3 on: June 14, 2018, 06:45:09 PM »
Cool Charles!

Josť Roca

  • Jr. Member
  • **
  • Posts: 87
Re: SWAP
« Reply #4 on: June 15, 2018, 05:46:55 AM »
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.

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3751
    • Oxygen Basic
Re: SWAP
« Reply #5 on: June 15, 2018, 08:04:16 AM »
It's really just a curiosity.

We can squash this impertinent behaviour with an error trap   ;D

Code: [Select]
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

Mike Lobanovsky

  • Admin Support Member
  • *****
  • Posts: 1857
Re: SWAP
« Reply #6 on: June 15, 2018, 10:23:38 AM »
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". :)
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, 2 x nVidia GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

John

  • Hero Member
  • *****
  • Posts: 3280
Re: SWAP
« Reply #7 on: June 15, 2018, 10:34:55 AM »
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.

Mike Lobanovsky

  • Admin Support Member
  • *****
  • Posts: 1857
Re: SWAP
« Reply #8 on: June 15, 2018, 10:49:46 AM »
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)
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, 2 x nVidia GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

John

  • Hero Member
  • *****
  • Posts: 3280
Re: SWAP
« Reply #9 on: June 15, 2018, 10:59:08 AM »
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.

Mike Lobanovsky

  • Admin Support Member
  • *****
  • Posts: 1857
Re: SWAP
« Reply #10 on: June 15, 2018, 11:07:32 AM »
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. :)
« Last Edit: June 15, 2018, 11:14:34 AM by Mike Lobanovsky »
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, 2 x nVidia GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

John

  • Hero Member
  • *****
  • Posts: 3280
Re: SWAP
« Reply #11 on: June 15, 2018, 11:35:07 AM »
Quote
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.
 
« Last Edit: June 15, 2018, 11:44:07 PM by John »