Oxygen Basic

Information => Development => Topic started by: Brian Alvarez on May 08, 2019, 11:18:27 PM

Title: Goto label inside a select case block?
Post by: Brian Alvarez on May 08, 2019, 11:18:27 PM

This code:

Code: [Select]
goto NOLabel
select case 1
    case 2
        NOLabel:       
end select

Or code like this:

Code: [Select]
goto NOLabel
if 1 then
   NOLabel:       
end if

Produces this:

Code: [Select]
Linker found unidentified names:
1102 nolabel "main source

Is this a known limitation?

 I know PHP does not allow to jump into blocks of code... and i never got why... as long as all the variables are initialized it should not be an issue...
Title: Re: Goto label inside a select case block?
Post by: Charles Pegge on May 09, 2019, 01:14:10 AM
Hi Brian,

in o2, labels and other symbols defined inside a block are not visible from the outside. It's good for modularity though occasionally inconvenient.
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 09, 2019, 11:55:22 AM

Can something be done to overcome this? A lot of code relies on being able to do so...
Title: Re: Goto label inside a select case block?
Post by: Charles Pegge on May 09, 2019, 09:11:32 PM
Block-scope is a fundamental feature of o2 at all levels.

Jumping into the middle of a block, indicates the code needs to be restructured. It cannot be safely maintained.
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 09, 2019, 09:30:02 PM
I tend to have a bittersweet opinion regarding that, but that is not the point. Since i need to be able to allow this, for all the existing code that uses this "dangerous" structuration, i would like to find a way.

Can i still use low level statements to jump to labels?

 I mean... Since PluriBASIC knows the label is there... is there a way to call... lets say:

Code: [Select]
jmp @nolabel
 Without compile-time errors? Otherwise i will have to re-write my own if/then select/end select /do/loop blocks using goto myself...

Title: Re: Goto label inside a select case block?
Post by: Aurel on May 09, 2019, 10:40:52 PM
Hey Brian

hmm ..i really don't know any basic dialect in which you can jump inside CASE statement  :o
don't get me wrong ..but that looks like suicide.
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 09, 2019, 10:55:01 PM
I thought everyone here knew PowerBASIC...

Title: Re: Goto label inside a select case block?
Post by: Arnold on May 10, 2019, 12:07:36 AM
Somehow these constructs look strange to me. It is almost like e.g. jumping into a while .. wend loop:

goto thislabel

string s
while 1
thislabel:
  s = "Hello"
  exit while
wend
print s " World"


I am happy that Oxygen will prevent nonsense like this:
...
goto lbl1
    select case wMsg
      case WM_CREATE
...
      case WM_DESTROY
lbl1:
      PostQuitMessage 0
...

To my surprise this is allowed:
...
    select wMsg
goto lbl1       
      case WM_CREATE
...
      case WM_DESTROY
lbl1:
print "here"         
      PostQuitMessage 0
...
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 10, 2019, 12:26:41 AM
Arnold, consider this situation:

Code: [Select]
SELECT CASE Situation
    CASE A, B, C
        ' Do something that i want to do here, but not in D, E and F.

        GOTO DoTheRestOfTheTasks
       
    CASE D, E, F
        ' Do something that i want to do here, but not in A, B or C.
       
        DoTheRestOfTheTasks:       
        ' Do the tasks that apply to All the cases.
       
END SELECT

If you are worried about the CPU registers getting messed up or not correctly initialized, then remember
that in some cases (not all) MACROS can also do stuff like that.

 The above code is more powerful and flexible than what other languages do. Have you seen
Swift's (relatively new programming language) fallthrough? Or... the automatic fallthrough in C++'s SWITCH
statement?

 For some, those can sound crazy, but are very powerful and once you get the hang of them, can become
easier to debug. Consider the above example, I could also do it like this:

Code: [Select]
SELECT CASE Situation
    CASE A, B, C
        ' Do something that i want to do here, but not in D, E and F.

        CALL DoGlobalTasks(A, LIST, OF, ALL, THE, PARAMETERS, THAT, I, NEED, TO, USE, TO, COMPLETE, THE, GLOBAL, TASKS)  ' Do the rest of the tasks...
       
    CASE D, E, F
        ' Do something that i want to do here, but not in A, B or C.

        CALL DoGlobalTasks(A, LIST, OF, ALL, THE, PARAMETERS, THAT, I, NEED, TO, USE, TO, COMPLETE, THE, GLOBAL, TASKS)  ' Do the rest of the tasks...
       
END SELECT

 Better, some would say, but for me, unnecessarily adding complex parameters in a language that was designed to be simpler and quicker to work with is an undesired  hassle.

 Sometimes i do that out of need, but when there are statements like this...

Code: [Select]
    CALL moduleParts(SD(), 10, ISTRUE(MOD_DATA(SD(oIndex).Index).DataType), 0, MODULE_DATATYPE, VAR_DATA(SD(oIndex).Index).Flags, UDT_DATA(SD(oIndex).Index).Flags, isConDes, Comp(SD(oIndex).Index).Tags, Memory.Globals.Sect(SD(oIndex).managed).Pointer, "")
Trust me... i rater use a small GOSUB than stating the parameters i need in each case, because, this last one, is much harder to mantain or debug than a single GOTO/GOSUB.




Title: Re: Goto label inside a select case block?
Post by: Arnold on May 10, 2019, 01:41:18 AM
Hi Brian,

comparing with the Situations cases example above, this would make more sense to me and it works also:

Code: [Select]
$ filename "TestLabel.exe"

'uses rtl32
'uses rtl64

FUNCTION PBMAIN() AS LONG

LOCAL ti AS LONG
'ti=88888
select CASE TI
   case 0
     GOTO thislabel

   CASE 88888
print "here"
    CASE ELSE
      thislabel:
        print " it works"

END switch

END FUNCTION

PBMAIN()


Would this appoach be applicable?
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 10, 2019, 02:07:58 AM
I am happy that Oxygen will prevent nonsense like this:

Code: [Select]
goto lbl1
    select case wMsg
      case WM_CREATE
      case WM_DESTROY
lbl1:
      PostQuitMessage 0

 Hehehe, to me, that looks like a deliberate attempt to make GOTO look dangerous. :P
That is an accident that can also happen without jumping into a SELECT/END SELECT block.
Sometimes a typo is all what is needed, and even then, nothing that a recompilation cannot
fix. Not the end of the world.

 Demonizing a GOTO with freedom because you can jump to a wrong place, is like demonizing
a knife because you could cut yourself with it.

I am sure you dont cut onions with a spoon.
Title: Re: Goto label inside a select case block?
Post by: Charles Pegge on May 10, 2019, 02:16:26 AM
Referring to Brian's first example:

Code: [Select]
SELECT CASE Situation
    CASE A, B, C
        ' Do something that i want to do here, but not in D, E and F.

        GOTO DoTheRestOfTheTasks
       
    CASE D, E, F
        ' Do something that i want to do here, but not in A, B or C.
       
        DoTheRestOfTheTasks:       
        ' Do the tasks that apply to All the cases.
       
END SELECT

The simplest solution might be:

Code: [Select]
SELECT CASE Situation
    CASE A, B, C
        ' Do something that i want to do here, but not in D, E and F.
       
    CASE D, E, F
        ' Do something that i want to do here, but not in A, B or C.
    CASE ELSE
       GOTO NSEL   
END SELECT
'Do the tasks that apply to All the cases.
NSEL:
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 10, 2019, 02:17:05 AM
Would this appoach be applicable?

 Hello Arnold, yes, of course. But im not trying to acomplish anything with that code except
explaining why is useful to jump from one case to another.

 What i am trying to do is to be able to compile a lot of existing code, some of which contains
GOTO/GOSUB statements that jump into structures with no restrictions. :(

 Is not like i have an option.
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 10, 2019, 02:33:47 AM
 I already removed the ability to jump into structures from PluriBASIC. It now generates a compile-time error. :)

If there is a need, i might end up creating my own structures. Using goto.  ;D

Code: [Select]
' DO
InitialDo00001:


'LOOP UNTIL Done
IF Done THEN GOTO EXITDo0001
GOTO  InitialDo00001
EXITDo0001:
Title: Re: Goto label inside a select case block?
Post by: Charles Pegge on May 10, 2019, 03:01:11 AM
As another solution, I found flags to be very useful when dealing with complex logic, especially in the o2 source code: The logic is easier to read.

Code: [Select]
INT t 'action flag
SELECT CASE Situation
    CASE A, B, C
        ' Do something that i want to do here, but not in D, E and F.
        t=1
    CASE D, E, F
        ' Do something that i want to do here, but not in A, B or C.
        t=1
END SELECT
IF t
'Do the tasks that apply to All the cases.
ENDIF
Title: Re: Goto label inside a select case block?
Post by: Aurel on May 10, 2019, 02:56:30 PM
I am not former PowerBasic user.
Yes i agree with Charles ..far better method!
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 10, 2019, 05:00:37 PM
Thanks Aurel. May i ask what software do you write?
Title: Re: Goto label inside a select case block?
Post by: Aurel on May 10, 2019, 11:11:31 PM
Of course ..you can  :)

Well...the most known is old Aurel Basic written in EB by IonicWind.
Then .. experimental Ruben Interpreter written in Oxygen Basic    .
Then.. failed attempt to bytecode toy interpreter ...i am too lazy for that  ::)
Then... JimKlutho basic written in PowerBasic ...translation to Oxygen Basic..
Then newest - token based interpreter micro(A) ...he he

also
Aurel Edit - code editor written completely in Oxygen Basic with pure winApi.
awinh.inc - include written in Oxygen basic for Oxygen Basic GUI apps.
etc..
etc..
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 11, 2019, 12:56:09 AM
I am not former PowerBasic user.

Then... JimKlutho basic written in PowerBasic ...translation to Oxygen Basic..

how? :o
Title: Re: Goto label inside a select case block?
Post by: Aurel on May 11, 2019, 05:42:53 AM
Let say ..easy with a little help of Charles of course.
Look BASIC is BASIC and syntax is very similar.
so i do  it function by function and made it  :)

here is link:
http://basicpro.mipropia.com/smf/index.php?topic=22.0
Title: Re: Goto label inside a select case block?
Post by: John on May 11, 2019, 07:41:13 AM
ScriptBasic allows GOTO / GOSUB within a function / sub and even using duplicate labels defined in other functions. You can't jump in or out of a function/sub with GOTO.
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 15, 2019, 04:36:02 PM
 Charles... i apologize for bringing this back, but, is there a way to add a more free goto? It doesnt
matter if its a lower level method... maybe i can take a look at oxygen's code...

 I am trying to re-implement exit macro, it works but by adding tricks that not always will work
depending on the content of the macro.

 Sometimes the macro can end where a block starts... so, emulating an EXIT MACRO will fail, because
exiting the macro would force jumping into a block like do/loop.

 Scopes are great, but sometimes skills are crippled by them...

 
Title: Re: Goto label inside a select case block?
Post by: Charles Pegge on May 16, 2019, 07:00:20 AM
Hi Brian,

The linker requires that 'global' labels get special handling so they are not disallocated when blocks close.

I'm testing a scheme in which global label names start with a dot:

Code: [Select]
'15:45 16/05/2019
'jumping into inner block
goto .gl
'jmp fwd .gl
(
  print "!"
  ..gl 'global label
)
print "label location " @.gl
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 16, 2019, 07:10:17 AM
 That is good news Charles!! This was driving me crazy!
Please let me know how it goes! :D

And... sorry to be a pain in the butt! :(

 I enhanced macros quite a lot, i promise this is going to be worth it. You will see soon!
Title: Re: Goto label inside a select case block?
Post by: John on May 16, 2019, 02:54:44 PM
Quote
 I enhanced macros quite a lot, i promise this is going to be worth it. You will see soon!

Just in time before PowerBasic vendors and users start jumping off buildings.  ;D
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 16, 2019, 07:36:54 PM
Just in time before PowerBasic vendors and users start jumping off buildings.  ;D

Thats a bit cruel, you know? :D

Title: Re: Goto label inside a select case block?
Post by: John on May 16, 2019, 07:52:04 PM
How does it feel to be a hero?
Title: Re: Goto label inside a select case block?
Post by: Brian Alvarez on May 16, 2019, 08:32:53 PM
?