473,734 Members | 2,362 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Has anyone implemented BASIC in Python?

Has anyone ever tried implementing a simple unstructured BASIC dialect
in Python? I'm getting interested in language implementation, and
looking at a reasonably simple example like that could be pretty
interesting.
Jul 18 '05 #1
19 3219
has
Leif K-Brooks <eu*****@ecritt ers.biz> wrote in message news:<2o******* *****@uni-berlin.de>...
Has anyone ever tried implementing a simple unstructured BASIC dialect
in Python? I'm getting interested in language implementation, and
looking at a reasonably simple example like that could be pretty
interesting.


Dunno about BASIC, but Ian Bicking has done Logo: http://pylogo.sourceforge.net/

HTH
Jul 18 '05 #2
ha*******@virgi n.net (has) writes:
Has anyone ever tried implementing a simple unstructured BASIC dialect
in Python? I'm getting interested in language implementation, and
looking at a reasonably simple example like that could be pretty
interesting.


Dunno about BASIC, but Ian Bicking has done Logo:
http://pylogo.sourceforge.net/


Dunno about Logo but I did Forth just out of general perversity.
I suppose I could post it.
Jul 18 '05 #3
Paul Rubin wrote:
ha*******@virgi n.net (has) writes:
Has anyone ever tried implementing a simple unstructured BASIC dialect
in Python? I'm getting interested in language implementation, and
looking at a reasonably simple example like that could be pretty
interestin g.


Dunno about BASIC, but Ian Bicking has done Logo:
http://pylogo.sourceforge.net/

Dunno about Logo but I did Forth just out of general perversity.
I suppose I could post it.


Please do!
Jul 18 '05 #4
On Sat, 21 Aug 2004 00:27:33 -0400, Leif K-Brooks
<eu*****@ecritt ers.biz> wrote:
Has anyone ever tried implementing a simple unstructured BASIC dialect
in Python? I'm getting interested in language implementation, and
looking at a reasonably simple example like that could be pretty
interesting.


I've no idea why you think that an unstructured language would
be a good starting point. IMO a good starting point would be just
an expression parser; after than an interpreter that just executes
a parsed tree and finally a true compiler that generates elementar
instructions. Ugly non-structured languages are not going to be
that easier to implement IMO.

Anyway I found the idea of a BASIC interpreter in python intriguing,
so I wrote one this weekend. I wrote it for my egoistic fun, so
I'm not sure if it would be useful for you (or anyone else).

Anyway here we go... http://www.gripho.it/py_basic.tgz

Comments are welcome...

Andrea
Jul 18 '05 #5
Andrea Griffini wrote:
I've no idea why you think that an unstructured language would
be a good starting point.
There's no stack to deal with, and generally less possible expressions
to worry about. You're probably right about doing an expression parser
first, though; I will. Thanks for the advice.
Anyway I found the idea of a BASIC interpreter in python intriguing,
so I wrote one this weekend. I wrote it for my egoistic fun, so
I'm not sure if it would be useful for you (or anyone else).


Whoa, that's your idea of a weekend project? I'm speechless.

I really appreciate the example, and I'll try my best to understand it.
Did you use a parser generator or anything like that, or is it purely
hand-coded?
Jul 18 '05 #6
Leif K-Brooks wrote:
Has anyone ever tried implementing a simple unstructured BASIC dialect
in Python? I'm getting interested in language implementation, and
looking at a reasonably simple example like that could be pretty
interesting.


On a whim, I implemented a Python BASIC interpreter which supported all
statements in primitive BASIC, but got bored before I implemented all
the standard operators in expressions.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Procrastination is the thief of time.
-- Edward Young
Jul 18 '05 #7
Not exactly what you're looking for, but I've implemented a scheme to
python translator. It converts a subset of scheme into python code
that should correctly run through eval/exec.

It can be found here:
http://www.isomorphism.org/~bbeck/pr...scheme2py.html
Leif K-Brooks <eu*****@ecritt ers.biz> wrote in message news:<2o******* *****@uni-berlin.de>...
Has anyone ever tried implementing a simple unstructured BASIC dialect
in Python? I'm getting interested in language implementation, and
looking at a reasonably simple example like that could be pretty
interesting.

Jul 18 '05 #8
On Sun, 22 Aug 2004 22:30:55 -0400, Leif K-Brooks
<eu*****@ecritt ers.biz> wrote:
Andrea Griffini wrote:
I've no idea why you think that an unstructured language would
be a good starting point.
There's no stack to deal with, and generally less possible expressions
to worry about. You're probably right about doing an expression parser
first, though; I will. Thanks for the advice.


I resorted to use two explicit stacks; one for FORs and one
for GOSUBs. There is no stack for expression evaluation because
python stack is used indirectly instead (expressions are evaluated
directly from the parse tree). Both of the explicit stacks
could have been dropped by considering a structured approach
with functions instead of the concept of just GOSUB-RETURN.
GOSUB-RETURN requires an explicit stack because it's a runtime
concept; not a parse-time concept. So it's not only uglier to
use... it's also harder to write.

Writing an interpreter that executes directly a parse tree of
a structured language program is IMO *easier* than writing an
interpreter for an unstructured language program.
That's why I think that starting from BASIC is not a good idea.

Things are different if we're talking about the VM approach,
but that's IMO something that should come later.
Anyway I found the idea of a BASIC interpreter in python intriguing,
so I wrote one this weekend. I wrote it for my egoistic fun, so
I'm not sure if it would be useful for you (or anyone else).


Whoa, that's your idea of a weekend project? I'm speechless.


Writing parsers, interpreters and compilers is a lot simpler than
many do think. Of course the evil is in the details and getting
something that comes even just close to (just for example ;-) )
python is a *LOT* harder.

I have "The dragon" book, and I saw that the part dedicated
to recursive descent parsers is incredibly small.
No wonder that writing compilers and interpreters is considered
such a difficult task... IMO the shift-reduce approach is just
*too* abstract (at least too abstract for *ME*, that is ;-) ).

Note also that working without any requirement (i.e. creating
requirements on the fly) and without any timeline (i.e. doing
whatever you think it would be nice to do) and without any
(pointy-haired) boss or (annoying) user is a lot easier.

I actually even think that the percentage of time I spent coding
the tic-tac-toe example in BASIC is not so small...
I really appreciate the example, and I'll try my best to understand it.
Did you use a parser generator or anything like that, or is it purely
hand-coded?


I never use yacc/lex, I just prefer hand-coding the parsers.

I never felt as pressing the problem of the speed of parsing
(I've read that this is one main superiority of shift-reduce
parsers). An hand-coded recursive descent parser is in my
opinion very easy to write... and very easy to maintain.
In the years I just found myself using tables for binary
operators to avoid repeating annoying code for just different
precedence levels or grouping; so I normally end up have just
one function for parsing "terms" (literals, varables, function
calls, parenthesized expressions and unary operators calls)
and one for parsing "expression s" that handles all binary
operators and that accepts the precedence level as a parameter.
Statements normally get their separate handcoded parsing
function (and this implies a lot of freedom about the syntax,
including making parsing dependent on the *meaning* of a symbol).
Also it's very easy to provide meaningful error messages.

For starting I would suggest however coding a separate
function at least for every precedence level (i.e. one for
mul/div, one for add/sub etc.... with lower precedence
ones that call higher precedence ones and with the highest
"term" one that calls the lowest "expr" for example for
parenthesized subexpressions) .

Andrea
Jul 18 '05 #9
Andrea Griffini wrote:
Writing an interpreter that executes directly a parse tree of
a structured language program is IMO *easier* than writing an
interpreter for an unstructured language program.
That's why I think that starting from BASIC is not a good idea.
I see what you're saying now. I had entirely forgotten about GOSUB and
FOR (I haven't used BASIC for a very long time, thank God), so I was
thinking thinking about stackless code with IF and GOTO for flow control.

Thanks a lot for the detailed replies, by the way; they've been very
helpful.
Writing parsers, interpreters and compilers is a lot simpler than
many do think.


Is there a (virtual) book you would recommend reading to learn about
writing them? A course isn't really possibly for me, so I'm hoping that
isn't the only option.
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1278
by: Etienne Posthumus | last post by:
I have been considering the various SSL/crypto libraries for Python, and they all have their plus and minus points. The main sticking point has been to have BOTH X509 file support and access to some crypto operations for signing/hashing. The only one which does both (please correct me if this is a misconception on my part) is http://sourceforge.net/projects/pow/ but I can find very few references to it, or projects that use it via...
9
5812
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is held in memory and therefore fast, without risk of damaging real files, and with none of the messiness of leftover files after testing. Googling the archives and the web suggests that only I and Remy Blank have done much along these lines. I...
33
2733
by: Quest Master | last post by:
I am interested in developing an application where the user has an ample amount of power to customize the application to their needs, and I feel this would best be accomplished if a scripting language was available. However, I want to code this application in Python, and I have not yet heard of an implementation of another scripting language into Python. An example of what I mean is this (an implementation of Lua into Ruby -- which I'd...
21
1563
by: nateastle | last post by:
I have a simple assignment for school but am unsure where to go. The assignment is to read in a text file, split out the words and say which line each word appears in alphabetical order. I have the basic outline of the program done which is: def Xref(filename): try: fp = open(filename, "r") lines = fp.readlines() fp.close()
1
1527
by: Chaven | last post by:
Hi Ive done some work on a basic python editor, with code folding, syntax highlighting and the idle shell. It also includes a forms editor to quickly build windows forms applications using wxpython widgets. The application still needs some finetuning especially the running of files as it doesnt pass code directly to the interpreter but uses a simple one liner os.startfile(args) which works only with certain files. The compiled file ~ 50 kB. ...
0
8778
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9242
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8188
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6033
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4551
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4814
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3262
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.