0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nand2Tetrisをやり切る

Posted at

はじめに

2021年の夏に7章まで進めていたのですが、コンパイラのコードが論理破綻してしまい、挫折していた。
今回、再度プログラムを見直し、最後までやりくることができた。

リファクタリング

まず、プログラムのリファクタリングから始めた。
大きく変えた事は、advanceメソッドの中で、コメントアウトの処理も併せて実行する事である。

そのため、一旦アセンブラのコードから見直すこととした。アセンブラのコメントアウトは、//で開始される行であるためその行を省くようにするというコードである。

PArser.rb
def advance
    while !@asmFile.eof? 
      line = @asmFile.readline
      line.strip!
      if line =~ /^\/\//
        next
      elsif line.empty?
        next
      else
        command = line.split('//')
        @command = command[0]
        @command.strip!
        @chars = @command.split(//)
        return @command
      end
    end
  end

ここは、以前もテストで問題なく出力できていたので確認程度で終了である。

次に、VMについても同様にした。基本的にはアセンブラと同じコードである。

PArser.rb
  def advance
    while !@vmFile.eof?
      line = @vmFile.readline
      line.strip!
      if line =~ /^\/\//
        next
      elsif line.empty?
        next
      else
        @chars = line.split(//)
        parseReadline(@chars)
        @parseState = S_COMMAND
        return line
      end
    end
  end
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?