-->
Page 1 of 3

switch / case statements

PostPosted: Wed Aug 10, 2016 8:27 am
by Ecoli-557
Is there any chance this could be added? It appears to be in the Arduino package so it might be available.
I believe it may execute faster than a series of if-then loops and wouldn't be as hard on the stack - if the stack is even a concern.

Thanks for all you do, This Buds for you, Bud!

Re: switch / case statements

PostPosted: Mon Sep 12, 2016 12:04 am
by livetv
I agree that this would be a nice addition! However, don't assume that because the Arduino IDE has it, this makes an easy port to ESPbasic. Library functions generally would be easy to add as it is a matter of adding a few lines of customized "boiler plate" to the source code. By contrast, the Case/Switch construct is inherent in C and not a library function. I'm pretty sure that to add it would require that it be written as a primitive flow control within the interpreter.

Would it be faster that a series of if/else statements? I think so. The processing, while it would still have to perform all the same conditionals, would not have to run each one through the command decoding (which is curiously a long series of if/else statements and not switch/case last time I looked). So the overhead would be one command decode plus N conditionals (and some scanning through the lines of BASIC code) versus N command decodes and N conditionals.

A cool alternative that would work in many cases would be to have the ability to GOTO or GOSUB dynamically. This would be an easy addition I think. When parsing the GOTO command (for example), check to see if the first character of the label is a bracket ( [ ). If it is, behave normally. If not, treat the operand as an argument such as a quoted string constant OR as a string variable. This way, the following would all behave the same way:

Code: Select allgoto [place]


Code: Select allgoto "[place]"


Code: Select alllet destination="[place]"
goto destination


Code: Select alldim destination(10)
let destination(5)="[place]"
goto destination(5)


This would allow us to set up vector tables. I have a cool ESPbasic application that allows me to define devices of any kind attached to the ESP8266 and it does sensing, reporting and device manipulation over the internet. By saving these device definitions in a local file, I can make hardware changes WITHOUT having to change any ESPbasic code and it all works. The problem is that I use a lot of if/else sequences to determine and perform the right functions based on the device parameters. It would be SO much easier for me to use a vector table as follows:

Code: Select alllet destinations="[po] [pwo] [servo] [pi] [pwi] [ai] [temp]"

' Magical code happens here which encounters a device on pin P performing function FNC with state S (for outputs)

let place=word(destinations,fnc)
gosub place

' Code continues with return value in RV set by called routine

wait

[po]
io(po,p,s)
return

[pi]
rv = io(pi,p)
return

[ai]
rv = io(ai)
return

' Etc, etc.

Re: switch / case statements

PostPosted: Tue Sep 13, 2016 10:51 am
by Mmiscool
I like the idea of a goto or gosub which allows for the branch label to be specified as a string variable.

This could provide for some interesting functionality.

Re: switch / case statements

PostPosted: Tue Sep 13, 2016 11:18 am
by Ecoli-557
I am in agreement - especially since Mike already told me it was NOT going to be an easy addition - more of a coding philosophy than a library thing.
I do NOT want to add to his enormous workload on the maintenance, creation, etc, etc of what is already on his plate.
Now, IF you can add the functionality of a GOTO or GOSUB with a branch LABEL as a STRING, that would be kinda kewl!
That would hold me for a year maybe two! <grin>.
Regards/