2

For the summer I am working through the nand2tetris course and am currently on the chapter 6 project (building an assembler). My choice for Python was motivated by the presence of the lovely dictionary. Problem is I cannot seem to work/create/access one correctly. In following the API for the chapter they recommend 3 distinct classes for the assembler, namely the Parser class, SymbolTable class, and Code class.

The machine we have built has the following specified mem. locations already dedicated to predefined symbols.

So with the following PreAllocated memory locations:

SP - (hex 00000, bin 0000000000000000)
LCL - (hex 00001, bin 0000000000000001)
ARG - (hex 00002, bin 0000000000000010)
THIS - (hex 00003, bin 0000000000000011)
THAT - (hex 0004, bin 0000000000000100)
R0-R15 - (hex 000[0-15], bin 000000000000[0-1111])
SCREEN - (hex 04000, bin 0100000000000000)
KBD - (hex 06000, bin 0110000000000000)

I wanted to create the class in such a manner that the table itself is the object, and when instantiated naturally assigns these {'key': value} pairs.

symbolTable.py:

class symbolTable(object):
def __init__(self):
        self.table = {'SP': 0000000000000000, 'LCL': 0000000000000001,
                      'ARG': 0000000000000010, 'THIS': 0000000000000011,
                      'THAT': 0000000000000100, 'R0': 0000000000000000,
                      'R1': 0000000000000001, 'R2': 0000000000000010,
                      'R3': 0000000000000011, 'R4': 000000000000100,
                      'R5': 0000000000000101, 'R6': 0000000000000110,
                      'R7': 0000000000000111, 'R8': 0000000000001000,
                      'R9': 0000000000001001, 'R10': 0000000000001010,
                      'R11': 0000000000001011, 'R12': 0000000000001100,
                      'R13': 0000000000001101, 'R14': 0000000000001110,
                      'R15': 0000000000001111, 'SCREEN': 0100000000000000,
                      'KBD': 0110000000000000};

Before moving on with the additions of the attributes I am trying to simply create this thing. Yet, when I pull up and run my main file, main.py:

__author__ = 'Cheech Wife'
import symbolTable
newTable = symbolTable.SymbolTable() 
print newTable

I get nothing:

C:\Python27\python.exe "C:/Users/Cheech Wife/Desktop/cs 271/Assembler_ch6/symbolTable.py"

Process finished with exit code 0

From what I have read, the file separation should not be the issue, and having worked with C++ I am a huge fan of the compartmentalization and would like the files to remain distinct. So what am I doing wrong?

is my error in trying to make the object of my class the dictionary itself? Am I doing something wrong when I attempt to instantiate the thing?

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • 1
    Are you running the `main.py` or the `symbolTable.py`? – Morgan Thrapp Jul 13 '15 at 18:19
  • Are you pasting actual code? Because the cases don't quite match. – pvg Jul 13 '15 at 18:24
  • There is also an indentation issue (see `__init__`). Anyway, after fixing these mistakes the code works correctly. – dlask Jul 13 '15 at 18:26
  • If you fix your syntax issues, this works just fine. And those constants are not binary but octal so you probably want to fix that as well. Oh and no semicolons, this is sparta. I mean, python. – pvg Jul 13 '15 at 18:29

1 Answers1

0

You do not need the semicolon at the end or you dictionary. C/C++ syntax remnants. Your definition in SymbolTable needs to be indented.

You get an exit code of 0 because you are running symbotTable.py instead of main.py.

You main.py should tell you where your object symboltable is. not it's content. something like:

And finally your numbers will be interpreted as octal not binary. Take a look at Binary numbers in Python

Community
  • 1
  • 1
Williamd
  • 16
  • 2
  • Awesome. Thank you. I forget pyCharm doesn't just run main when you say run.C:\Python27\python.exe "C:/Users/Cheech Wife/Desktop/cs 271/Assembler_ch6/main.py" – Charlotte Rose Hamilton Jul 14 '15 at 12:53
  • Also, will a simple for loop suffice for printing? Or will I need traditional getters and setters? – Charlotte Rose Hamilton Jul 14 '15 at 13:08
  • The table in SymbolTable can be accessed directly with something like:" 'newTable = SymbolTable.SymbolTable() print(newTable.table)' ' This would print the dictionary table If you want to print single elements you have to access them. 'print(newTable.table["SP"]) ' or you can loop through them 'for a in newTable.table: print (a) ' As for using getter or setters in my opinion, it's your call. – Williamd Jul 14 '15 at 14:33