mirror of
https://github.com/usatiuk/nand2tetris.git
synced 2025-10-28 16:17:48 +01:00
move interesting projects to the root
This commit is contained in:
@@ -1,200 +0,0 @@
|
||||
function Ball.new 0
|
||||
push constant 5
|
||||
call Memory.alloc 1
|
||||
pop pointer 0
|
||||
push argument 0
|
||||
pop this 0
|
||||
push argument 1
|
||||
pop this 1
|
||||
push argument 2
|
||||
pop this 2
|
||||
push argument 3
|
||||
pop this 3
|
||||
push argument 4
|
||||
pop this 4
|
||||
push pointer 0
|
||||
return
|
||||
function Ball.setPos 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Ball.erase 1
|
||||
pop temp 0
|
||||
push argument 1
|
||||
pop this 0
|
||||
push argument 2
|
||||
pop this 1
|
||||
push constant 0
|
||||
return
|
||||
function Ball.setSpeed 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push argument 1
|
||||
pop this 3
|
||||
push argument 2
|
||||
pop this 4
|
||||
push constant 0
|
||||
return
|
||||
function Ball.draw 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push constant 0
|
||||
not
|
||||
call Screen.setColor 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
push this 2
|
||||
call Screen.drawCircle 3
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Ball.erase 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push constant 0
|
||||
call Screen.setColor 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
push this 2
|
||||
call Screen.drawCircle 3
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Ball.update 5
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Ball.getLeft 1
|
||||
pop local 1
|
||||
push pointer 0
|
||||
call Ball.getRight 1
|
||||
pop local 2
|
||||
push pointer 0
|
||||
call Ball.getTop 1
|
||||
pop local 3
|
||||
push pointer 0
|
||||
call Ball.getBottom 1
|
||||
pop local 4
|
||||
push pointer 0
|
||||
call Ball.erase 1
|
||||
pop temp 0
|
||||
push local 1
|
||||
push constant 2
|
||||
lt
|
||||
push this 3
|
||||
and
|
||||
push constant 0
|
||||
lt
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push pointer 0
|
||||
call Ball.invertHspeed 1
|
||||
pop temp 0
|
||||
label IF_FALSE0
|
||||
push local 2
|
||||
push constant 508
|
||||
gt
|
||||
push this 3
|
||||
and
|
||||
push constant 0
|
||||
gt
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push pointer 0
|
||||
call Ball.invertHspeed 1
|
||||
pop temp 0
|
||||
label IF_FALSE1
|
||||
push local 3
|
||||
push constant 2
|
||||
lt
|
||||
push this 4
|
||||
and
|
||||
push constant 0
|
||||
lt
|
||||
if-goto IF_TRUE2
|
||||
goto IF_FALSE2
|
||||
label IF_TRUE2
|
||||
push pointer 0
|
||||
call Ball.invertVspeed 1
|
||||
pop temp 0
|
||||
label IF_FALSE2
|
||||
push local 4
|
||||
push constant 250
|
||||
gt
|
||||
push this 4
|
||||
and
|
||||
push constant 0
|
||||
gt
|
||||
if-goto IF_TRUE3
|
||||
goto IF_FALSE3
|
||||
label IF_TRUE3
|
||||
push pointer 0
|
||||
call Ball.invertVspeed 1
|
||||
pop temp 0
|
||||
label IF_FALSE3
|
||||
push this 0
|
||||
push this 3
|
||||
add
|
||||
pop this 0
|
||||
push this 1
|
||||
push this 4
|
||||
add
|
||||
pop this 1
|
||||
push pointer 0
|
||||
call Ball.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Ball.invertVspeed 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
neg
|
||||
pop this 4
|
||||
push constant 0
|
||||
return
|
||||
function Ball.invertHspeed 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 3
|
||||
neg
|
||||
pop this 3
|
||||
push constant 0
|
||||
return
|
||||
function Ball.getLeft 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 0
|
||||
push this 2
|
||||
sub
|
||||
return
|
||||
function Ball.getRight 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 0
|
||||
push this 2
|
||||
add
|
||||
return
|
||||
function Ball.getTop 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 1
|
||||
push this 2
|
||||
sub
|
||||
return
|
||||
function Ball.getBottom 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 1
|
||||
push this 2
|
||||
add
|
||||
return
|
||||
function Ball.getVspeed 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
return
|
||||
@@ -1,581 +0,0 @@
|
||||
function Game.new 3
|
||||
push constant 6
|
||||
call Memory.alloc 1
|
||||
pop pointer 0
|
||||
push constant 0
|
||||
pop this 2
|
||||
push constant 8
|
||||
pop this 0
|
||||
push constant 3
|
||||
pop this 1
|
||||
push constant 250
|
||||
push constant 220
|
||||
push constant 50
|
||||
push constant 10
|
||||
call Rect.new 4
|
||||
pop this 3
|
||||
push constant 250
|
||||
push constant 200
|
||||
push constant 6
|
||||
push constant 1
|
||||
push constant 1
|
||||
call Ball.new 5
|
||||
pop this 4
|
||||
push this 0
|
||||
push this 1
|
||||
call Math.multiply 2
|
||||
call Array.new 1
|
||||
pop this 5
|
||||
label WHILE_EXP0
|
||||
push local 0
|
||||
push this 0
|
||||
lt
|
||||
not
|
||||
if-goto WHILE_END0
|
||||
label WHILE_EXP1
|
||||
push local 1
|
||||
push this 1
|
||||
lt
|
||||
not
|
||||
if-goto WHILE_END1
|
||||
push local 0
|
||||
push constant 3
|
||||
call Math.multiply 2
|
||||
push local 1
|
||||
add
|
||||
push this 5
|
||||
add
|
||||
push local 0
|
||||
push constant 60
|
||||
call Math.multiply 2
|
||||
push constant 20
|
||||
add
|
||||
push local 1
|
||||
push constant 30
|
||||
call Math.multiply 2
|
||||
push constant 20
|
||||
add
|
||||
push constant 50
|
||||
push constant 15
|
||||
call Rect.new 4
|
||||
pop temp 0
|
||||
pop pointer 1
|
||||
push temp 0
|
||||
pop that 0
|
||||
push local 0
|
||||
push constant 3
|
||||
call Math.multiply 2
|
||||
push local 1
|
||||
add
|
||||
push this 5
|
||||
add
|
||||
pop pointer 1
|
||||
push that 0
|
||||
pop local 2
|
||||
push local 2
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push local 1
|
||||
push constant 1
|
||||
add
|
||||
pop local 1
|
||||
goto WHILE_EXP1
|
||||
label WHILE_END1
|
||||
push constant 0
|
||||
pop local 1
|
||||
push local 0
|
||||
push constant 1
|
||||
add
|
||||
pop local 0
|
||||
goto WHILE_EXP0
|
||||
label WHILE_END0
|
||||
push pointer 0
|
||||
return
|
||||
function Game.loop 1
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
label WHILE_EXP0
|
||||
push constant 0
|
||||
not
|
||||
not
|
||||
if-goto WHILE_END0
|
||||
push constant 16
|
||||
call Sys.wait 1
|
||||
pop temp 0
|
||||
push this 2
|
||||
push constant 0
|
||||
eq
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push pointer 0
|
||||
call Game.movePaddle 1
|
||||
pop temp 0
|
||||
push this 4
|
||||
call Ball.update 1
|
||||
pop temp 0
|
||||
push pointer 0
|
||||
call Game.chackBallPaddleCol 1
|
||||
pop temp 0
|
||||
push pointer 0
|
||||
call Game.checkBallBricksCol 1
|
||||
pop temp 0
|
||||
push pointer 0
|
||||
call Game.checkGameOver 1
|
||||
pop temp 0
|
||||
goto IF_END0
|
||||
label IF_FALSE0
|
||||
call Keyboard.keyPressed 0
|
||||
pop local 0
|
||||
push local 0
|
||||
push constant 82
|
||||
eq
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push pointer 0
|
||||
call Game.resetGame 1
|
||||
pop temp 0
|
||||
label IF_FALSE1
|
||||
label IF_END0
|
||||
goto WHILE_EXP0
|
||||
label WHILE_END0
|
||||
push constant 0
|
||||
return
|
||||
function Game.resetGame 3
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
call Screen.clearScreen 0
|
||||
pop temp 0
|
||||
push this 3
|
||||
push constant 250
|
||||
push constant 220
|
||||
call Rect.setPos 3
|
||||
pop temp 0
|
||||
push this 4
|
||||
push constant 250
|
||||
push constant 200
|
||||
call Ball.setPos 3
|
||||
pop temp 0
|
||||
push this 4
|
||||
push constant 1
|
||||
push constant 1
|
||||
call Ball.setSpeed 3
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
call Math.multiply 2
|
||||
pop local 1
|
||||
label WHILE_EXP0
|
||||
push local 0
|
||||
push local 1
|
||||
lt
|
||||
not
|
||||
if-goto WHILE_END0
|
||||
push local 0
|
||||
push this 5
|
||||
add
|
||||
pop pointer 1
|
||||
push that 0
|
||||
pop local 2
|
||||
push local 2
|
||||
push constant 0
|
||||
call Rect.setHidden 2
|
||||
pop temp 0
|
||||
push local 2
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push local 0
|
||||
push constant 1
|
||||
add
|
||||
pop local 0
|
||||
goto WHILE_EXP0
|
||||
label WHILE_END0
|
||||
push constant 0
|
||||
pop this 2
|
||||
push constant 0
|
||||
return
|
||||
function Game.checkGameOver 1
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
call Ball.getBottom 1
|
||||
pop local 0
|
||||
push local 0
|
||||
push constant 240
|
||||
gt
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push constant 1
|
||||
pop this 2
|
||||
push constant 0
|
||||
push constant 0
|
||||
call Output.moveCursor 2
|
||||
pop temp 0
|
||||
call Output.println 0
|
||||
pop temp 0
|
||||
push constant 9
|
||||
call String.new 1
|
||||
push constant 103
|
||||
call String.appendChar 2
|
||||
push constant 97
|
||||
call String.appendChar 2
|
||||
push constant 109
|
||||
call String.appendChar 2
|
||||
push constant 101
|
||||
call String.appendChar 2
|
||||
push constant 32
|
||||
call String.appendChar 2
|
||||
push constant 111
|
||||
call String.appendChar 2
|
||||
push constant 118
|
||||
call String.appendChar 2
|
||||
push constant 101
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
call Output.printString 1
|
||||
pop temp 0
|
||||
call Output.println 0
|
||||
pop temp 0
|
||||
push constant 18
|
||||
call String.new 1
|
||||
push constant 112
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
push constant 101
|
||||
call String.appendChar 2
|
||||
push constant 115
|
||||
call String.appendChar 2
|
||||
push constant 115
|
||||
call String.appendChar 2
|
||||
push constant 32
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
push constant 32
|
||||
call String.appendChar 2
|
||||
push constant 116
|
||||
call String.appendChar 2
|
||||
push constant 111
|
||||
call String.appendChar 2
|
||||
push constant 32
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
push constant 101
|
||||
call String.appendChar 2
|
||||
push constant 115
|
||||
call String.appendChar 2
|
||||
push constant 116
|
||||
call String.appendChar 2
|
||||
push constant 97
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
push constant 116
|
||||
call String.appendChar 2
|
||||
call Output.printString 1
|
||||
pop temp 0
|
||||
label IF_FALSE0
|
||||
push constant 0
|
||||
return
|
||||
function Game.movePaddle 3
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
call Keyboard.keyPressed 0
|
||||
pop local 0
|
||||
push this 3
|
||||
call Rect.getLeft 1
|
||||
pop local 1
|
||||
push this 3
|
||||
call Rect.getRight 1
|
||||
pop local 2
|
||||
push local 0
|
||||
push constant 130
|
||||
eq
|
||||
push local 1
|
||||
push constant 0
|
||||
gt
|
||||
and
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push this 3
|
||||
push constant 2
|
||||
call Rect.moveLeft 2
|
||||
pop temp 0
|
||||
label IF_FALSE0
|
||||
push local 0
|
||||
push constant 132
|
||||
eq
|
||||
push local 2
|
||||
push constant 510
|
||||
lt
|
||||
and
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push this 3
|
||||
push constant 2
|
||||
call Rect.moveRight 2
|
||||
pop temp 0
|
||||
label IF_FALSE1
|
||||
push this 3
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Game.chackBallPaddleCol 11
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 3
|
||||
call Rect.getLeft 1
|
||||
pop local 0
|
||||
push this 3
|
||||
call Rect.getRight 1
|
||||
pop local 1
|
||||
push this 3
|
||||
call Rect.getTop 1
|
||||
pop local 2
|
||||
push this 3
|
||||
call Rect.getBottom 1
|
||||
pop local 3
|
||||
push local 1
|
||||
push local 0
|
||||
add
|
||||
push constant 2
|
||||
call Math.divide 2
|
||||
pop local 4
|
||||
push this 4
|
||||
call Ball.getLeft 1
|
||||
pop local 5
|
||||
push this 4
|
||||
call Ball.getRight 1
|
||||
pop local 6
|
||||
push this 4
|
||||
call Ball.getTop 1
|
||||
pop local 7
|
||||
push this 4
|
||||
call Ball.getBottom 1
|
||||
pop local 8
|
||||
push local 6
|
||||
push local 5
|
||||
add
|
||||
push constant 2
|
||||
call Math.divide 2
|
||||
pop local 9
|
||||
push this 4
|
||||
call Ball.getVspeed 1
|
||||
pop local 10
|
||||
push local 5
|
||||
push local 0
|
||||
gt
|
||||
push local 6
|
||||
push local 1
|
||||
lt
|
||||
and
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push local 8
|
||||
push local 2
|
||||
gt
|
||||
push local 10
|
||||
push constant 0
|
||||
gt
|
||||
and
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push local 9
|
||||
push local 4
|
||||
gt
|
||||
if-goto IF_TRUE2
|
||||
goto IF_FALSE2
|
||||
label IF_TRUE2
|
||||
push this 4
|
||||
push constant 1
|
||||
push constant 1
|
||||
neg
|
||||
call Ball.setSpeed 3
|
||||
pop temp 0
|
||||
goto IF_END2
|
||||
label IF_FALSE2
|
||||
push this 4
|
||||
push constant 1
|
||||
neg
|
||||
push constant 1
|
||||
neg
|
||||
call Ball.setSpeed 3
|
||||
pop temp 0
|
||||
label IF_END2
|
||||
label IF_FALSE1
|
||||
label IF_FALSE0
|
||||
push constant 0
|
||||
return
|
||||
function Game.checkBallBricksCol 21
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
call Ball.getLeft 1
|
||||
pop local 5
|
||||
push this 4
|
||||
call Ball.getRight 1
|
||||
pop local 6
|
||||
push this 4
|
||||
call Ball.getTop 1
|
||||
pop local 7
|
||||
push this 4
|
||||
call Ball.getBottom 1
|
||||
pop local 8
|
||||
push this 4
|
||||
call Ball.getVspeed 1
|
||||
pop local 9
|
||||
push this 0
|
||||
push this 1
|
||||
call Math.multiply 2
|
||||
pop local 19
|
||||
label WHILE_EXP0
|
||||
push local 18
|
||||
push local 19
|
||||
lt
|
||||
not
|
||||
if-goto WHILE_END0
|
||||
push local 18
|
||||
push this 5
|
||||
add
|
||||
pop pointer 1
|
||||
push that 0
|
||||
pop local 20
|
||||
push local 20
|
||||
call Rect.getLeft 1
|
||||
pop local 0
|
||||
push local 20
|
||||
call Rect.getRight 1
|
||||
pop local 1
|
||||
push local 20
|
||||
call Rect.getTop 1
|
||||
pop local 2
|
||||
push local 20
|
||||
call Rect.getBottom 1
|
||||
pop local 3
|
||||
push local 20
|
||||
call Rect.getHidden 1
|
||||
pop local 4
|
||||
push local 4
|
||||
push constant 1
|
||||
lt
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push local 20
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push local 5
|
||||
push local 0
|
||||
gt
|
||||
push local 6
|
||||
push local 1
|
||||
lt
|
||||
and
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push local 8
|
||||
push local 2
|
||||
gt
|
||||
push local 7
|
||||
push local 3
|
||||
lt
|
||||
and
|
||||
if-goto IF_TRUE2
|
||||
goto IF_FALSE2
|
||||
label IF_TRUE2
|
||||
push local 20
|
||||
push constant 1
|
||||
call Rect.setHidden 2
|
||||
pop temp 0
|
||||
push local 20
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push local 6
|
||||
push local 0
|
||||
sub
|
||||
pop local 10
|
||||
push local 1
|
||||
push local 5
|
||||
sub
|
||||
pop local 11
|
||||
push local 8
|
||||
push local 2
|
||||
sub
|
||||
pop local 12
|
||||
push local 3
|
||||
push local 7
|
||||
sub
|
||||
pop local 13
|
||||
push local 10
|
||||
call Util.abs 1
|
||||
push local 11
|
||||
call Util.abs 1
|
||||
lt
|
||||
if-goto IF_TRUE3
|
||||
goto IF_FALSE3
|
||||
label IF_TRUE3
|
||||
push constant 1
|
||||
pop local 14
|
||||
push local 10
|
||||
pop local 16
|
||||
goto IF_END3
|
||||
label IF_FALSE3
|
||||
push local 11
|
||||
pop local 16
|
||||
label IF_END3
|
||||
push local 12
|
||||
call Util.abs 1
|
||||
push local 13
|
||||
call Util.abs 1
|
||||
lt
|
||||
if-goto IF_TRUE4
|
||||
goto IF_FALSE4
|
||||
label IF_TRUE4
|
||||
push constant 1
|
||||
pop local 15
|
||||
push local 12
|
||||
pop local 17
|
||||
goto IF_END4
|
||||
label IF_FALSE4
|
||||
push local 13
|
||||
pop local 17
|
||||
label IF_END4
|
||||
push local 16
|
||||
call Util.abs 1
|
||||
push local 17
|
||||
call Util.abs 1
|
||||
lt
|
||||
if-goto IF_TRUE5
|
||||
goto IF_FALSE5
|
||||
label IF_TRUE5
|
||||
push this 4
|
||||
call Ball.invertHspeed 1
|
||||
pop temp 0
|
||||
goto IF_END5
|
||||
label IF_FALSE5
|
||||
push this 4
|
||||
call Ball.invertVspeed 1
|
||||
pop temp 0
|
||||
label IF_END5
|
||||
label IF_FALSE2
|
||||
label IF_FALSE1
|
||||
label IF_FALSE0
|
||||
push local 18
|
||||
push constant 1
|
||||
add
|
||||
pop local 18
|
||||
goto WHILE_EXP0
|
||||
label WHILE_END0
|
||||
push constant 0
|
||||
return
|
||||
@@ -1,8 +0,0 @@
|
||||
function Main.main 1
|
||||
call Game.new 0
|
||||
pop local 0
|
||||
push local 0
|
||||
call Game.loop 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
@@ -1,173 +0,0 @@
|
||||
function Rect.new 0
|
||||
push constant 5
|
||||
call Memory.alloc 1
|
||||
pop pointer 0
|
||||
push argument 0
|
||||
pop this 0
|
||||
push argument 1
|
||||
pop this 1
|
||||
push argument 2
|
||||
pop this 2
|
||||
push argument 3
|
||||
pop this 3
|
||||
push constant 0
|
||||
pop this 4
|
||||
push pointer 0
|
||||
return
|
||||
function Rect.setPos 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push argument 1
|
||||
pop this 0
|
||||
push argument 2
|
||||
pop this 1
|
||||
push constant 0
|
||||
return
|
||||
function Rect.draw 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
push constant 1
|
||||
eq
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
label IF_FALSE0
|
||||
push constant 0
|
||||
not
|
||||
call Screen.setColor 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
push this 0
|
||||
push this 2
|
||||
add
|
||||
push this 1
|
||||
push this 3
|
||||
add
|
||||
call Screen.drawRectangle 4
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.erase 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push constant 0
|
||||
call Screen.setColor 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
push this 0
|
||||
push this 2
|
||||
add
|
||||
push this 1
|
||||
push this 3
|
||||
add
|
||||
call Screen.drawRectangle 4
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.moveLeft 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push argument 1
|
||||
sub
|
||||
pop this 0
|
||||
push pointer 0
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.moveRight 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push argument 1
|
||||
add
|
||||
pop this 0
|
||||
push pointer 0
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.moveUp 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push this 1
|
||||
push argument 1
|
||||
sub
|
||||
pop this 1
|
||||
push pointer 0
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.moveDown 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push this 1
|
||||
push argument 1
|
||||
add
|
||||
pop this 1
|
||||
push pointer 0
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.getLeft 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 0
|
||||
return
|
||||
function Rect.getRight 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 0
|
||||
push this 2
|
||||
add
|
||||
return
|
||||
function Rect.getTop 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 1
|
||||
return
|
||||
function Rect.getBottom 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 1
|
||||
push this 3
|
||||
add
|
||||
return
|
||||
function Rect.getHidden 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
return
|
||||
function Rect.setHidden 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push argument 1
|
||||
pop this 4
|
||||
push constant 0
|
||||
return
|
||||
@@ -1,15 +0,0 @@
|
||||
function Util.abs 0
|
||||
push argument 0
|
||||
push constant 0
|
||||
lt
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push argument 0
|
||||
neg
|
||||
return
|
||||
goto IF_END0
|
||||
label IF_FALSE0
|
||||
push argument 0
|
||||
return
|
||||
label IF_END0
|
||||
@@ -1,104 +0,0 @@
|
||||
class Ball {
|
||||
field int x,y, r, hspeed, vspeed;
|
||||
|
||||
constructor Ball new(int lx, int ly, int lr, int lhspeed, int lvspeed) {
|
||||
let x = lx;
|
||||
let y = ly;
|
||||
let r = lr;
|
||||
|
||||
let hspeed = lhspeed;
|
||||
let vspeed = lvspeed;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
method void setPos(int lx, int ly) {
|
||||
do erase();
|
||||
|
||||
let x = lx;
|
||||
let y = ly;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void setSpeed(int lhspeed, int lvspeed) {
|
||||
let hspeed = lhspeed;
|
||||
let vspeed = lvspeed;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void draw() {
|
||||
do Screen.setColor(true);
|
||||
do Screen.drawCircle(x,y,r);
|
||||
return;
|
||||
}
|
||||
|
||||
method void erase() {
|
||||
do Screen.setColor(false);
|
||||
do Screen.drawCircle(x,y,r);
|
||||
return;
|
||||
}
|
||||
|
||||
method void update() {
|
||||
var int keyPressed, bLeft, bRight, bTop, bBottom;
|
||||
|
||||
let bLeft = getLeft();
|
||||
let bRight = getRight();
|
||||
let bTop = getTop();
|
||||
let bBottom = getBottom();
|
||||
|
||||
do erase();
|
||||
|
||||
// 2 pixel margin on the sides just in case
|
||||
if (bLeft < 2 & hspeed < 0) {
|
||||
do invertHspeed();
|
||||
}
|
||||
if (bRight > 508 & hspeed > 0) {
|
||||
do invertHspeed();
|
||||
}
|
||||
if (bTop < 2 & vspeed < 0) {
|
||||
do invertVspeed();
|
||||
}
|
||||
if (bBottom > 250 & vspeed > 0) {
|
||||
do invertVspeed();
|
||||
}
|
||||
|
||||
let x = x + hspeed;
|
||||
let y = y + vspeed;
|
||||
|
||||
do draw();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void invertVspeed() {
|
||||
let vspeed = -vspeed;
|
||||
return;
|
||||
}
|
||||
|
||||
method void invertHspeed() {
|
||||
let hspeed = -hspeed;
|
||||
return;
|
||||
}
|
||||
|
||||
method int getLeft() {
|
||||
return x - r;
|
||||
}
|
||||
|
||||
method int getRight() {
|
||||
return x + r;
|
||||
}
|
||||
|
||||
method int getTop() {
|
||||
return y - r;
|
||||
}
|
||||
|
||||
method int getBottom() {
|
||||
return y + r;
|
||||
}
|
||||
|
||||
method int getVspeed() {
|
||||
return vspeed;
|
||||
}
|
||||
}
|
||||
@@ -1,224 +0,0 @@
|
||||
class Game {
|
||||
field int hbricks, vbricks;
|
||||
field int gameOver;
|
||||
|
||||
field Rect paddle;
|
||||
field Ball ball;
|
||||
field Array bricks;
|
||||
|
||||
constructor Game new() {
|
||||
var int i, j;
|
||||
var Rect curBrick;
|
||||
|
||||
let gameOver = 0;
|
||||
let hbricks = 8;
|
||||
let vbricks = 3;
|
||||
|
||||
let paddle = Rect.new(250,220,50,10);
|
||||
let ball = Ball.new(250, 200, 6, 1, 1);
|
||||
let bricks = Array.new(hbricks * vbricks);
|
||||
|
||||
while (i < hbricks) {
|
||||
while (j < vbricks) {
|
||||
let bricks[i * 3 + j] = Rect.new(i * 60 + 20, j * 30 + 20, 50, 15);
|
||||
let curBrick = bricks[i * 3 + j];
|
||||
do curBrick.draw();
|
||||
let j = j + 1;
|
||||
}
|
||||
let j = 0;
|
||||
let i = i + 1;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
method void loop() {
|
||||
var int keyPressed;
|
||||
|
||||
while(true) {
|
||||
// Approximately 60 updates per second
|
||||
do Sys.wait(16);
|
||||
|
||||
if (gameOver = 0) {
|
||||
do movePaddle();
|
||||
do ball.update();
|
||||
do chackBallPaddleCol();
|
||||
do checkBallBricksCol();
|
||||
|
||||
do checkGameOver();
|
||||
} else {
|
||||
let keyPressed = Keyboard.keyPressed();
|
||||
if (keyPressed = 82) {
|
||||
do resetGame();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
method void resetGame() {
|
||||
var int i, end;
|
||||
var Rect brick;
|
||||
|
||||
do Screen.clearScreen();
|
||||
|
||||
do paddle.setPos(250, 220);
|
||||
do ball.setPos(250, 200);
|
||||
do ball.setSpeed(1, 1);
|
||||
|
||||
let end = hbricks * vbricks;
|
||||
while (i < end) {
|
||||
let brick = bricks[i];
|
||||
|
||||
do brick.setHidden(0);
|
||||
do brick.draw();
|
||||
|
||||
let i = i + 1;
|
||||
}
|
||||
|
||||
let gameOver = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void checkGameOver() {
|
||||
var int bBottom;
|
||||
|
||||
let bBottom = ball.getBottom();
|
||||
|
||||
if (bBottom > 240) {
|
||||
let gameOver = 1;
|
||||
|
||||
do Output.moveCursor(0,0);
|
||||
do Output.println();
|
||||
do Output.printString("game over");
|
||||
do Output.println();
|
||||
do Output.printString("press r to restart");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void movePaddle() {
|
||||
var int keyPressed, paddleLeft, paddleRight;
|
||||
|
||||
let keyPressed = Keyboard.keyPressed();
|
||||
let paddleLeft = paddle.getLeft();
|
||||
let paddleRight = paddle.getRight();
|
||||
// Move left
|
||||
if((keyPressed = 130) & (paddleLeft > 0)) {
|
||||
do paddle.moveLeft(2);
|
||||
}
|
||||
// Move right
|
||||
if((keyPressed = 132) & (paddleRight < 510)) {
|
||||
do paddle.moveRight(2);
|
||||
}
|
||||
do paddle.draw();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void chackBallPaddleCol() {
|
||||
var int paddleLeft, paddleRight, paddleTop, paddleBottom, paddleMiddle;
|
||||
var int bLeft, bRight, bTop, bBottom, bMiddle, bVspeed;
|
||||
|
||||
let paddleLeft = paddle.getLeft();
|
||||
let paddleRight = paddle.getRight();
|
||||
let paddleTop = paddle.getTop();
|
||||
let paddleBottom = paddle.getBottom();
|
||||
let paddleMiddle = (paddleRight + paddleLeft) / 2;
|
||||
|
||||
let bLeft = ball.getLeft();
|
||||
let bRight = ball.getRight();
|
||||
let bTop = ball.getTop();
|
||||
let bBottom = ball.getBottom();
|
||||
let bMiddle = (bRight + bLeft) / 2;
|
||||
let bVspeed = ball.getVspeed();
|
||||
|
||||
if((bLeft > paddleLeft) & (bRight < paddleRight)) {
|
||||
if((bBottom > paddleTop) & (bVspeed > 0)) {
|
||||
if (bMiddle > paddleMiddle) {
|
||||
do ball.setSpeed(1, -1);
|
||||
} else {
|
||||
do ball.setSpeed(-1, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void checkBallBricksCol() {
|
||||
var int brLeft, brRight, brTop, brBottom, brHidden;
|
||||
var int bLeft, bRight, bTop, bBottom, bVspeed;
|
||||
var int overlapLeft, overlapRight, overlapTop, overlapBottom, fromLeft, fromTop;
|
||||
var int minOverlapX, minOverlapY;
|
||||
var int i, end;
|
||||
var Rect brick;
|
||||
|
||||
let bLeft = ball.getLeft();
|
||||
let bRight = ball.getRight();
|
||||
let bTop = ball.getTop();
|
||||
let bBottom = ball.getBottom();
|
||||
let bVspeed = ball.getVspeed();
|
||||
|
||||
let end = hbricks * vbricks;
|
||||
while (i < end) {
|
||||
let brick = bricks[i];
|
||||
|
||||
let brLeft = brick.getLeft();
|
||||
let brRight = brick.getRight();
|
||||
let brTop = brick.getTop();
|
||||
let brBottom = brick.getBottom();
|
||||
let brHidden = brick.getHidden();
|
||||
|
||||
if(brHidden < 1) {
|
||||
do brick.draw();
|
||||
|
||||
// Check if there's intersection at all first
|
||||
if((bLeft > brLeft) & (bRight < brRight)) {
|
||||
if((bBottom > brTop) & (bTop < brBottom)) {
|
||||
do brick.setHidden(1);
|
||||
do brick.draw();
|
||||
|
||||
// Now calculate the collision side
|
||||
// Algorithm basically copied from this video
|
||||
// https://youtu.be/_4K3tsKa1Uc?t=1820
|
||||
|
||||
let overlapLeft = bRight - brLeft;
|
||||
let overlapRight = brRight - bLeft;
|
||||
let overlapTop = bBottom - brTop;
|
||||
let overlapBottom = brBottom - bTop;
|
||||
|
||||
if(Util.abs(overlapLeft) < Util.abs(overlapRight)) {
|
||||
let fromLeft = 1;
|
||||
let minOverlapX = overlapLeft;
|
||||
} else {
|
||||
let minOverlapX = overlapRight;
|
||||
}
|
||||
|
||||
if(Util.abs(overlapTop) < Util.abs(overlapBottom)) {
|
||||
let fromTop = 1;
|
||||
let minOverlapY = overlapTop;
|
||||
} else {
|
||||
let minOverlapY = overlapBottom;
|
||||
}
|
||||
|
||||
if(Util.abs(minOverlapX) < Util.abs(minOverlapY)) {
|
||||
do ball.invertHspeed();
|
||||
} else {
|
||||
do ball.invertVspeed();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let i = i + 1;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
class Main {
|
||||
function void main() {
|
||||
var Game game;
|
||||
let game = Game.new();
|
||||
do game.loop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
class Rect {
|
||||
field int x, y, width, height, hidden;
|
||||
|
||||
constructor Rect new(int xl, int yl, int widthl, int heightl) {
|
||||
let x = xl;
|
||||
let y = yl;
|
||||
let width = widthl;
|
||||
let height = heightl;
|
||||
let hidden = 0;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
method void setPos(int lx, int ly) {
|
||||
do erase();
|
||||
let x = lx;
|
||||
let y = ly;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void draw() {
|
||||
if (hidden = 1) {
|
||||
do erase();
|
||||
return;
|
||||
}
|
||||
do Screen.setColor(true);
|
||||
do Screen.drawRectangle(x,y,x+width,y+height);
|
||||
return;
|
||||
}
|
||||
|
||||
method void erase() {
|
||||
do Screen.setColor(false);
|
||||
do Screen.drawRectangle(x,y,x+width,y+height);
|
||||
return;
|
||||
}
|
||||
|
||||
method void moveLeft(int dist) {
|
||||
do erase();
|
||||
let x = x - dist;
|
||||
do draw();
|
||||
return;
|
||||
}
|
||||
|
||||
method void moveRight(int dist) {
|
||||
do erase();
|
||||
let x = x + dist;
|
||||
do draw();
|
||||
return;
|
||||
}
|
||||
|
||||
method void moveUp(int dist) {
|
||||
do erase();
|
||||
let y = y - dist;
|
||||
do draw();
|
||||
return;
|
||||
}
|
||||
|
||||
method void moveDown(int dist) {
|
||||
do erase();
|
||||
let y = y + dist;
|
||||
do draw();
|
||||
return;
|
||||
}
|
||||
|
||||
method int getLeft() {
|
||||
return x;
|
||||
}
|
||||
|
||||
method int getRight() {
|
||||
return x + width;
|
||||
}
|
||||
|
||||
method int getTop() {
|
||||
return y;
|
||||
}
|
||||
|
||||
method int getBottom() {
|
||||
return y + height;
|
||||
}
|
||||
|
||||
method int getHidden() {
|
||||
return hidden;
|
||||
}
|
||||
|
||||
method void setHidden(int lhidden) {
|
||||
let hidden = lhidden;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
class Util {
|
||||
function int abs(int n) {
|
||||
if (n < 0) {
|
||||
return -n;
|
||||
} else {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
}
|
||||
63
projects/JackCompiler/.gitattributes
vendored
63
projects/JackCompiler/.gitattributes
vendored
@@ -1,63 +0,0 @@
|
||||
###############################################################################
|
||||
# Set default behavior to automatically normalize line endings.
|
||||
###############################################################################
|
||||
* text=auto
|
||||
|
||||
###############################################################################
|
||||
# Set default behavior for command prompt diff.
|
||||
#
|
||||
# This is need for earlier builds of msysgit that does not have it on by
|
||||
# default for csharp files.
|
||||
# Note: This is only used by command line
|
||||
###############################################################################
|
||||
#*.cs diff=csharp
|
||||
|
||||
###############################################################################
|
||||
# Set the merge driver for project and solution files
|
||||
#
|
||||
# Merging from the command prompt will add diff markers to the files if there
|
||||
# are conflicts (Merging from VS is not affected by the settings below, in VS
|
||||
# the diff markers are never inserted). Diff markers may cause the following
|
||||
# file extensions to fail to load in VS. An alternative would be to treat
|
||||
# these files as binary and thus will always conflict and require user
|
||||
# intervention with every merge. To do so, just uncomment the entries below
|
||||
###############################################################################
|
||||
#*.sln merge=binary
|
||||
#*.csproj merge=binary
|
||||
#*.vbproj merge=binary
|
||||
#*.vcxproj merge=binary
|
||||
#*.vcproj merge=binary
|
||||
#*.dbproj merge=binary
|
||||
#*.fsproj merge=binary
|
||||
#*.lsproj merge=binary
|
||||
#*.wixproj merge=binary
|
||||
#*.modelproj merge=binary
|
||||
#*.sqlproj merge=binary
|
||||
#*.wwaproj merge=binary
|
||||
|
||||
###############################################################################
|
||||
# behavior for image files
|
||||
#
|
||||
# image files are treated as binary by default.
|
||||
###############################################################################
|
||||
#*.jpg binary
|
||||
#*.png binary
|
||||
#*.gif binary
|
||||
|
||||
###############################################################################
|
||||
# diff behavior for common document formats
|
||||
#
|
||||
# Convert binary document formats to text before diffing them. This feature
|
||||
# is only available from the command line. Turn it on by uncommenting the
|
||||
# entries below.
|
||||
###############################################################################
|
||||
#*.doc diff=astextplain
|
||||
#*.DOC diff=astextplain
|
||||
#*.docx diff=astextplain
|
||||
#*.DOCX diff=astextplain
|
||||
#*.dot diff=astextplain
|
||||
#*.DOT diff=astextplain
|
||||
#*.pdf diff=astextplain
|
||||
#*.PDF diff=astextplain
|
||||
#*.rtf diff=astextplain
|
||||
#*.RTF diff=astextplain
|
||||
341
projects/JackCompiler/.gitignore
vendored
341
projects/JackCompiler/.gitignore
vendored
@@ -1,341 +0,0 @@
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
##
|
||||
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
|
||||
|
||||
# User-specific files
|
||||
*.rsuser
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||
*.userprefs
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
[Rr]elease/
|
||||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
[Aa][Rr][Mm]/
|
||||
[Aa][Rr][Mm]64/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
[Ll]og/
|
||||
|
||||
# Visual Studio 2015/2017 cache/options directory
|
||||
.vs/
|
||||
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||
#wwwroot/
|
||||
|
||||
# Visual Studio 2017 auto generated files
|
||||
Generated\ Files/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
# NUNIT
|
||||
*.VisualState.xml
|
||||
TestResult.xml
|
||||
|
||||
# Build Results of an ATL Project
|
||||
[Dd]ebugPS/
|
||||
[Rr]eleasePS/
|
||||
dlldata.c
|
||||
|
||||
# Benchmark Results
|
||||
BenchmarkDotNet.Artifacts/
|
||||
|
||||
# .NET Core
|
||||
project.lock.json
|
||||
project.fragment.lock.json
|
||||
artifacts/
|
||||
|
||||
# StyleCop
|
||||
StyleCopReport.xml
|
||||
|
||||
# Files built by Visual Studio
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_h.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.iobj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.ipdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*_wpftmp.csproj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Chutzpah Test files
|
||||
_Chutzpah*
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opendb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
*.VC.db
|
||||
*.VC.VC.opendb
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
*.sap
|
||||
|
||||
# Visual Studio Trace Files
|
||||
*.e2e
|
||||
|
||||
# TFS 2012 Local Workspace
|
||||
$tf/
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# JustCode is a .NET coding add-in
|
||||
.JustCode
|
||||
|
||||
# TeamCity is a build add-in
|
||||
_TeamCity*
|
||||
|
||||
# DotCover is a Code Coverage Tool
|
||||
*.dotCover
|
||||
|
||||
# AxoCover is a Code Coverage Tool
|
||||
.axoCover/*
|
||||
!.axoCover/settings.json
|
||||
|
||||
# Visual Studio code coverage results
|
||||
*.coverage
|
||||
*.coveragexml
|
||||
|
||||
# NCrunch
|
||||
_NCrunch_*
|
||||
.*crunch*.local.xml
|
||||
nCrunchTemp_*
|
||||
|
||||
# MightyMoose
|
||||
*.mm.*
|
||||
AutoTest.Net/
|
||||
|
||||
# Web workbench (sass)
|
||||
.sass-cache/
|
||||
|
||||
# Installshield output folder
|
||||
[Ee]xpress/
|
||||
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.[Pp]ublish.xml
|
||||
*.azurePubxml
|
||||
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||
# but database connection strings (with potential passwords) will be unencrypted
|
||||
*.pubxml
|
||||
*.publishproj
|
||||
|
||||
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||
# in these scripts will be unencrypted
|
||||
PublishScripts/
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
# The packages folder can be ignored because of Package Restore
|
||||
**/[Pp]ackages/*
|
||||
# except build/, which is used as an MSBuild target.
|
||||
!**/[Pp]ackages/build/
|
||||
# Uncomment if necessary however generally it will be regenerated when needed
|
||||
#!**/[Pp]ackages/repositories.config
|
||||
# NuGet v3's project.json files produces more ignorable files
|
||||
*.nuget.props
|
||||
*.nuget.targets
|
||||
|
||||
# Microsoft Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Microsoft Azure Emulator
|
||||
ecf/
|
||||
rcf/
|
||||
|
||||
# Windows Store app package directories and files
|
||||
AppPackages/
|
||||
BundleArtifacts/
|
||||
Package.StoreAssociation.xml
|
||||
_pkginfo.txt
|
||||
*.appx
|
||||
|
||||
# Visual Studio cache files
|
||||
# files ending in .cache can be ignored
|
||||
*.[Cc]ache
|
||||
# but keep track of directories ending in .cache
|
||||
!?*.[Cc]ache/
|
||||
|
||||
# Others
|
||||
ClientBin/
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.dbproj.schemaview
|
||||
*.jfm
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
orleans.codegen.cs
|
||||
|
||||
# Including strong name files can present a security risk
|
||||
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||
#*.snk
|
||||
|
||||
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||
#bower_components/
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file
|
||||
# to a newer Visual Studio version. Backup files are not needed,
|
||||
# because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
ServiceFabricBackup/
|
||||
*.rptproj.bak
|
||||
|
||||
# SQL Server files
|
||||
*.mdf
|
||||
*.ldf
|
||||
*.ndf
|
||||
|
||||
# Business Intelligence projects
|
||||
*.rdl.data
|
||||
*.bim.layout
|
||||
*.bim_*.settings
|
||||
*.rptproj.rsuser
|
||||
*- Backup*.rdl
|
||||
|
||||
# Microsoft Fakes
|
||||
FakesAssemblies/
|
||||
|
||||
# GhostDoc plugin setting file
|
||||
*.GhostDoc.xml
|
||||
|
||||
# Node.js Tools for Visual Studio
|
||||
.ntvs_analysis.dat
|
||||
node_modules/
|
||||
|
||||
# Visual Studio 6 build log
|
||||
*.plg
|
||||
|
||||
# Visual Studio 6 workspace options file
|
||||
*.opt
|
||||
|
||||
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||
*.vbw
|
||||
|
||||
# Visual Studio LightSwitch build output
|
||||
**/*.HTMLClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/ModelManifest.xml
|
||||
**/*.Server/GeneratedArtifacts
|
||||
**/*.Server/ModelManifest.xml
|
||||
_Pvt_Extensions
|
||||
|
||||
# Paket dependency manager
|
||||
.paket/paket.exe
|
||||
paket-files/
|
||||
|
||||
# FAKE - F# Make
|
||||
.fake/
|
||||
|
||||
# JetBrains Rider
|
||||
.idea/
|
||||
*.sln.iml
|
||||
|
||||
# CodeRush personal settings
|
||||
.cr/personal
|
||||
|
||||
# Python Tools for Visual Studio (PTVS)
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
||||
# Cake - Uncomment if you are using it
|
||||
# tools/**
|
||||
# !tools/packages.config
|
||||
|
||||
# Tabs Studio
|
||||
*.tss
|
||||
|
||||
# Telerik's JustMock configuration file
|
||||
*.jmconfig
|
||||
|
||||
# BizTalk build output
|
||||
*.btp.cs
|
||||
*.btm.cs
|
||||
*.odx.cs
|
||||
*.xsd.cs
|
||||
|
||||
# OpenCover UI analysis results
|
||||
OpenCover/
|
||||
|
||||
# Azure Stream Analytics local run output
|
||||
ASALocalRun/
|
||||
|
||||
# MSBuild Binary and Structured Log
|
||||
*.binlog
|
||||
|
||||
# NVidia Nsight GPU debugger configuration file
|
||||
*.nvuser
|
||||
|
||||
# MFractors (Xamarin productivity tool) working folder
|
||||
.mfractor/
|
||||
|
||||
# Local History for Visual Studio
|
||||
.localhistory/
|
||||
|
||||
# BeatPulse healthcheck temp database
|
||||
healthchecksdb
|
||||
/JackAnalyzer/Properties/launchSettings.json
|
||||
@@ -1,31 +0,0 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.29411.108
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JackAnalyzer", "JackAnalyzer\JackAnalyzer.csproj", "{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JackAnalyzerTest", "SymbolTableTest\JackAnalyzerTest.csproj", "{54357789-1274-4FCB-8EEA-80C75EC656CE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{54357789-1274-4FCB-8EEA-80C75EC656CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{54357789-1274-4FCB-8EEA-80C75EC656CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{54357789-1274-4FCB-8EEA-80C75EC656CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{54357789-1274-4FCB-8EEA-80C75EC656CE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {D5BD3FCB-D928-41AD-AD56-948B65966B3A}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -1,18 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace JackAnalyzer
|
||||
{
|
||||
class Constants
|
||||
{
|
||||
public const string inExt = ".jack";
|
||||
|
||||
public static readonly char[] symbols = { '{', '}', '(', ')', '[', ']', '.', ',', ';', '+', '-', '*', '/', '&', '|', '<', '>', '=', '~' };
|
||||
public static readonly string[] keywords = { "class", "constructor", "function", "method", "field", "static", "var", "int", "char", "boolean", "void", "true", "false", "null", "this", "let", "do", "if", "else", "while", "return" };
|
||||
public static readonly char[] op = { '+', '-', '*', '/', '&', '|', '<', '>', '=' };
|
||||
public static readonly char[] unaryOp = { '-', '~' };
|
||||
|
||||
public const int indentLevel = 2;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,87 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace JackAnalyzer
|
||||
{
|
||||
public class MetaSymbolTable
|
||||
{
|
||||
private SymbolTable ClassSymbolTable = new SymbolTable();
|
||||
private SymbolTable FunctionSymbolTable = new SymbolTable();
|
||||
|
||||
public MetaSymbolTable()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Define(string name, string type, SymbolKind kind)
|
||||
{
|
||||
if (kind == SymbolKind.Field || kind == SymbolKind.Static)
|
||||
{
|
||||
ClassSymbolTable.Define(name, type, kind);
|
||||
}
|
||||
else
|
||||
{
|
||||
FunctionSymbolTable.Define(name, type, kind);
|
||||
}
|
||||
}
|
||||
|
||||
internal void ResetFieldTable()
|
||||
{
|
||||
ClassSymbolTable.ResetFields();
|
||||
}
|
||||
|
||||
public int VarCount(SymbolKind kind)
|
||||
{
|
||||
if (kind == SymbolKind.Field || kind == SymbolKind.Static)
|
||||
{
|
||||
return ClassSymbolTable.VarCount(kind);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FunctionSymbolTable.VarCount(kind);
|
||||
}
|
||||
}
|
||||
|
||||
public SymbolKind KindOf(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return FunctionSymbolTable.KindOf(name);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return ClassSymbolTable.KindOf(name);
|
||||
}
|
||||
}
|
||||
|
||||
public string TypeOf(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return FunctionSymbolTable.TypeOf(name);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return ClassSymbolTable.TypeOf(name);
|
||||
}
|
||||
}
|
||||
|
||||
public int IndexOf(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return FunctionSymbolTable.IndexOf(name);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return ClassSymbolTable.IndexOf(name);
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetFunctionTable()
|
||||
{
|
||||
FunctionSymbolTable = new SymbolTable();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace JackAnalyzer
|
||||
{
|
||||
class Program
|
||||
{
|
||||
|
||||
static int Main(string[] args)
|
||||
{
|
||||
if (args.Length < 1)
|
||||
{
|
||||
Console.WriteLine("Not enough arguments");
|
||||
return 1;
|
||||
}
|
||||
|
||||
string inPath = args[0];
|
||||
|
||||
List<string> inFiles = new List<string>();
|
||||
|
||||
if (Directory.Exists(inPath))
|
||||
{
|
||||
string[] files = Directory.GetFiles(inPath);
|
||||
foreach(string file in files)
|
||||
{
|
||||
if(Path.GetExtension(file) == Constants.inExt)
|
||||
{
|
||||
inFiles.Add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (File.Exists(inPath))
|
||||
{
|
||||
if (Path.GetExtension(inPath) == Constants.inExt)
|
||||
{
|
||||
inFiles.Add(inPath);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("No input files");
|
||||
return 2;
|
||||
}
|
||||
|
||||
foreach(string file in inFiles)
|
||||
{
|
||||
Tokenizer tokenizer = new Tokenizer(file);
|
||||
tokenizer.WriteTokens(file);
|
||||
var tokens = tokenizer.Tokens;
|
||||
Writer writer = new Writer(file, tokens);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace JackAnalyzer
|
||||
{
|
||||
public enum SymbolKind
|
||||
{
|
||||
Static,
|
||||
Field,
|
||||
Arg,
|
||||
Var,
|
||||
None
|
||||
}
|
||||
public class SymbolTable
|
||||
{
|
||||
Dictionary<string, ValueTuple<string, SymbolKind, int>> table = new Dictionary<string, (string, SymbolKind, int)>();
|
||||
Dictionary<SymbolKind, int> kindCounter = new Dictionary<SymbolKind, int>();
|
||||
|
||||
public SymbolTable()
|
||||
{
|
||||
}
|
||||
|
||||
public void Define(string name, string type, SymbolKind kind)
|
||||
{
|
||||
int count;
|
||||
kindCounter.TryGetValue(kind, out count);
|
||||
kindCounter[kind] = count + 1;
|
||||
|
||||
table.Add(name, (type, kind, count));
|
||||
}
|
||||
|
||||
public int VarCount(SymbolKind kind)
|
||||
{
|
||||
try
|
||||
{
|
||||
return kindCounter[kind];
|
||||
} catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public SymbolKind KindOf(string name)
|
||||
{
|
||||
return table[name].Item2;
|
||||
}
|
||||
|
||||
public string TypeOf(string name)
|
||||
{
|
||||
return table[name].Item1;
|
||||
}
|
||||
|
||||
public int IndexOf(string name)
|
||||
{
|
||||
return table[name].Item3;
|
||||
}
|
||||
|
||||
public void ResetFields()
|
||||
{
|
||||
kindCounter[SymbolKind.Field] = 0;
|
||||
var itemsToRemove = table.Where(f => f.Value.Item2 == SymbolKind.Field).ToArray();
|
||||
foreach (var item in itemsToRemove)
|
||||
{
|
||||
table.Remove(item.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace JackAnalyzer
|
||||
{
|
||||
enum TokenType
|
||||
{
|
||||
keyword,
|
||||
identifier,
|
||||
symbol,
|
||||
integerConstant,
|
||||
stringConstant
|
||||
}
|
||||
}
|
||||
@@ -1,179 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System.Security;
|
||||
|
||||
namespace JackAnalyzer
|
||||
{
|
||||
class Tokenizer
|
||||
{
|
||||
public List<KeyValuePair<TokenType, string>> Tokens { get; }
|
||||
|
||||
public Tokenizer(string file)
|
||||
{
|
||||
Tokens = new List<KeyValuePair<TokenType, string>>();
|
||||
|
||||
string[] text = File.ReadAllLines(file);
|
||||
|
||||
string word = "";
|
||||
|
||||
bool readingComment = false;
|
||||
|
||||
foreach (string line in text)
|
||||
{
|
||||
// Ignore comments and empty lines
|
||||
if (line.Length > 0)
|
||||
{
|
||||
bool readingString = false;
|
||||
|
||||
// This is used for comment detection
|
||||
char prevSymbol = '\0';
|
||||
|
||||
foreach (char c in line)
|
||||
{
|
||||
if (readingComment)
|
||||
{
|
||||
if (c == '/' && prevSymbol == '*')
|
||||
{
|
||||
readingComment = false;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
prevSymbol = c;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (c == '"')
|
||||
{
|
||||
if (readingString)
|
||||
{
|
||||
readingString = false;
|
||||
|
||||
PutStringConst(word);
|
||||
word = "";
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
readingString = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (readingString)
|
||||
{
|
||||
word += c;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Constants.symbols.Contains(c))
|
||||
{
|
||||
// Detect comments
|
||||
if (prevSymbol == '/' && c == '/')
|
||||
{
|
||||
// If we're looking at a comment, that means
|
||||
// we need to remove the previous symbol and
|
||||
// discard the rest of the string
|
||||
if (Tokens.Count > 0)
|
||||
{
|
||||
Tokens.RemoveAt(Tokens.Count - 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (prevSymbol == '/' && c == '*')
|
||||
{
|
||||
// If we're looking at a multi-line, that means
|
||||
// we need to remove the previous symbol and
|
||||
// discard the rest of the comment
|
||||
if (Tokens.Count > 0)
|
||||
{
|
||||
Tokens.RemoveAt(Tokens.Count - 1);
|
||||
}
|
||||
readingComment = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
prevSymbol = c;
|
||||
|
||||
PutString(word);
|
||||
PutString(char.ToString(c));
|
||||
word = "";
|
||||
continue;
|
||||
}
|
||||
|
||||
word += c;
|
||||
|
||||
if (c == ' ')
|
||||
{
|
||||
PutString(word);
|
||||
word = "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteTokens(string path)
|
||||
{
|
||||
string filename = Path.GetDirectoryName(path) + '/' + Path.GetFileNameWithoutExtension(path) + "T.xml";
|
||||
using (StreamWriter file = new StreamWriter(filename))
|
||||
{
|
||||
file.WriteLine("<tokens>");
|
||||
foreach (var token in Tokens)
|
||||
{
|
||||
var name = Enum.GetName(typeof(TokenType), token.Key);
|
||||
var value = token.Value;
|
||||
file.WriteLine('<' + name + "> " + SecurityElement.Escape(value) + " </" + name + '>');
|
||||
}
|
||||
file.WriteLine("</tokens>");
|
||||
}
|
||||
}
|
||||
|
||||
private void PutStringConst(string token)
|
||||
{
|
||||
var str = new KeyValuePair<TokenType, string>(TokenType.stringConstant, token);
|
||||
Tokens.Add(str);
|
||||
return;
|
||||
}
|
||||
|
||||
private void PutString(string token)
|
||||
{
|
||||
var trimmed = token.Trim();
|
||||
|
||||
if (trimmed.Length == 1 && Constants.symbols.Contains(trimmed[0]))
|
||||
{
|
||||
var symbol = new KeyValuePair<TokenType, string>(TokenType.symbol, trimmed);
|
||||
Tokens.Add(symbol);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Constants.keywords.Contains(trimmed))
|
||||
{
|
||||
var keyword = new KeyValuePair<TokenType, string>(TokenType.keyword, trimmed);
|
||||
Tokens.Add(keyword);
|
||||
return;
|
||||
}
|
||||
|
||||
if (int.TryParse(trimmed, out _))
|
||||
{
|
||||
var integerConstant = new KeyValuePair<TokenType, string>(TokenType.integerConstant, trimmed);
|
||||
Tokens.Add(integerConstant);
|
||||
return;
|
||||
}
|
||||
|
||||
if (trimmed.Length > 0)
|
||||
{
|
||||
var identifier = new KeyValuePair<TokenType, string>(TokenType.identifier, trimmed);
|
||||
Tokens.Add(identifier);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace JackAnalyzer
|
||||
{
|
||||
enum Segment
|
||||
{
|
||||
Constant, Argument, Local, Static, This, That, Pointer, Temp
|
||||
}
|
||||
|
||||
enum ArithmeticOp
|
||||
{
|
||||
Add, Sub, Neg, Eq, Gt, Lt, And, Or, Not
|
||||
}
|
||||
class VMWriter
|
||||
{
|
||||
StreamWriter file;
|
||||
List<string> cache = new List<string>();
|
||||
int funcDef = 0;
|
||||
|
||||
public VMWriter(string path)
|
||||
{
|
||||
string filename = Path.ChangeExtension(path, ".vm");
|
||||
file = new StreamWriter(filename);
|
||||
file.AutoFlush = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (string l in cache)
|
||||
{
|
||||
file.WriteLine(l);
|
||||
}
|
||||
file.Close();
|
||||
file.Dispose();
|
||||
}
|
||||
|
||||
public void WritePush(Segment segment, int index)
|
||||
{
|
||||
cache.Add($"push {Enum.GetName(typeof(Segment),segment).ToLower()} {index}");
|
||||
}
|
||||
|
||||
public void WritePop(Segment segment, int index)
|
||||
{
|
||||
cache.Add($"pop {Enum.GetName(typeof(Segment), segment).ToLower()} {index}");
|
||||
}
|
||||
|
||||
public void WriteArithmetic(ArithmeticOp op)
|
||||
{
|
||||
cache.Add(Enum.GetName(typeof(ArithmeticOp), op).ToLower());
|
||||
}
|
||||
|
||||
public void WriteLabel(string label)
|
||||
{
|
||||
cache.Add($"label {label}");
|
||||
}
|
||||
|
||||
public void WriteGoto(string label)
|
||||
{
|
||||
cache.Add($"goto {label}");
|
||||
}
|
||||
|
||||
public void WriteIf(string label)
|
||||
{
|
||||
cache.Add($"if-goto {label}");
|
||||
}
|
||||
|
||||
public void WriteCall(string name, int nargs)
|
||||
{
|
||||
cache.Add($"call {name} {nargs}");
|
||||
}
|
||||
|
||||
public void WriteFunction(string name, int nlocal)
|
||||
{
|
||||
cache.Add($"function {name} {nlocal}");
|
||||
funcDef = cache.Count - 1;
|
||||
}
|
||||
|
||||
public void RewriteFunctionLocals(string name, int nlocal)
|
||||
{
|
||||
cache[funcDef]=($"function {name} {nlocal}");
|
||||
}
|
||||
|
||||
public void WriteReturn()
|
||||
{
|
||||
cache.Add("return");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,914 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Security;
|
||||
using System.Linq;
|
||||
|
||||
namespace JackAnalyzer
|
||||
{
|
||||
class Writer
|
||||
{
|
||||
private Queue<KeyValuePair<TokenType, string>> tokenQueue;
|
||||
private static MetaSymbolTable MetaSymbolTable;
|
||||
VMWriter VMWriter;
|
||||
string ClassName;
|
||||
int LabelCounter = 0;
|
||||
public Writer(string path, List<KeyValuePair<TokenType, string>> tokens)
|
||||
{
|
||||
if(MetaSymbolTable == null)
|
||||
{
|
||||
MetaSymbolTable = new MetaSymbolTable();
|
||||
} else
|
||||
{
|
||||
MetaSymbolTable.ResetFieldTable();
|
||||
}
|
||||
VMWriter = new VMWriter(path);
|
||||
string filename = Path.ChangeExtension(path, ".xml");
|
||||
ClassName = Path.GetFileNameWithoutExtension(filename);
|
||||
tokenQueue = new Queue<KeyValuePair<TokenType, string>>(tokens);
|
||||
using (StreamWriter file = new StreamWriter(filename))
|
||||
{
|
||||
var token = tokenQueue.Peek();
|
||||
|
||||
if (token.Key == TokenType.keyword)
|
||||
{
|
||||
string keyword = token.Value;
|
||||
if (keyword == "class")
|
||||
{
|
||||
string[] classLines = CompileClass();
|
||||
foreach (string line in classLines)
|
||||
{
|
||||
file.WriteLine(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
VMWriter.Dispose();
|
||||
}
|
||||
|
||||
// 'class' className '{' classVarDec* subroutineDec* '}'
|
||||
private string[] CompileClass()
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(0, "class"));
|
||||
|
||||
output.Add(PutToken(1));
|
||||
var token = tokenQueue.Dequeue();
|
||||
output.Add(PutLineTag(1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "class, defined"));
|
||||
output.Add(PutToken(1));
|
||||
|
||||
// classVarDec*
|
||||
token = tokenQueue.Peek();
|
||||
while (token.Value == "static" || token.Value == "field")
|
||||
{
|
||||
output.AddRange(CompileClassVarDec(1));
|
||||
token = tokenQueue.Peek();
|
||||
}
|
||||
|
||||
// subroutineDec*
|
||||
token = tokenQueue.Peek();
|
||||
while (token.Value == "function" || token.Value == "constructor" || token.Value == "method" || token.Value == "void")
|
||||
{
|
||||
output.AddRange(CompileSubroutine(1));
|
||||
token = tokenQueue.Peek();
|
||||
}
|
||||
|
||||
// }
|
||||
output.Add(PutToken(1));
|
||||
|
||||
output.Add(PutCloseTag(0, "class"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// ('static' | 'field') type varName (',' varName)* ';'
|
||||
private string[] CompileClassVarDec(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "classVarDec"));
|
||||
|
||||
var token = tokenQueue.Dequeue();
|
||||
SymbolKind kind;
|
||||
switch (token.Value)
|
||||
{
|
||||
case "field":
|
||||
kind = SymbolKind.Field;
|
||||
break;
|
||||
case "static":
|
||||
kind = SymbolKind.Static;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
|
||||
|
||||
token = tokenQueue.Dequeue();
|
||||
string type = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
|
||||
|
||||
token = tokenQueue.Dequeue();
|
||||
string name = token.Value;
|
||||
MetaSymbolTable.Define(name, type, kind);
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
|
||||
|
||||
|
||||
token = tokenQueue.Peek();
|
||||
while (token.Value == ",")
|
||||
{
|
||||
output.Add(PutToken(indent + 1));
|
||||
token = tokenQueue.Dequeue();
|
||||
name = token.Value;
|
||||
MetaSymbolTable.Define(name, type, kind);
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
|
||||
token = tokenQueue.Peek();
|
||||
}
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
output.Add(PutCloseTag(indent, "classVarDec"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// ('constructor' | 'function' | 'method')
|
||||
// ('void' | type) subroutineName '(' parameterList ')' subroutineBody
|
||||
private string[] CompileSubroutine(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "subroutineDec"));
|
||||
MetaSymbolTable.ResetFunctionTable();
|
||||
var token = tokenQueue.Dequeue();
|
||||
string type = token.Value;
|
||||
output.Add(PutToken(indent + 1));
|
||||
token = tokenQueue.Dequeue();
|
||||
string name = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "subroutine, defined"));
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
// parameterList
|
||||
if(type == "method")
|
||||
{
|
||||
MetaSymbolTable.Define("this", ClassName, SymbolKind.Arg);
|
||||
}
|
||||
output.AddRange(CompileParameterList(indent + 1));
|
||||
VMWriter.WriteFunction(ClassName + "." + name, MetaSymbolTable.VarCount(SymbolKind.Var));
|
||||
output.Add(PutToken(indent + 1));
|
||||
switch(type)
|
||||
{
|
||||
case "constructor":
|
||||
VMWriter.WritePush(Segment.Constant, MetaSymbolTable.VarCount(SymbolKind.Field));
|
||||
VMWriter.WriteCall("Memory.alloc", 1);
|
||||
VMWriter.WritePop(Segment.Pointer, 0);
|
||||
break;
|
||||
case "function":
|
||||
break;
|
||||
case "method":
|
||||
VMWriter.WritePush(Segment.Argument, 0);
|
||||
VMWriter.WritePop(Segment.Pointer, 0);
|
||||
break;
|
||||
}
|
||||
output.AddRange(CompileSubroutineBody(indent + 1));
|
||||
VMWriter.RewriteFunctionLocals(ClassName + "." + name, MetaSymbolTable.VarCount(SymbolKind.Var));
|
||||
output.Add(PutCloseTag(indent, "subroutineDec"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// '{' varDec* statements '}'
|
||||
private string[] CompileSubroutineBody(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "subroutineBody"));
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
// varDec*
|
||||
var token = tokenQueue.Peek();
|
||||
while (token.Value == "var")
|
||||
{
|
||||
output.AddRange(CompileVarDec(indent + 1));
|
||||
token = tokenQueue.Peek();
|
||||
}
|
||||
|
||||
output.AddRange(CompileStatements(indent + 1));
|
||||
|
||||
// }
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
output.Add(PutCloseTag(indent, "subroutineBody"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// statement*
|
||||
private string[] CompileStatements(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "statements"));
|
||||
|
||||
bool finished = false;
|
||||
|
||||
var token = tokenQueue.Peek();
|
||||
|
||||
while (!finished)
|
||||
{
|
||||
switch (token.Value)
|
||||
{
|
||||
case "do":
|
||||
output.AddRange(CompileDoStatement(indent + 1));
|
||||
break;
|
||||
case "let":
|
||||
output.AddRange(CompileLetStatement(indent + 1));
|
||||
break;
|
||||
case "while":
|
||||
output.AddRange(CompileWhileStatement(indent + 1));
|
||||
break;
|
||||
case "return":
|
||||
output.AddRange(CompileReturnStatement(indent + 1));
|
||||
break;
|
||||
case "if":
|
||||
output.AddRange(CompileIfStatement(indent + 1));
|
||||
break;
|
||||
default:
|
||||
finished = true;
|
||||
break;
|
||||
}
|
||||
token = tokenQueue.Peek();
|
||||
}
|
||||
|
||||
|
||||
output.Add(PutCloseTag(indent, "statements"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// 'do' subroutineCall ';'
|
||||
private string[] CompileDoStatement(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "doStatement"));
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
var queueEnumerator = tokenQueue.GetEnumerator();
|
||||
queueEnumerator.MoveNext();
|
||||
queueEnumerator.MoveNext();
|
||||
var nextToken = queueEnumerator.Current;
|
||||
string name = "";
|
||||
|
||||
int ellen = 0;
|
||||
|
||||
// subroutineName '(' expressionList ')' | (className |
|
||||
// varName) '.' subroutineName '(' expressionList ')'
|
||||
if (nextToken.Value == ".")
|
||||
{
|
||||
|
||||
var token = tokenQueue.Dequeue();
|
||||
string classname = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "class, used"));
|
||||
output.Add(PutToken(indent + 1));
|
||||
token = tokenQueue.Dequeue();
|
||||
string fname = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "function, used"));
|
||||
output.Add(PutToken(indent + 1));
|
||||
try
|
||||
{
|
||||
MetaSymbolTable.KindOf(classname);
|
||||
Segment seg = Segment.Local;
|
||||
switch (MetaSymbolTable.KindOf(classname))
|
||||
{
|
||||
case SymbolKind.Static:
|
||||
seg = Segment.Static;
|
||||
break;
|
||||
case SymbolKind.Field:
|
||||
seg = Segment.This;
|
||||
break;
|
||||
case SymbolKind.Arg:
|
||||
seg = Segment.Argument;
|
||||
break;
|
||||
case SymbolKind.Var:
|
||||
seg = Segment.Local;
|
||||
break;
|
||||
case SymbolKind.None:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
VMWriter.WritePush(seg, MetaSymbolTable.IndexOf(classname));
|
||||
ellen += 1;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ellen += CompileExpressionList(indent + 1);
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
try
|
||||
{
|
||||
name = MetaSymbolTable.TypeOf(classname) + "." + fname;
|
||||
|
||||
} catch
|
||||
{
|
||||
name = classname + "." + fname;
|
||||
}
|
||||
}
|
||||
else if (nextToken.Value == "(")
|
||||
{
|
||||
VMWriter.WritePush(Segment.Pointer, 0);
|
||||
var token = tokenQueue.Dequeue();
|
||||
name = ClassName + "." + token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "function, used"));
|
||||
output.Add(PutToken(indent + 1));
|
||||
ellen += CompileExpressionList(indent + 1);
|
||||
ellen += 1;
|
||||
output.Add(PutToken(indent + 1));
|
||||
}
|
||||
// ;
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
VMWriter.WriteCall(name, ellen);
|
||||
VMWriter.WritePop(Segment.Temp, 0);
|
||||
output.Add(PutCloseTag(indent, "doStatement"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// 'let' varName ('[' expression ']')? '=' expression ';'
|
||||
private string[] CompileLetStatement(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "letStatement"));
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
var token = tokenQueue.Dequeue();
|
||||
string name = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + MetaSymbolTable.TypeOf(name) + " " + "kind=" + Enum.GetName(typeof(SymbolKind), MetaSymbolTable.KindOf(name)) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "used"));
|
||||
|
||||
token = tokenQueue.Peek();
|
||||
if (token.Value == "[")
|
||||
{
|
||||
output.Add(PutToken(indent + 1));
|
||||
Segment sega = Segment.Local;
|
||||
switch (MetaSymbolTable.KindOf(name))
|
||||
{
|
||||
case SymbolKind.Static:
|
||||
sega = Segment.Static;
|
||||
break;
|
||||
case SymbolKind.Field:
|
||||
sega = Segment.This;
|
||||
break;
|
||||
case SymbolKind.Arg:
|
||||
sega = Segment.Argument;
|
||||
break;
|
||||
case SymbolKind.Var:
|
||||
sega = Segment.Local;
|
||||
break;
|
||||
case SymbolKind.None:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
VMWriter.WritePush(sega, MetaSymbolTable.IndexOf(name));
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Add);
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
VMWriter.WritePop(Segment.Temp, 0);
|
||||
VMWriter.WritePop(Segment.Pointer, 1);
|
||||
VMWriter.WritePush(Segment.Temp, 0);
|
||||
VMWriter.WritePop(Segment.That, 0);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
Segment seg = Segment.Local;
|
||||
switch (MetaSymbolTable.KindOf(name))
|
||||
{
|
||||
case SymbolKind.Static:
|
||||
seg = Segment.Static;
|
||||
break;
|
||||
case SymbolKind.Field:
|
||||
seg = Segment.This;
|
||||
break;
|
||||
case SymbolKind.Arg:
|
||||
seg = Segment.Argument;
|
||||
break;
|
||||
case SymbolKind.Var:
|
||||
seg = Segment.Local;
|
||||
break;
|
||||
case SymbolKind.None:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
VMWriter.WritePop(seg, MetaSymbolTable.IndexOf(name));
|
||||
}
|
||||
|
||||
output.Add(PutCloseTag(indent, "letStatement"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// 'while' '(' expression ')' '{' statements '}'
|
||||
private string[] CompileWhileStatement(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
LabelCounter++;
|
||||
int localcounter = LabelCounter;
|
||||
output.Add(PutOpenTag(indent, "whileStatement"));
|
||||
VMWriter.WriteLabel("WHILE" + localcounter + 0);
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Not);
|
||||
VMWriter.WriteIf("WHILE" + localcounter + 1);
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileStatements(indent + 1));
|
||||
VMWriter.WriteGoto("WHILE" + localcounter + 0);
|
||||
output.Add(PutToken(indent + 1));
|
||||
VMWriter.WriteLabel("WHILE" + localcounter + 1);
|
||||
|
||||
output.Add(PutCloseTag(indent, "whileStatement"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// 'return' expression? ';'
|
||||
private string[] CompileReturnStatement(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "returnStatement"));
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
var token = tokenQueue.Peek();
|
||||
if (token.Value != ";")
|
||||
{
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
} else
|
||||
{
|
||||
VMWriter.WritePush(Segment.Constant, 0);
|
||||
}
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
VMWriter.WriteReturn();
|
||||
output.Add(PutCloseTag(indent, "returnStatement"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// 'if' '(' expression ')' '{' statements '}' ('else' '{' statements '}')?
|
||||
private string[] CompileIfStatement(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
LabelCounter++;
|
||||
int localcounter = LabelCounter;
|
||||
|
||||
output.Add(PutOpenTag(indent, "ifStatement"));
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Not);
|
||||
VMWriter.WriteIf("IF" + localcounter + 0);
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileStatements(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
VMWriter.WriteGoto("IF" + localcounter + 1);
|
||||
VMWriter.WriteLabel("IF" + localcounter + 0);
|
||||
var token = tokenQueue.Peek();
|
||||
if (token.Value == "else")
|
||||
{
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileStatements(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
}
|
||||
VMWriter.WriteLabel("IF" + localcounter + 1);
|
||||
|
||||
output.Add(PutCloseTag(indent, "ifStatement"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// 'var' type varName (',' varName)* ';'
|
||||
private string[] CompileVarDec(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "varDec"));
|
||||
|
||||
var token = tokenQueue.Dequeue();
|
||||
SymbolKind kind = SymbolKind.Var;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
|
||||
|
||||
token = tokenQueue.Dequeue();
|
||||
string type = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
|
||||
|
||||
token = tokenQueue.Dequeue();
|
||||
string name = token.Value;
|
||||
MetaSymbolTable.Define(name, type, kind);
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
|
||||
|
||||
token = tokenQueue.Peek();
|
||||
while (token.Value == ",")
|
||||
{
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
token = tokenQueue.Dequeue();
|
||||
name = token.Value;
|
||||
MetaSymbolTable.Define(name, type, kind);
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
|
||||
token = tokenQueue.Peek();
|
||||
}
|
||||
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
output.Add(PutCloseTag(indent, "varDec"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// ((type varName) (',' type varName)*)?
|
||||
private string[] CompileParameterList(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "parameterList"));
|
||||
|
||||
var token = tokenQueue.Peek();
|
||||
if (token.Key == TokenType.keyword || token.Key == TokenType.identifier)
|
||||
{
|
||||
token = tokenQueue.Dequeue();
|
||||
string type = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
|
||||
|
||||
token = tokenQueue.Dequeue();
|
||||
string name = token.Value;
|
||||
SymbolKind kind = SymbolKind.Arg;
|
||||
MetaSymbolTable.Define(name, type, kind);
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
|
||||
|
||||
token = tokenQueue.Peek();
|
||||
while (token.Value == ",")
|
||||
{
|
||||
output.Add(PutToken(indent + 1));
|
||||
token = tokenQueue.Dequeue();
|
||||
type = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
|
||||
|
||||
token = tokenQueue.Dequeue();
|
||||
name = token.Value;
|
||||
kind = SymbolKind.Arg;
|
||||
MetaSymbolTable.Define(name, type, kind);
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
|
||||
token = tokenQueue.Peek();
|
||||
}
|
||||
}
|
||||
|
||||
output.Add(PutCloseTag(indent, "parameterList"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// term (op term)*
|
||||
private string[] CompileExpression(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "expression"));
|
||||
|
||||
output.AddRange(CompileTerm(indent + 1));
|
||||
|
||||
var token = tokenQueue.Peek();
|
||||
while (Constants.op.Contains(token.Value[0]))
|
||||
{
|
||||
string op = token.Value;
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileTerm(indent + 1));
|
||||
switch (op)
|
||||
{
|
||||
case "+":
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Add);
|
||||
break;
|
||||
case "-":
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Sub);
|
||||
break;
|
||||
case "*":
|
||||
VMWriter.WriteCall("Math.multiply", 2);
|
||||
break;
|
||||
case "/":
|
||||
VMWriter.WriteCall("Math.divide", 2);
|
||||
break;
|
||||
case "&":
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.And);
|
||||
break;
|
||||
case "|":
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Or);
|
||||
break;
|
||||
case "<":
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Lt);
|
||||
break;
|
||||
case ">":
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Gt);
|
||||
break;
|
||||
case "=":
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Eq);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
token = tokenQueue.Peek();
|
||||
}
|
||||
|
||||
output.Add(PutCloseTag(indent, "expression"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// integerConstant | stringConstant | keywordConstant |
|
||||
// varName | varName '[' expression ']' | subroutineCall |
|
||||
// '(' expression ')' | unaryOp term
|
||||
private string[] CompileTerm(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
output.Add(PutOpenTag(indent, "term"));
|
||||
|
||||
var token = tokenQueue.Peek();
|
||||
|
||||
var queueEnumerator = tokenQueue.GetEnumerator();
|
||||
|
||||
queueEnumerator.MoveNext();
|
||||
queueEnumerator.MoveNext();
|
||||
|
||||
var nextToken = queueEnumerator.Current;
|
||||
|
||||
if (Constants.unaryOp.Contains(token.Value[0]))
|
||||
{
|
||||
string unaryOp = token.Value;
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileTerm(indent + 1));
|
||||
switch (unaryOp)
|
||||
{
|
||||
case "-":
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Neg);
|
||||
break;
|
||||
case "~":
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Not);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (token.Key == TokenType.identifier && nextToken.Value == ".")
|
||||
{
|
||||
// subroutineName '(' expressionList ')' | (className |
|
||||
// varName) '.' subroutineName '(' expressionList ')'
|
||||
|
||||
token = tokenQueue.Dequeue();
|
||||
string classname = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "class, used"));
|
||||
output.Add(PutToken(indent + 1));
|
||||
token = tokenQueue.Dequeue();
|
||||
string funcname = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "function, used"));
|
||||
output.Add(PutToken(indent + 1));
|
||||
int ellen = CompileExpressionList(indent + 1);
|
||||
try
|
||||
{
|
||||
MetaSymbolTable.KindOf(classname);
|
||||
Segment seg = Segment.Local;
|
||||
switch (MetaSymbolTable.KindOf(classname))
|
||||
{
|
||||
case SymbolKind.Static:
|
||||
seg = Segment.Static;
|
||||
break;
|
||||
case SymbolKind.Field:
|
||||
seg = Segment.This;
|
||||
break;
|
||||
case SymbolKind.Arg:
|
||||
seg = Segment.Argument;
|
||||
break;
|
||||
case SymbolKind.Var:
|
||||
seg = Segment.Local;
|
||||
break;
|
||||
case SymbolKind.None:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
VMWriter.WritePush(seg, MetaSymbolTable.IndexOf(classname));
|
||||
ellen += 1;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
try
|
||||
{
|
||||
VMWriter.WriteCall(MetaSymbolTable.TypeOf(classname) + "." + funcname, ellen);
|
||||
} catch
|
||||
{
|
||||
VMWriter.WriteCall(classname + "." + funcname, ellen);
|
||||
}
|
||||
output.Add(PutToken(indent + 1));
|
||||
}
|
||||
else if (token.Key == TokenType.identifier && nextToken.Value == "(")
|
||||
{
|
||||
token = tokenQueue.Dequeue();
|
||||
string funcname = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "function, used"));
|
||||
output.Add(PutToken(indent + 1));
|
||||
int ellen = CompileExpressionList(indent + 1);
|
||||
VMWriter.WritePush(Segment.Pointer, 0);
|
||||
VMWriter.WriteCall(ClassName + "." + funcname, ellen + 1);
|
||||
output.Add(PutToken(indent + 1));
|
||||
}
|
||||
else if (nextToken.Value == "[")
|
||||
{
|
||||
token = tokenQueue.Dequeue();
|
||||
string name = token.Value;
|
||||
|
||||
Segment sega = Segment.Local;
|
||||
switch (MetaSymbolTable.KindOf(name))
|
||||
{
|
||||
case SymbolKind.Static:
|
||||
sega = Segment.Static;
|
||||
break;
|
||||
case SymbolKind.Field:
|
||||
sega = Segment.This;
|
||||
break;
|
||||
case SymbolKind.Arg:
|
||||
sega = Segment.Argument;
|
||||
break;
|
||||
case SymbolKind.Var:
|
||||
sega = Segment.Local;
|
||||
break;
|
||||
case SymbolKind.None:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
VMWriter.WritePush(sega, MetaSymbolTable.IndexOf(name));
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Add);
|
||||
output.Add(PutToken(indent + 1));
|
||||
|
||||
VMWriter.WritePop(Segment.Pointer, 1);
|
||||
VMWriter.WritePush(Segment.That, 0);
|
||||
}
|
||||
else if (token.Value == "(")
|
||||
{
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
output.Add(PutToken(indent + 1));
|
||||
}
|
||||
else if (token.Key == TokenType.identifier)
|
||||
{
|
||||
token = tokenQueue.Dequeue();
|
||||
string name = token.Value;
|
||||
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + MetaSymbolTable.TypeOf(name) + " " + "kind=" + Enum.GetName(typeof(SymbolKind), MetaSymbolTable.KindOf(name)) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "used"));
|
||||
Segment seg = Segment.Local;
|
||||
switch (MetaSymbolTable.KindOf(name))
|
||||
{
|
||||
case SymbolKind.Static:
|
||||
seg = Segment.Static;
|
||||
break;
|
||||
case SymbolKind.Field:
|
||||
seg = Segment.This;
|
||||
break;
|
||||
case SymbolKind.Arg:
|
||||
seg = Segment.Argument;
|
||||
break;
|
||||
case SymbolKind.Var:
|
||||
seg = Segment.Local;
|
||||
break;
|
||||
case SymbolKind.None:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
VMWriter.WritePush(seg, MetaSymbolTable.IndexOf(name));
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (token.Key)
|
||||
{
|
||||
case TokenType.keyword:
|
||||
switch(token.Value)
|
||||
{
|
||||
case "this":
|
||||
VMWriter.WritePush(Segment.Pointer, 0);
|
||||
break;
|
||||
case "false":
|
||||
case "null":
|
||||
VMWriter.WritePush(Segment.Constant, 0);
|
||||
break;
|
||||
case "true":
|
||||
VMWriter.WritePush(Segment.Constant, 0);
|
||||
VMWriter.WriteArithmetic(ArithmeticOp.Not);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case TokenType.symbol:
|
||||
break;
|
||||
case TokenType.integerConstant:
|
||||
VMWriter.WritePush(Segment.Constant, int.Parse(token.Value));
|
||||
break;
|
||||
case TokenType.stringConstant:
|
||||
string str = token.Value;
|
||||
VMWriter.WritePush(Segment.Constant, str.Length);
|
||||
VMWriter.WriteCall("String.new", 1);
|
||||
foreach(char c in str)
|
||||
{
|
||||
VMWriter.WritePush(Segment.Constant, c);
|
||||
VMWriter.WriteCall("String.appendChar", 2);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
output.Add(PutToken(indent + 1));
|
||||
}
|
||||
|
||||
output.Add(PutCloseTag(indent, "term"));
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
// (expression (',' expression)* )?
|
||||
private int CompileExpressionList(int indent)
|
||||
{
|
||||
List<string> output = new List<string>();
|
||||
|
||||
int ExpressionListLength = 0;
|
||||
output.Add(PutOpenTag(indent, "expressionList"));
|
||||
|
||||
var token = tokenQueue.Peek();
|
||||
if (token.Value != ")")
|
||||
{
|
||||
ExpressionListLength = 1;
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
token = tokenQueue.Peek();
|
||||
while (token.Value == ",")
|
||||
{
|
||||
ExpressionListLength++;
|
||||
output.Add(PutToken(indent + 1));
|
||||
output.AddRange(CompileExpression(indent + 1));
|
||||
token = tokenQueue.Peek();
|
||||
}
|
||||
}
|
||||
|
||||
output.Add(PutCloseTag(indent, "expressionList"));
|
||||
|
||||
return ExpressionListLength;
|
||||
}
|
||||
|
||||
private string PutToken(int indent)
|
||||
{
|
||||
var token = tokenQueue.Dequeue();
|
||||
return PutLineTag(indent, Enum.GetName(typeof(TokenType), token.Key), token.Value);
|
||||
}
|
||||
|
||||
private string PutOpenTag(int indent, string name)
|
||||
{
|
||||
return new string(' ', indent * Constants.indentLevel) + '<' + name + '>';
|
||||
}
|
||||
|
||||
private string PutCloseTag(int indent, string name)
|
||||
{
|
||||
return new string(' ', indent * Constants.indentLevel) + "</" + name + '>';
|
||||
}
|
||||
|
||||
private string PutLineTag(int indent, string name, string value, string add = "")
|
||||
{
|
||||
return new string(' ', indent * Constants.indentLevel) + '<' + name + " " + add + "> " + SecurityElement.Escape(value) + " </" + name + '>';
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,20 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="1.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\JackAnalyzer\JackAnalyzer.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,43 +0,0 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using JackAnalyzer;
|
||||
|
||||
namespace MetaSymbolTableTest
|
||||
{
|
||||
[TestClass]
|
||||
public class MetaSymbolTableTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void CounterTest()
|
||||
{
|
||||
MetaSymbolTable st = new MetaSymbolTable();
|
||||
|
||||
st.Define("x", "int", SymbolKind.Var);
|
||||
Assert.AreEqual(1, st.VarCount(SymbolKind.Var));
|
||||
Assert.AreEqual(0, st.IndexOf("x"));
|
||||
|
||||
st.Define("y", "String", SymbolKind.Var);
|
||||
Assert.AreEqual(2, st.VarCount(SymbolKind.Var));
|
||||
Assert.AreEqual(1, st.IndexOf("y"));
|
||||
|
||||
st.Define("z", "String", SymbolKind.Field);
|
||||
Assert.AreEqual(1, st.VarCount(SymbolKind.Field));
|
||||
Assert.AreEqual(0, st.IndexOf("z"));
|
||||
}
|
||||
|
||||
public void ResetTest()
|
||||
{
|
||||
MetaSymbolTable st = new MetaSymbolTable();
|
||||
|
||||
st.Define("x", "int", SymbolKind.Var);
|
||||
Assert.AreEqual(1, st.VarCount(SymbolKind.Var));
|
||||
Assert.AreEqual(0, st.IndexOf("x"));
|
||||
|
||||
st.ResetFunctionTable();
|
||||
try
|
||||
{
|
||||
st.IndexOf("x");
|
||||
Assert.Fail();
|
||||
} catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using JackAnalyzer;
|
||||
|
||||
namespace SymbolTableTest
|
||||
{
|
||||
[TestClass]
|
||||
public class SymbolTableTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void CounterTest()
|
||||
{
|
||||
SymbolTable st = new SymbolTable();
|
||||
|
||||
st.Define("x", "int", SymbolKind.Var);
|
||||
Assert.AreEqual(1, st.VarCount(SymbolKind.Var));
|
||||
Assert.AreEqual(0, st.IndexOf("x"));
|
||||
|
||||
st.Define("y", "String", SymbolKind.Var);
|
||||
Assert.AreEqual(2, st.VarCount(SymbolKind.Var));
|
||||
Assert.AreEqual(1, st.IndexOf("y"));
|
||||
|
||||
st.Define("z", "String", SymbolKind.Field);
|
||||
Assert.AreEqual(1, st.VarCount(SymbolKind.Field));
|
||||
Assert.AreEqual(0, st.IndexOf("z"));
|
||||
|
||||
Assert.AreEqual(0, st.VarCount(SymbolKind.Static));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: Google
|
||||
IndentWidth: 4
|
||||
...
|
||||
|
||||
4
projects/VMTranslator/.gitignore
vendored
4
projects/VMTranslator/.gitignore
vendored
@@ -1,4 +0,0 @@
|
||||
build/
|
||||
*.kdev4
|
||||
.kdev4/
|
||||
.history
|
||||
@@ -1,18 +0,0 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"/usr/include/c++/9/**",
|
||||
"/usr/include/**"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "c11",
|
||||
"cppStandard": "c++17",
|
||||
"intelliSenseMode": "${default}"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
45
projects/VMTranslator/.vscode/settings.json
vendored
45
projects/VMTranslator/.vscode/settings.json
vendored
@@ -1,45 +0,0 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"array": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"fstream": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"optional": "cpp",
|
||||
"ostream": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"string": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"tuple": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"utility": "cpp",
|
||||
"chrono": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"ctime": "cpp",
|
||||
"deque": "cpp",
|
||||
"functional": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"ratio": "cpp",
|
||||
"filesystem": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
project(VMTranslator)
|
||||
add_executable(VMTranslator main.cpp Parser.cpp Writer.cpp Commands.cpp)
|
||||
|
||||
set_property(TARGET VMTranslator PROPERTY CXX_STANDARD 14)
|
||||
install(TARGETS VMTranslator RUNTIME DESTINATION bin)
|
||||
target_link_libraries(${PROJECT_NAME} stdc++fs)
|
||||
@@ -1,35 +0,0 @@
|
||||
#include "Commands.h"
|
||||
|
||||
std::unordered_map<std::string, CommandType> CommandTypeDict = {
|
||||
{"", CommandType::Empty},
|
||||
{"push", CommandType::Push},
|
||||
{"pop", CommandType::Pop},
|
||||
{"add", CommandType::Add},
|
||||
{"sub", CommandType::Sub},
|
||||
{"neg", CommandType::Neg},
|
||||
{"eq", CommandType::Eq},
|
||||
{"gt", CommandType::Gt},
|
||||
{"lt", CommandType::Lt},
|
||||
{"and", CommandType::And},
|
||||
{"or", CommandType::Or},
|
||||
{"not", CommandType::Not},
|
||||
{"label", CommandType::Label},
|
||||
{"if-goto", CommandType::IfGoto},
|
||||
{"goto", CommandType::Goto},
|
||||
{"call", CommandType::Call},
|
||||
{"function", CommandType::Function},
|
||||
{"return", CommandType::Return},
|
||||
};
|
||||
|
||||
std::unordered_map<CommandType, std::string> TypeCommandDict = []() {
|
||||
std::unordered_map<CommandType, std::string> rev;
|
||||
for (auto &cmd : CommandTypeDict) {
|
||||
rev[cmd.second] = cmd.first;
|
||||
}
|
||||
return rev;
|
||||
}();
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Command &cmd) {
|
||||
os << TypeCommandDict[cmd.type] << " " << cmd.arg1 << " " << cmd.arg2;
|
||||
return os;
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
#ifndef COMMANDS_H
|
||||
#define COMMANDS_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
enum class CommandType {
|
||||
Add,
|
||||
Sub,
|
||||
Neg,
|
||||
Eq,
|
||||
Gt,
|
||||
Lt,
|
||||
And,
|
||||
Or,
|
||||
Not,
|
||||
Push,
|
||||
Pop,
|
||||
Label,
|
||||
IfGoto,
|
||||
Goto,
|
||||
If,
|
||||
Function,
|
||||
Call,
|
||||
Return,
|
||||
All,
|
||||
Empty
|
||||
};
|
||||
|
||||
extern std::unordered_map<std::string, CommandType> CommandTypeDict;
|
||||
extern std::unordered_map<CommandType, std::string> TypeCommandDict;
|
||||
|
||||
struct Command {
|
||||
CommandType type = CommandType::Empty;
|
||||
std::string arg1 = "";
|
||||
int arg2 = 0;
|
||||
|
||||
Command() {}
|
||||
Command(CommandType type) : type(type) {}
|
||||
Command(CommandType type, std::string arg1, int arg2 = 0)
|
||||
: type(type), arg1(arg1), arg2(arg2) {}
|
||||
|
||||
inline bool isEmpty() { return type == CommandType::Empty; }
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Command &cmd);
|
||||
};
|
||||
|
||||
#endif // COMMANDS_H
|
||||
@@ -1,54 +0,0 @@
|
||||
#include "Parser.h"
|
||||
|
||||
Parser::Parser(std::string file) {
|
||||
infile.open(file);
|
||||
if (!infile.is_open()) {
|
||||
throw std::invalid_argument("Input file doesn't exist");
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::close() { infile.close(); }
|
||||
|
||||
bool Parser::more() { return infile.good(); }
|
||||
|
||||
Command Parser::next() {
|
||||
std::string line;
|
||||
std::getline(infile, line);
|
||||
|
||||
while ((line.substr(0, 2) == "//" || line.empty() ||
|
||||
(line[line.length() - 1] == '\r' && line.length() <= 1)) &&
|
||||
infile.good()) {
|
||||
std::getline(infile, line);
|
||||
}
|
||||
|
||||
if (line.length() > 1 && line[line.length() - 1] == '\r') {
|
||||
line = line.substr(0, line.length() - 1);
|
||||
}
|
||||
|
||||
if (line.length() == 0) {
|
||||
return Command(CommandType::Empty);
|
||||
}
|
||||
|
||||
std::istringstream ss(line);
|
||||
std::string word;
|
||||
Command cmd;
|
||||
int n = 0;
|
||||
|
||||
while (ss >> word) {
|
||||
if(word == "//") {
|
||||
break;
|
||||
}
|
||||
if (n == 0) {
|
||||
cmd.type = CommandTypeDict[word];
|
||||
} else if (n == 1) {
|
||||
cmd.arg1 = word;
|
||||
} else if (n == 2) {
|
||||
cmd.arg2 = std::stoi(word);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
|
||||
return cmd;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "Commands.h"
|
||||
|
||||
class Parser {
|
||||
private:
|
||||
std::ifstream infile;
|
||||
|
||||
public:
|
||||
Parser(std::string file);
|
||||
void close();
|
||||
bool more();
|
||||
Command next();
|
||||
};
|
||||
|
||||
#endif // PARSER_H
|
||||
Binary file not shown.
@@ -1,469 +0,0 @@
|
||||
#include "Writer.h"
|
||||
|
||||
Writer::Writer(std::string file) {
|
||||
filename = getFileName(file);
|
||||
classname = getClassName(filename);
|
||||
|
||||
outfile.open(file, std::ios::out | std::ios::trunc);
|
||||
if (!outfile.is_open()) {
|
||||
throw std::invalid_argument("Can't open output file");
|
||||
}
|
||||
}
|
||||
|
||||
std::string Writer::getFileName(std::string path) {
|
||||
int slashPos = path.rfind("/");
|
||||
return path.substr(slashPos + 1, path.length());
|
||||
}
|
||||
|
||||
std::string Writer::getClassName(std::string filename) {
|
||||
int dotPos = filename.rfind(".");
|
||||
return filename.substr(0, dotPos);
|
||||
}
|
||||
|
||||
void Writer::setFile(std::string path) {
|
||||
filename = getFileName(path);
|
||||
classname = getClassName(filename);
|
||||
}
|
||||
|
||||
void Writer::close() { outfile.close(); }
|
||||
|
||||
void Writer::writeFunction(Command cmd) {
|
||||
outfile << "(" << cmd.arg1 << ")" << std::endl;
|
||||
for (int i = 0; i < cmd.arg2; i++) {
|
||||
writeCmd(Command(CommandType::Push, "constant", 0));
|
||||
}
|
||||
|
||||
functionName = cmd.arg1;
|
||||
retCounter = 0;
|
||||
}
|
||||
|
||||
void Writer::writeCall(Command cmd) {
|
||||
std::string retLabel = functionName + "$ret." + std::to_string(retCounter);
|
||||
// push retAdress
|
||||
outfile << "@" << retLabel << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M+1" << std::endl;
|
||||
|
||||
outfile << "@LCL" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M+1" << std::endl;
|
||||
|
||||
outfile << "@ARG" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M+1" << std::endl;
|
||||
|
||||
outfile << "@THIS" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M+1" << std::endl;
|
||||
|
||||
outfile << "@THAT" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M+1" << std::endl;
|
||||
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@" << 5 + cmd.arg2 << std::endl;
|
||||
outfile << "D=D-A" << std::endl;
|
||||
outfile << "@ARG" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@LCL" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
outfile << "@" << cmd.arg1 << std::endl;
|
||||
outfile << "-1;JMP" << std::endl;
|
||||
|
||||
outfile << "(" << retLabel << ")" << std::endl;
|
||||
retCounter++;
|
||||
}
|
||||
|
||||
void Writer::writeReturn(Command cmd) {
|
||||
// endFrame(R13)=LCL
|
||||
outfile << "@LCL" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
// retAddr(R14)=*(endFrame-5)
|
||||
outfile << "@5" << std::endl;
|
||||
outfile << "A=D-A" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
//*ARG = pop()
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M-1" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@ARG" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
// SP = ARG+1
|
||||
outfile << "@ARG" << std::endl;
|
||||
outfile << "D=M+1" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
// that =*(endFrame-1)
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@1" << std::endl;
|
||||
outfile << "A=D-A" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@THAT" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
// this=*(endFrame-2)
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@2" << std::endl;
|
||||
outfile << "A=D-A" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@THIS" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
// ARG=*(endFrame-3)
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@3" << std::endl;
|
||||
outfile << "A=D-A" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@ARG" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
// LCL=*(endFrame-4)
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@4" << std::endl;
|
||||
outfile << "A=D-A" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@LCL" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "0;JMP" << std::endl;
|
||||
}
|
||||
|
||||
void Writer::writeCmd(Command cmd) {
|
||||
outfile << "// " << cmd << std::endl;
|
||||
switch (cmd.type) {
|
||||
case CommandType::Empty:
|
||||
break;
|
||||
case CommandType::Add:
|
||||
case CommandType::Sub:
|
||||
case CommandType::Neg:
|
||||
case CommandType::Eq:
|
||||
case CommandType::Gt:
|
||||
case CommandType::Lt:
|
||||
case CommandType::And:
|
||||
case CommandType::Or:
|
||||
case CommandType::Not:
|
||||
writeArith(cmd);
|
||||
break;
|
||||
case CommandType::Push:
|
||||
case CommandType::Pop:
|
||||
writePushPop(cmd);
|
||||
break;
|
||||
case CommandType::IfGoto:
|
||||
case CommandType::Goto:
|
||||
case CommandType::Label:
|
||||
writeBranch(cmd);
|
||||
break;
|
||||
case CommandType::Function:
|
||||
writeFunction(cmd);
|
||||
break;
|
||||
case CommandType::Call:
|
||||
writeCall(cmd);
|
||||
break;
|
||||
case CommandType::Return:
|
||||
writeReturn(cmd);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void Writer::writeBranch(Command cmd) {
|
||||
switch (cmd.type) {
|
||||
case CommandType::IfGoto:
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M-1" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@" << functionName << "$" << cmd.arg1 << std::endl;
|
||||
outfile << "D;JNE" << std::endl;
|
||||
break;
|
||||
case CommandType::Goto:
|
||||
outfile << "@" << functionName << "$" << cmd.arg1 << std::endl;
|
||||
outfile << "-1;JMP" << std::endl;
|
||||
break;
|
||||
case CommandType::Label:
|
||||
outfile << "(" << functionName << "$" << cmd.arg1 << ")"
|
||||
<< std::endl;
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void Writer::writeInit() {
|
||||
outfile << "@256" << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
writeCmd(Command(CommandType::Call, "Sys.init", 0));
|
||||
}
|
||||
|
||||
void Writer::writeArith(Command cmd) {
|
||||
static int arithCounter = 0;
|
||||
if (cmd.type != CommandType::Neg && cmd.type != CommandType::Not) {
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M-1" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "M=0" << std::endl;
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M-1" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
}
|
||||
|
||||
switch (cmd.type) {
|
||||
case CommandType::Add:
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M+D" << std::endl;
|
||||
break;
|
||||
case CommandType::Sub:
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M-D" << std::endl;
|
||||
break;
|
||||
case CommandType::Neg:
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M-1" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "D=-M" << std::endl;
|
||||
break;
|
||||
case CommandType::Not:
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M-1" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "D=!M" << std::endl;
|
||||
break;
|
||||
|
||||
case CommandType::Eq:
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M-D" << std::endl;
|
||||
outfile << "@T" << arithCounter << std::endl;
|
||||
outfile << "D;JEQ" << std::endl;
|
||||
outfile << "@END" << arithCounter << std::endl;
|
||||
outfile << "D=0;JMP" << std::endl;
|
||||
outfile << "(T" << arithCounter << ")" << std::endl;
|
||||
outfile << "D=-1" << std::endl;
|
||||
break;
|
||||
case CommandType::Gt:
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M-D" << std::endl;
|
||||
outfile << "@T" << arithCounter << std::endl;
|
||||
outfile << "D;JGT" << std::endl;
|
||||
outfile << "@END" << arithCounter << std::endl;
|
||||
outfile << "D=0;JMP" << std::endl;
|
||||
outfile << "(T" << arithCounter << ")" << std::endl;
|
||||
outfile << "D=-1" << std::endl;
|
||||
break;
|
||||
case CommandType::Lt:
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M-D" << std::endl;
|
||||
outfile << "@T" << arithCounter << std::endl;
|
||||
outfile << "D;JLT" << std::endl;
|
||||
outfile << "@END" << arithCounter << std::endl;
|
||||
outfile << "D=0;JMP" << std::endl;
|
||||
outfile << "(T" << arithCounter << ")" << std::endl;
|
||||
outfile << "D=-1" << std::endl;
|
||||
break;
|
||||
case CommandType::And:
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M&D" << std::endl;
|
||||
break;
|
||||
case CommandType::Or:
|
||||
outfile << "@R13" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M|D" << std::endl;
|
||||
break;
|
||||
}
|
||||
outfile << "(END" << arithCounter << ")" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M+1" << std::endl;
|
||||
arithCounter++;
|
||||
return;
|
||||
}
|
||||
|
||||
void Writer::writePushPop(Command cmd) {
|
||||
switch (cmd.type) {
|
||||
case CommandType::Push:
|
||||
if (cmd.arg1 == "constant") {
|
||||
outfile << "@" << cmd.arg2 << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
} else if (cmd.arg1 == "local") {
|
||||
outfile << "@" << cmd.arg2 << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@LCL" << std::endl;
|
||||
outfile << "A=M+D" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
} else if (cmd.arg1 == "argument") {
|
||||
outfile << "@" << cmd.arg2 << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@ARG" << std::endl;
|
||||
outfile << "A=M+D" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
} else if (cmd.arg1 == "this") {
|
||||
outfile << "@" << cmd.arg2 << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@THIS" << std::endl;
|
||||
outfile << "A=M+D" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
} else if (cmd.arg1 == "that") {
|
||||
outfile << "@" << cmd.arg2 << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@THAT" << std::endl;
|
||||
outfile << "A=M+D" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
} else if (cmd.arg1 == "static") {
|
||||
outfile << "@" << classname << "." << cmd.arg2 << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
} else if (cmd.arg1 == "temp") {
|
||||
outfile << "@R" << 5 + cmd.arg2 << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
} else if (cmd.arg1 == "pointer") {
|
||||
if (cmd.arg2 == 0) {
|
||||
outfile << "@THIS" << std::endl;
|
||||
} else if (cmd.arg2 == 1) {
|
||||
outfile << "@THAT" << std::endl;
|
||||
}
|
||||
outfile << "D=M" << std::endl;
|
||||
}
|
||||
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M+1" << std::endl;
|
||||
break;
|
||||
case CommandType::Pop:
|
||||
outfile << "@SP" << std::endl;
|
||||
outfile << "M=M-1" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
|
||||
if (cmd.arg1 == "local") {
|
||||
outfile << "@" << cmd.arg2 << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@LCL" << std::endl;
|
||||
outfile << "D=M+D" << std::endl;
|
||||
outfile << "@R15" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R15" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
} else if (cmd.arg1 == "argument") {
|
||||
outfile << "@" << cmd.arg2 << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@ARG" << std::endl;
|
||||
outfile << "D=M+D" << std::endl;
|
||||
outfile << "@R15" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R15" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
} else if (cmd.arg1 == "this") {
|
||||
outfile << "@" << cmd.arg2 << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@THIS" << std::endl;
|
||||
outfile << "D=M+D" << std::endl;
|
||||
outfile << "@R15" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R15" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
} else if (cmd.arg1 == "that") {
|
||||
outfile << "@" << cmd.arg2 << std::endl;
|
||||
outfile << "D=A" << std::endl;
|
||||
outfile << "@THAT" << std::endl;
|
||||
outfile << "D=M+D" << std::endl;
|
||||
outfile << "@R15" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
outfile << "@R14" << std::endl;
|
||||
outfile << "D=M" << std::endl;
|
||||
outfile << "@R15" << std::endl;
|
||||
outfile << "A=M" << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
} else if (cmd.arg1 == "static") {
|
||||
outfile << "@" << classname << "." << cmd.arg2 << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
} else if (cmd.arg1 == "temp") {
|
||||
outfile << "@R" << 5 + cmd.arg2 << std::endl;
|
||||
outfile << "M=D" << std::endl;
|
||||
} else if (cmd.arg1 == "pointer") {
|
||||
if (cmd.arg2 == 0) {
|
||||
outfile << "@THIS" << std::endl;
|
||||
} else if (cmd.arg2 == 1) {
|
||||
outfile << "@THAT" << std::endl;
|
||||
}
|
||||
outfile << "M=D" << std::endl;
|
||||
}
|
||||
break;
|
||||
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
#ifndef WRITER_H
|
||||
#define WRITER_H
|
||||
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <experimental/filesystem>
|
||||
|
||||
#include "Commands.h"
|
||||
class Writer {
|
||||
private:
|
||||
std::ofstream outfile;
|
||||
void writeArith(Command cmd);
|
||||
void writePushPop(Command cmd);
|
||||
void writeBranch(Command cmd);
|
||||
void writeCall(Command cmd);
|
||||
void writeFunction(Command cmd);
|
||||
void writeReturn(Command cmd);
|
||||
|
||||
std::string getFileName(std::string path);
|
||||
std::string getClassName(std::string filename);
|
||||
std::string filename;
|
||||
std::string classname;
|
||||
|
||||
std::string functionName;
|
||||
int retCounter = 0;
|
||||
|
||||
public:
|
||||
Writer(std::string file);
|
||||
void setFile(std::string filename);
|
||||
void close();
|
||||
void writeCmd(Command cmd);
|
||||
void writeInit();
|
||||
};
|
||||
|
||||
#endif // WRITER_H
|
||||
@@ -1 +0,0 @@
|
||||
debug
|
||||
@@ -1,79 +0,0 @@
|
||||
#include <exception>
|
||||
#include <experimental/filesystem>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Parser.h"
|
||||
#include "Writer.h"
|
||||
|
||||
Parser *parser;
|
||||
Writer *writer;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
std::cout << "Not enough arguments" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string inpath(argv[1]);
|
||||
bool shouldWriteInit = false;
|
||||
std::string indir;
|
||||
std::vector<std::string> infiles;
|
||||
std::string outfile;
|
||||
|
||||
if (std::experimental::filesystem::is_directory(inpath)) {
|
||||
if (inpath[inpath.length()] == '/') {
|
||||
inpath = inpath.substr(0, inpath.length() - 1);
|
||||
}
|
||||
int slashPos = inpath.rfind("/");
|
||||
indir = inpath.substr(slashPos + 1, inpath.length());
|
||||
outfile = inpath + "/" + indir + ".asm";
|
||||
|
||||
for (auto &f :
|
||||
std::experimental::filesystem::directory_iterator(inpath)) {
|
||||
if (std::experimental::filesystem::is_regular_file(
|
||||
f.path().string())) {
|
||||
if (f.path().extension() == ".vm") {
|
||||
infiles.push_back(f.path().string());
|
||||
}
|
||||
}
|
||||
}
|
||||
shouldWriteInit = true;
|
||||
} else if (std::experimental::filesystem::is_regular_file(inpath)) {
|
||||
int dotPos = inpath.rfind(".");
|
||||
outfile = inpath.substr(0, dotPos);
|
||||
outfile += ".asm";
|
||||
|
||||
infiles.push_back(inpath);
|
||||
} else {
|
||||
std::cout << "Input file doesn't exist" << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
writer = new Writer(outfile);
|
||||
} catch (const std::invalid_argument &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (shouldWriteInit) {
|
||||
writer->writeInit();
|
||||
}
|
||||
|
||||
for (auto &f : infiles) {
|
||||
parser = new Parser(f);
|
||||
writer->setFile(f);
|
||||
while (parser->more()) {
|
||||
Command cmd;
|
||||
cmd = parser->next();
|
||||
writer->writeCmd(cmd);
|
||||
}
|
||||
parser->close();
|
||||
delete parser;
|
||||
}
|
||||
|
||||
writer->close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user