Python implementation and reading lines

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Python implementation and reading lines

AndyGeo
I'm trying to get the assembler with no symbols working first.

So in Python that requires 2 classes. Parser and Code.

I havn't done much file I/O in python and there seems to be a few ways to read the lines.

First of all i'm initializing my input file and opening it with something like this: f=open(inputFile, "r").
I'm guessing we need to keep track of which line we are on so i've created a variable called currentCommand.

Now for the hasMoreCommands method. I can use f.readline() which iterates over the lines, but there is no easy way to rewind a line back. Or is that the only way?

Reply | Threaded
Open this post in threaded view
|

Re: Python implementation and reading lines

ybakos
Do you see how your approach is at the mercy of treating the instructions as a file, and subject to the file I/O api that you are using? Can you think of how you might "move" the instructions into a data structure of some sort?
Reply | Threaded
Open this post in threaded view
|

Re: Python implementation and reading lines

cadet1620
Administrator
In reply to this post by AndyGeo
Two approaches work well in Python.

If you read the file line by line as you are currently doing, tweak the Parser interface a bit: eliminate hasMoreCommands and simply return True or False from parser.advance so your main parsing loop is simply:
    while parser.advance():
        # process parsed line

Another option is to read the entire file's lines into a list (there's a single call to do this) and run through the list twice.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Python implementation and reading lines

AndyGeo
Thank you for the help.
Reply | Threaded
Open this post in threaded view
|

Re: Python implementation and reading lines

AndyGeo
Just finished the assembler. I used Parser and Code Module and a dictionary for the symbol table.


Most of my python programs before were run straight from the IDLE and passing in files and running from the command prompt is new to me.

So an example of running the code:

C:\Python27\python assembler.py prog.asm

This is quite alot of typing and only works if both the assembler and .asm are in the same location.

I'm guessing the solution is to create a path variable much like I did to get python working in the command prompt by just typing python and it knows where to look. But how do i get the assembler to run in python without specifying like I did above?



Reply | Threaded
Open this post in threaded view
|

Re: Python implementation and reading lines

ybakos
AndyGeo wrote
I'm guessing the solution is to create a path variable much like I did to get python working in the command prompt by just typing python and it knows where to look. But how do i get the assembler to run in python without specifying like I did above?
On Windows, you "have to" do it this way unless you're using cygwin, a simulated unix environment.

A couple tips:
- if you're manually typing this at the shell prompt (the console window) then note that you can press the up arrow to recall the previous command
- create a text file called assemble.bat that contains the text "python assembler.py prog.asm" and run the .bat file

Reply | Threaded
Open this post in threaded view
|

Re: Python implementation and reading lines

cadet1620
Administrator
In reply to this post by AndyGeo
AndyGeo wrote
C:\Python27\python assembler.py prog.asm

This is quite alot of typing and only works if both the assembler and .asm are in the same location.

I'm guessing the solution is to create a path variable much like I did to get python working in the command prompt by just typing python and it knows where to look. But how do i get the assembler to run in python without specifying like I did above?
Add the directory containing your Python scripts to the PATH environment variable and add ".py" to the PATHEXT environment variable.

See http://docs.python.org/3/faq/windows.html#how-do-i-make-python-scripts-executable

P:\TECS\projects\06>echo %PATHEXT%
.py;.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.RB;.RBW

P:\TECS\projects\06>hasm Pong.asm
Code size = 27483 (0x6B5B)
Data size =    30 (0x001E)

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Python implementation and reading lines

AndyGeo
Sorted thanks.

I have one more question, the pong.asm file is quite large and it takes a few minutes to run while the provided assembler does this instantly. Is this just down to python?
Reply | Threaded
Open this post in threaded view
|

Re: Python implementation and reading lines

cadet1620
Administrator
Don't worry about your assembler's speed. Like the rest of the TECS course, getting it working is the goal.

Timing on my ancient 3 GHz Celeron running XP:
Supplied assembler about 0.5 sec.
My assembler 4.1 sec.

Part of the difference is that the supplied assembler only assembles memory-to-memory when you hit the translate button; the time it takes to read and write the files happens when you load or save.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Python implementation and reading lines

Anjum
Hi Mark,

I have only recently joined and have been working through the projects over the last 3 days - I have completed everything up to project 7.

The assembler I wrote is in Python and I get the following results with it when compiling Pong.asm:

Built-in assembler: just under 2s
My assembler: under 0.5s

I can make it available to you for review if you are interested as I have noted that you seem to be the main fountain of knowledge in the forums.

I wrote this in a Test-Driven manner (TDD) so my code contains a full set of unit tests as well.
Anjum.