Jump to content
rje

My BASIC pre-processor

Recommended Posts

It's not very sophisticated, and it's not a BASIC parser.

What it does, though, is provide some nice features:

* Labels.  You can write BASIC without line numbers.  And it's refreshing.
* Variable labels.  You can write as if BASIC 2.0 allowed long variable names.  This is similarly refreshing.  And my script will complain if you re-declare one.  What does this get you?  Read on.
* Labels with dots.  You can use the period in label names.  What does this get you?  Read on.
* Variable labels with dots.  You can use the period in variable labels.  Why?
* Multiple files.  You can split your BASIC up into multiple source files.

This is essentially a measure of syntactic sugar.  And you still have to be careful.  

But.

As long as you're careful, it gets you LOOSE COUPLING and HIGH COHESION with your program segments.

Here's how:

1. Treat each source file as a subsystem.  An object, if you prefer.  For example, you might have a file named

record.list

which contains data and subroutines for managing "records" in your program.

The rest of your program shouldn't care WHAT those records look like or HOW they're managed.  You just need to expose subroutines that manipulate the data in needed ways.... yeah I mean an API. 

2. Give each source file its own long variables.

3. Name these variables as if they were fields in structures.  For example:

longvar \record.name       r0
longvar \record.strength   r1
longvar \record.xp         r3

4. Encapsulate your longvars with subroutines, using labels as entry points into "methods":

{:record.tostring}
o$ = \record.name$ + ", " + \record.strength + \record.xp
return

 

Voila.  Each file could have its own API, and its labels look like method calls (even though they're not).  Again, this is SYNTACTIC SUGAR.  What's the big deal?  Well suppose you're editing main.list, and you want to manipulate some records.  If you define a nice API, you could do it like this:

;
; CONTRIVED EXAMPLE
; (p.s. these are comments that won't show up in the "compiled" BASIC 2.0 file)
; (you can still use REM for preserved comments)
;
\sort.format$ = "asc" :gosub {:records.sort}
gosub {:records.rollup}
gosub {:records.print}

Ever lose your variables?  Ever have to write them down in a notebook?  Do you STILL have to write them down in a notebook?

NO -- if you're careful!

Question.  In the above fragment, where is records data managed?   I would guess you put it in the file named "records.list".  And where are the subroutines records.sort(), records.rollup(), and records.print()?   Same place, I'd say.

And if you did it that way, then you'd find it much easier to:

1. Keep track of your variables

2. Keep track of your subroutines

3. ENCAPSULATE your logic based on your data


It's not foolproof by any means, and it doesn't simulate any nice flow control like while() and for(;;).  But... but it does give you a way to break down your code  into conceptual pieces, manage your variables, and even provide a form of "trusted" encapsulation.  You're free to violate all of this, but the point of the script is to help me manage my code.  Maybe it can help you as well.  Or inspire you to write something better.

 

 

It requires Perl.  But then, doesn't every great tool?  (Ha!)

 


 

basic-labelmaster.pl

Edited by rje
  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
21 hours ago, Lorin Millsap said:

I would like a true parser but that’s probably not going to happen.

I don't want to hijack rje's thread, but I'm working on that... there just was zero interest the last time I announced it, so I haven't bothered talking about it here. 

  • Like 1

Share this post


Link to post
Share on other sites

@TomXP411, please share where you announced it, I missed it.

By the way, @Nate.N also created similar tool, which for some reason caught my interest. It's here: https://www.commanderx16.com/forum/index.php?/topic/563-qbasic-style-basic-transpiler-anyone-interested/

So with @rje's tool that makes three. ) Which is good I think. It's alway nice to have options.

And I remeber @The 8-Bit Guy saying he wanted similar functionality embedded in system. @Lorin Millsap also mentioned it in Facebook group with a little more detail.

@The 8-Bit Guy, @Lorin Millsap, please share some updates on the topic. And may be features from tools I mentioned here might be useful in final X16 implementation.

Edited by Cyber
typos fixed

Share this post


Link to post
Share on other sites
4 minutes ago, Cyber said:

please share where you announced it, I missed it.

Like I said, I don't want to derail rje's thread. When I resume work on my parser, I'll announce it. 

Share this post


Link to post
Share on other sites
On 10/12/2020 at 6:36 PM, Lorin Millsap said:

Very nice. I would like a true parser but that’s probably not going to happen.


Sent from my iPhone using Tapatalk

There is a true (Apple) BASIC interpreter in CPAN, that's been there for something like a decade.  I didn't use it, although I could if I needed to.

https://metacpan.org/pod/Language::Basic

And I believe I know the guy who wrote a JavaScript BASIC interpreter (again, Apple).  Yep, here it is:

https://github.com/inexorabletash/jsbasic

 

Edited by rje

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Please review our Terms of Use