guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Rudimentary BASIC->Guile converter


From: Richard Todd
Subject: Rudimentary BASIC->Guile converter
Date: Sat, 29 Nov 2003 13:21:15 -0600
User-agent: Mutt/1.5.4i

I've written the start of a BASIC -> Guile converter.
It's here: 
http://www.vzavenue.net/~rwtodd5128/index.html

It's at a sort of proof-of-concept stage.  It can do assignments,
print, input, for/next, goto, if/else/elseif.  There are some example
programs in the tests/ directory.  Two programs in the src/ directory
use the language module to 1) execute a .bas file, and 2) print out
scheme equivalent of a .bas file.

I don't know if I'll finish it...I guess it depends a little on
whether anyone finds it useful.  That's why I'm posting this message.

I just wrote it to get a feel for what writing an X->guile translator
would take.  It's a little different than other little interpreters
I've done.  A lot of stuff I consider 'normal' for compiling to asm or
bytecode has to be rethought for converting to guile.  Primarily
because there's no JMP-to-label command, and also because you can't
blindly string code together (because IF's parens must surround the
conditional code, for instance).

I'd like to get some comments back if people like or don't like the
approach I've taken.  If there are ways it could be improved, I'd
like to know about it.

DEPENDENCY: I've used an excellent GPL'ed LALR(1) parser generator,
but I can't seem to get any response from the author about publishing
the guile port of it.  So, on my website, you'll also see a 
(parse lalr) module, which you'll need.  (google found me the actual
work for the port in the form of a message from Thien-Thi Nguyen, so
all I had to actually do was add the (define-module) declaration, and
put it in an autotools framework.

Implementation Comments: 
The main weakness of the code I've got (other than not supporting full
BASIC yet), is that there's no traceability between the scheme source
and the original BASIC.  So, when errors pop up, you have to
understand the problem in terms of the scheme code, to a degree.
Since projects like this are presumably so people can use BASIC
_instead of_ scheme, the error reporting issue needs work.

INTEROPERABILITY

The plan is to let basic files define modules, so that basic functions
can be used from guile.  Also, to a limited degree, BASIC should be able
to invoke guile functions as well.  This way, it should be possible to
use BASIC to extend programs that can currently be extended via guile.

I've added a |-quoted |*this-is:a-quoted-symbol*| syntax, so that all
scheme symbols can be referenced from BASIC.  Also, the converter checks
to see if globals referenced in the BASIC code are already defined, at which
point it doesn't define them.  This way, BASIC code can read and write to
scheme globals.


GOTO STATEMENTS

Supporting GOTO turned out to be the hardest thing, and forced the
entire shape of the translation, for a couple reasons:

 1) Scheme doesn't really have an equivalent.  I played with call/cc a
little, but eventually decided that I wasn't going to get that
approach to work very easily). I settled on putting each basic block
into a letrec expression, so every basic program (and eventually basic
subroutines) will look like this:

  (letrec ((blk-1 (lambda () ... (blk-2)))
           (blk-2 (lambda () ... (blk-1)))
           ...)
     (blk-1))

So, it doesn't look natural, but is not that hard to read, either.
Some label names have suffixes on them to help you read the scheme
text.

 2) Because you can GOTO into and out of loops, I didn't see an easy
way to preserve WHILE/WEND and FOR/NEXT loops as equivalent scheme
loops.  Instead, I had to write the loops like you would in assembly
language, with explicit checks and jumps.  In the future, I suppose I
could scan the program and use more efficient scheme loops where there
is no GOTO action.

Richard Todd 
richardt at  vzavenue  net 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]