November 30, 2020, 01:44:37 AM

Author Topic: can we bring back The Goto and #label commands into NG  (Read 1365 times)

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2566
  • I mostly code at night... Mostly.
    • SyntaxBomb - Indie Coders
Re: can we bring back The Goto and #label commands into NG
« Reply #15 on: August 12, 2020, 04:27:45 PM »
the program i have written in blitzmax works fine

with no errors in 8551 lines of code and no one can help me with a goto alternative

thanks
You will have to ditch goto and instead think of functions as your goto label.

I do not know what me = 500 is about ( or your code, lol ) but...

So something on the lines of :

Code: BlitzMax
  1. local me
  2. for i = 1 to 1000
  3.         if i = 500
  4.                 me = 500
  5.                 me2( 500 )
  6.         endif
  7.         if i = 750
  8.                 me = 750
  9.                 me2( 750 )
  10.         endif
  11. next
  12.  
  13. ' loads more code here say another 1000 lines
  14.  
  15. ' nothing below this line will run except when called
  16.  
  17. function me2( value )
  18.         if value = 500
  19.                 ' do funky stuff
  20.         endif
  21.         if value = 750
  22.                 ' do funky stuff followed by
  23.                 me3()
  24.         endif
  25. end function
  26.  
  27. function me3()
  28.         ' do more funky stuff
  29. end function
  30.  
Mac mini ( 2018 / 2020 ), 3 GHz 6-Core Intel Core i5, 16 GB 2667 MHz DDR4, 1TB NVMe, eGPU Radeon Pro 580 8 GB, LG Ultragear 27GL83A-B 27 Inch
Commodore VIC-20, 1.1Mhz MOS 6502 CPU, 5KB RAM, VIC ( 6560 ) GPU

Until the next time...

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 739
Re: can we bring back The Goto and #label commands into NG
« Reply #16 on: August 12, 2020, 06:59:23 PM »
If i understood Blitzmax and NG correctly, the original created assembler where you could support Goto essily as a compiler. NG on the other hand translates to C++ which gets compiled by a Compiler. and there Goto and Gosub doesn't exist.

Offline Henri

  • Sr. Member
  • ****
  • Posts: 265
Re: can we bring back The Goto and #label commands into NG
« Reply #17 on: August 12, 2020, 09:53:16 PM »
NG produces C code and goto is possible in C as far as I know (although all the people using it in higher level languages are looked down upon :-)).

Another matter is that goto is a legacy feature of basic to begin with and was only supported in non-strict mode or the 'hippie mode' in the original Blitzmax and ng is strict by default. I would give it a snowballs chance to ever surface in ng...

The good news is that it is not really needed. Every possible scenario can be done without it. And, I bet you don't even have to write 10 times more code to achieve the same. At the end you might even develop a different way of structuring your code, evolve as a programmer. At least I did..
 
 Anyways, good luck.

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Offline wadmixfm

  • Jr. Member
  • **
  • Posts: 64
Re: can we bring back The Goto and #label commands into NG
« Reply #18 on: August 13, 2020, 12:33:37 PM »
thanks for the great replies

so here is what i am looking for

for example :-

repeat

If KeyHit(KEY_1) Then goto drumplay
If KeyHit(KEY_2) Then goto kited
If KeyHit(KEY_3) Then goto loadnsave
If KeyHit(KEY_4) Then goto quit

until


#drumplay

..... code here


#kited

........ code here


#loadnsave

........ code here



#quit

end program code here

if there is an alternative to this then i will be very happy

lee

sorry to cause issues


ps the me was a variable

if i = 500 then me = 500

lee

its not part of my original code i just threw that in





Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 2862
  • elgol 2021
Re: can we bring back The Goto and #label commands into NG
« Reply #19 on: August 13, 2020, 12:39:39 PM »
Just replace labels with functions:

Code: [Select]

If KeyHit(KEY_1) Then drumplay()

...

function drumplay()

... code here

end function

Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 400, Pi4, C64, ZX Spectrum Next.

Offline wadmixfm

  • Jr. Member
  • **
  • Posts: 64
Re: can we bring back The Goto and #label commands into NG
« Reply #20 on: August 13, 2020, 12:46:04 PM »
yeah i tried that

can you call another place from that function ???

can you add a function in a function ??


Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 2862
  • elgol 2021
Re: can we bring back The Goto and #label commands into NG
« Reply #21 on: August 13, 2020, 12:50:42 PM »
Quote
yeah i tried that

can you call another place from that function ???

can you add a function in a function ??

You tried that and what?  It didn't work??  It should.

Yes, you can call other functions from a function.

Why would you add a function to a function??  Just call several functions from a function if you want.
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 400, Pi4, C64, ZX Spectrum Next.

Offline wadmixfm

  • Jr. Member
  • **
  • Posts: 64
Re: can we bring back The Goto and #label commands into NG
« Reply #22 on: August 13, 2020, 12:57:19 PM »
tried the code you sent

when calling a function do you address it as

program()

or just

program

for example


Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 2862
  • elgol 2021
Re: can we bring back The Goto and #label commands into NG
« Reply #23 on: August 13, 2020, 01:06:25 PM »
You would use program() because you pass in data through variables within the brackets.  No data is required if you're just emulating a label therefore just use ().  But if you wanted to send say, x and y values to a function you could use program( x, y ).  Now BlitzMax needs to know what kind of data type it is, so when you define a function with integer (whole numbers) as an example you would say:

Code: [Select]
x = 10
y = 20

program( x, y )

...

Function program( x:Int, y:Int )

... process x and y here

End Function


Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 400, Pi4, C64, ZX Spectrum Next.

Offline wadmixfm

  • Jr. Member
  • **
  • Posts: 64
Re: can we bring back The Goto and #label commands into NG
« Reply #24 on: August 13, 2020, 01:27:35 PM »
' how is this , it works but is this the right way to do it :)
'new specdrum 2
Import BaH.RtMidi
Import BRL.StandardIO
Import brl.D3D9Max2D
Import brl.Max2D
Import BRL.Retro
Import BRL.Random
Import BRL.Math
Import BRL.LinkedList
Import MaxGui.Drivers
Const GFX_WIDTH = 1024, GFX_HEIGHT = 768, BIT_DEPTH = 0, HERTZ = 75
AppTitle ="SpecDrum2 64bit Version "
'Center the screen on startup
SetGraphicsDriver GLMax2DDriver()
Graphics GFX_WIDTH, GFX_HEIGHT, BIT_DEPTH, HERTZ


Function begin()
Local hiya
hiya=0

Repeat
Cls

DrawText"Press 1 or 2 or 3 to quit",20,40
If KeyHit(KEY_1) Then test1
If KeyHit(KEY_2) Then test2
If KeyHit(KEY_3) Then End
Flip
Until hiya=1
End Function


Function test1()
Cls
DrawText "you have reached test 1",60,60
Flip
Delay 1500
begin()
End Function


Function test2()
Cls
DrawText "you have reached test 2",60,60
Flip
Delay 1500
begin()
End Function

begin()  ' this is the main call of the functions

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 2862
  • elgol 2021
Re: can we bring back The Goto and #label commands into NG
« Reply #25 on: August 13, 2020, 02:19:07 PM »
Well it works is a good thing!   :D

I would re-write it a bit differently, but it works is the main thing.  Maybe somebody will jump in here as I don't have time right now.
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry Pi 400, Pi4, C64, ZX Spectrum Next.

Offline GfK

  • Full Member
  • ***
  • Posts: 161
Re: can we bring back The Goto and #label commands into NG
« Reply #26 on: August 13, 2020, 03:20:04 PM »
That code 'as-is' would eventually cause a stack overflow, since you're repeatedly calling your begin() function, which calls test1()/test2(), which both call the begin() function, which calls test1()/test2() and so on ad infinitum.  Fair do's, in the scale of things it would take a long time for that to happen, but an ever-growing stack trace is never good.

Losing the calls to begin() from within test1() and test2() will fix that - it's not needed, since code execution will return to where it left off in the begin() function anyway, once test1/2 have completed (at which point they're removed from the stack buffer).
Intel I9-9900K 3.6-5.0GHz | GeForce RTX2070 8GB | 32GB RAM | 500GB NVMe M.2 SSD | 1TB HDD | Windows 10 x64.
MSI Apache Pro | I7-7700HQ | GeForce GTX1060 3GB | 8GB RAM | 128GB SSD | 1TB HDD | Windows 10 x64.

Offline wadmixfm

  • Jr. Member
  • **
  • Posts: 64
Re: can we bring back The Goto and #label commands into NG
« Reply #27 on: August 13, 2020, 03:23:32 PM »
ahhhhhh i see

so once its called and ended it will have to be called again if i want to go back to that part of code

right ?


Offline GfK

  • Full Member
  • ***
  • Posts: 161
Re: can we bring back The Goto and #label commands into NG
« Reply #28 on: August 13, 2020, 03:47:40 PM »
Right.

Every time you call a function, your code has to "remember" where it was called from, so it can return to it later.  Once the function has finished executing, code execution goes back to that point and it "forgets" the thing it remembered as it's not needed any more.  In laymen's terms, that's what the stack buffer does.

In your code, neither of your test functions were ever completed (by reaching the end of the function), since you were calling the begin() function before that happened.  As a result the stack buffer would just keep growing and growing, filling up with pointers to begin(), test1/2(), begin(), test1/2() and so on.

Not sure if BlitzMax has any way of looking at the stack trace.  If it does, I don't remember it.  But (having used Unity for years now) it's a handy thing to have access to as you can see the exact path your code has taken to get to where it is.  It's super-useful in debugging.

Intel I9-9900K 3.6-5.0GHz | GeForce RTX2070 8GB | 32GB RAM | 500GB NVMe M.2 SSD | 1TB HDD | Windows 10 x64.
MSI Apache Pro | I7-7700HQ | GeForce GTX1060 3GB | 8GB RAM | 128GB SSD | 1TB HDD | Windows 10 x64.

Offline TomToad

  • Hero Member
  • *****
  • Posts: 517
Re: can we bring back The Goto and #label commands into NG
« Reply #29 on: August 13, 2020, 05:11:11 PM »
There is a stack trace of sorts when using the debugger.  As you step through a program, line by line, the function call stack will be in the "debug" tab on the right.
------------------------------------------------
8 rabbits equals 1 rabbyte.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal