[Tutor] Write a programming language! (Was: Iterating through a list of strings)

Eike Welk eike.welk at gmx.net
Wed May 5 16:17:42 CEST 2010


Hello Thomas!

On Monday May 3 2010 07:16:17 Thomas C. Hicks wrote:
> I am using Python 2.6.4 in Ubuntu.  Since I use Ubuntu (with its every
> 6 months updates) and want to learn Python I have been working on a
> post-install script that would get my Ubuntu system up and running with
> my favorite packages quickly.  Basically the script reads a text file,
> processes the lines in the file and then does an apt-get for each line
> that is a package name.  The text file looks like this:
> 
> %Comment introducing the next block of packages
> %Below are the packages for using Chinese on the system
> %Third line of comment because I am a verbose guy!
> ibus-pinyin
> ibus-table-wubi
> language-pack-zh-hans
> 
> etc.

Write a programming language!

Experienced programmers know, that every file format will eventually evolve 
into a fully featured programming language. Therefore you should accept the 
laws of nature, and implement your file format as a programming language from 
the start.


You don't have to start from scratch though. I have written a rough framework 
for a special purpose language tailored to your needs. The code is here:
    http://pastebin.com/MVdFW3a9

To run a program, put it into a string, and call the function: 
    execute_program_str( program_text )
   
This little program shows all of the language's few features:

%Call operator (function) apt-get with multiple arguments
apt-get ibus-pinyin ibus-table-wubi "any letters can be here"
%Operators can be joined into a pipeline:
print | nop | cat foo bar baz
%There are brackets to group sub-expressions:
print first (cat foo bar baz) boo boum


You may ask: Is this a joke? Well it kind of is. I wanted to find out how long 
it would take me to write a very simpleminded, but usable language. It took me 
8 hours. Much longer than I had anticipated, even though the concept of the 
language is severely limited. 

Extending the language to have (for, while) loops is impossible without 
completely changing the concept. All computations are done in the parser's 
parse actions, there is no abstract syntax tree or byte-code. The parse 
actions are however only called once, when the parser recognizes the 
associated pattern in the text. 

Extending the language so that you can define new functions should be 
possible, but requires some thinking. Pipelines could be converted on the fly  
to lambdas. Partial function application (functools.partial) could be used to 
create new, special purpose versions of more general built in functions. 
Partial function application could also be used to express "if" statements 
IMHO.


Alternatively you could use a shell script to do your additional installation 
tasks. This is IMHO what the shell is good at. 


Eike.


More information about the Tutor mailing list