Recurring issue (continued)

After sometimes Mr. Lug Ha revisited the agency.

You welcomed him with the oriental welcome of warmth, with a traditional round of coffee, tea being banned from imports. It would they said, protect the local growers whose products, of high quality were exported while show off packets appeared on the local market now and then  .  .  .

Around the light smell of beans which circumambulated around, Mr. Lug had few words to tell.

Mr. Lug Ha : The program works fine for now. However, i’d like to repeat boring stuffs without having to retype. The other day i copy pasted a line 100 times to get what i wanted . . . tiring.

You : ok you want to remove the tedious part . . . no worry i’ll give you. No don’t take the duo-rail back home, it won’t be long . . .

Butlerboooot take Mr. Lug to for a guided walk around, the time i finish. Send the bills to financebot as usual. 

butlerbot : fine master, commands noted.


you : ok now now he wants to repeat but in fewer lines he wants a loop. However a loop is too complex for him. A loop implies the program’s awareness of variables. A syntax for it, bools . . .

codebot : slow slow take it slow master. Design comes first

you : ah sorry, let us see. How can we do that he wants to repeat one line 100 times easily

some syntax exploration

loop the lamp is in the glass case 10 times

while x< ....

codebot : no use a simple syntax

you :

repeat the glass case is like a shining star 11 times

reshow walk on 2 legs 28 times

codebot : the repeat syntax is nice. However, putting the number of times wanted at the end makes the code difficult to read

you : let us see

repeat 11 times the glass case is like a shining star

yes better guess it’s ok now

codebot : yes but you are adding another keyword to your language. dealing with lesser keywords is better, provided readeability and easiness of use is not affected.

you : hum yes we can use show straight away

show 11 times the glass case is like a shining star

but then how do we differentiate between the above and

show the glass case is like a shining star

codebot : the difference between the two statements is the word ‘times’ as the 3rd word, we’ll use that.

you : but now what if i want to show ‘2 times the glass case is like a shining star’ would the program not get confused ?

show 2 times the glass case is like a shining star

show 2 times the glass case is like a shining star

codebot : yes you are right, users will have to stop using the word times as third word in a phrase starting with show. We’ll have to put a notice somewhere. The docs, that’s why it’s goof to read it . . . never assume.

Tightening the nuts

you : yes now let us see our code for now

reader =open('printlang.lug','r')

langstring = 

statementslist = langstring.split('\n')

for i in range (len(statementslist)):

    if statementslist[i][0:4] == 'show':

        print( statementslist[i][5:len(statementslist[i])])

    elif statementslist[i][0:5] == '@note':

codebot : speak out the logic you’ll have to build. Put yourself in the place of the program . . .

you : so,

if i see the word show

.    if the third word is ‘times’,

.        i print what is after the word ‘times’ the number of times specified before the word times

.    else,

.        i print what comes after the word show

codebot : nice now how do you pick out words in a phrase like

the sky is decorated

word 1: the

word 2:  sky

word 3: is

word 4: decorated

you : hum i split at whitespace

.split(‘ ‘) or .split()

then i get [the, sky, is, decorated]

the has index 0, is has index 1 and so on .  .  .

in ‘show 2 times something’ , times will have index 2

codebot : now code there i have removed the lines coming after the show part . . .

reader =open('printlang.lug','r')

langstring = 

statementslist = langstring.split('\n')

for i in range (len(statementslist)):

    if statementslist[i][0:4] == 'show':

        # *logic here*

    elif statementslist[i][0:5] == '@note':

you : hum yes and the

langstring = 

statementslist = langstring.split('\n')

for i in range (len(statementslist)):

    if statementslist[i][0:4] == 'show':

        showpart =statementslist[i].split()

        if showpart[2] =='times':

            # hum wait i must ask

            print( statementslist[i][5:len(statementslist[i])])
    elif statementslist[i][0:5] == '@note': 

codebot : yes master?

you : well i must print what if there is the word ‘times’?

codebot : print the 4th to the last words

you : or i think we can also print from x to end

show 10 times what bounties

we must print from what so, from the

show –                            4 characters

10 –                                 2 characters

times –                           5 characters

2 spaces in between – 2 characters

we must print from the 4+2+5+2=13 i.e. from the 13th character to the end / length of the string

codebot : yes but the user can say 1, 10, 100, 1000 …….. so that one will vary.

we already isolated it as it is in showpart at index 1 (second position). now to get it’s number of characters we use len()


you : yes we can put those which will not vary : 4+5+2 = 11

startindex = 11 + len(showpart[1])

and we print


codebot : yes but we must also specify the number of times to print. we can use a for loop

for x in range(int(showpart[1])):


you :

langstring = 

statementslist = langstring.split('\n')

for i in range (len(statementslist)):

    if statementslist[i][0:4] == 'show':

        showpart =statementslist[i].split()

        if showpart[2] =='times':

            startindex = 11 + len(showpart[1])

            for x in range(int(showpart[1])): range(the number of times user specified)



            print( statementslist[i][5:len(statementslist[i])])

    elif statementslist[i][0:5] == '@note': 

codebot : comprehention comes with coding . test it !

you :

show aaa

show 5 times bbb

 _____________________ output :







codebot : nice but remember to catch exceptions using the try catch block. You never know what can go wrong . . . but we’ll leave it out for now

codeinfig magazine

The author of a language called fig published his blog in magazine form. He was giving away tips for beginners in language creation he says in his usual unique style :

if you want to learn how to write a complex language first, get a big, big book on implementing programming languages, and get to work. people do, its probably how youd go about it if you had a college course in the subject and wanted your first language to look and walk and quack like the big languages you use everyday.

if, on the other hand you want to write a programming language that is simple enough to understand in a week or so, and start working your way up to more complex languages after having an easy one under your belt– well, keep reading. after i wrote fig, i started looking at how more complex languages were written, hoping to find the “secret” of making them far more complicated.

the secret is, there isnt one. learn how to get a more complicated parser, and youll have a more complicated language.

you can do it the following ways:

  • get a parser generator and learn and use its syntax (its supposed to be easier, especially if your target language is c.)
  • make your language free software and adapt an existing free software parser to your needs
  • learn how to write recursive parsers, perhaps starting as so many do with a parser for lisp-like languages

a big, big book can help you with any of those. as i said, i skip all that and go for:

  • write a simpler language to parse, and a simpler parser to parse it

writing an interpreter is easier if your language is going to have simple features. if its not going to have loops and conditionals, then an interpreter is simply easier to implement– you might even do it without it feeling like youre writing a programming language.

a couple tips for starters:

you can have limited loop functionality (the easy way) in an interpreted language, by putting the entire interpreter in a big loop, and implementing an exit command to stop looping. i watched one person with a tiny interpreter do this, and its one way to have some looping functionality without a lot of trouble.

you can have limited conditional functionality by creating functions that are conditional based on input– use your imagination there.

what makes an interpreter difficult to implement with full loop and conditional functionality is that you have to implement branching– you have to be able to move from one part of your program to another, rather than simply intepreting each line. in the old days, this was done with an intermediate translation to a “bytecode.” (this isnt a dead practice, as java shows, but i think it has probably evolved.)

im going to walk you through a simple interpreter, tell you how to avoid bytecode, and then offer you the option of compiling to a high-level language as the easiest way to have all the complex branching features you like. (thats what fig does.)

in other words, fig (as a language) offers the functionality of conditionals and loops, by translating (compiling) to python conditionals and python loops. thats the easiest way to have that– write a source-to-source compiler. but if you dont need that functionality (no loops or conditionals in the language i will walk you through) then an interpreter is even easier.


too easy:

if you really want to “cheat” then you can implement at the very least, a pseudo-language interpreter by abusing the exec() command in python. python has a ton of features that are wonderful for language implementation, and exec() is the one i deliberately avoided in fig, both for security and educational purposes.

wanna see a print command implemented using exec in python 2? you can laugh:

x = raw_input() ; x = x.split()
if len(x) > 1:
    if x[0].lower() == "print":
        exec("print " + " ".join(x[1:]))

The Journey Begins

Sunlight poured through the palm leaf-patterned ceiling while you wondered what the future held for you. It was, without doubt the start of an awesome and who knows . . . never ending journey reducing complexities never touched by many programmers into something you deem as . . .

Dear reader o] wait for next post [o

This is a sub chapter of a book


In need of managing bookmarks online? Try the awesome Bookmark Ninja