Showing posts with label run. Show all posts
Showing posts with label run. Show all posts

Wednesday, January 06, 2016

Tiny BASIC part 3 - Adding GRAPHICWIN statement


We are going to add GRAPHICWIN and PSET statements to Tiny BASIC.

Let's start with the really easy one.  We will add a  case "graphicwin"  block to the end of the select case blocks that we examined in the last post.  Here is what it looks like.  The new code is in bold red.
 CASE "let"
  GOSUB [GetLabel]
  IF E$<>"" THEN [Ready]
 CASE "graphicwin"
  IF GWINOPEN = 1 THEN
    PRINT "Graphics window is already open."
  ELSE
    GWINOPEN = 1
    OPEN "Graphics" FOR graphics AS #GWIN
  END IF
  GOTO [FinishStatement]

 END SELECT
So when you run Tiny BASIC and type the command graphicwin and press Enter it will open a small graphics window.  When it does this it also sets the GWINOPEN flag to 1 to that we can check it if the program tries to open another graphics window.  Only one graphics window will be allowed at a time.

The other thing that we want to do it close the graphics window and reset GWINOPEN to 0 if the program is started using the RUN statement.

So, we need to add the following code into the  case "run"  block.  Here is how that code should look.  The new code is in bold red.

 CASE "run"
  IF GWINOPEN = 1 THEN
    CLOSE #GWIN
    GWINOPEN = 0
  END IF

  FOR I=27 TO 52 : A(I)=0 : NEXT I
  L=27 : C=1
  GOTO [FinishStatement2]
So now the program will always start off in a clean state each time it is run!

In our next post we will figure out how to add a PSET statement so we can draw some graphics!


Wednesday, May 30, 2007

Modules and Run/Liberty BASIC

People have been asking for a way to create code libraries in Liberty BASIC for some time. I have thinking about how to do this using an in-memory model. Over the weekend Scott and I worked on adding some modularity to the Run/Liberty BASIC language. Here's how it works so far. We are open to suggestions.

The RUN statement has a new form. If I want to use mymodule.bas as a module in a program I am writing I would code it like this:

run "mymodule.bas", #myModule

This causes mymodule.bas to be compiled into memory. It will execute code up to the first wait statement. This is how values will be initialized. The program then becomes an object assigned to the #myModule handle.

Then you can call any function on that program from within the calling program, like so:

#myModule myFunction(myParameter)

or even use the invokation in a more complex expression:

print "digits: "; len(#myModule myFunction(myParameter))

You may recognize this syntax as being the same as for controlling widgets. It relies on the same underlying mechanisms to do its work.

When we're done with this, modular programs loaded this way will be cached in memory and will only recompile when the source file changes.