Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info

READ-ONLY-MODE: PLEASE DO NOT POST NEW STUFF!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile       

The time now is Mon 10 Aug 2020, 02:34
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
how to create a simple programming language in python
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [9 Posts]  
Author Message
learnhow2code

Joined: 12 Jun 2016
Posts: 1015

PostPosted: Wed 22 Jun 2016, 14:09    Post subject:  how to create a simple programming language in python  

not much of a "howto," more of a simple example.

you wont be able to parse very complicated expressions this way... for that you either need to write a parser or use a custom parser/parser generator.

not wanting to bother with a recursive parser is no reason to avoid making your own language. if you simply create functions and a simple parser like the one here, you can actually change the way the language gets parsed in a later version if you really want.

Code:
# a very simple language interpreter, to demonstrate how to write a simple one
# works in python 2 or 3

#### license: creative commons cc0 1.0 (public domain)
#### http://creativecommons.org/publicdomain/zero/1.0/

from __future__ import print_function

prog = """# here is a very simple, public domain interpreter written in python.
# what you are reading is the example program.
let x=hello there
let y=this is a program

display y
say this will print out directly
display x

display y
display y
stop
"""

vars={}

def stopprog(run):
    print() ; print("error in this line:")
    print(run)
    print() ; print("stopping program.")
    quit()

def getfirst(run):
    if run.strip()[0] == "#":
        return "#"
    return run.lstrip().split(" ")[0].lower()

def getvars(run, p):
    if p != 0:
        p = 1
    run = run.lstrip().lower()[4:].split("=")
    return run[p].strip()

def getbalance(run, command):
    return run.lstrip().lower()[len(command) + 1:]

def quitprog(vars):
    stopverbose = 1
    if stopverbose == 1:
        print() ; print("normal program stop. here are the variables used:")
        k = list(vars.keys()) ; k.sort()
        for v in k:
            print(v + " is set to: " + vars[v])
    quit()

while 1:
    for run in prog.replace("\r\n","\n").split("\n"):
        if run.strip() != "":


            if getfirst(run) == "stop":
                quitprog(vars)


            elif getfirst(run) == "display":
                try:
                    v = vars[run.lstrip().lower().split(" ")[1].strip()]
                    print(v)
                except: stopprog(run)


            elif getfirst(run) == "say":
                try:
                    v = getbalance(run, "say")
                    print(v)
                except: stopprog(run)


            elif getfirst(run) == "let":
                try:
                    n = getvars(run, 0)
                    v = getvars(run, 1)
                    vars[n] = v
                except: stopprog(run)


            elif getfirst(run) == "#":
                pass


            else: stopprog(run)
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Wed 22 Jun 2016, 14:14    Post subject:  

Euh... I thought Python was already a programming language?!
(Icon: scratching my head) Smile

_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
learnhow2code

Joined: 12 Jun 2016
Posts: 1015

PostPosted: Wed 22 Jun 2016, 15:11    Post subject:  

musher0 wrote:
Euh... I thought Python was already a programming language?!

youre 100% correct.

more to the point, its a higher level language-- symbolically, it doesnt map directly to machine code like lower-level language does.

its becoming more common to have languages implemented in other languages:

* python interpreters implemented in javascript
* basic intepreters implemented in python or js
* basic compilers implemented in c++
* basic interpreters and compilers implemented in c

fig is a source-to-source compiler implemented in python, and this is an interpreter implemented in python. here is another: https://sourceforge.net/projects/pcbasic/

and a very complex tutorial on implementing a basic dialect in python: https://www.youtube.com/playlist?list=PLBOh8f9FoHHiKx3ZCPxOZWUtZswrj2zI0

why implement a language in a higher level language? for one, its a lot easier. not just easier to accomplish, but easier to learn, to maintain, and update.

id rather have 500 people make a toy language and some go on to do more "professional" grade compilers, than have only those that are going to write professional-level compilers even try to start on a toy version.

5 reasons to create your own language?

1. its easier than you think
2. its fun and rewarding
3. it will help you understand how programming really works
4. it will help you appreciate programming and language developers more
5. you can write a useful language if you dont make it too general-purpose

thats 5 of 20 Smile but unless people realize the first one: its easier than they think, most wont try. my advice to anyone even slightly interested, is spend a few days or a week.

the interpreter listed in the first post: probably an hour or so.

if you want to understand how computers and language interpreters work, then watch this machine:

https://www.youtube.com/watch?v=IvUU8joBb1Q

it interprets legos or pegs stuck in legos, and uses marbles the way that current is used in transistors.
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Wed 22 Jun 2016, 15:51    Post subject:  

Hi learnhow2code.

Thanks for the kind invitation, but (hehe) I'm busy enough (still) learning bash and
urxvt (after all those years!).

I don't think I want to add my computer sub-language to the hundreds and hundreds
of computer languages already in existence:
https://en.wikipedia.org/wiki/List_of_programming_languages Shocked

Remember Ockham's Razor? My medieval buddy William of Ockham once said:
"Let's not multiply logical entities needlessly." I think he's right: we already have too
many needless logical entities as it is! (Not to mention the illogical ones!) Wink

BFN.

_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
learnhow2code

Joined: 12 Jun 2016
Posts: 1015

PostPosted: Wed 22 Jun 2016, 16:15    Post subject:  

musher0 wrote:
I don't think I want to add my computer sub-language to the hundreds and hundreds
of computer languages already in existence:
https://en.wikipedia.org/wiki/List_of_programming_languages Shocked


its so funny to me that people would see the point of creating their own distro, but not their own language.

the more people that make a puplet, the smaller the odds that an individual hobby distro is going to rise to prominence. in other words, there is basically zero risk of distrowatch getting flooded with puplets.

same with languages. people make them all the time-- you could make 3 toy langauges this week, it wouldnt affect wikipedia in the slightest.

Quote:
Remember Ockham's Razor? My medieval buddy William of Ockham once said:
"Let's not multiply logical entities needlessly." I think he's right:


no you dont, thats a very silly interpretation of what he said.

again, there are lots of reasons to create a distro (or spin, or puplet) or a language.

the easiest reason NOT TO is "i dont want to." no one will make you (unless you take a language development course.) im just here to make CERTAIN you dont want to Wink

i would feel good if i could even help you trade a lame excuse for a better one Smile and even if you dont create a language (or learn to code, but you ARE learning to if youre using rxvt or urxvt) at least you might end up with a better idea of how they work from thinking about it.

no worries, take care.

_________________
users | devs
fig os 1.3 md5
Back to top
View user's profile Send private message 
6502coder


Joined: 23 Mar 2009
Posts: 682
Location: Western United States

PostPosted: Wed 22 Jun 2016, 17:10    Post subject:  

Given the "in python" qualifier in the thread title, my remarks here are technically off-topic. For that I apologize.

Anyway, I agree completely that tinkering with parsers is a fine way to learn -- and not just about parsers. Hash tables, linked lists, and stacks are just a few of the topics that almost inevitably come up when you study parsers.

Those who would rather not tackle Python, but who are already familiar with C or a C-like language (e.g. Javascript), might consider AWK as an alternative implementation language. Indeed, "The AWK Programming Language" by Aho, Weinberger, and Kernighan, devotes a whole chapter to showing how to implement "little languages" in AWK. Their focus is not on creating Yet Another General-Purpose Language, but in crafting tiny special-purpose languages to solve particular problems.

As an example they write a plain-language front-end for the "sort" command, which translates

Code:
     field separator is :
     primary key is field 1
          increasing alphabetic
     secondary key is field 5
          reverse numeric


into

Code:
     sort -t':' +0 -1 +4rn -5


Leaving aside the question of whether AWK is the implementation language of choice, this sort of example illustrates the kind of tinkering with parsers that can be both useful and instructive for a modest investment of effort, if one is so inclined.
Back to top
View user's profile Send private message 
learnhow2code

Joined: 12 Jun 2016
Posts: 1015

PostPosted: Wed 22 Jun 2016, 19:16    Post subject:  

6502coder wrote:
Given the "in python" qualifier in the thread title, my remarks here are technically off-topic. For that I apologize.


not at all! your input is most welcome.


Quote:
Anyway, I agree completely that tinkering with parsers is a fine way to learn -- and not just about parsers. Hash tables, linked lists, and stacks are just a few of the topics that almost inevitably come up when you study parsers.


hash tables (in python speak, dictionaries) are extremely useful for this-- one is used in the example to track variables, but they arent necessary.


Quote:
Those who would rather not tackle Python, but who are already familiar with C or a C-like language (e.g. Javascript), might consider AWK as an alternative implementation language.


i like awk. however, since regexes cant even parse html reliably, it might be difficult for a beginner to use awk for any sort of compiling. but it depends on the language. with clear demarcation between fields (like awk and the example language have) it may be a reasonable tool for the job.

Quote:
"The AWK Programming Language" by Aho, Weinberger, and Kernighan, devotes a whole chapter to showing how to implement "little languages" in AWK.


i may check that out, thanks.


Quote:
Their focus is not on creating Yet Another General-Purpose Language, but in crafting tiny special-purpose languages to solve particular problems.


that makes it easier to prove such a task is practical, but i would be looking for arguments that its easy and fun.

tiny "general purpose" languages are also possible. they just have limited (though general purpose) features.


Quote:
As an example they write a plain-language front-end for the "sort" command


cool. like a real database query!


Quote:
Leaving aside the question of whether AWK is the implementation language of choice, this sort of example illustrates the kind of tinkering with parsers that can be both useful and instructive for a modest investment of effort, if one is so inclined.


...how extensive is that plain language sort front-end? loc, for example?


about this thread: by no means did i intend to restrict it to python. bash, javascript, basic, even lua are welcome. (im not a huge lua fan-- i probably ought to be, i just dont find it as friendly as so many others do.)

as for other languages, im not going to try to stop anyone. the languages i mentioned are the ones where im more likely to know what someone is talking about.

c is also welcome, though i dont personally think its nearly friendly enough to write a language with (if youre a beginner coder.) the advantage of c apart from being a language that can do practically anything, is that most of the tools designed to generate parsers are in c or c-like languages. but there are generators for python, js and i think even php.

i try avoid languages that change dramatically between generations, perl in particular. im not at all thrilled about python 3, though i do use it. perl is sort of a cool language. too much punctuation for no standards between old and new versions, for my taste.

fig actually is a good language for demonstrating how compilers and interpreters work. i reimplemented someones esolang intepreter in fig. but for all of pythons flexibility, fig with inline python code is going to be better for making your own language than say, fig with only native commands-- fig itself is implemented in python, and uses one or two dictionaries (hash tables) that are so useful.

for anyone wondering what a hash table is:

its just an array. an array (or in python, "list") is to a variable what a month or week is to a day:

a variable is a value and a label.

an array is a group of one or more values with a single label-- generally with a numeric index to keep track of which value youre accessing.

an dictionary (aka hash table) is a group of one or more value-pairs (called key/value pairs) where instead of an ordered group (numeric index) of values, you can get them from a name (the key) (this post is in the public domain):


variable:
-----
name: xwhatev
value: 10 (or perhaps "helllo")
...xwhatev is set to 10


array:
-----
name: ysomething
indexes: 0, 1, 2, 3, 4
values: 10, 0, 5, 12 (or perhaps "milk", "eggs", "sugar", "cheese", "butter")
...ysomething(index 2) is set to 5


hash table:
-----
name: zetc
keys: "age", "weight", "gender", "hair", "height"
values: 21, 140, "female", "red", "5'2"
...zetc(key height) is set to "5'2"


you can technically have numeric keys, but they arent indexes: hash table pairs are not stored in consecutive order.


the fun thing about implementing an interpreter (or compiler) using python is, you get these features in your own language.

as shown in the example, you can track the name/value pairs called "variables" in an interpreter using one hash table (python dictionary.)

however, python lets you have hash tables of arrays (lists) and hash tables of hash tables, so if you want all of those you can keep everything for your interpreter in a SINGLE hash table... if youre so inclined.



for a toy language, just implementing variables is sufficient.

you can do it with two arrays (lists) of the same length. hash tables are easier to manage, though if you insist on using two lists (or one list and some index math) it is possible without hash tables.

_________________
users | devs
fig os 1.3 md5
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Wed 22 Jun 2016, 20:17    Post subject:  

learnhow2code wrote:
(...)
Quote:
Remember Ockham's Razor? My medieval buddy William of Ockham once said:
"Let's not multiply logical entities needlessly." I think he's right:
no you dont, thats a very silly interpretation of what he said.
(...)
i would feel good if i could even help you trade a lame excuse for a better one Smile (...)
no worries, take care.
I'm not "picking up the gauntlet" you just threw at me.
"Silly interpretation"? "Lame excuse"? Hmm...
Watch your (human!) language, my friend. Being impolite won't get you far.

I try to take the Razor Principle very seriously in my bash scripts. Also, I believe
that the only way to learn a language thoroughly is to "stick with it".

What I mean is learning one language thoroughly is enough of a challenge. Why
create a sub-language when you've not mastered the main one yet? If you don't
know the main language through and through, it's very likely that the sub-language
you create will reflect such shortcomings.

To take an example: sure, you can find on the market two-octave electronic pianos
anyone can play; but I'd rather learn more and wait until I can play fluently on the
real 88-keys acoustic piano. Now that, will be really rewarding.

Creating sub-routines of awk -- or what have you -- for learning purposes, as
6502coder suggests, is fine, but I wouldn't confuse sample sub-routines with a
sub-language.

Obviously, I'm no Nolan Bushnell, nor am I Kenneth Iverson, so I'll stick to
familiar ground.

BFN.

_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
learnhow2code

Joined: 12 Jun 2016
Posts: 1015

PostPosted: Wed 22 Jun 2016, 22:06    Post subject:  

musher0 wrote:
I'm not "picking up the gauntlet" you just threw at me.
"Silly interpretation"? "Lame excuse"? Hmm...


Watch your (human!) language, my friend. Being impolite won't get you far.


desole! you definitely misunderstand me!

it was not a gauntlet i was throwing, i thought i was answering your banter is all. no impolite intentions at all... yes, i do think your interpretation of occam was "silly" but i thought you intended it as such. occam was not (imo) against the increasing of logic but of missing the point of reality/truth through an overabundance of detours.

or to paraphrase: there are never too many truths-- only too many justifications!

Quote:
I try to take the Razor Principle very seriously in my bash scripts. Also, I believe
that the only way to learn a language thoroughly is to "stick with it".


well, i tried that for 25 years, but in that time basic became something "not so basic." so i started looking for a language with the same advantages. then THAT language (python) became not so basic too, so i started trying to go towards "still more basic."

i tried teaching basic and python to people with less interest in programming. i wanted to spend more time on (usable, practical) concepts, not a quagmire of pointless syntax underfoot. apply the razor to syntax conventions-- do you really need so darned many as one finds in languages today?

i am not trying to get you to abandon your language of choice. but occam had little to say about computer languages. and i personally think you are twisting his words a little, but i might not understand what you said, just as you didnt understand what i intended.


Quote:
What I mean is learning one language thoroughly is enough of a challenge. Why
create a sub-language when you've not mastered the main one yet?


now there i dont really disagree. if that is your preference, i cant argue with it. i could try to say that you might be surprised how what you do in another language helps you improve your skills in the one you like-- but its not a point i would try to apply with pressure.

Quote:
If you don't
know the main language through and through, it's very likely that the sub-language
you create will reflect such shortcomings.


this is true, but it is possible to learn the language better through using it to make a language. i know that writing fig made me (at least a little) better at coding in python and in general.

think of what marathon-training does for someones abilities as a runner. but then writing a toy language isnt a marathon, its just another project. thats what id like to help people understand, its not a very big project unless you want it to be.

and how do you learn language? use it. how do you write a language? by using language. its not so linear, this learning thing. its holistic.

Quote:
I wouldn't confuse sample sub-routines with a
sub-language.


if you can implement a language without using subroutines then that is impressive. actually, you could have a big loop with if then statements... this is the thing about turing completeness. a turing machine is basically a big loop with if then statements.

i dont confuse a parser with a programming language, but people get stuck starting there. creating a group of subroutines that work just how you like is a great first step... you can start with either, but its easier to start with subroutines.

Quote:
Obviously, I'm no Nolan Bushnell, nor am I Kenneth Iverson, so I'll stick to
familiar ground.


shame, i hoped to make unfamiliar ground more friendly, but i dont want to twist anyones arm-- i do wish i could get you to be less dismissive, regardless of whether you change your mind.

hence the part about "lame excuse." i thought you were being playfully dismissive, and i only intended to parry and riposte. my remarks could be deliberately rude if i thought you were being rude, but that isnt what i thought or intended.

Quote:
BFN.


on the contrary, thank you for coming back. at least you did not get offended without letting me know. i will certainly take some care and patience to avoid another such misunderstanding, but dont be a stranger. cheers.

_________________
users | devs
fig os 1.3 md5
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [9 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0731s ][ Queries: 11 (0.0112s) ][ GZIP on ]