Author Topic: QB64-interpreter  (Read 10219 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
QB64-interpreter
« on: July 27, 2017, 01:09:56 am »
This started when Joe Jankovic (I can't for the life of me remember what his user name in the .net forum is) asked for help with a parsing routine he was writing for the CIRCLE command.

I started building this interpreter around his CIRCLE routine, with my added fixes.

After that it was just a matter of "hey, I think I can make it do this" and "it'd be easy to do that" and so it keeps on growing till today.

The source code so far:
https://raw.githubusercontent.com/FellippeHeitor/QB64-interpreter/master/basiccmdline.bas


Command Reference:
Files
NEW
LOAD <file>
RELOAD
SAVE [file]

Interpreter/editing
RUN
LIST [line number]
LIST PAUSE
LIST VARIABLES
EDIT <line number>
INSERT <line number>
DELETE <line number/interval>

Functions supported:
INT
ASC
COS
SIN
LEN
RND
TIMER
TIME$
DATE$
INKEY$
_WIDTH
_HEIGHT
_MOUSEX
_MOUSEY
_MOUSEBUTTON
(no need to query _MOUSEINPUT for mouse functions)

Disk
CHDIR <path>
MKDIR <path>
FILES [filespec$]
KILL <file>

Text and Graphics output
PRINT (limited support)
SCREEN (legacy modes only)
_DISPLAY
WIDTH
COLOR
LOCATE
CIRCLE
CLS

Others
RANDOMIZE
_LIMIT
KEY ON/OFF
SLEEP
_TITLE
SYSTEM
END
INPUT

Special blocks
DO (no WHILE or UNTIL yet)
    EXIT DO
LOOP

IF <condition> THEN (no single line IFs yet)
    'code
END IF

CTRL+C can be used to break out of a running program.

Commands are run immediately after they are typed. If you load a file from disk with the LOAD command you can see the listing with LIST and then RUN it. All of the following samples have been created to test the current state of the interpreter and work as expected:

Code: QB64: [Select]
  1. PRINT "Counting to 10:"
  2. a = 0
  3.     a = a + 1
  4.     COLOR a
  5.     PRINT a
  6.     IF a = 10 THEN
  7.         EXIT DO
  8.     END IF
  9. PRINT "Done."

Code: QB64: [Select]
  1. COLOR 3, 1
  2. PRINT "Hello! ";
  3. COLOR 15, 6
  4. PRINT "It's me!"
  5. COLOR 7, 0
  6. PRINT "What's your name? ";
  7. PRINT "Enter a color for the text: ";
  8. PRINT "Enter a color for the bg: ";
  9. COLOR fg, bg
  10. PRINT "Listen, ";
  11. PRINT "..."
  12. PRINT "I'll sleep for 2 seconds..."
  13. COLOR 7, 0

Code: QB64: [Select]
  1.     x = RND * _WIDTH
  2.     y = RND * _HEIGHT
  3.     r = RND * 20
  4.     c% = RND * 255
  5.  
  6.     CIRCLE (x, y), r, c%
  7.  
  8.     _DISPLAY

Code: QB64: [Select]
  1. PRINT "Click around..."
  2.     IF _MOUSEBUTTON(1) = -1 THEN
  3.         r = RND * 50
  4.         c% = RND * 255
  5.         CIRCLE (_MOUSEX, _MOUSEY), r, c%
  6.     END IF
  7.  
  8.     _LIMIT 60
  9.     _DISPLAY

Code: QB64: [Select]
  1. PRINT "Seconds to count:";
  2. INPUT sec
  3. IF sec = 0 THEN
  4.     PRINT "Bummer..."
  5.     END
  6.  
  7. IF sec > 10 THEN
  8.     sec = 10
  9.     PRINT "That's just too long."
  10.  
  11. PRINT "Will wait for";
  12. PRINT sec;
  13. PRINT "seconds..."
  14.  
  15. s = TIMER
  16.     t = TIMER - s
  17.     IF t > sec THEN
  18.         EXIT DO
  19.     END IF
  20.  
  21. PRINT "done!"

Code: QB64: [Select]
  1.     CLS
  2.     a = RND * 14
  3.     a = a + 1
  4.     COLOR a
  5.     LOCATE 1, 1
  6.     PRINT TIME$
  7.     _DISPLAY
  8.     _LIMIT 30
  9.     a$ = INKEY$
  10.     b = LEN(a$)
  11.     IF b > 0 THEN
  12.         PRINT "A key was pressed."
  13.         COLOR 7, 0
  14.         KEY ON
  15.         EXIT DO
  16.     END IF
« Last Edit: July 27, 2017, 01:12:42 am by FellippeHeitor »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: QB64-interpreter
« Reply #1 on: July 27, 2017, 05:34:34 am »
  • Best Answer
  • AWESOME!!!!!!
    if (Me.success) {Me.improve()} else {Me.tryAgain()}


    My Projects - https://github.com/AshishKingdom?tab=repositories
    OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: QB64-interpreter
    « Reply #2 on: July 27, 2017, 08:05:10 am »
  • Best Answer
  • Hey Fellippe!

    You do the do loop like I do! Only way out is through an Exit command, right?

    ELSE for IF THEN block done very much like looking for LOOP at same level as EXIT.

    Oh wait, can yours do nested loops? It doesn't look like you are checking levels.

    Have you tested code with an internal loop after EXIT DO command in outer loop?

    Do
        ...
        if ...
            exit do
        end if

        do
        ...
        loop
        ...
    Loop

    « Last Edit: July 27, 2017, 08:14:01 am by bplus »

    FellippeHeitor

    • Guest
    Re: QB64-interpreter
    « Reply #3 on: July 27, 2017, 08:19:00 am »
  • Best Answer
  • Hi bplus,

    No nested structures in this iteration.

    This is still very much in infancy; I'll eventually get to that.

    Fellippe

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: QB64-interpreter
    « Reply #4 on: July 27, 2017, 08:22:03 am »
  • Best Answer
  • Hi Fellippe,

    So yours can list variables? Nice idea, if I am correct (I am only skimming through code).

    And macros for save, ... nice! (again if I read right)

    FellippeHeitor

    • Guest
    Re: QB64-interpreter
    « Reply #5 on: July 27, 2017, 08:28:10 am »
  • Best Answer
  • LISTing variables and code only works in immediate mode, as well as source code file features like SAVE, LOAD, NEW.

    FellippeHeitor

    • Guest
    Re: QB64-interpreter
    « Reply #6 on: July 27, 2017, 08:28:53 am »
  • Best Answer
  • I must also add that math features are very limited.

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: QB64-interpreter
    « Reply #7 on: July 27, 2017, 08:38:49 am »
  • Best Answer
  • Hi Fellippe,

    Well I love your start!

    Offline STxAxTIC

    • Library Staff
    • Forum Resident
    • Posts: 1091
    • he lives
      • View Profile
    Re: QB64-interpreter
    « Reply #8 on: July 27, 2017, 09:16:35 am »
  • Best Answer
  • Lovely, just lovely. This will be a great toy to play with. While it's still early, I'd like to hear your arguments for or against the idea of separating mechanism from interface, such that the (for lack of a better term) "logic unit" can be decoupled from screen drawing, for example. This way, you can install the brain into different places - chatbots, a scripting option within inform, etc.
    You're not done when it works, you're done when it's right.

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: QB64-interpreter
    « Reply #9 on: July 27, 2017, 09:30:13 am »
  • Best Answer
  • Hi,

    I just tested a drag and drop of a bas file onto the exe, it works! :)

    FellippeHeitor

    • Guest
    Re: QB64-interpreter
    « Reply #10 on: July 27, 2017, 03:05:27 pm »
  • Best Answer
  • Good to hear!

    If you write any sample code that runs well with it, please share it here.