Author Topic: Buggy code, needs help  (Read 3540 times)

0 Members and 1 Guest are viewing this topic.

Offline Larryrl

  • Newbie
  • Posts: 26
    • View Profile
Buggy code, needs help
« on: December 02, 2021, 01:20:00 pm »
I have a program called switch which is going to be a programming language of an esoteric nature. I hope to make it worl like a subset of basic, if I can fix this ghost bug. The code is as follows:

Code: QB64: [Select]
  1. 100 ' This idea uses switches that have 3 states either
  2. 110 ' up (a number), down (another number), or off(middle position)
  3. 120 '
  4. 130 ' This programming language does not use a tape, or an array
  5. 140 ' of cells. Instead, it uses an internal array of switches.
  6. 150 '
  7. 160 ' the first switch is #0 which represents the number 0
  8. 170 ' the second switch is is #1 which represents either a 1 or 3
  9. 180 ' the third switch is is #2 which represents either a 2 or 4
  10. 190 ' the fourth switch is is #3 which represents either a 5 or 7
  11. 200 ' the fifth switch is is #4 which represents either a 6 or 8
  12. 210 ' the sixth switch is is #5 which represents either a 9 or 11
  13. 220 ' the seventh switch is is #6 which represents either a 10 or 12
  14. 230 ' the eighth switch is is #7 which represents either a 13 or 15
  15. 250 '
  16. 260 '
  17. 270 ' there is no 14 so the programmer would have to use math using
  18. 280 ' various numbers to get to 14, 7+7, 15-1, 28/2, 7*2 etc
  19. 290 '
  20. 300 ' In addition to these switches, there are 4 special memory areas
  21. 310 ' which are used to hold values resluting from addition, subtraction,
  22. 320 ' multiplication or division. They can be used temporarily and erased.
  23. 330 '
  24. 340 ' There is a pointer which points to a numbered switch from 0 to 8
  25. 350 ' all math functions are done by moving the pointer between the switches
  26. 360 ' to point to a specific numbered switch. Then you simply store the number
  27. 370 ' represented by the position of the numbered switch whether up down. Switches in the
  28. 380 ' middle or off position do not contain a number.  By adding, subtracting, multiplying
  29. 390 ' or dividing numbers from switches, you arrive at a value that can then be used for
  30. 400 ' output or other things.
  31. 410 '
  32. 420 ' The syntax of switch resembles assembly language.
  33. 430 '
  34. 440 ' mu or MU moves the pointer up one position. If at 1, it will wrap around back to 7
  35. 450 ' md or MD moves the pointer down one position. If at 7, it will wrap around to 1
  36. 460 ' a or A starts the addition process.
  37. 470 ' s or S starts the subtraction process.
  38. 480 ' m or M starts the multiplication process.
  39. 490 ' d or D starts the division process.
  40. 500 ' ea or EA stops the addition process and wipes the result from memory
  41. 510 ' es or ES stops the subtraction process and wipes the result from memory
  42. 520 ' em or EM stops the multiplication process and wipes the result from memory
  43. 530 ' ed or ED stops the division process and wipes the result from memory
  44. 540 ' oa or OA will output the value in the addition memory.
  45. 550 ' os or OS will output the value in the subtraction memory.
  46. 560 ' om or OM will output the value in the multiplication memory.
  47. 570 ' od or OD will output the value in the division memory.
  48. 580 ' on or ON will output a new line.
  49. 590 '
  50. 600 ' Each consecutive a,A,s,S,m,M,d or D command continues the math process.
  51. 610 ' a4 adds 4 to the position in memory another a4 makes the value 8
  52. 620 ' It works like this so long as you do not use any ea,EA,es,ES,em,EM,ed or ED
  53. 630 ' commands. If you have a4, then ea followed by another a4, then the result will
  54. 640 ' be 4 because you will have erased the first 4.
  55. 650 '
  56. 660 ' You can output ascii characters by creating totals that equal to the ascii
  57. 670 ' decimal value of the letter to be outputted. If you want a to output a
  58. 680 ' blank line, you must use the on or ON command.
  59. 690 '
  60. 700 ' gi or GI tells the interpreter to get input from the user.
  61. 710 ' If you want the input to be specific, then you can use the
  62. 720 ' math and output commands to ask the user a question.  
  63. 730 '
  64. 740 ' In addition to the memory storage place for the 4 different math functions,
  65. 750 ' there are four more that can store data. These can be used as variables,
  66. 760 ' however they do not get a name. You can word with them using the following:
  67. 770 '
  68. 780 ' s0a or S0A stores the value of "a" in storage unit 0
  69. 781 ' s0s or S0S stores the value of "s" in storage unit 0
  70. 782 ' s0m or S0M stores the value of "m" in storage unit 0
  71. 783 ' s0d or S0D stores the value of "d" in storage unit 0
  72. 784 ' s1a or S1A stores the value of "a" in storage unit 1
  73. 785 ' s1s or S1S stores the value of "s" in storage unit 1
  74. 786 ' s1m or S1M stores the value of "m" in storage unit 1
  75. 787 ' s1d or S1D stores the value of "d" in storage unit 1
  76. 788 ' s2a or S2A stores the value of "a" in storage unit 2
  77. 789 ' s2s or S2S stores the value of "s" in storage unit 2
  78. 790 ' s2m or S2M stores the value of "m" in storage unit 2
  79. 791 ' s2d or S2D stores the value of "d" in storage unit 2
  80. 792 ' s3a or S3A stores the value of "a" in storage unit 3
  81. 793 ' s3s or S3S stores the value of "s" in storage unit 3
  82. 794 ' s3m or S3M stores the value of "m" in storage unit 3
  83. 795 ' s3d or S3D stores the value of "d" in storage unit 3
  84. 800 '
  85. 830 ' This allows for certain games to be created. Simple though they may be,
  86. 840 ' they are still fun. They are considered simple in that they work with
  87. 850 ' the 4 storage units that are available.
  88. 860 '
  89. 870 ' fu or FU flips the switch at the pointer location to up
  90. 871 ' fd or FD flips the switch at the pointer location to down
  91. 872 ' fo or FO turns on the switch at the pointer location
  92. 875 ' fx or FX turns off the switch at the pointer location
  93. 876 '
  94. 880 ' Begin the inteerpreter
  95. 890 '
  96. 900 '_fullscreen
  97. 910 dim shared switch$(7,3),x,tlns,lnctr,a,s,m,d,u0,u1,u2,u3,p$(20000),ptr
  98. 915 lnctr=1:ptr=0
  99. 920 switch$(0,1)="0":switch$(0,2)="0":switch$(0,3)="up"
  100. 921 switch$(1,1)="1":switch$(1,2)="3":switch$(1,3)="up"
  101. 922 switch$(2,1)="2":switch$(2,2)="4":switch$(2,3)="up"
  102. 923 switch$(3,1)="5":switch$(3,2)="7":switch$(3,3)="up"
  103. 924 switch$(4,1)="6":switch$(4,2)="8":switch$(4,3)="up"
  104. 925 switch$(5,1)="9":switch$(5,2)="11":switch$(5,3)="up"
  105. 926 switch$(6,1)="10":switch$(6,2)="12":switch$(6,3)="up"
  106. 927 switch$(7,1)="13":switch$(7,2)="15":switch$(7,3)="up"
  107. 930 cls
  108. 940 ' Open a file to run
  109. 950 filename$=command$
  110. 960 if filename$="" then filename$="\switch\hello world.switch"
  111. 970 x=0
  112. 980 open filename$ for input as #1
  113. 990 while not eof(1)
  114. 1000 x=x+1
  115. 1010 line input #1,p$(x)
  116. 1020 wend
  117. 1030 close #1
  118. 1040 tlns=x
  119. 1050 '
  120. 1060 ' Parse commands
  121. 1070 if p$(lnctr)="mu" then gosub 8010:lnctr=lnctr+1:goto 1060
  122. 1080 if p$(lnctr)="md" then gosub 8040:lnctr=lnctr+1:goto 1060
  123. 1090 if p$(lnctr)="a"  then gosub 8070:lnctr=lnctr+1:goto 1060
  124. 1100 if p$(lnctr)="s"  then gosub 8110 :lnctr=lnctr+1:goto 1060
  125. 1110 if p$(lnctr)="m"  then gosub 8150:lnctr=lnctr+1:goto 1060
  126. 1120 if p$(lnctr)="d"  then gosub 8190 :lnctr=lnctr+1:goto 1060
  127. 1130 if p$(lnctr)="ea" then gosub 8240 :lnctr=lnctr+1:goto 1060
  128. 1140 if p$(lnctr)="es" then gosub 8270:lnctr=lnctr+1:goto 1060
  129. 1150 if p$(lnctr)="em" then gosub 8290:lnctr=lnctr+1:goto 1060
  130. 1160 if p$(lnctr)="ed" then gosub 8330:lnctr=lnctr+1:goto 1060
  131. 1170 if p$(lnctr)="oa" then gosub 8360:lnctr=lnctr+1:goto 1060
  132. 1180 if p$(lnctr)="os" then gosub 8390:lnctr=lnctr+1:goto 1060
  133. 1190 if p$(lnctr)="om" then gosub 8420:lnctr=lnctr+1:goto 1060
  134. 1200 if p$(lnctr)="od" then gosub 8450:lnctr=lnctr+1:goto 1060
  135. 1210 if p$(lnctr)="on" then gosub 8480:lnctr=lnctr+1:goto 1060
  136. 1211 if p$(lnctr)="oca" then gosub 9090:lnctr=lnctr+1:goto 1060
  137. 1212 if p$(lnctr)="ocs" then gosub 9110:lnctr=lnctr+1:goto 1060
  138. 1213 if p$(lnctr)="ocm" then gosub 9130:lnctr=lnctr+1:goto 1060
  139. 1214 if p$(lnctr)="ocd" then gosub 9150:lnctr=lnctr+1:goto 1060
  140. 1220 if p$(lnctr)="s0a" then gosub 8510:lnctr=lnctr+1:goto 1060
  141. 1221 if p$(lnctr)="s0s" then gosub 8540:lnctr=lnctr+1:goto 1060
  142. 1222 if p$(lnctr)="s0m" then gosub 8570:lnctr=lnctr+1:goto 1060
  143. 1223 if p$(lnctr)="s0d" then gosub 8600:lnctr=lnctr+1:goto 1060
  144. 1224 if p$(lnctr)="s1a" then gosub 8630:lnctr=lnctr+1:goto 1060
  145. 1225 if p$(lnctr)="s1s" then gosub 8660:lnctr=lnctr+1:goto 1060
  146. 1226 if p$(lnctr)="s1m" then gosub 8690:lnctr=lnctr+1:goto 1060
  147. 1227 if p$(lnctr)="s1d" then gosub 8720:lnctr=lnctr+1:goto 1060
  148. 1228 if p$(lnctr)="s2a" then gosub 8750:lnctr=lnctr+1:goto 1060
  149. 1229 if p$(lnctr)="s2s" then gosub 8780:lnctr=lnctr+1:goto 1060
  150. 1230 if p$(lnctr)="s2m" then gosub 8810:lnctr=lnctr+1:goto 1060
  151. 1231 if p$(lnctr)="s2d" then gosub 8840:lnctr=lnctr+1:goto 1060
  152. 1232 if p$(lnctr)="s3a" then gosub 8870:lnctr=lnctr+1:goto 1060
  153. 1233 if p$(lnctr)="s3s" then gosub 8900:lnctr=lnctr+1:goto 1060
  154. 1234 if p$(lnctr)="s3m" then gosub 8930:lnctr=lnctr+1:goto 1060
  155. 1235 if p$(lnctr)="s3d" then gosub 8960:lnctr=lnctr+1:goto 1060
  156. 1260 if p$(lnctr)="//" then lnctr=lnctr+1:goto 1060
  157. 1270 if left$(p$(lnctr),2)="//" then lnctr=lnctr+1:goto 1060
  158. 1280 if left$(p$(lnctr),3)="// " then lnctr=lnctr+1:goto 1060
  159. 1290 if p$(lnctr)="fu" then gosub 8990:lnctr=lnctr+1:goto 1060
  160. 1300 if p$(lnctr)="fd" then gosub 9020:lnctr=lnctr+1:goto 1060
  161. 1330 if p$(lnctr)="ep" then print:print "Program ";filename$;" ended with no errors.":end
  162. 1340 if p$(lnctr)="rp" then ptr=0:lnctr=lnctr+1:goto 1060
  163. 1350 if p$(lnctr)="" then lnctr=lnctr+1:goto 1060
  164. 1360 if p$(lnctr)="oo" then print chr$(32);:lnctr=lnctr+1:goto 1060
  165. 1370 if p$(lnctr)="!" then gosub 9170:lnctr=lnctr+1:goto 1060
  166.  
  167. 7990 print:print "Illegal / unknown command in line ";lnctr:end
  168.  
  169. 8010 if ptr>0 then ptr=ptr-1
  170. 8015 if ptr=0 then ptr=7
  171. 8020 return
  172.  
  173. 8040 if ptr<7 then ptr=ptr+1
  174. 8045 if ptr=7 then ptr=1
  175. 8050 return
  176.  
  177.  
  178. 8070 if switch$(ptr,3)="up" then a=a+val(switch$(ptr,1)) else a=a+val(switch$(ptr,2))
  179. 8090 return
  180.  
  181. 8110 if switch$(ptr,3)="up" then s=s-val(switch$(ptr,1)) else s=s-val(switch$(ptr,2))
  182. 8130 return
  183.  
  184. 8150 if switch$(ptr,3)="up" then m=m*val(switch$(ptr,1)) else m=m*val(switch$(ptr,2))
  185. 8170 return
  186.  
  187. 8190 if switch$(ptr,3)="up" and d>0 and ptr=0 then print:print "Illegal attempt to divide by 0 in line ";lnctr:end
  188. 8210 if switch$(ptr,3)="up" then d=d/val(switch$(ptr,1)) else d=d/val(switch$(ptr,2))
  189. 8220 return
  190.  
  191. 8240 a=0
  192. 8250 return
  193.  
  194. 8270 s=0
  195. 8280 return
  196.  
  197. 8290 m=0
  198. 8310 return
  199.  
  200. 8330 d=0
  201. 8340 return
  202.  
  203. 8360 print ltrim$(str$(a));
  204. 8370 return
  205.  
  206. 8390 print ltrim$(str$(s));
  207. 8400 return
  208.  
  209. 8420 print ltrim$(str$(m));
  210. 8430 return
  211.  
  212. 8450 print ltrim$(str$(d));
  213. 8460 return
  214.  
  215. 8480 print
  216. 8490 return
  217.  
  218. 8510 u0=a
  219. 8520 return
  220.  
  221. 8540 u0=s
  222. 8550 return
  223.  
  224. 8570 u0=m
  225. 8580 return
  226.  
  227. 8600 u0=d
  228. 8610 return
  229.  
  230. 8630 u1=a
  231. 8640 return
  232.  
  233. 8660 u1=s
  234. 8670 return
  235.  
  236. 8690 u1=m
  237. 8700 return
  238.  
  239. 8720 u1=d
  240. 8730 return
  241.  
  242. 8750 u2=a
  243. 8760 return
  244.  
  245. 8780 u2=s
  246. 8790 return
  247.  
  248. 8810 u2=m
  249. 8820 return
  250.  
  251. 8840 u2=d
  252. 8850 return
  253.  
  254. 8870 u3=a
  255. 8880 return
  256.  
  257. 8900 u3=s
  258. 8910 return
  259.  
  260. 8930 u3=m
  261. 8940 return
  262.  
  263. 8960 u3=d
  264. 8970 return
  265.  
  266. 8990 switch$(ptr,3)="up"
  267. 9000 return
  268.  
  269. 9020 switch$(ptr,3)="dn"
  270. 9030 return
  271.  
  272. 9090 print chr$(a);
  273. 9100 return
  274.  
  275. 9110 print chr$(s);
  276. 9120 return
  277.  
  278. 9130 print chr$(m);
  279. 9140 return
  280.  
  281. 9150 print chr$(d);
  282. 9160 return
  283.  
  284. 9170 print:print "The pointer is pointing to switch ";ptr;". Which is in the ";switch$(ptr,3);" Position."
  285. 9190 print "A = ";a;" S = ";s;" M = ";m;" D = ";d:print:return
  286.  
  287.  

This is the switch source code the intyerpreter runs that causes the bug.

Code: QB64: [Select]
  1. // hello world. switch
  2. //
  3. // This is the famous first program hello world written in the interpreted lan guage switch.
  4. // Each command must be on a seperate line.
  5. //
  6. // move the pointer to 2nd switch which is 2 up or 4 down
  7. md
  8. md
  9. // flip the switch to the up position
  10. fu
  11. // add it's value to the memory
  12. a
  13. // move the pointer to the 7th switch which is 10 up or 12 down
  14. md
  15. md
  16. md
  17. md
  18. // flip the switch to the up position
  19. fu
  20. // add the value of that switch 7 times to what is already in the memory
  21. a
  22. a
  23. a
  24. a
  25. a
  26. a
  27. a
  28. // print the character represented by the ascii decimal value
  29. oca
  30. ea
  31. rp
  32. md
  33. md
  34. md
  35. md
  36. md
  37. md
  38. fu
  39. a
  40. a
  41. a
  42. a
  43. a
  44. a
  45. mu
  46. fu
  47. a
  48. oca
  49. rp
  50. ea
  51. md
  52. md
  53. md
  54. md
  55. md
  56. md
  57. a
  58. a
  59. a
  60. a
  61. a
  62. a
  63. a
  64. mu
  65. mu
  66. fu
  67. a
  68. oca
  69. oca
  70. mu
  71. mu
  72. mu
  73. fu
  74. a
  75. a
  76. a
  77. oca
  78. ea
  79. rp
  80. md
  81. md
  82. md
  83. md
  84. md
  85. md
  86. fu
  87. a
  88. mu
  89. mu
  90. mu
  91. mu
  92. mu
  93. fu
  94. a
  95. oca
  96. ea
  97. rp
  98. // down to here it prints hello
  99. //
  100. // this part is here to test to see why it prints HELLO on line 1, then the next thing printed prints over the H, on the same row.
  101. // if you print 4 more things, it will totally write over HELLO, instead of moving over to the next column after the O to print.
  102. // The on command below makes it print a newline, and only then will the next printed character or value print right.
  103. // The problem is, nothing tells it where to print. Except for the on command, they're  all just print commands ending in a semicolon
  104. // which in basic means to stay on the same row and not move to a new row. That is why the overprinting of the already printed
  105. // positions is so odd. The oo command is supposed to print a space, but if  it comes after the HELLO, then it also writes over the
  106. // already printed text.    
  107. md
  108. fu
  109. a
  110. oca
  111. ea
  112. rp
  113. // end program
  114. ep

Run the interpreter and it will automatically load this hello world source code. Run it as is, then run it without the on command near the end, and see what happens. As long as the "on", command for a newline is present it works ok, but if I do not want a newline, and I want the smileyface chr$(1) to show up after the hello, it prints over the H, that was already printed. I have no locate commands anywhere. I only print with ; after, or just print for the newline. I need to find out what is making it revert back to column 1 since the qb64 compiler gives no errors, and it runs. 

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Buggy code, needs help
« Reply #1 on: December 02, 2021, 03:48:11 pm »
Check this out for Chr$(1)

http://qb64.org/wiki/CONTROLCHR

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Buggy code, needs help
« Reply #2 on: December 02, 2021, 04:45:51 pm »
Yeah it fixes your problem but _ControlChr OFF has made another symbol appear:
  [ You are not allowed to view this attachment ]  

Offline Larryrl

  • Newbie
  • Posts: 26
    • View Profile
Re: Buggy code, needs help
« Reply #3 on: December 02, 2021, 06:04:16 pm »
Yes I see the extra character. It's interesting. I'm still trying to figure out regardless of the control charactr or it's qb64 command, what causes it to go back to the left edge of the screen. It's acting like the screen only has 5 colums total.

Offline Larryrl

  • Newbie
  • Posts: 26
    • View Profile
Re: Buggy code, needs help
« Reply #4 on: December 02, 2021, 06:08:48 pm »
Check this out for Chr$(1)

http://qb64.org/wiki/CONTROLCHR

The contreol character I do not feel is the issue. Thank you for your help, and will keep that command in mind for when I might need it. I used it now and it showed the other character before the smiley. The problem is nothing I print to the screen after the hello prints except from the left side of the screen again. Nothing will print after the hello. Itg won't even print a space. I can print a newline, and then it will print on the next line.   

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Buggy code, needs help
« Reply #5 on: December 02, 2021, 06:37:49 pm »
@Larryrl

It looks like a Chr$(11) is being snuck into your code program or interpreter:
Press spacebar when you see "ZZZ" the first Z gets replaced by "X" when the next item is printed, same thing you were getting when ON removed from interpreted program.

Code: QB64: [Select]
  1. Print "ZZZ";
  2. Print Chr$(11);
  3. Print "X";
  4.  
  5.  
  6.  

The _ControlChr OFF command turned off that effect on Chr$(11), allot of Control Chars mess with printing that's why there is this command. Chr$(9) prints a tab, Chr$(13) a Carriage Return... (next line) like Chr$(10).

Offline Larryrl

  • Newbie
  • Posts: 26
    • View Profile
Re: Buggy code, needs help
« Reply #6 on: December 02, 2021, 07:25:29 pm »
Tried that, but it still does not explain what causes it to start back over at column 1 when after each print statememt I have the ;

Offline Larryrl

  • Newbie
  • Posts: 26
    • View Profile
Re: Buggy code, needs help
« Reply #7 on: December 02, 2021, 07:47:03 pm »
Fixed my own problem. The math generated by the source code hello world.switch, was adding up to 11 and causing a control character to go nuts.  It's fixed now.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Buggy code, needs help
« Reply #8 on: December 02, 2021, 08:55:36 pm »
Fixed my own problem. The math generated by the source code hello world.switch, was adding up to 11 and causing a control character to go nuts.  It's fixed now.

Just what I said in Reply #5 ;-))
Quote
It looks like a Chr$(11) is being snuck into your code program or interpreter: