Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
a5e88b3
Create VMTranslator.py
ATherkel May 7, 2024
82cab9e
Test stuff for VMTranslator
ATherkel May 7, 2024
e909c25
readlines function (method) created
ATherkel May 7, 2024
0d95b17
Prepare classes
ATherkel May 7, 2024
a9a9d33
Create Resources.md
ATherkel May 7, 2024
d861070
Update Resources.md
ATherkel May 7, 2024
0a56b2b
Create read/write functions.
ATherkel May 7, 2024
d70c716
Unittest for writelines
ATherkel May 7, 2024
eb11ea4
Add regex101 explanation
ATherkel May 7, 2024
1a32324
Create changeExtension method
ATherkel May 8, 2024
f633ce9
Development on `main`
ATherkel May 8, 2024
fdcf5c1
Following unit 1.8 for parser
ATherkel May 8, 2024
481ea32
Prepare codeWriter
ATherkel May 9, 2024
ea74103
Create translation scripts.
ATherkel May 12, 2024
cc3db87
Minor work
ATherkel May 12, 2024
63fba16
pop newline
ATherkel May 12, 2024
0b8dd80
Create pushSegment.asm
ATherkel May 12, 2024
cd66f49
Move functions to module
ATherkel May 12, 2024
758df9f
Create dictionary module
ATherkel May 12, 2024
00c507a
Function and dictionary prep
ATherkel May 12, 2024
5a353bc
Prepare f string translation
ATherkel May 12, 2024
08d1416
pycache files
ATherkel May 12, 2024
f863ce5
Move `.asm` to subfolder
ATherkel May 12, 2024
122b947
Restructure project
ATherkel May 13, 2024
4443974
Remove redundant files
ATherkel May 13, 2024
a9df59a
Restructure part 2
ATherkel May 13, 2024
f8596c6
Rename and parser work
ATherkel May 13, 2024
43645ea
Add Python version requirement
ATherkel May 13, 2024
fdd4072
Remove dead stuff
ATherkel May 13, 2024
f53b5cd
Add fns to parser class
ATherkel May 13, 2024
94572de
More parser trying
ATherkel May 13, 2024
2f22b63
New dictionary handling
ATherkel May 14, 2024
f4e8283
test .asm file with error
ATherkel May 14, 2024
b98cdff
New main - and testing
ATherkel May 14, 2024
128febe
dict __init__
ATherkel May 14, 2024
cca5e59
Parser class work
ATherkel May 14, 2024
271881e
Archived dict class, not used
ATherkel May 14, 2024
997557c
Garbage cleanup
ATherkel May 14, 2024
50fc014
Delete old version
ATherkel May 14, 2024
9a47139
Rename parser2 to parser
ATherkel May 14, 2024
1462d13
Remove constructor, handling in main
ATherkel May 14, 2024
bd6aea7
codewriter first work
ATherkel May 14, 2024
6b8a6b0
Remove wrong docstring
ATherkel May 14, 2024
59d33d5
Rename to avoid overload
ATherkel May 14, 2024
56929d4
Initial codewriter work
ATherkel May 14, 2024
6b7d18d
Update parser.py
ATherkel May 14, 2024
a919093
Clean up
ATherkel May 15, 2024
db10a76
Clean up
ATherkel May 15, 2024
df7ff27
Test
ATherkel May 15, 2024
9e1f74f
Corrected pointer
ATherkel May 15, 2024
db4e6ed
Simplify
ATherkel May 15, 2024
028a81a
writePushPop work
ATherkel May 15, 2024
7a9707b
Ignore created asm files
ATherkel May 16, 2024
08d80b0
Test with constant
ATherkel May 16, 2024
e8e44c9
Create pushConstant.asm
ATherkel May 16, 2024
d33c54d
Correct pushSegment.asm
ATherkel May 16, 2024
4131992
Update segment in dict.py
ATherkel May 16, 2024
e9fc652
Move class call inside `main`
ATherkel May 16, 2024
2d67dda
Update codewriter.writePushPop
ATherkel May 16, 2024
40ed9bc
Segment handling
ATherkel May 16, 2024
195655b
VMinstruction correction.
ATherkel May 16, 2024
10ed314
VMinstruction correction
ATherkel May 16, 2024
5a5d316
Update writePushPop
ATherkel May 16, 2024
e43f12e
Create arithmetic asm files
ATherkel May 16, 2024
bfe12df
Update VMTranslator
ATherkel May 16, 2024
3a484e7
Update VMTranslator
ATherkel May 16, 2024
9377149
Update codewriter, create processCommands
ATherkel May 16, 2024
ff10599
Update dict.py
ATherkel May 16, 2024
c239eec
Create test_arithmetic.py
ATherkel May 16, 2024
250356d
Update test_arithmetic.py
ATherkel May 16, 2024
bea7c01
Change for Python debugger
ATherkel May 17, 2024
4ae24d8
Python debugger
ATherkel May 17, 2024
d3a9feb
Fix vwriteArithmetic
ATherkel May 17, 2024
582e054
Create test scripts
ATherkel May 17, 2024
58709b8
Merge pull request #20 from ATherkel/codewriter
ATherkel May 17, 2024
3881e02
Create .gitignore
ATherkel May 17, 2024
3104b09
Run from command line
ATherkel May 17, 2024
90261fb
Create extra test `.vm` scripts
ATherkel May 18, 2024
ebb1fa0
Archive previous .asm versions
ATherkel May 18, 2024
c4fb87a
Create `.asm` elements
ATherkel May 18, 2024
1746c04
Update codewriter
ATherkel May 18, 2024
e75baed
Push commands fixed
ATherkel May 18, 2024
e5407ed
Create pushTemp.md
ATherkel May 18, 2024
af6434e
Create pushSegment.md
ATherkel May 18, 2024
3bfaec2
Create pushConstant.md
ATherkel May 18, 2024
112e05f
Create pushStatic.md
ATherkel May 18, 2024
42d6cd1
Pop command work
ATherkel May 18, 2024
2ea980a
Passes BasicTest
ATherkel May 18, 2024
3cc56ed
Passes PointerTest
ATherkel May 19, 2024
4e20eec
Merge pull request #21 from ATherkel/writePushPop-splitTasks
ATherkel May 19, 2024
a3a53b8
Create pushPointer.md
ATherkel May 19, 2024
ea0de73
Update pushStatic.md
ATherkel May 19, 2024
105079a
Create test cases
ATherkel May 19, 2024
885e29d
Update VMTranslator.py
ATherkel May 19, 2024
1507f00
Update VMTranslator.py
ATherkel May 19, 2024
a770e13
Merge pull request #22 from ATherkel/documentation_push
ATherkel May 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}\\nand2tetris\\projects\\7\\VMTranslator",
"env": {
"PYTHONPATH": "${workspaceFolder}\\nand2tetris\\projects\\7\\VMTranslator"
}
},
{
"name": "Python Debugger: Python File",
"type": "debugpy",
"request": "launch",
"program": "${file}"
}
]
}
1 change: 1 addition & 0 deletions nand2tetris/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tools/*
2 changes: 2 additions & 0 deletions nand2tetris/projects/7/MemoryAccess/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.asm
*.out
23 changes: 23 additions & 0 deletions nand2tetris/projects/7/Mytests/popLocal.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// push constant 10
@10
D = A
@SP
A = M
M = D
@SP
M = M + 1
// pop local 2
@2
D = A
@LCL
D = D + A
@SP
M = M - 1
@R13
M = D
@SP
A = M
D = M
@R13
A = M
M = D
14 changes: 14 additions & 0 deletions nand2tetris/projects/7/Mytests/popLocal.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

load popLocal.asm,

set RAM[0] 256, // initializes the stack pointer

repeat 450 { // enough cycles to complete the execution
ticktock;
}

// Outputs the value at the stack's base, THIS, THAT, and
// some values from the the this and that segments
output-list RAM[256]%D1.6.1 RAM[3]%D1.6.1
RAM[4]%D1.6.1 RAM[3032]%D1.6.1 RAM[3046]%D1.6.1;
output;
2 changes: 2 additions & 0 deletions nand2tetris/projects/7/Mytests/popLocal.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
push constant 10
pop local 2
11 changes: 11 additions & 0 deletions nand2tetris/projects/7/Mytests/popLocalVME.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load popLocal.vm,

set sp 256, // stack pointer
set local 300, // base address of the local segment
set argument 400, // base address of the argument segment
set this 3000, // base address of the this segment
set that 3010, // base address of the that segment

repeat 2 {
vmstep;
}
37 changes: 37 additions & 0 deletions nand2tetris/projects/7/Mytests/testPointer.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// push constant 3000
@3000
D = A
@SP
A = M
M = D
@SP
M = M + 1
// pop pointer 0
@THIS
D = A
@SP
M = M - 1
@R13
M = D
@SP
A = M
D = M
@R13
A = M
M = D
// push constant 5
@5
D = A
@SP
A = M
M = D
@SP
M = M + 1
// push pointer 0
@THIS
D = M
@SP
A = M
M = D
@SP
M = M + 1
8 changes: 8 additions & 0 deletions nand2tetris/projects/7/Mytests/testPointer.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

load testPointer.asm,

set RAM[0] 256, // initializes the stack pointer

repeat 450 { // enough cycles to complete the execution
ticktock;
}
4 changes: 4 additions & 0 deletions nand2tetris/projects/7/Mytests/testPointer.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
push constant 3000
pop pointer 0
push constant 5
push pointer 0
11 changes: 11 additions & 0 deletions nand2tetris/projects/7/Mytests/testPointerVME.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load testPointer.vm,

set sp 256, // stack pointer
set local 300, // base address of the local segment
set argument 400, // base address of the argument segment
set this 3000, // base address of the this segment
set that 3010, // base address of the that segment

repeat 6 {
vmstep;
}
71 changes: 71 additions & 0 deletions nand2tetris/projects/7/Mytests/testPush.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// push constant 2
@2
D = A
@SP
A = M
M = D
@SP
M = M + 1
// push local 3
@3
D = A
@LCL
A = D + A
D = M
@SP
A = M
M = D
@SP
M = M + 1
// push argument 4
@4
D = A
@ARG
A = D + A
D = M
@SP
A = M
M = D
@SP
M = M + 1
// push this 0
@0
D = A
@THIS
A = D + A
D = M
@SP
A = M
M = D
@SP
M = M + 1
// push that 0
@0
D = A
@THAT
A = D + A
D = M
@SP
A = M
M = D
@SP
M = M + 1
// push pointer 0
@0
D = A
@None
A = D + A
D = M
@SP
A = M
M = D
@SP
M = M + 1
// push static 5
@testPush.5
D = A
@SP
A = M
M = D
@SP
M = M + 1
9 changes: 9 additions & 0 deletions nand2tetris/projects/7/Mytests/testPush.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


push constant 2
push local 3
push argument 4
push this 0
push that 0
push pointer 0
push static 5
11 changes: 11 additions & 0 deletions nand2tetris/projects/7/Mytests/testPushVME.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load testPush.vm,

set sp 256, // stack pointer
set local 300, // base address of the local segment
set argument 400, // base address of the argument segment
set this 3000, // base address of the this segment
set that 3010, // base address of the that segment

repeat 7 { // testPush.vm has 25 VM commands
vmstep;
}
6 changes: 6 additions & 0 deletions nand2tetris/projects/7/Mytests/testStatic.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Testing static

push static 5
push static 6

add
2 changes: 2 additions & 0 deletions nand2tetris/projects/7/StackArithmetic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.asm
*.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/7/StackArithmetic/SimpleAdd/SimpleAdd.vm

// Pushes and adds two constants.

push constant 7
puhhh constant 8
add
5 changes: 5 additions & 0 deletions nand2tetris/projects/7/VMTranslator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## https://stackoverflow.com/questions/3719243/best-practices-for-adding-gitignore-file-for-python-projects

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Empty file.
120 changes: 120 additions & 0 deletions nand2tetris/projects/7/VMTranslator/VMTranslator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
## How the program is supposed to be run:
# https://www.coursera.org/learn/nand2tetris2/lecture/qmJl3/unit-1-8-vm-translator-proposed-implementation


#### ---- import ----
# https://stackoverflow.com/a/37867717/3560695

import re
import sys

## Import parser
## We have to use import_module since the path contains an illegal directory name (7).
# parser_module = importlib.import_module("src.parser.parser")
import src.parser.parser as p

# writer_module = importlib.import_module("src.codewriter.codewriter")
import src.codewriter.codewriter as cw


#### ---- main ----

#def main():


def main(filename = None):
print("VMTranslator start ...")
if filename is None:
# Check if the correct number of command-line arguments are provided
if len(sys.argv) != 2:
print("Usage: py VMTranslator.py <filename>")
return
filename = sys.argv[1]

## https://regex101.com/r/x0v99o/3
## https://stackoverflow.com/a/59696768/3560695
regex = r'^(?P<path>.*[\\\/])?(?P<name>\.*.*?)(?P<extension>\.[^.]+?|)$'

filename_write = re.sub(regex, r"\1\2.asm", filename)


Parser = p.parser
codewriter = cw.codewriter

with open(filename, 'r') as file, open(filename_write, "w") as file_write:
parser = Parser(file)
writer = codewriter(file_write)

lines = None

while parser.hasMoreCommands(): ## As long as file has more lines, do:
parser.advance() ## Go to the next line in the file.
parser.getinstruction()
if not parser.instruction: ## If instruction is blank, skip. Line consisted only of a comment.
continue
parser.getVMinstruction() ## sets parser.VMinstruction to current instruction

# print(f"---- Line {parser.lineNo} ----")
# print(f"instruction = {parser.instruction}")
# print(f"commandType() = {parser.commandType()}, type = {type(parser.commandType())}")
# print(f"arg1() = {parser.arg1()}")
# print(f"arg2() = {parser.arg2()}")


commandType = parser.commandType()
if parser.commandType() == "C_ARITHMETIC":
# Write arithmetic from arithmetic.asm
arg1 = parser.arg1()
lines = writer.writeArithmetic(arg1)

elif parser.commandType() in ["C_PUSH", "C_POP"]:
arg1 = parser.arg1()
arg2 = parser.arg2()
lines = writer.writePushPop(commandType, arg1, arg2, filename = filename)
else:
...

## lines is now the full .asm script. Write to output asm file.
file_write.write(f'// {parser.instruction}\n')
for line in lines:
file_write.write(f'{line}\n')
print("VMTranslator finished!")





import importlib

# ## Testing
# filename = '.../StackArithmetic/SimpleAdd/SimpleAdd.vm'
# filename = '.../StackArithmetic/Stacktest/Stacktest.vm'
# filename = '.../StackArithmetic/Stacktest/Stacktest.vm'

# importlib.reload(p)
# importlib.reload(cw)

# main(filename[1:])

if __name__ == "__main__":
main()




















1 change: 1 addition & 0 deletions nand2tetris/projects/7/VMTranslator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__package__ = ''
Loading