Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recommend a book on how to write a BASIC interpreter?

362 views
Skip to first unread message

Frederick Williams

unread,
Feb 9, 2007, 1:25:28 PM2/9/07
to
Can anyone recommend a book (or, perhaps, a web site) on how to write a
BASIC interpreter?
--
Remove "antispam" and ".invalid" for e-mail address.
We have lingered in the chambers of the sea
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.

rhnlogic

unread,
Feb 9, 2007, 3:53:05 PM2/9/07
to
On Feb 9, 10:25 am,
Frederick Williams <Frederick_Willi...@antispamhotmail.co.uk.invalid>
wrote:

> Can anyone recommend a book (or, perhaps, a web site) on how to write a
> BASIC interpreter?

There are over a dozen Basic interpreters with source code listed
on my Chipmunk Basic web page: http://www.nicholson.com/rhn/basic
The source code of one of them, "MiniBasic", is supposed to be the
subject of a book.


IMHO. YMMV.
--
rhn A.T nicholson d.0.t C-o-M


Stephen J. Rush

unread,
Feb 9, 2007, 9:12:17 PM2/9/07
to
On Fri, 09 Feb 2007 18:25:28 +0000, Frederick Williams wrote:

> Can anyone recommend a book (or, perhaps, a web site) on how to write a
> BASIC interpreter?

It's not aimed at BASIC interpreters, but Jack Crenshaw's "Let's Build A
Compiler" tutorial develops a simple (Tiny BASIC scale) compiler and, in
a digression, an interpreter. This old series of articles is perhaps
unique in emphasizing code that works, without spending much time on
CompSci theory. The series was, unfortunately, never finished, but if you
follow it and understand it, you will probably be able to continue to a
real BASIC interpreter. Be warned that Crenshaw's example is written in
Pascal, and compiles to assembly language for the Motorola 6800, so you
will need to know both elementary Pascal and a bit about assembly-language
programming for your target processor to translate it. I did it, but
never went beyond its (premature) end, translating to QuickBasic and then
FreeBASIC as I went along. There is a project called QBTiny that contains
a package of 8086 assembly-language routines that provides the necessary
runtime support for Crenshaw's Tiny compiler; a Web search should find it
and the tutorial in several places.


Frederick Williams

unread,
Feb 10, 2007, 9:23:10 AM2/10/07
to

Thanks.

Frederick Williams

unread,
Feb 10, 2007, 9:23:30 AM2/10/07
to
rhnlogic wrote:
>
> On Feb 9, 10:25 am,
> Frederick Williams <Frederick_Willi...@antispamhotmail.co.uk.invalid>
> wrote:
> > Can anyone recommend a book (or, perhaps, a web site) on how to write a
> > BASIC interpreter?
>
> There are over a dozen Basic interpreters with source code listed
> on my Chipmunk Basic web page: http://www.nicholson.com/rhn/basic
> The source code of one of them, "MiniBasic", is supposed to be the
> subject of a book.

Thanks.

Eric

unread,
Feb 12, 2007, 5:00:15 PM2/12/07
to
On Feb 9, 1:25 pm, Frederick Williams

<Frederick_Willi...@antispamhotmail.co.uk.invalid> wrote:
> Can anyone recommend a book (or, perhaps, a web site) on how to write a
> BASIC interpreter?

http://www.amazon.com/Writing-Interactive-Compilers-Interpreters-Computing/dp/0471100722

http://www.amazon.com/Assemblers-compilers-translation-Computer-engineering/dp/0914894234

I like the BWBasic interpreter as a good example of a highly
functional interpreter written in ANSI C. But this would not be a good
first-choice unless you are familiar with C. But then most serious
interpreters are written in C or assembly, in order to get fast
runtime performance.

Does anyone know of a somewhat full-featured Basic interpreter written
in Basic?

Vic Drastik

unread,
Feb 12, 2007, 6:35:52 PM2/12/07
to

"Eric" <engle...@yahoo.com> wrote in message
news:1171317615....@h3g2000cwc.googlegroups.com...


XBasic is written in XBasic, and the early versions of XBasic were written
in QuickBasic. You can examine the layers of interpreter/compiler code like
an archaeological dig - early code has lots of GOTO's, while later code has
references to the 88000 chip (the second CPU XB was ported to) and Unix.
Syntax changed over time, so that early code sometimes needed parentheses
around expressions, such as RETURN (x+y), whereas later the need for this
was eliminated, so now it is just RETURN x+y. The early versions were
entirely written in XB, but later versions have a core library of assembler
routines for speed. The assembler XB uses is open-source, but written in C.
http://www.xbasic.org/


Vic

Tom Lake

unread,
Feb 12, 2007, 6:49:23 PM2/12/07
to
> Does anyone know of a somewhat full-featured Basic interpreter written
> in Basic?

Cheesecake Basic V3 is a full QBasic compatible (it even runs Gorilla.bas)
and can compile itself.

http://www.noktosoft.megafolio.com/

Tom Lake

Stephen J. Rush

unread,
Feb 12, 2007, 9:39:29 PM2/12/07
to
On Mon, 12 Feb 2007 18:49:23 -0500, Tom Lake wrote:

>> Does anyone know of a somewhat full-featured Basic interpreter written
>> in Basic?
>
> Cheesecake Basic V3 is a full QBasic compatible (it even runs Gorilla.bas)
> and can compile itself.

The FreeBASIC compiler is used to maintain itself, although much of its
support library is written in C. Be warned that a modern BASIC compiler
is a ***BIG*** project, with scores of source files. You can't just read
the source like a book. That's why I first mentioned the Crenshaw "Let's
Build A Compiler!" tutorial series, even though it was never finished.

ArarghMai...@not.at.arargh.com

unread,
Feb 13, 2007, 12:11:04 AM2/13/07
to
On Mon, 12 Feb 2007 21:39:29 -0500, "Stephen J. Rush"
<sjr...@comcast.net> wrote:

>On Mon, 12 Feb 2007 18:49:23 -0500, Tom Lake wrote:
>
>>> Does anyone know of a somewhat full-featured Basic interpreter written
>>> in Basic?
>>
>> Cheesecake Basic V3 is a full QBasic compatible (it even runs Gorilla.bas)
>> and can compile itself.
>
>The FreeBASIC compiler is used to maintain itself, although much of its
>support library is written in C. Be warned that a modern BASIC compiler
>is a ***BIG*** project, with scores of source files.

Not really. BCET only has 5 source files for the compiler.

On the other hand, the runtime has over 375 files, mostly for ease of
maintenance. And of those, 139 are actually generated by two
programs, and contain error message data. If I wanted, the runtime
could consist of 2 asm files, and maybe 6 include files.

>You can't just read the source like a book.

>That's why I first mentioned the Crenshaw "Let's Build A Compiler!"
>tutorial series, even though it was never finished.

Even though never finished, it is still useful. It helped me way back
when I was starting BCET.
--
ArarghMail702 at [drop the 'http://www.' from ->] http://www.arargh.com
BCET Basic Compiler Page: http://www.arargh.com/basic/index.html

To reply by email, remove the garbage from the reply address.

rhnlogic

unread,
Feb 13, 2007, 4:05:56 AM2/13/07
to
On Feb 12, 6:39 pm, "Stephen J. Rush" <sjr...@comcast.net> wrote:
> On Mon, 12 Feb 2007 18:49:23 -0500, Tom Lake wrote:
> >> Does anyone know of a somewhat full-featured Basic interpreter written
> >> in Basic?
>
> > Cheesecake Basic V3 is a full QBasic compatible (it even runs Gorilla.bas)
> > and can compile itself.
>
> The FreeBASIC compiler is used to maintain itself, although much of its
> support library is written in C. Be warned that a modern BASIC compiler
> is a ***BIG*** project

A modern Basic compiler can be a big project. But
note the the dds Basic compiler in obfuscated c fits
on less than one page; and the dds Basic interpreter
is only a couple dozen lines of "code". Quite a bit
could be learned by un-obfuscating these two tiny
Basic implementations and seeing how they work.


IMHO. YMMV.
--
rhn
Basic resources: http://www.nicholson.com/rhn/basic


Eric

unread,
Feb 13, 2007, 11:03:59 AM2/13/07
to
On Feb 12, 6:35 pm, "Vic Drastik" <s...@bigpond.com> wrote:
> "Eric" <englere_...@yahoo.com> wrote in message

> > Does anyone know of a somewhat full-featured Basic interpreter written
> > in Basic?
>
> XBasic is written in XBasic, and the early versions of XBasic were written
> in QuickBasic.

XBasic is quite interesting. The source code is pretty deep and
difficult to follow, but the dialect of Basic they support looks quite
strong. The graphics features also look interesting.

I'd like a scaled down version of XBasic, so I looked at XBasic Lite.
But that's written in assembly language, and wouldn't be easy for most
of us to work on.

The main reason I like BWBasic is that it's source code is small
enough to understand without a great deal of effort, but its Basic
dialect uses line numbers. I learned programming on the early micros,
so line numbers aren't a big problem for me. But I understand the
desire to go without them. The larger application programs benefit
greatly from modern control structures and no line numbers.

Eric

Eric

unread,
Feb 13, 2007, 11:16:15 AM2/13/07
to

I saw a reference to this earlier in this group and it mentioned the
phrase "byte code". Does this mean that it compiles programs to an
intermediate code that is later interpreted? I favor this model
because it's a reasonable trade-off between a classic interpreter and
a native code compiler. But the main reason I like it for the ease of
porting the compiler to other platforms, assuming it can compile
itself. Has anyone considered how feasible it would be to port this to
linux?

http://noktosoft.megafolio.com/

Also, I'm confused about the link between PowerBasic and Cheesecake
Basic. Someone here gave a link to the PowerBasic download area, and
they have descriptions that make it look like you need to compile
Cheesecake Basic using PowerBasic? Is the Basic dialect of these 2
compilers/interpreters source code compatible?

http://www.powerbasic.com/files/pub/pbwin/misc/

Eric

Eric

unread,
Feb 13, 2007, 11:26:54 AM2/13/07
to
On Feb 12, 9:39 pm, "Stephen J. Rush" <sjr...@comcast.net> wrote:

> The FreeBASIC compiler is used to maintain itself, although much of its
> support library is written in C. Be warned that a modern BASIC compiler
> is a ***BIG*** project, with scores of source files. You can't just read
> the source like a book. That's why I first mentioned the Crenshaw "Let's
> Build A Compiler!" tutorial series, even though it was never finished.

Crenshaw's work is indeed classic. I have various ports of his work to
several other languages. I think I even have a Basic language port I
found on the net. If there's any modern compiler writers who haven't
read Crenshaw's work I haven't met them yet.

I've also got some early microprocessor Basic interpreter source code
written in assembly language, including Applesoft, TRS-80, and several
kinds of 8080 interpreters. This stuff is interesting for us old
timers, but not a good foundation for any new interpreter projects. I
also found a C language interpreter for linux that supports a subset
of Applesoft and that might be cool to play with, but I don't think it
supports graphics, and it requires line numbers, so it has limited
appeal.

Eric

Eric

unread,
Feb 13, 2007, 11:33:43 AM2/13/07
to
On Feb 13, 12:11 am, ArarghMail702NOS...@NOT.AT.Arargh.com wrote:

> BCET only has 5 source files for the compiler.

But I don't think you ever released the source code, right?

Eric

Eric

unread,
Feb 13, 2007, 11:46:27 AM2/13/07
to
On Feb 13, 4:05 am, "rhnlogic" <rhnlo...@yahoo.com> wrote:

> A modern Basic compiler can be a big project. But
> note the the dds Basic compiler in obfuscated c fits
> on less than one page; and the dds Basic interpreter
> is only a couple dozen lines of "code". Quite a bit
> could be learned by un-obfuscating these two tiny
> Basic implementations and seeing how they work.
>
> IMHO. YMMV.
> --
> rhn
> Basic resources:http://www.nicholson.com/rhn/basic

Ron, your links are awesome! And I see that you have a port of
Chipmunk Basic for Windows. Is the source code available?

Tom Lake

unread,
Feb 13, 2007, 2:49:49 PM2/13/07
to
A while ago you said HotPaw would be getting a fix to allow uppercase
keywords. Any progress on that front?

Tom Lake


ArarghMai...@not.at.arargh.com

unread,
Feb 13, 2007, 5:19:55 PM2/13/07
to
On 13 Feb 2007 08:33:43 -0800, "Eric" <engle...@yahoo.com> wrote:

>On Feb 13, 12:11 am, ArarghMail702NOS...@NOT.AT.Arargh.com wrote:
>
>> BCET only has 5 source files for the compiler.

I forgot to mention that it uses several dozen routines from my
standard library. Things like:

DECLARE FUNCTION AscMid(aString$, byval Offset)

which does a=ASC(MID$(aString,Offset,1))

in one call, instead of two.

In the older days, that sort of thing was important. :-) Now, I am
just used to them.

>
>But I don't think you ever released the source code, right?
>

No, I haven't. All the compiler source is mine, but parts of the
runtime are from other packages, which I have permission to use, but
not to distribute. I can look into getting those permissions, but, as
one of the other packages is a commerical product, I wouldn't get my
hopes real high.

rhnlogic

unread,
Feb 13, 2007, 7:14:41 PM2/13/07
to

I have lots of fixes & updates in development versions
of both Chipmunk Basic & HotPaw Basic.

Drop me some private email (as per below) if you
want to beta test either. Name one of the Basic's
to help avoid spam traps.


IMHO. YMMV.
--
rhn A.T nicholson d.0.t C-o-M

HotPaw Basic for PalmOS: http://www.hotpaw.com/rhn/hotpaw


Vic Drastik

unread,
Feb 14, 2007, 12:25:27 AM2/14/07
to

"Eric" <engle...@yahoo.com> wrote in message
news:1171382639....@h3g2000cwc.googlegroups.com...

> On Feb 12, 6:35 pm, "Vic Drastik" <s...@bigpond.com> wrote:
> > "Eric" <englere_...@yahoo.com> wrote in message
> > > Does anyone know of a somewhat full-featured Basic interpreter written
> > > in Basic?
> >
> > XBasic is written in XBasic, and the early versions of XBasic were
written
> > in QuickBasic.
>
> XBasic is quite interesting. The source code is pretty deep and
> difficult to follow, but the dialect of Basic they support looks quite
> strong. The graphics features also look interesting.
>
> I'd like a scaled down version of XBasic, so I looked at XBasic Lite.
> But that's written in assembly language, and wouldn't be easy for most
> of us to work on.


XBlite is exactly the same as XBasic, but without support for or
compatibility with Linux. I don't know how you decided that it's 'written in
assembly language', but this is not true. Maybe you are confusing the fact
that XBlite supports direct inline assembly whereas XBasic only supports
virtual functions and external linking of separately assembled machine code,
or maybe because XBlite has been upgraded to use (closed-source) FASM as its
assembler instead of open-source Spasm.

If anything, XBlite should change its name to XBheavy because of the all the
extra (Windows-only) features it now has.

Vic

Eric

unread,
Feb 14, 2007, 5:22:15 AM2/14/07
to
On Feb 14, 12:25 am, "Vic Drastik" <s...@bigpond.com> wrote:

> XBlite is exactly the same as XBasic, but without support for or
> compatibility with Linux. I don't know how you decided that it's 'written in
> assembly language', but this is not true.

I saw some very large .asm functions and jumped to the wrong
conclusion. These are apparently only used for libraries. Thanks for
clearing this up.

Eric


ca...@libertybasic.com

unread,
Feb 14, 2007, 8:53:24 AM2/14/07
to
On Feb 12, 5:00 pm, "Eric" <englere_...@yahoo.com> wrote:
> On Feb 9, 1:25 pm, Frederick Williams
>
> <Frederick_Willi...@antispamhotmail.co.uk.invalid> wrote:
> > Can anyone recommend a book (or, perhaps, a web site) on how to write a
> > BASIC interpreter?
>
> http://www.amazon.com/Writing-Interactive-Compilers-Interpreters-Comp...
>
> http://www.amazon.com/Assemblers-compilers-translation-Computer-engin...

>
> I like the BWBasic interpreter as a good example of a highly
> functional interpreter written in ANSI C. But this would not be a good
> first-choice unless you are familiar with C. But then most serious
> interpreters are written in C or assembly, in order to get fast
> runtime performance.
>
> Does anyone know of a somewhat full-featured Basic interpreter written
> in Basic?

I wouldn't say that it's full featured by any stretch of the
imagination, but there is an port of Tiny BASIC originally written for
iziBasic (a BASIC for the Palm OS found here http://www.aldweb.com/)
on the Run BASIC website. The sources are there to be modified and
run right on the page.

I mention it because it is an interpreter (not a compiler) and because
it is easily enough understood. Study it well enough to add a few new
commands for fun, and then go build your own improved version. ;-)

http://www.runbasic.com/?_page=tinybasic.bas

You use it just like you use GW-BASIC. It is line numbered and
understands LIST, RUN and supports immediate evaluation. There is no
support for string variables, arrays, GOSUB, etc. When you run it,
type help at the prompt for a list of commands.

-Carl Gundel
http://www.libertybasic.com

Eric

unread,
Feb 15, 2007, 11:46:43 AM2/15/07
to
On Feb 14, 8:53 am, "c...@libertybasic.com" <c...@libertybasic.com>
wrote:

> I wouldn't say that it's full featured by any stretch of the
> imagination, but there is an port of Tiny BASIC originally written for

> iziBasic (a BASIC for the Palm OS found herehttp://www.aldweb.com/)


> on the Run BASIC website. The sources are there to be modified and
> run right on the page.

Thanks Carl. There's a definite niche for cool interpreters that are
easy to understand. We can't learn easily from a complex example.

Your "run from the web" idea is also cool. The way you generate a
graphic image (using your full Basic interpreter) and then rendering
it to the returned page is sweet.

Eric

ca...@libertybasic.com

unread,
Feb 15, 2007, 8:51:20 PM2/15/07
to

Thanks. Try running this code. :-)

graphic #color, 10, 10
color$ = "red green blue yellow orange purple brown black white"
for x = 1 to 9
#color cls(word$(color$, x))
#color box(9,9)
render #color;
print " "; word$(color$, x)
next x

-Carl Gundel
http://www.runbasic.com


ed_davis2

unread,
Feb 19, 2007, 7:16:12 AM2/19/07
to
On Feb 9, 1:25 pm, Frederick Williams wrote:
> Can anyone recommend a book (or, perhaps, a web site) on how to
> write a BASIC interpreter?

I always liked "Writing Interactive Compilers and Interpreter" by
P. J. Brown. It is older (1982), but it has lots of good ideas,
especially in regards to writing a BASIC interpreter. Used
copies can be had for a few dollars.

To help you get started, here is a simple Tiny Basic interpreter
I wrote over the weekend. It supports the following BASIC
commands:

bye
goto
if expression then statement
input
list
new
print
rem
run
stop
assignment: i = i + 3 * 4

It supports 26 integer variables, a-z. Everything except quoted
strings must be lowercase.

It supports line number between 1 and 300. If you enter
something without a line number, it is immediately performed.
Just like old gw-basic or basica.

---------------------
rem Tiny Basic interpreter
dim shared ch$
dim shared thelin$
dim shared pgm$(301)
dim shared tok$

dim shared vars(26) as integer

dim shared curline as integer
dim shared num as integer
dim shared textp as integer
dim shared errors as integer

do
errors = 0
line input "> ", pgm$(0)
if pgm$(0) <> "" then
call initgetsym(0)
if left$(tok$, 1) >= "0" and left$(tok$, 1) <= "9" then
call validlinenum
pgm$(num) = mid$(pgm$(0), textp, len(pgm$(0)) - textp + 1)
else
call docmd
end if
end if
loop

sub docmd
dim i as integer
again:
if errors <> 0 then exit sub
while tok$ = ""
if curline = 0 or curline >= 300 then exit sub
call initgetsym(curline + 1)
wend
if accept("stop") or accept("end") then exit sub
if accept("new") then
call clearvars
for i = 1 to 300
pgm$(i) = ""
next i
exit sub
end if
if accept("bye") then end

if accept("list") then call liststmt: goto again
if accept("run") then
call clearvars
call initgetsym(1)
goto again
end if
if accept("goto") then call gotostmt: goto again
if accept("if") then call ifstmt: goto again
if accept("input") then call inputstmt: goto again
if accept("print") then call printstmt: goto again
if accept("rem") then call skiptoeol: goto again
if left$(tok$, 1) >= "a" and left$(tok$, 1) <= "z" then
call idstmt
goto again
end if
print "Unknown token "; tok$; " at line "; curline
end sub

rem "print" [ expr "," expr ] [","]
rem expr can also be a literal string
sub printstmt
dim printnl as integer
printnl = 1
do while tok$ <> ""
printnl = 1
if left$(tok$, 1) = chr$(34) then
print mid$(tok$, 2, len(tok$) - 1);
call getsym
else
print expression;
end if

if accept(",") = 0 and accept(";") = 0 then exit do
printnl = 0
loop
if printnl <> 0 then print
end sub

rem "input" [string ","] var
sub inputstmt
dim var as integer
if tok$ = chr$(34) then
print mid$(tok$, 2, len(tok$) - 1);
call getsym
call expect(",")
else
print "? ";
end if
var = getvarindex
input vars(var)
end sub

sub idstmt
dim var as integer
var = getvarindex
call expect("=")
vars(var) = expression
end sub

sub liststmt
dim i as integer
for i = 1 to 300
if pgm$(i) <> "" then print i; " "; pgm$(i)
next i
print
end sub

sub gotostmt
if left$(tok$, 1) >= "0" and left$(tok$, 1) <= "9" then
call gotoline
exit sub
endif
print "Line number must follow goto": errors = 1
end sub

sub ifstmt
dim b as integer
if expression = 0 then call skiptoeol: exit sub
b = accept("then")
if left$(tok$, 1) >= "0" and left$(tok$, 1) <= "9" then
call gotoline
end if
end sub

sub gotoline
call validlinenum
call initgetsym(num)
end sub

sub validlinenum
if num > 0 and num <= 300 then exit sub
print "Line number out of range": errors = 1
end sub

sub clearvars
dim i as integer
for i = 1 to 26
vars(i) = 0
next i
end sub

function expression%
dim n
n = addexpr%
exprl:
if tok$ = "=" then call getsym: n = n = addexpr%: goto exprl
if tok$ = "<" then call getsym: n = n < addexpr%: goto exprl
if tok$ = ">" then call getsym: n = n > addexpr%: goto exprl
if tok$ = "<>" then call getsym: n = n <> addexpr%: goto exprl
if tok$ = "<=" then call getsym: n = n <= addexpr%: goto exprl
if tok$ = ">=" then call getsym: n = n >= addexpr%: goto exprl
expression% = n
end function

function addexpr%
dim n
n = term%
addel:
if tok$ = "+" then call getsym: n = n + term%: goto addel
if tok$ = "-" then call getsym: n = n - term%: goto addel
addexpr% = n
end function

function term%
dim n
n = factor%
terml:
if tok$ = "*" then call getsym: n = n * factor%: goto terml
if tok$ = "/" then call getsym: n = n / factor%: goto terml
term% = n
end function

function factor%
if accept("-") then
factor% = -factor%
exit function
end if
if accept("(") then
factor% = expression
call expect(")")
exit function
end if
if left$(tok$, 1) >= "0" and left$(tok$, 1) <= "9" then
factor% = num
call getsym
exit function
end if
if left$(tok$, 1) >= "a" and left$(tok$, 1) <= "z" then
factor% = vars(getvarindex)
exit function
end if
print "Unexpected sym "; tok$; " in factor": errors = 1
end function

function getvarindex%
if left$(tok$, 1) < "a" or left$(tok$, 1) > "z" then
print "Not a variable": errors = 1: exit function
end if
getvarindex% = asc(left$(tok$, 1)) - asc("a")
call getsym
end function

sub expect(s as string)
if accept(s) <> 0 then exit sub
errors = 1
print "expecting "; s; " but found "; tok$
end sub

function accept%(s as string)
accept% = 0
if tok$ = s then accept% = 1: call getsym
end function

sub initgetsym(n as integer)
curline = n
textp = 1
thelin$ = pgm$(curline)
ch$ = " "
call getsym
end sub

sub skiptoeol
while ch$ <> ""
call getch
wend
call getsym
end sub

sub getsym
tok$ = ""
while ch$ <= " "
if ch$ = "" then exit sub
call getch
wend

tok$ = ch$
if instr(",;=+-*/()", ch$) > 0 then call getch: exit sub
if ch$ = "<" then
call getch
if ch$ = "=" or ch$ = ">" then
tok$ = tok$ + ch$
call getch
end if
exit sub
end if
if ch$ = ">" then
call getch
if ch$ = "=" then tok$ = tok$ + ch$: call getch
exit sub
end if
if ch$ = chr$(34) then call readstr: exit sub
if ch$ >= "a" and ch$ <= "z" then call readident: exit sub
if ch$ >= "0" and ch$ <= "9" then call readint: exit sub
print "What->"; ch$: errors = 1
end sub

sub readstr
tok$ = chr$(34)
call getch
while ch$ <> chr$(34)
if ch$ = "" then
print "String not terminated": errors = 1: exit sub
end if
tok$ = tok$ + ch$
call getch
wend
call getch
end sub

sub readint
tok$ = ""
while ch$ >= "0" and ch$ <= "9"
tok$ = tok$ + ch$
call getch
wend
num = val(tok$)
end sub

sub readident
tok$ = ""
while ch$ >= "a" and ch$ <= "z"
tok$ = tok$ + ch$
call getch
wend
end sub

sub getch
if textp > len(thelin$) then ch$ = "": exit sub
ch$ = mid$(thelin$, textp, 1)
textp = textp + 1
end sub
------ the end ------


ca...@libertybasic.com

unread,
Feb 20, 2007, 8:50:14 AM2/20/07
to
On Feb 15, 11:46 am, "Eric" <englere_...@yahoo.com> wrote:
> Your "run from the web" idea is also cool. The way you generate a
> graphic image (using your full Basic interpreter) and then rendering
> it to the returned page is sweet.

Run BASIC is not an interpreter. It compiles, but not to native
code. It compiles to objects which execute themselves on top of our
runtime environment (in Smalltalk).

-Carl Gundel
http://www.runbasic.com

Frederick Williams

unread,
Feb 20, 2007, 10:05:00 AM2/20/07
to
ed_davis2 wrote:
>
> On Feb 9, 1:25 pm, Frederick Williams wrote:
> > Can anyone recommend a book (or, perhaps, a web site) on how to
> > write a BASIC interpreter?
>
> I always liked "Writing Interactive Compilers and Interpreter" by
> P. J. Brown. It is older (1982), but it has lots of good ideas,
> especially in regards to writing a BASIC interpreter. Used
> copies can be had for a few dollars.

I'll seek it out. And thanks for the code!

Frederick Williams

unread,
Feb 20, 2007, 10:06:47 AM2/20/07
to
Eric wrote:
>
> On Feb 9, 1:25 pm, Frederick Williams
> <Frederick_Willi...@antispamhotmail.co.uk.invalid> wrote:
> > Can anyone recommend a book (or, perhaps, a web site) on how to write a
> > BASIC interpreter?
>
> http://www.amazon.com/Writing-Interactive-Compilers-Interpreters-Computing/dp/0471100722
>
> http://www.amazon.com/Assemblers-compilers-translation-Computer-engineering/dp/0914894234
>
> I like the BWBasic interpreter as a good example of a highly
> functional interpreter written in ANSI C. But this would not be a good
> first-choice unless you are familiar with C.

Which I am! Thanks for the links.

> But then most serious
> interpreters are written in C or assembly, in order to get fast
> runtime performance.
>
> Does anyone know of a somewhat full-featured Basic interpreter written
> in Basic?

0 new messages