(continued)

It is time to organise ideas a bit. Codebot invited Professor Rigidobot Mikelo Ermichino from the University of CYOL. He had a few advices for the agency.

He was obfuscated to view the code used for the LugHa interpreter. He began right away.

Prof. Rigidobot : Well,

Butlerbot : Can i take away your bags from your shoulder Prof. ?

Prof. Rigidobot : ah sure,

Butlerbot : feel free to ask for help !

The Prof. began

Prof. Rigidobot : Well using slicing makes your code too rigid. difficult to update. Look at that.

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

langstring = reader.read() 

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])): #...in range(the number of times user specified)

                 print(statementslist[startindex:len(statementslist)])

        else:

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

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

it’s just crazy, we need to rewrite it all over

let us name the language “qwerty” with extention .qwer

first we open the file to read the source file

sourceText =open(‘source.qwer’,’r’).read()

nice now what are your keywords ? These make up the language right. What is you want to change the keyword? You go and make 1000 of changes. That’s what you are doing currently. Put them in variables. The keywords are @note and show. Now, let us capitalise them to view them better

PRINT_KEYWORD = "show"

TIMES_KEYWORD = "times"

COMMENTS_KEYWORD = "@note"

now let us reflect. we want the whole data in what format ? like that right

[[show, the, flaming, stars], [show, i, am, here], [show, qwertyqwerty]]

better shown as

[

-first statement

[

— word 1

show,

— word 2

the,

— word 3

flaming,

— word 4

stars],

-second statement

[show, … etc

so you first split at newline. yes, let us also put a new line variable

NEWLINE = “\n”let us consider the source file

show how are you
@note printing
show 2 times aaa
we split at new line to get the three statements separately
statements_list = sourceText.split(NEWLINE)

now we get the three statements separately.

Using a for loop we analyse each statement. The first then the second then the third

we split at white space to get a list of words

for i in range( len(statements_list) ):

    current_statement = statements_list[i]

    current_wordList =  current_statement.split()
now the first word of each sentence is  current_wordList[0]. we can create a variable for it if you wish.
    first_word = current_wordList[0]

    third_word = current_wordList[2]
but we must enclose it in a try-catch block as there are no guarantee that our line will be 3 words as in the case of
show flower
    try:

        first_word = current_wordList[0]

         third_word = current_wordList[2]

    except:

        pass
now we can add our conditionals
    if first_word == PRINT_KEYWORD :

         #do that

    elif first_word == PRINT_KEYWORD and third_word == TIMES_KEYWORD:

        #do this

    elif first_word == COMMENTS_KEYWORD :

        #do this
you can also write the version below to organise it better :
    if first_word == PRINT_KEYWORD :

        if third_word == TIMES_KEYWORD:

            #do this

        else:

            #do this

    elif first_word == COMMENTS_KEYWORD :

        #do this
our code upto now :
NEWLINE = "\n"

PRINT_KEYWORD = "show"

TIMES_KEYWORD = "times"

COMMENTS_KEYWORD = "@note"

sourceText =open('source.qwer','r').read()

statements_list = sourceText.split(NEWLINE)

for i in range( len(statements_list) ):

    current_statement = statements_list[i]

    current_wordList =  current_statement.split()

    try:

        first_word = current_wordList[0]

        third_word = current_wordList[2]

    except:

        pass

    if first_word == PRINT_KEYWORD :

        if third_word == TIMES_KEYWORD:

            #do this

        else:

            #do this

    elif first_word == COMMENTS_KEYWORD :

        #do this
now we add logic we added in the previous code. Let us begin for comments first.
    elif first_word == COMMENTS_KEYWORD :

        continue

now for print. let us take a simple show

we want to print from the second word

our data in current_wordList might be

[show, the, clock, is, ticking]

to print

the clock is ticking we do

whitespace =' '

part_to_print = whitespace.join(str(word) for word in current_wordList[1:])

whitespace.join means separate the elements by white space

current_wordList[1:] means select the second word onwards

        else:

            part_to_print = whitespace.join(str(word) for word in current_wordList[1:])

            print(part_to_print)

and the first condition:

we begin from the fourth word (with index 3)

        if third_word == TIMES_KEYWORD:

            part_to_print = whitespace.join(str(word) for word in current_wordList[3:])

            print(part_to_print)
but we must also print it the required number of times, which is the second word
            req_times = current_wordList[1]
but we must also covert it to a number
            req_times = int(current_wordList[1])
now we use a for loop to print it the number of times needed
            for a in range(req_times):

                print(part_to_print)

Our piece of code :
    if first_word == PRINT_KEYWORD :

        if third_word == TIMES_KEYWORD:

            part_to_print = whitespace.join(str(word) for word in current_wordList[3:])

            req_times = int(current_wordList[1])

            for a in range(req_times):

                print(part_to_print)
our whole code :
It’s a pleasure to read !
NEWLINE = "\n"

PRINT_KEYWORD = "show"

TIMES_KEYWORD = "times"

COMMENTS_KEYWORD = "@note"

sourceText =open('source.qwer','r').read()

statements_list = sourceText.split(NEWLINE)

for i in range( len(statements_list) ):

    current_statement = statements_list[i]
 
    current_wordList = current_statement.split()
 
    try:
 
        first_word = current_wordList[0]

        third_word = current_wordList[2]
    
    except:

        pass

    whitespace = ' '

    if first_word == PRINT_KEYWORD :

        if third_word == TIMES_KEYWORD:

            part_to_print = whitespace.join(str(word) for word in current_wordList[3:])
 
            req_times = int(current_wordList[1])
 
            for a in range (req_times):

                print(part_to_print)

        else:

            part_to_print = whitespace.join(str(word) for word in current_wordList[1:])

            print(part_to_print)

    elif first_word == COMMENTS_KEYWORD :

        continue

You : What that code is candy to the eyes.

Prof. Rigido :

Always name your variables right

Evaluate the readeability of your code

Make your code adaptive to changes . . .

You and codebot : Thank you Professor.

You hear the Professor going away while mumbling :

Crappy codes, i can’t stand them . . .

And of course, butlerbot had to wheel fast behind him to give him his bags

FUTURE STRUCTURATION PROJECTS

You continue your reading of The little book of language patternic implementations

You must perform a complete analysis of your source file You must know the position of each letter, the exact number of lines. Every thing . . .
This will help you include a debugging system . . . You can then tell the user : The error is found on line x
This will also help you more than you think of
You might want mapping everything.
… You flip a few more pages
How can a language manage without variables? It will be dumped right away
… Brr you continue flipping
And conditionals ? No conditionals? forget AI
… Yes, you must upgrade you tell yourself, you’ll do it
Craps no maths? dump it right now 
… Yes, you promise yourself that you will make it upto the end
No functions ? People have no time to waste on the scripts of a toddler. Repetitive and boring are alien words to programmers
… No you are not a toddler, you will master it
No classes ??? OOP fans want to punch you

… Ah you can create your own lang with OOP. Great it is cooler than you’ve ever thought

You close the book . . . You’ve been told enough

 

 

Technical notes :

we could’ve done instead of

for i in range( len(statements_list) ):
this :
for statement in statements_list:

    current_wordList = statement.split()
which is a more pythonic approach

Dear reader o] wait for next post [o

This is the sub chapter of a book

 

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