Friday, December 6, 2013

Petit Computer Journal #28

Petit BrainF

Yeah, I was wondering just exactly how hard it is to implement a language that has only 8 operators involved. Not hard at all! Of course, I cheated and used graphic screens for my memory. It'll be probably a bit more complicated if I use straight memory. A bit.

The program cannot fit into one screen, but it's certainly a candidate for less than 100 lines program. It took me a couple hours because I was careless in moving the pointers.

The CYCLE subroutine is there to keep the variables within range, and the FB subroutine is there as a debugger that I use when debugging the program. So, I commented it out in the final version.

Another person, IAmAPersson (what a name!) was working on a version of the compiler as well. Check Petit Wikia for it. Maybe he posted it there by now.

I don't know the policy of this blogspot, but I know my policy and that is family friendly. I don't want to use the full name of the program. Thank you. Next time you designed a programming language, make sure you name it proper!



  1. REM PBF BY HARRY HARDJONO
  2. @INIT
  3. ACLS:CLEAR
  4. GPAGE 0:GCLS
  5. GPAGE 1:GCLS
  6. MS=30000
  7. PTR=0
  8. PC=0
  9. MPC=0
  10. CSR=0

  11. GPAGE1:RESTORE @HELLO
  12. @LOADP
  13. READ A$
  14. IF A$=="" GOTO @RUNP
  15. FOR I=0 TO LEN(A$)-1
  16. GPSET PC%256,PC/256,ASC(MID$(A$,I,1))
  17. PC=PC+1:NEXT
  18. GOTO @LOADP

  19. @RUNP
  20. PC=0:GPAGE 0
  21. @LOOP
  22. GPAGE 0:VAR=GSPOIT(PTR%256,PTR/256)
  23. GPAGE 1:C=GSPOIT(PC%256,PC/256)
  24. 'GOSUB @FB
  25. IF C==62 THEN PTR=PTR+1:GOSUB @CYCLE:GOTO @LOOP2 
  26. IF C==60 THEN PTR=PTR-1:GOSUB @CYCLE:GOTO @LOOP2
  27. IF C==43 THEN VAR=VAR+1:GOSUB @CYCLE
  28. IF C==45 THEN VAR=VAR-1:GOSUB @CYCLE
  29. IF C==46 THEN LOCATE CSR%32,CSR/32:?CHR$(VAR);:CSR=CSR+1:GOTO @LOOP2
  30. IF C==44 THEN GOSUB @GETCHAR 
  31. IF C==91 THEN MPC=1:GOSUB @SR:GOTO @LOOP2
  32. IF C==93 THEN MPC=1:GOSUB @SL:GOTO @LOOP2
  33. IF C==0 GOTO @END 
  34. GPAGE 0:GPSET PTR%256,PTR/256,VAR
  35. @LOOP2
  36. PC=PC+1:GOSUB @CYCLE
  37. GOTO @LOOP

  38. @FB
  39. LOCATE 0,5:?PTR,PC,VAR
  40. LOCATE 0,6:?CHR$(C)
  41. WAIT 6
  42. RETURN

  43. @CYCLE
  44. IF PC<0 THEN PC=PC+49152
  45. IF PTR<0 THEN PTR=PTR+MS
  46. IF VAR<0 THEN VAR=VAR+256
  47. PC=PC%49152
  48. PTR=PTR%MS
  49. VAR=VAR%256
  50. CSR=CSR%768
  51. RETURN

  52. @GETCHAR
  53. A$=INKEY$():IF A$=="" GOTO @GETCHAR
  54. VAR=ASC(A$):WAIT 30
  55. RETURN

  56. @SR
  57. IF VAR!=0 THEN RETURN
  58. PC=PC+1:GOSUB @CYCLE
  59. GPAGE 1:C=GSPOIT(PC%256,PC/256)
  60. IF C==91 THEN MPC=MPC+1
  61. IF C==93 THEN MPC=MPC-1
  62. IF MPC<=0 THEN RETURN
  63. GOTO @SR

  64. @SL
  65. IF VAR==0 THEN RETURN
  66. PC=PC+1:GOSUB @CYCLE
  67. GPAGE 1:C=GSPOIT(PC%256,PC/256)
  68. IF C==91 THEN MPC=MPC-1
  69. IF C==93 THEN MPC=MPC+1
  70. IF MPC<=0 THEN RETURN
  71. GOTO @SL

  72. @END
  73. WAIT 600
  74. END

  75. @PLOP
  76. DATA "-[>-[.-]<]"
  77. DATA ">>++++[.->++++++[.-]<]"
  78. DATA ""

  79. @CAT
  80. DATA ",[.,]"
  81. DATA ""

  82. @HELLO
  83. DATA "+++++ +++++ [>+++++++"
  84. DATA ">+++++ +++++ >+++ >+ <<<<-]"
  85. DATA ">++.>+.+++++++ ..+++.>++.<<"
  86. DATA "+++++ +++++ +++++"
  87. DATA ".>.+++. ------ .----- --- .>+.>."
  88. DATA ""




No comments:

Post a Comment