hackasm 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +7 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +6 -0
  7. data/Gemfile.lock +139 -0
  8. data/README.md +40 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/exe/hackasm +18 -0
  13. data/hackasm.gemspec +64 -0
  14. data/lib/hackasm.rb +6 -0
  15. data/lib/hackasm/assembler/constants/dests.rb +1 -0
  16. data/lib/hackasm/assembler/constants/jumps.rb +1 -0
  17. data/lib/hackasm/assembler/hack_parser.rb +50 -0
  18. data/lib/hackasm/assembler/instructions/address_instruction.rb +18 -0
  19. data/lib/hackasm/assembler/instructions/command_instruction.rb +76 -0
  20. data/lib/hackasm/assembler/symbol_table.rb +53 -0
  21. data/lib/hackasm/assembler/translator.rb +51 -0
  22. data/lib/hackasm/cli.rb +45 -0
  23. data/lib/hackasm/command.rb +121 -0
  24. data/lib/hackasm/commands/.gitkeep +1 -0
  25. data/lib/hackasm/commands/asm2binary.rb +26 -0
  26. data/lib/hackasm/commands/vm2asm.rb +36 -0
  27. data/lib/hackasm/templates/.gitkeep +1 -0
  28. data/lib/hackasm/templates/assemble/.gitkeep +1 -0
  29. data/lib/hackasm/templates/vm2asm/.gitkeep +1 -0
  30. data/lib/hackasm/version.rb +3 -0
  31. data/lib/hackasm/vm/instructions/arithmetic_instruction.rb +30 -0
  32. data/lib/hackasm/vm/instructions/arithmetic_operations/binary_operation.rb +47 -0
  33. data/lib/hackasm/vm/instructions/arithmetic_operations/comparison.rb +50 -0
  34. data/lib/hackasm/vm/instructions/arithmetic_operations/unary_operation.rb +34 -0
  35. data/lib/hackasm/vm/instructions/memory_access_instruction.rb +36 -0
  36. data/lib/hackasm/vm/instructions/memory_access_operations/constant_operation.rb +30 -0
  37. data/lib/hackasm/vm/instructions/memory_access_operations/fixed_segment_operation.rb +62 -0
  38. data/lib/hackasm/vm/instructions/memory_access_operations/static_operation.rb +43 -0
  39. data/lib/hackasm/vm/instructions/memory_access_operations/virtual_segment_operation.rb +65 -0
  40. data/lib/hackasm/vm/translator.rb +46 -0
  41. data/lib/hackasm/vm/vm_code_parser.rb +39 -0
  42. metadata +467 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 88236b6481cbcde7e39128b82bed0b1dcb0a7733a7b7030fa1a63cac5e4b912d
4
+ data.tar.gz: 3c5b745edb1599518666061cad5f102dc513ee43e03cfb2f7275bbe2a6086de7
5
+ SHA512:
6
+ metadata.gz: 8d21a9765bfbb637bc0b894fc7de8d9e4f4ddbfb75fd47d334a45ec12d11483947f33a371ddaa7b53e2aee16e1299daa08494a8a445400b6406eebd119598138
7
+ data.tar.gz: 24f3c3c5a97ba0591b7775b1d687ff940c6c69046691f1a6be536c79c2c14e704570d3a4d8d1d2e10e22588fa130b51d2e976c3cb5f3c35d1df7bff4df84d1a8
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.txt
10
+ *.gem
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.7.0
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at artur.martsinkovskyi@perfectial.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in hackasm.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,139 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hackasm (0.1.0)
5
+ parslet (~> 2)
6
+ pastel (~> 0.7.2)
7
+ thor (~> 0.20.0)
8
+ tty-box (~> 0.4.1)
9
+ tty-color (~> 0.5)
10
+ tty-command (~> 0.9.0)
11
+ tty-config (~> 0.3.2)
12
+ tty-cursor (~> 0.7)
13
+ tty-editor (~> 0.5)
14
+ tty-file (~> 0.8.0)
15
+ tty-font (~> 0.4.0)
16
+ tty-logger (~> 0.2.0)
17
+ tty-markdown (~> 0.6.0)
18
+ tty-pager (~> 0.12)
19
+ tty-pie (~> 0.3.0)
20
+ tty-platform (~> 0.2)
21
+ tty-progressbar (~> 0.17)
22
+ tty-prompt (~> 0.19)
23
+ tty-screen (~> 0.7)
24
+ tty-spinner (~> 0.9)
25
+ tty-table (~> 0.11.0)
26
+ tty-tree (~> 0.3)
27
+ tty-which (~> 0.4)
28
+
29
+ GEM
30
+ remote: https://rubygems.org/
31
+ specs:
32
+ coderay (1.1.2)
33
+ diff-lcs (1.3)
34
+ equatable (0.6.1)
35
+ kramdown (1.16.2)
36
+ method_source (1.0.0)
37
+ necromancer (0.5.1)
38
+ parslet (2.0.0)
39
+ pastel (0.7.3)
40
+ equatable (~> 0.6)
41
+ tty-color (~> 0.5)
42
+ pry (0.13.1)
43
+ coderay (~> 1.1)
44
+ method_source (~> 1.0)
45
+ rake (12.3.3)
46
+ rouge (3.17.0)
47
+ rspec (3.9.0)
48
+ rspec-core (~> 3.9.0)
49
+ rspec-expectations (~> 3.9.0)
50
+ rspec-mocks (~> 3.9.0)
51
+ rspec-core (3.9.1)
52
+ rspec-support (~> 3.9.1)
53
+ rspec-expectations (3.9.1)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.9.0)
56
+ rspec-mocks (3.9.1)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.9.0)
59
+ rspec-support (3.9.2)
60
+ strings (0.1.8)
61
+ strings-ansi (~> 0.1)
62
+ unicode-display_width (~> 1.5)
63
+ unicode_utils (~> 1.4)
64
+ strings-ansi (0.1.0)
65
+ thor (0.20.3)
66
+ tty-box (0.4.1)
67
+ pastel (~> 0.7.2)
68
+ strings (~> 0.1.6)
69
+ tty-cursor (~> 0.7)
70
+ tty-color (0.5.1)
71
+ tty-command (0.9.0)
72
+ pastel (~> 0.7.0)
73
+ tty-config (0.3.2)
74
+ tty-cursor (0.7.1)
75
+ tty-editor (0.5.1)
76
+ tty-prompt (~> 0.19)
77
+ tty-which (~> 0.4)
78
+ tty-file (0.8.0)
79
+ diff-lcs (~> 1.3)
80
+ pastel (~> 0.7.2)
81
+ tty-prompt (~> 0.18)
82
+ tty-font (0.4.0)
83
+ tty-logger (0.2.0)
84
+ pastel (~> 0.7.0)
85
+ tty-markdown (0.6.0)
86
+ kramdown (~> 1.16.2)
87
+ pastel (~> 0.7.2)
88
+ rouge (~> 3.3)
89
+ strings (~> 0.1.4)
90
+ tty-color (~> 0.4)
91
+ tty-screen (~> 0.6)
92
+ tty-pager (0.12.1)
93
+ strings (~> 0.1.4)
94
+ tty-screen (~> 0.6)
95
+ tty-which (~> 0.4)
96
+ tty-pie (0.3.0)
97
+ pastel (~> 0.7.3)
98
+ tty-cursor (~> 0.7)
99
+ tty-platform (0.3.0)
100
+ tty-progressbar (0.17.0)
101
+ strings-ansi (~> 0.1.0)
102
+ tty-cursor (~> 0.7)
103
+ tty-screen (~> 0.7)
104
+ unicode-display_width (~> 1.6)
105
+ tty-prompt (0.21.0)
106
+ necromancer (~> 0.5.0)
107
+ pastel (~> 0.7.0)
108
+ tty-reader (~> 0.7.0)
109
+ tty-reader (0.7.0)
110
+ tty-cursor (~> 0.7)
111
+ tty-screen (~> 0.7)
112
+ wisper (~> 2.0.0)
113
+ tty-screen (0.7.1)
114
+ tty-spinner (0.9.3)
115
+ tty-cursor (~> 0.7)
116
+ tty-table (0.11.0)
117
+ equatable (~> 0.6)
118
+ necromancer (~> 0.5)
119
+ pastel (~> 0.7.2)
120
+ strings (~> 0.1.5)
121
+ tty-screen (~> 0.7)
122
+ tty-tree (0.4.0)
123
+ tty-which (0.4.2)
124
+ unicode-display_width (1.7.0)
125
+ unicode_utils (1.4.0)
126
+ wisper (2.0.1)
127
+
128
+ PLATFORMS
129
+ ruby
130
+
131
+ DEPENDENCIES
132
+ bundler (~> 2.1)
133
+ hackasm!
134
+ pry (~> 0.12)
135
+ rake (~> 12.3)
136
+ rspec (~> 3.0)
137
+
138
+ BUNDLED WITH
139
+ 2.1.4
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Hackasm
2
+
3
+ A Ruby-based compiler for the Hack platform developed in the scope of the NAND to Tetris course(parts [1](https://www.coursera.org/learn/nand2tetris) and [2](https://www.coursera.org/learn/nand2tetris2).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hackasm'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hackasm
20
+
21
+ ## Usage
22
+
23
+ This tool is a translator that is aimed to translate code from the higher level programming language in the stack of Hack platform to the lower one.
24
+
25
+ ### `new` command
26
+
27
+ Translate *.asm file of assembler commands to *.hack file of binary commands. This command matches the project 7 and 9 of NAND to Tetris.
28
+
29
+
30
+ ### `asm2binary`command
31
+
32
+ Translate *.vm file with virtual machine code or a folder of *.vm files to the single *.asm file of assembler commands. This command matches the project 6 of NAND to Tetris.
33
+
34
+ ## Code of Conduct
35
+
36
+ Everyone interacting in the Hackasm project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/artur-martsinkovskyi/hackasm/blob/master/CODE_OF_CONDUCT.md).
37
+
38
+ ## Copyright
39
+
40
+ Copyright (c) 2020 Artur Martsinkovskyi. See [MIT License](LICENSE.txt) for further details.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hackasm"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/hackasm ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib_path = File.expand_path('../lib', __dir__)
5
+ $:.unshift(lib_path) if !$:.include?(lib_path)
6
+ require 'hackasm/cli'
7
+
8
+ Signal.trap('INT') do
9
+ warn("\n#{caller.join("\n")}: interrupted")
10
+ exit(1)
11
+ end
12
+
13
+ begin
14
+ Hackasm::CLI.start
15
+ rescue Hackasm::CLI::Error => err
16
+ puts "ERROR: #{err.message}"
17
+ exit 1
18
+ end
data/hackasm.gemspec ADDED
@@ -0,0 +1,64 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hackasm/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hackasm"
8
+ spec.license = "MIT"
9
+ spec.version = Hackasm::VERSION
10
+ spec.authors = ["Artur Martsinkovskyi"]
11
+ spec.email = ["deimoss42@gmail.com"]
12
+
13
+ spec.summary = %q{Assembler and vm code translator program for Hack assembler from course From NAND to Tetris.}
14
+ spec.homepage = "https://github.com/artur-martsinkovskyi/hackasm"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = spec.homepage + "/CHANGELOG.md"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_dependency "tty-box", "~> 0.4.1"
37
+ spec.add_dependency "tty-color", "~> 0.5"
38
+ spec.add_dependency "tty-command", "~> 0.9.0"
39
+ spec.add_dependency "tty-config", "~> 0.3.2"
40
+ spec.add_dependency "tty-cursor", "~> 0.7"
41
+ spec.add_dependency "tty-editor", "~> 0.5"
42
+ spec.add_dependency "tty-file", "~> 0.8.0"
43
+ spec.add_dependency "tty-font", "~> 0.4.0"
44
+ spec.add_dependency "tty-logger", "~> 0.2.0"
45
+ spec.add_dependency "tty-markdown", "~> 0.6.0"
46
+ spec.add_dependency "tty-pager", "~> 0.12"
47
+ spec.add_dependency "tty-pie", "~> 0.3.0"
48
+ spec.add_dependency "tty-platform", "~> 0.2"
49
+ spec.add_dependency "tty-progressbar", "~> 0.17"
50
+ spec.add_dependency "tty-prompt", "~> 0.19"
51
+ spec.add_dependency "tty-screen", "~> 0.7"
52
+ spec.add_dependency "tty-spinner", "~> 0.9"
53
+ spec.add_dependency "tty-table", "~> 0.11.0"
54
+ spec.add_dependency "tty-tree", "~> 0.3"
55
+ spec.add_dependency "tty-which", "~> 0.4"
56
+ spec.add_dependency "pastel", "~> 0.7.2"
57
+ spec.add_dependency "thor", "~> 0.20.0"
58
+ spec.add_dependency "parslet", "~> 2"
59
+
60
+ spec.add_development_dependency "bundler", "~> 2.1"
61
+ spec.add_development_dependency "rake", "~> 12.3"
62
+ spec.add_development_dependency "rspec", "~> 3.0"
63
+ spec.add_development_dependency "pry", "~> 0.12"
64
+ end
data/lib/hackasm.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "hackasm/version"
2
+
3
+ module Hackasm
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1 @@
1
+ DESTS = %w[M D MD A AM AD AMD].freeze
@@ -0,0 +1 @@
1
+ JUMPS = %w[JGT JEQ JGE JLT JNE JLE JMP].freeze
@@ -0,0 +1,50 @@
1
+ require 'parslet'
2
+ require_relative './constants/jumps'
3
+ require_relative './constants/dests'
4
+
5
+ module Assembler
6
+ class HackParser < Parslet::Parser
7
+ COMMENT_DELIMITER = "//".freeze
8
+ ADDRESS_DELIMITER = "@".freeze
9
+ JUMP_DELIMITER = ";".freeze
10
+ DEST_DELIMITER = "=".freeze
11
+ root :lines
12
+
13
+ # UTIL
14
+
15
+ rule(:line) { space? >> expression.maybe >> space? >> comment.maybe >> newline }
16
+ rule(:lines) { line.repeat }
17
+
18
+ rule(:expression) { address_instruction | jump_label | command_instruction }
19
+
20
+ rule(:comment) { (str(COMMENT_DELIMITER) >> (newline.absent? >> any).repeat) }
21
+
22
+ rule(:space) { match('[[:blank:]]').repeat(1) }
23
+ rule(:space?) { space.maybe }
24
+ rule(:newline) { str("\r").maybe >> str("\n") >> str("\r").maybe }
25
+
26
+ #A-INSTRUCTIONS
27
+
28
+ rule(:at) { match(ADDRESS_DELIMITER) }
29
+ rule(:memory_address) { (match("0") | (match("[1-9]") >> match('[0-9]').repeat)).as(:memory_address) }
30
+ rule(:identifier) { (match('[a-zA-Z_\-.$:]') >> match('[a-zA-Z_\-.$:0-9]').repeat(1)).as(:identifier) }
31
+
32
+ rule(:address_instruction) { (at >> (memory_address | identifier)).as(:address_instruction) }
33
+
34
+ # JUMP LABELS
35
+
36
+ rule(:jump_label) { (str("(") >> identifier >> str(")")).as(:jump_label) }
37
+
38
+ # C-INSTRUCTIONS
39
+
40
+ rule(:dest) { DESTS.map { |s| str(s).as(:dest) >> space? >> str(DEST_DELIMITER) }.inject(:|) }
41
+
42
+ rule(:comp_unary) { match("[-!]").maybe >> match("[ADM]") }
43
+ rule(:comp_binary) { match("[ADM]") >> space? >> match("[+&|-]") >> space? >> match("[1ADM]") }
44
+ rule(:comp) { (str("0") | str("1") | str("-1") | comp_binary | comp_unary).as(:comp) }
45
+
46
+ rule(:jump) { JUMPS.map { |s| str(s) }.inject(:|).as(:jump) }
47
+
48
+ rule(:command_instruction) { ((dest).maybe >> space? >> comp >> space? >> (str(JUMP_DELIMITER) >> space? >> jump).maybe).as(:command_instruction) }
49
+ end
50
+ end