Author Topic: An interactive BASIC interpreter implemented in Python 3  (Read 5735 times)

Offline richpl

  • BASIC Developer
  • Posts: 5
An interactive BASIC interpreter implemented in Python 3
« on: December 29, 2018, 01:46:26 PM »
If this is of interest to anyone, I’m in the process of writing a BASIC interpreter in Python. This started as a personal project, but I mention it here as I’ve put quite a lot of work into it and the implementation is non-trivial. I think I’ve done a reasonably elegant and well structured piece of work. Error checking is comprehensive and error reporting should at least allow the user to narrow down which line of their BASIC program is invalid.

I’ve implemented all of the major control structures as well as numeric and string variables. There is still some work to be done (arrays are the only major omission now), but the end is now in sight.

The interpreter is interactive in the style of a 1980s home computer (which was my introduction to programming, via the TI99/4a, RML 380Z and BBC Micro). At the prompt you may enter unstructured BASIC statements to create a program, run, list and save it, as well as load previously written BASIC programs. Given that it is written in Python 3, the interpreter should run on multiple platforms.

The interpreter is available for free in Github using this link: https://github.com/richpl/PyBasic.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: An interactive BASIC interpreter implemented in Python 3
« Reply #1 on: December 29, 2018, 02:44:18 PM »
Welcome Rich to the All BASIC forum. You are our 16th BASIC developer here on the forum.

Your BASIC should generate interest being written in Python which should be a good tutorial how to write an interpreter.

I couldn't find in the README what is the limitations for string size and variable names.
« Last Edit: December 29, 2018, 02:50:34 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: An interactive BASIC interpreter implemented in Python 3
« Reply #2 on: December 29, 2018, 08:27:12 PM »
I was unable to start the interpreter. The problem may be I'm running Python 2.7.15+.


jrs@jrs-laptop:~/pyBasic/PyBasic-master$ python interpreter.py
  File "interpreter.py", line 93
    print("Unrecognised input", file=stderr)
                                    ^
SyntaxError: invalid syntax


This resolved it.


jrs@jrs-laptop:~/pyBasic/PyBasic-master$ python3 interpreter.py

        PPPP   Y   Y  BBBB    AAA    SSSS    I     CCC
        P   P   Y Y   B   B  A   A  S        I    C   
        P   P   Y Y   B   B  A   A  S        I    C
        PPPP     Y    BBBB   AAAAA  SSSS     I    C
        P        Y    B   B  A   A      S    I    C
        P        Y    B   B  A   A      S    I    C
        P        Y    BBBB   A   A  SSSS     I     CCC
       
> 10 FOR X=1 TO 5
> 20 PRINT X
> 30 NEXT x
> run
1
2
3
4
5
>


I can't seem to LOAD a BASIC program.


$ python3 interpreter.py factorial.bas

        PPPP   Y   Y  BBBB    AAA    SSSS    I     CCC
        P   P   Y Y   B   B  A   A  S        I    C   
        P   P   Y Y   B   B  A   A  S        I    C
        PPPP     Y    BBBB   AAAAA  SSSS     I    C
        P        Y    B   B  A   A      S    I    C
        P        Y    B   B  A   A      S    I    C
        P        Y    BBBB   A   A  SSSS     I     CCC
       
> list
> load "factorial.bas"
could not find MARK
> list
> load "regression.bas"
could not find MARK
>



« Last Edit: December 29, 2018, 08:48:57 PM by John »

Offline richpl

  • BASIC Developer
  • Posts: 5
Re: An interactive BASIC interpreter implemented in Python 3
« Reply #3 on: December 30, 2018, 02:03:30 AM »
Thanks for trying it. Yep, running Python 2 will be problematic, you definitely need Python 3.

Saving uses Python pickling to store the object containing the program. So you need to enter it into the interpreter, then save it. You should then be able to load it again. Storing BASIC programs as editable text is potentially an enhancement.

Regards

Rich