Author Topic: New Cases  (Read 3654 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
New Cases
« on: May 25, 2014, 04:14:34 PM »
New case feature (Thanks to Mike)

Alternative cases can be placed on one line, separated by commas:

select a
case 9,10,13,32
  isWhiteSpace=1
case "A" to "Z"
  isAlpha=1
case else
   isWhiteSpace=0
  isAlpha=0
end select


Switch syntax may also be used. You must use break to prevent fall-thru to the next case, but it is often useful to allow this to happen:


switch a
case "_"
case "A" to "Z"
case "a" to "z"
   isAlpha=1
  break
default
  isAlpha=0
end switch


http://www.oxygenbasic.org/o2zips/Oxygen.zip
« Last Edit: May 25, 2014, 09:10:40 PM by Charles Pegge »

Mike Lobanovsky

  • Guest
Re: New Cases
« Reply #1 on: May 25, 2014, 04:19:48 PM »
Wow!

Thanks a lot Charles, this is extremely helpful and elegant! :D

Mike Lobanovsky

  • Guest
Re: New Cases
« Reply #2 on: May 26, 2014, 08:45:13 AM »
Charles,

Are mixed cases permitted?

select case a
  case "A" to "Z", "a" to "z"
    isAlpha=1
  case else
    isAlpha=0
end select

Charles Pegge

  • Guest
Re: New Cases
« Reply #3 on: May 26, 2014, 10:32:31 AM »

Yes they are now. You can mix both ranges and singles.

a=97
isName=0

select a
case "_", "#",  "A" to "Z",  "a" to "z" : isName=1
end select

print isName



http://www.oxygenbasic.org/o2zips/Oxygen.zip

Mike Lobanovsky

  • Guest
Re: New Cases
« Reply #4 on: May 26, 2014, 10:49:38 AM »
Thank you, Charles!

Oxygen is becoming richer and richer with every passing day. :)

Charles Pegge

  • Guest
Re: New Cases
« Reply #5 on: May 26, 2014, 12:52:30 PM »
More hidden treasures:  :)

This feature has been around for some time and concerns bit booleans, combined into one variable.

It works in concert with bit enumerations. On a 32 bit system, you can hold up to 32 booleans in one sys variable

Bit enumerations go like this: 1 2 4 8 16 32 64 128 ...

Each successive number is doubled to shift the bit position ( instead of incrementing).



Code: [Select]
enum bit flags aa=1,bb,cc,dd,ee,ff

a= cc or ff

switch bit a
case aa : print "aa"
case bb : print "bb"
case cc : print "cc" 'yes
case dd : print "dd"
case ee : print "ee"
case ff : print "ff" 'yes
end switch

http://www.oxygenbasic.org/o2zips/Oxygen.zip
« Last Edit: May 26, 2014, 09:50:04 PM by Charles Pegge »

Charles Pegge

  • Guest
Re: New Cases
« Reply #6 on: May 26, 2014, 02:32:37 PM »
Floats can also be used with case ranges. These can be expressed as float literals or float variable. Paradoxically, float variables are more efficient, because there is no direct way of loading a number literal or constant to the FPU.

This example also demonstrates how infinities, negative zero, and nans can be handled.
(nan means not a number)

Code: [Select]
float inf=1/0, nan=0/0, minf=-1/0, mzero=-0.0

float f

'f=.21
f=-1/0 'minus infinity
'f=-0.0
'f=0/0

select ?f
case ?mzero : print "minus 0.0"
case ?nan   : print "nan"
case else
  select f
  case 0.0 to 0.1 : print "0.0 to 0.1 :   " str f,2
  case 0.1 to 0.2 : print "0.1 to 0.2 :   " str f,2
  case 0.2 to 0.3 : print "0.2 to 0.3 :   " str f,2
  case inf        : print "infinity"
  case minf       : print "minus infinity"
  end select
end select

Aurel

  • Guest
Re: New Cases
« Reply #7 on: May 26, 2014, 02:40:24 PM »
Charles is this work now:

select code_arr

Charles Pegge

  • Guest
Re: New Cases
« Reply #8 on: May 26, 2014, 02:53:19 PM »
Is code_arr an ascii integer? If so, then yes. But it won't work for a string.

Charles Pegge

  • Guest
Re: New Cases
« Reply #9 on: May 26, 2014, 09:31:54 PM »
One further refinement required for float range cases: 'to <'

case 0.1 to < 0.2

I know this is not perfect syntax, so I'm open to ideas on improving it :)

Code: [Select]

float zero=0.0, mzero=-0.0, inf=1/0, minf=-1/0, nan=0/0

float f

'f=0.0  'zero
'f=-0.0 'minus zero
'f=1/0  'infinity
'f=-1/0 'minus infinity
'f=0/0  'not a number
'f=.19999999999999999 'range boundary test
 f=.20
'f=.2125
'
'THESE CASES USE DWORD (SYS) MATCHING INSTEAD OF FLOAT EVALUATION
select ?f
case ?zero  : print "zero"
case ?mzero : print "minus zero"
case ?inf   : print "infinity"
case ?minf  : print "minus infinity"
case ?nan   : print "nan (not a number)"
case else
  'CASES USING NORMAL FLOAT EVALUATION
  select f
  case 0.0 to < 0.1 : print "0.0 to < 0.1 :   " str f,2
  case 0.1 to < 0.2 : print "0.1 to < 0.2 :   " str f,2
  case 0.2 to < 0.3 : print "0.2 to < 0.3 :   " str f,2
  end select
end select

update:
http://www.oxygenbasic.org/o2zips/Oxygen.zip
« Last Edit: May 26, 2014, 09:44:54 PM by Charles Pegge »

Mike Lobanovsky

  • Guest
Re: New Cases
« Reply #10 on: May 26, 2014, 10:53:09 PM »
Pages like these should immediately go into oxygen_help.chm in their entirety. :)

I should also note that all those additions have been made within less than 1KB of Oxygen.dll extra footprint.

Excellent! :D