Jump to content
  • 0

BASIC Transpiler: pseudo-procedures


rje
 Share

Question

So say a procedure looks something like this:

SUB PROCEDURE_NAME( \ARG1, \ARG2$, \ARG3% )
  LOCAL \MYVAR$
  ; A LOCAL IS A TEMPORARY VAR.
  ; IT WOULD NOT BLOW AWAY AN EXISTING VAR.
  ; DO STUFF WITH ARGS
  ; YOU CAN ALSO WORK WITH OTHER GLOBAL VARS
  \MYVAR$ = ARG2$ + " OUCH!"
  \ARG1=\ARG1+1
  CALL ANOTHER_PROCEDURE( \ARG2$, \MYVAR$ )
RETURN

The REAL challenge here is handling variables.  So here's my house rules:

  1. You may use CALL inside of a PROC, but it will trample your parameters and local vars.
  2. Therefore, the only safe place to call another proc is at the end (tail call).
  3. I designate some number of variables for locals and parametrics.  Say, 36.  Go over that and the transpiler dies in agony.

So the above code transpiles down to

REM SUB PROCEDURE_NAME(ZA, ZB$, ZC%)
REM LOCAL ZD$
ZD$=ZB$+" OUCH!"
ZA=ZA+1
REM CALL ANOTHER_PROCEDURE(ZA$,ZB$)
ZA$=ZB$ :ZB$=ZD$ :GOSUB 9000
RETURN

Okay, that might work.

The problem NOW is how to modify global variables.  I think the best way is to add a notation that says "This is the ACTUAL variable name.  Don't use a temporary name!"  Like a splat (*).  

 

Edited by rje
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Let me use a REAL routine I'd like to make a subroutine.  Here's (essentially) the BASIC 2 version, sans line numbers:

REM
REM distance(C1,R1,C2,R2) -> D%
REM
A1 = R1 + INT(C1/2)
A2 = R2 + INT(C2/2)
D1% = ABS(A1-A2)
D2% = ABS(C1-C2)
D% = ABS( A1 - A2 - C1 + C2 )
IF (D1% >= D2%) AND (D1% >= D%) THEN D% = D1%
IF (D2% >= D1%) AND (D2% >= D%) THEN D% = D2%
RETURN

Converting to a structured, PROC format:

SUB DISTANCE( \COL1, \ROW1, \COL2, \ROW2, \*DISTANCE )
  LOCAL \A1
  LOCAL \A2
  LOCAL \D1
  LOCAL \D2

  \A1 = \ROW1 + INT(\COL1/2)
  \A2 = \ROW2 + INT(\COL2/2)
  \D1% = ABS(\A1-\A2)
  \D2% = ABS(\COL1-\COL2)
  \DISTANCE = ABS(\A1-\A2-\COL1+\COL2)

  IF (\D1% >= \D2%) AND (\D1% >= \DISTANCE) THEN \DISTANCE = \D1%
  IF (\D2% >= \D1%) AND (\D2% >= \DISTANCE) THEN \DISTANCE = \D2%
RETURN

The asterisk in \*DISTANCE means not to create a temporary variable, but to use the actual one named here.

Hmmm, have to think about that.

That would transpile to:

REM SUB DISTANCE(ZA,ZB,ZC,ZD,Distance)
REM LOCAL ZE,ZF,ZG,ZH
ZE=ZB+INT(ZA/2)
ZF=ZD+INT(ZC/2)
ZG%=ABS(ZE-ZF)
ZH%=ABS(ZA-ZC)
Distance=ABS(ZE-ZF-ZA+ZC)
IF (ZG% >= ZH%) AND (ZG% >= Distance) THEN Distance = ZG%
IF (ZH% >= ZG%) AND (ZH% >= Distance) THEN Distance = ZH%
RETURN

Where "Distance" is whatever variable I'm using to store distance globally...

 

Why is this good?

This is good because it establishes a set of temporary variables that I can re-use in a way that's readable in the source.

Edited by rje
Link to comment
Share on other sites

  • 0
2 hours ago, rje said:

Let me use a REAL routine I'd like to makThe asterisk in \*DISTANCE means not to create a temporary variable, but to use the actual one named here.

Hmmm, have to think about that.

 

History repeating itself?  You're using the asterisk to declare that you're passing by reference instead of the default of passing by value.  About 50 years ago the C guys decided to pass by value by default, and to pass by reference they'd use pointers denoted by asterisk.  

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

Please review our Terms of Use