Petr Schreiber
22-10-2011, 14:12
I recently got into situation I wanted to write some code on my calculator, the problem is it does not support FOR/NEXT, only labels and jumps (it is very low level).
As the problem required 3 nested loops, this gets quite ugly to maintain.
The solution to this problem was to use power of ThinBASIC Tokenizer module, and automate the generation of FOR/NEXT equivalent code.
So as the input goes this:
FOR I = 1 TO 5
FOR J = 1 TO 10
FOR K = 1 TO 15
NEXT K
NEXT J
NEXT I
And the produced output is the following calculator program template (LBL = label, GTO = jump to label, STO = store value from stack to variable, RCL = get value from variable to stack, X<=Y? = compare stack values for X and Y register, and if the condition is valid perform next line, otherwise skip next line):
LBL 00
1
STO I
LBL 01
RCL I
5
X<=Y?
GTO 02
<Place FOR/NEXT body code here>
LBL 03
1
STO J
LBL 04
RCL J
10
X<=Y?
GTO 05
<Place FOR/NEXT body code here>
LBL 06
1
STO K
LBL 07
RCL K
15
X<=Y?
GTO 08
<Place FOR/NEXT body code here>
1
STO+ K
GTO 07
LBL 08
1
STO+ J
GTO 04
LBL 05
1
STO+ I
GTO 01
LBL 02
I realize this program is not of particular use for people without legacy HP calculator, but the point is that the Tokenizer module is very well suited for such a conversions. I would probably go crazy inventing that calculator code from the scratch, with script help it is very comfortable.
I attach the commented code for possible study.
Petr
As the problem required 3 nested loops, this gets quite ugly to maintain.
The solution to this problem was to use power of ThinBASIC Tokenizer module, and automate the generation of FOR/NEXT equivalent code.
So as the input goes this:
FOR I = 1 TO 5
FOR J = 1 TO 10
FOR K = 1 TO 15
NEXT K
NEXT J
NEXT I
And the produced output is the following calculator program template (LBL = label, GTO = jump to label, STO = store value from stack to variable, RCL = get value from variable to stack, X<=Y? = compare stack values for X and Y register, and if the condition is valid perform next line, otherwise skip next line):
LBL 00
1
STO I
LBL 01
RCL I
5
X<=Y?
GTO 02
<Place FOR/NEXT body code here>
LBL 03
1
STO J
LBL 04
RCL J
10
X<=Y?
GTO 05
<Place FOR/NEXT body code here>
LBL 06
1
STO K
LBL 07
RCL K
15
X<=Y?
GTO 08
<Place FOR/NEXT body code here>
1
STO+ K
GTO 07
LBL 08
1
STO+ J
GTO 04
LBL 05
1
STO+ I
GTO 01
LBL 02
I realize this program is not of particular use for people without legacy HP calculator, but the point is that the Tokenizer module is very well suited for such a conversions. I would probably go crazy inventing that calculator code from the scratch, with script help it is very comfortable.
I attach the commented code for possible study.
Petr