Skip to content

Commit d183e15

Browse files
committed
VMT bug-fixes, i.e. static variables now work as intended.
1 parent 59ad03b commit d183e15

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

07/vmt.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
from vmt_modules.code_writer import CodeWriter
1717

1818

19+
__author__ = "Merrick Ryman"
20+
__version__ = "1.1"
21+
22+
1923
def main():
2024
vm_path = Path(sys.argv[-1])
2125
vm_glob = []
@@ -35,6 +39,7 @@ def main():
3539

3640
code_writer = CodeWriter(asm_path)
3741
for vm_file in vm_glob:
42+
code_writer.set_file_name(vm_file.name)
3843
vm_parser = Parser(vm_file)
3944
while vm_parser.has_more_commands():
4045
if vm_parser.command_type() is Command.C_ARITHMETIC:

07/vmt_modules/code_writer.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@
99
from .command import Command
1010

1111

12+
__author__ = "Merrick Ryman"
13+
__version__ = "1.1"
14+
15+
1216
class CodeWriter:
1317
def __init__(self, out_path):
1418
self.out_path = out_path
1519
self._dynamic_labels = {'lt':0, 'eq':0, 'gt':0}
1620
self._bool_jmp_logic_symbol = ''
1721
self._asm_file = open(out_path, 'w+')
22+
self._vm_file_name = None
1823

1924

2025
def set_file_name(self, file_name):
2126
"""Informs the code writer that the
2227
translation of a new VM file has started.
23-
(Possibly for dynamic label making)?
2428
"""
29+
self._vm_file_name = file_name[:file_name.find('.')]
2530

2631

2732
def write_arithmetic(self, command):
@@ -105,8 +110,7 @@ def write_push_pop(self, command, segment, index):
105110
self._asm_file.write('@5\n')
106111
self._asm_file.write('A=D+A\n')
107112
elif segment == 'static':
108-
self._asm_file.write('@16\n')
109-
self._asm_file.write('A=D+A\n')
113+
self._asm_file.write('@'+self._vm_file_name+'.'+str(index)+'\n')
110114
self._asm_file.write('D=M\n')
111115
self._asm_file.write('@SP\n')
112116
self._asm_file.write('AM=M+1\n')
@@ -136,8 +140,8 @@ def write_push_pop(self, command, segment, index):
136140
self._asm_file.write('@5\n')
137141
self._asm_file.write('D=D+A\n')
138142
elif segment == 'static':
139-
self._asm_file.write('@16\n')
140-
self._asm_file.write('D=D+A\n')
143+
self._asm_file.write('@'+self._vm_file_name+'.'+str(index)+'\n')
144+
self._asm_file.write('D=A\n') # d = d + a
141145
self._asm_file.write('@R13\n')
142146
self._asm_file.write('M=D\n')
143147
self._asm_file.write('@SP\n')

07/vmt_modules/parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
from .command import Command
1313

1414

15+
__author__ = "Merrick Ryman"
16+
__version__ = "1.0"
17+
18+
1519
class Parser:
1620
def __init__(self, in_path):
1721
self.in_path = in_path

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# nand2tetris (The Elements of Computing Systems)
2-
My code for the hardware and software projects as described in The Elements of Computing Systems by Noam Nisan and Shimon Schocken.
3-
2+
My code for the hardware and software projects as described in The Elements of Computing Systems by Noam Nisan and Shimon Schocken.\
43
Abstraction rules all.
54

65
### Chapter 01 - Boolean Logic
@@ -29,11 +28,11 @@ Also take note that all A-Instructions have essentially 'piggy backed' onto C-In
2928
* *Macro-commands are like so:* 'M=D[123]' is equivalent to '@123' followed by 'M=D'.
3029
Of course, this also works for symbols: 'M=D[foo]' is equivalent to '@foo' followed by 'M=D'.
3130

32-
### Chapter 07 - VM I: Stack Arithmetic
31+
### Chapter 07 - Virtual Machine I: Stack Arithmetic
3332
Python3 for Virtual Machine Translator.\
3433
Python3 for VMT modules CodeWriter, Parser, and Command.
3534

36-
### Chapter 08 - VM II: Program Control
35+
### Chapter 08 - Virtual Machine II: Program Control
3736
#### In Progress.
3837

3938
### Chapter 09 - High-Level Language

0 commit comments

Comments
 (0)