hackasm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,39 @@
1
+ require 'parslet'
2
+
3
+ module Vm
4
+ class VmCodeParser < Parslet::Parser
5
+ COMMENT_DELIMITER = "//".freeze
6
+ ARITHMETIC_OPERATIONS = %w[add sub neg eq gt lt and or not]
7
+ MEMORY_ACCESS_OPERATIONS = %w[push pop]
8
+ MEMORY_SEGMENTS = %w[argument local static constant this that pointer temp]
9
+
10
+ root :lines
11
+
12
+ # UTIL
13
+
14
+ rule(:line) { space? >> instruction.maybe >> space? >> comment.maybe >> newline }
15
+ rule(:lines) { line.repeat }
16
+
17
+ rule(:instruction) { arithmetic_instruction | memory_access_instruction }
18
+
19
+ rule(:comment) { (str(COMMENT_DELIMITER) >> (newline.absent? >> any).repeat) }
20
+
21
+ rule(:space) { match('[[:blank:]]').repeat(1) }
22
+ rule(:space?) { space.maybe }
23
+ rule(:newline) { str("\r").maybe >> str("\n") >> str("\r").maybe }
24
+
25
+ rule(:integer) { match('[0-9]').repeat(1) }
26
+
27
+ # ARITHMETIC INSTRUCTIONS
28
+
29
+ rule(:arithmetic_instruction) { ARITHMETIC_OPERATIONS.map { |e| str(e).as(:operation) }.inject(:|).as(:arithmetic_instruction) }
30
+
31
+ # MEMORY ACCESS INSTRUCTIONS
32
+
33
+ rule(:memory_access_instruction) { (operation >> space >> segment >> space >> integer.as(:index)).as(:memory_access_instruction) }
34
+
35
+ rule(:operation) { MEMORY_ACCESS_OPERATIONS.map { |e| str(e).as(:operation) }.inject(:|) }
36
+
37
+ rule(:segment) { MEMORY_SEGMENTS.map { |e| str(e).as(:segment) }.inject(:|) }
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,467 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hackasm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artur Martsinkovskyi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tty-box
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-color
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-command
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-config
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.3.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: tty-cursor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.7'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: tty-editor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.5'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.5'
97
+ - !ruby/object:Gem::Dependency
98
+ name: tty-file
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.8.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.8.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: tty-font
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.4.0
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.4.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: tty-logger
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.2.0
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.2.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: tty-markdown
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.6.0
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.6.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: tty-pager
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.12'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.12'
167
+ - !ruby/object:Gem::Dependency
168
+ name: tty-pie
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.3.0
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.3.0
181
+ - !ruby/object:Gem::Dependency
182
+ name: tty-platform
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '0.2'
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '0.2'
195
+ - !ruby/object:Gem::Dependency
196
+ name: tty-progressbar
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: '0.17'
202
+ type: :runtime
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: '0.17'
209
+ - !ruby/object:Gem::Dependency
210
+ name: tty-prompt
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '0.19'
216
+ type: :runtime
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '0.19'
223
+ - !ruby/object:Gem::Dependency
224
+ name: tty-screen
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '0.7'
230
+ type: :runtime
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: '0.7'
237
+ - !ruby/object:Gem::Dependency
238
+ name: tty-spinner
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - "~>"
242
+ - !ruby/object:Gem::Version
243
+ version: '0.9'
244
+ type: :runtime
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - "~>"
249
+ - !ruby/object:Gem::Version
250
+ version: '0.9'
251
+ - !ruby/object:Gem::Dependency
252
+ name: tty-table
253
+ requirement: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - "~>"
256
+ - !ruby/object:Gem::Version
257
+ version: 0.11.0
258
+ type: :runtime
259
+ prerelease: false
260
+ version_requirements: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - "~>"
263
+ - !ruby/object:Gem::Version
264
+ version: 0.11.0
265
+ - !ruby/object:Gem::Dependency
266
+ name: tty-tree
267
+ requirement: !ruby/object:Gem::Requirement
268
+ requirements:
269
+ - - "~>"
270
+ - !ruby/object:Gem::Version
271
+ version: '0.3'
272
+ type: :runtime
273
+ prerelease: false
274
+ version_requirements: !ruby/object:Gem::Requirement
275
+ requirements:
276
+ - - "~>"
277
+ - !ruby/object:Gem::Version
278
+ version: '0.3'
279
+ - !ruby/object:Gem::Dependency
280
+ name: tty-which
281
+ requirement: !ruby/object:Gem::Requirement
282
+ requirements:
283
+ - - "~>"
284
+ - !ruby/object:Gem::Version
285
+ version: '0.4'
286
+ type: :runtime
287
+ prerelease: false
288
+ version_requirements: !ruby/object:Gem::Requirement
289
+ requirements:
290
+ - - "~>"
291
+ - !ruby/object:Gem::Version
292
+ version: '0.4'
293
+ - !ruby/object:Gem::Dependency
294
+ name: pastel
295
+ requirement: !ruby/object:Gem::Requirement
296
+ requirements:
297
+ - - "~>"
298
+ - !ruby/object:Gem::Version
299
+ version: 0.7.2
300
+ type: :runtime
301
+ prerelease: false
302
+ version_requirements: !ruby/object:Gem::Requirement
303
+ requirements:
304
+ - - "~>"
305
+ - !ruby/object:Gem::Version
306
+ version: 0.7.2
307
+ - !ruby/object:Gem::Dependency
308
+ name: thor
309
+ requirement: !ruby/object:Gem::Requirement
310
+ requirements:
311
+ - - "~>"
312
+ - !ruby/object:Gem::Version
313
+ version: 0.20.0
314
+ type: :runtime
315
+ prerelease: false
316
+ version_requirements: !ruby/object:Gem::Requirement
317
+ requirements:
318
+ - - "~>"
319
+ - !ruby/object:Gem::Version
320
+ version: 0.20.0
321
+ - !ruby/object:Gem::Dependency
322
+ name: parslet
323
+ requirement: !ruby/object:Gem::Requirement
324
+ requirements:
325
+ - - "~>"
326
+ - !ruby/object:Gem::Version
327
+ version: '2'
328
+ type: :runtime
329
+ prerelease: false
330
+ version_requirements: !ruby/object:Gem::Requirement
331
+ requirements:
332
+ - - "~>"
333
+ - !ruby/object:Gem::Version
334
+ version: '2'
335
+ - !ruby/object:Gem::Dependency
336
+ name: bundler
337
+ requirement: !ruby/object:Gem::Requirement
338
+ requirements:
339
+ - - "~>"
340
+ - !ruby/object:Gem::Version
341
+ version: '2.1'
342
+ type: :development
343
+ prerelease: false
344
+ version_requirements: !ruby/object:Gem::Requirement
345
+ requirements:
346
+ - - "~>"
347
+ - !ruby/object:Gem::Version
348
+ version: '2.1'
349
+ - !ruby/object:Gem::Dependency
350
+ name: rake
351
+ requirement: !ruby/object:Gem::Requirement
352
+ requirements:
353
+ - - "~>"
354
+ - !ruby/object:Gem::Version
355
+ version: '12.3'
356
+ type: :development
357
+ prerelease: false
358
+ version_requirements: !ruby/object:Gem::Requirement
359
+ requirements:
360
+ - - "~>"
361
+ - !ruby/object:Gem::Version
362
+ version: '12.3'
363
+ - !ruby/object:Gem::Dependency
364
+ name: rspec
365
+ requirement: !ruby/object:Gem::Requirement
366
+ requirements:
367
+ - - "~>"
368
+ - !ruby/object:Gem::Version
369
+ version: '3.0'
370
+ type: :development
371
+ prerelease: false
372
+ version_requirements: !ruby/object:Gem::Requirement
373
+ requirements:
374
+ - - "~>"
375
+ - !ruby/object:Gem::Version
376
+ version: '3.0'
377
+ - !ruby/object:Gem::Dependency
378
+ name: pry
379
+ requirement: !ruby/object:Gem::Requirement
380
+ requirements:
381
+ - - "~>"
382
+ - !ruby/object:Gem::Version
383
+ version: '0.12'
384
+ type: :development
385
+ prerelease: false
386
+ version_requirements: !ruby/object:Gem::Requirement
387
+ requirements:
388
+ - - "~>"
389
+ - !ruby/object:Gem::Version
390
+ version: '0.12'
391
+ description:
392
+ email:
393
+ - deimoss42@gmail.com
394
+ executables:
395
+ - hackasm
396
+ extensions: []
397
+ extra_rdoc_files: []
398
+ files:
399
+ - ".gitignore"
400
+ - ".rspec"
401
+ - ".rspec_status"
402
+ - ".travis.yml"
403
+ - CODE_OF_CONDUCT.md
404
+ - Gemfile
405
+ - Gemfile.lock
406
+ - LICENSE.txt
407
+ - README.md
408
+ - Rakefile
409
+ - bin/console
410
+ - bin/setup
411
+ - exe/hackasm
412
+ - hackasm.gemspec
413
+ - lib/hackasm.rb
414
+ - lib/hackasm/assembler/constants/dests.rb
415
+ - lib/hackasm/assembler/constants/jumps.rb
416
+ - lib/hackasm/assembler/hack_parser.rb
417
+ - lib/hackasm/assembler/instructions/address_instruction.rb
418
+ - lib/hackasm/assembler/instructions/command_instruction.rb
419
+ - lib/hackasm/assembler/symbol_table.rb
420
+ - lib/hackasm/assembler/translator.rb
421
+ - lib/hackasm/cli.rb
422
+ - lib/hackasm/command.rb
423
+ - lib/hackasm/commands/.gitkeep
424
+ - lib/hackasm/commands/asm2binary.rb
425
+ - lib/hackasm/commands/vm2asm.rb
426
+ - lib/hackasm/templates/.gitkeep
427
+ - lib/hackasm/templates/assemble/.gitkeep
428
+ - lib/hackasm/templates/vm2asm/.gitkeep
429
+ - lib/hackasm/version.rb
430
+ - lib/hackasm/vm/instructions/arithmetic_instruction.rb
431
+ - lib/hackasm/vm/instructions/arithmetic_operations/binary_operation.rb
432
+ - lib/hackasm/vm/instructions/arithmetic_operations/comparison.rb
433
+ - lib/hackasm/vm/instructions/arithmetic_operations/unary_operation.rb
434
+ - lib/hackasm/vm/instructions/memory_access_instruction.rb
435
+ - lib/hackasm/vm/instructions/memory_access_operations/constant_operation.rb
436
+ - lib/hackasm/vm/instructions/memory_access_operations/fixed_segment_operation.rb
437
+ - lib/hackasm/vm/instructions/memory_access_operations/static_operation.rb
438
+ - lib/hackasm/vm/instructions/memory_access_operations/virtual_segment_operation.rb
439
+ - lib/hackasm/vm/translator.rb
440
+ - lib/hackasm/vm/vm_code_parser.rb
441
+ homepage: https://github.com/artur-martsinkovskyi/hackasm
442
+ licenses:
443
+ - MIT
444
+ metadata:
445
+ homepage_uri: https://github.com/artur-martsinkovskyi/hackasm
446
+ source_code_uri: https://github.com/artur-martsinkovskyi/hackasm/CHANGELOG.md
447
+ post_install_message:
448
+ rdoc_options: []
449
+ require_paths:
450
+ - lib
451
+ required_ruby_version: !ruby/object:Gem::Requirement
452
+ requirements:
453
+ - - ">="
454
+ - !ruby/object:Gem::Version
455
+ version: '0'
456
+ required_rubygems_version: !ruby/object:Gem::Requirement
457
+ requirements:
458
+ - - ">="
459
+ - !ruby/object:Gem::Version
460
+ version: '0'
461
+ requirements: []
462
+ rubygems_version: 3.1.2
463
+ signing_key:
464
+ specification_version: 4
465
+ summary: Assembler and vm code translator program for Hack assembler from course From
466
+ NAND to Tetris.
467
+ test_files: []