Oxygen Basic

Programming => Problems & Solutions => Topic started by: José Roca on September 29, 2018, 07:16:06 AM

Title: Gosub.02bas error
Post by: José Roca on September 29, 2018, 07:16:06 AM
This example fails with an error: "Unidentified instruction when".

Code: [Select]
'% filename "t.exe"
'uses rtl64


function f()
  int a=42
  int b

  'gosub g
  gosub g when a>0
  print b
  return
  '
  g:
  b=a/2
  ret
  '
end function

f
Title: Re: Gosub.02bas error
Post by: Aurel on September 29, 2018, 07:37:32 AM
Jose
as you know GOSUB is keyword so there is no WHEN

gosub to g... ?
g - should be SUBROUTINE or FUNCTION... ok  ;)
Title: Re: Gosub.02bas error
Post by: José Roca on September 29, 2018, 08:33:59 AM
The program is not mine. It is one of the examples that comes with the compiler and I'm just pointing that it fails.

> gosub to g... ?
> g - should be SUBROUTINE or FUNCTION... ok  ;)

No, it should be a label.
Title: Re: Gosub.02bas error
Post by: Charles Pegge on September 29, 2018, 08:37:18 AM
Will be fixed.

if, when, while, until are  currently supported for goto and gosub.
Title: Re: Gosub.02bas error
Post by: José Roca on September 29, 2018, 08:48:16 AM
> if, when, while, until are  currently supported for goto and gosub.

What do you mean?
Title: Re: Gosub.02bas error
Post by: Charles Pegge on September 29, 2018, 08:54:49 AM
In this context, when and while are the same as if.

Until is the same as if not ...

This is experimental syntax :)
Title: Re: Gosub.02bas error
Post by: Aurel on September 29, 2018, 10:09:34 AM
Well ... i don't know that we have WHEN in o2  ::)
and i never use gosub to jump to label only to call function
also i never use until too
Title: Re: Gosub.02bas error
Post by: José Roca on September 29, 2018, 10:21:10 AM
Do you use Gosub  to call a Function? How?
Title: Re: Gosub.02bas error
Post by: JRS on September 29, 2018, 10:32:55 AM
SB allows doing a GOSUB within a FUNCTION/SUB to a LABEL but using GOSUB/GOTO to reference a function sounds non-BASIC to me.
Title: Re: Gosub.02bas error
Post by: Charles Pegge on September 29, 2018, 11:27:02 AM
Yes, it is possible to use gosub to call a function that has no parameters, though you would not normally do this.
Title: Re: Gosub.02bas error
Post by: Aurel on September 29, 2018, 01:23:55 PM
Yes ..like this:

'gosub calls
declare sub hey()

gosub hey

sub hey
print "Hey Oxygen"
return
end sub


Of course I use it very rare
Title: Re: Gosub.02bas error
Post by: José Roca on September 29, 2018, 01:40:07 PM
Doesn't look worthwile.
Title: Re: Gosub.02bas error
Post by: JRS on September 29, 2018, 02:14:05 PM
Quote
Doesn't look worthwile.

+1

BASIC is diluted enough. We don't need core syntax doing tricks.
Title: Re: Gosub.02bas error
Post by: Mike Lobanovsky on September 29, 2018, 02:28:46 PM
I'm almost sure that's kind of an (almost) unnoticed side effect left over from the early stages of syntax experimentation; a transition from gosub to genuine subprocedures. Used to have some such in FBSL BASIC myself. :D
Title: Re: Gosub.02bas error
Post by: Charles Pegge on September 29, 2018, 09:31:30 PM
But There is a problem in 64bit mode. You will lose 16byte stack pointer alignment when making a gosub to a procedure. Therefore gosubs should only be used to call a subroutine, or piece of asm.

PS:

I can remove this vulnerability to stack-pointer misalignment by adding one instruction to the procedure's internal prolog. It rounds the stack pointer down to the nearest 16-byte boundary:

and rsp, -16

This will cost about half a nanosecond per call :)
Title: Re: Gosub.02bas error
Post by: AlyssonR on September 30, 2018, 12:56:39 AM
I'm almost sure that's kind of an (almost) unnoticed side effect left over from the early stages of syntax experimentation; a transition from gosub to genuine subprocedures. Used to have some such in FBSL BASIC myself. :D

GOSUB was *the* thing when a function definition was still a single statement.

Jumping into a SUB or a FUNCTION just seems like a recipe for disaster to me, but what do I know - if it works  for you, why not abuse the syntax all you want?
Title: Re: Gosub.02bas error
Post by: Charles Pegge on September 30, 2018, 01:55:55 AM

I'm keeping gosub because (when used internally) all the caller's local/static variables remain visible. I really missed it in FreeBasic.
Title: Re: Gosub.02bas error
Post by: Mike Lobanovsky on September 30, 2018, 09:34:50 AM
FBSL BASIC allowed functions within functions (sort of "closures"). The "parent" function's locals and statics were accessible from its "children". The "children" didn't have access to each other's locals or statics but could call other "children" of the same "parent" and all higher-level procedures. The "children" were not visible from outside their "parent". This hierarchy was a by-product of FBSL implementation of classes and namespaces.
Title: Re: Gosub.02bas error
Post by: Charles Pegge on September 30, 2018, 12:09:01 PM
A scope block in subroutines will enable them to deploy their own private locals and statics.
Title: Re: Gosub.02bas error
Post by: JRS on September 30, 2018, 01:31:28 PM
I didn'r realize you guys were talking about a FUNCTION definition within a FUNCTION definition. This is the best SB can do with FUNCTION/MODULE scope.

Code: Script BASIC
  1. MODULE Class
  2.  
  3. a = 1
  4.  
  5. FUNCTION One(a)
  6.   CALL Two(a)
  7.   a += 1
  8.   One = a
  9. END FUNCTION
  10.  
  11. SUB Two(a)
  12.   PRINT a, "\n"
  13. END SUB
  14.  
  15. END MODULE
  16.  
  17.  
  18. PRINT a, "\n"
  19. PRINT Class::a, "\n"
  20. PRINT Class::One(2), "\n"
  21.  


jrs@jrs-laptop:~/sb$ scriba functest.sb
undef
1
2
3
jrs@jrs-laptop:~/sb$


FYI:  Although the use of a MODULE prefix isn't required, main is the start MODULE.

Code: Script BASIC
  1. main::a = 1
  2.  

How one might do a direct assignment to the main MODULE global variable from another MODULE.
Title: Re: Gosub.02bas error
Post by: JRS on September 30, 2018, 05:31:49 PM

I'm keeping gosub because (when used internally) all the caller's local/static variables remain visible. I really missed it in FreeBasic.

We should have a cool icon for the docs that represents Charle's special language features.
Title: Re: Gosub.02bas error
Post by: Mike Lobanovsky on October 01, 2018, 01:13:33 AM
Code: Script BASIC
  1. MODULE Class
  2. ....

This basically looks like a NAMESPACE whereby you can call its member functions through the :: notation from the outside of that namespace.

The "functions within a function" ("closures") cannot be called from outside their parent function, only from the parent function code and from their siblings within that parent. The parent's own local and static variables are accessible to all its children down to the deepest nested level ("closures" can also be infinitely nested becoming "parents" to their own "children") but not vice versa.

That was the first step towards true encapsulation that already allowed the portions of app code to have neat formatting based on the parent/children's functional purpose.

The gosubs' host and portions of code all share common variables that aren't isolated from one another in any way and cannot therefore reuse the variables' names for their own "local" purposes in a gosub. ...

[ADDED] ... unless the language has a SCOPE directive, like Charles has noted above, to arbitrarily limit the visibility of variables to within the scope bounds.
Title: Re: Gosub.02bas error
Post by: Aurel on October 01, 2018, 09:21:24 AM
I don't want to dive into the deep explanation
but GOSUB is good in first place on the begining of program like i use it
with gloabal scope..

GOSUB InitProgram
GOSUB InitData
...

Main program
....
....
...
....

SUB Initprogram
---
END SUB
SUB InitData
----
END SUB