whitespace-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/Gemfile +3 -0
  4. data/LICENSE.md +21 -0
  5. data/README.md +52 -0
  6. data/Rakefile +10 -0
  7. data/bin/whitespace +67 -0
  8. data/examples/count.ws +76 -0
  9. data/examples/fact.ws +135 -0
  10. data/examples/hello.ws +110 -0
  11. data/examples/name.ws +135 -0
  12. data/lib/whitespace.rb +14 -0
  13. data/lib/whitespace/data_structures/console.rb +56 -0
  14. data/lib/whitespace/data_structures/counter.rb +24 -0
  15. data/lib/whitespace/data_structures/memory.rb +19 -0
  16. data/lib/whitespace/data_structures/stack.rb +25 -0
  17. data/lib/whitespace/instructions/arithmetic/add.rb +9 -0
  18. data/lib/whitespace/instructions/arithmetic/binop.rb +18 -0
  19. data/lib/whitespace/instructions/arithmetic/div.rb +9 -0
  20. data/lib/whitespace/instructions/arithmetic/mod.rb +9 -0
  21. data/lib/whitespace/instructions/arithmetic/mul.rb +9 -0
  22. data/lib/whitespace/instructions/arithmetic/sub.rb +9 -0
  23. data/lib/whitespace/instructions/flow_control/call.rb +19 -0
  24. data/lib/whitespace/instructions/flow_control/end.rb +7 -0
  25. data/lib/whitespace/instructions/flow_control/label.rb +16 -0
  26. data/lib/whitespace/instructions/flow_control/njmp.rb +20 -0
  27. data/lib/whitespace/instructions/flow_control/return.rb +7 -0
  28. data/lib/whitespace/instructions/flow_control/ujmp.rb +18 -0
  29. data/lib/whitespace/instructions/flow_control/zjmp.rb +20 -0
  30. data/lib/whitespace/instructions/heap_access/retrieve.rb +9 -0
  31. data/lib/whitespace/instructions/heap_access/store.rb +10 -0
  32. data/lib/whitespace/instructions/instruction.rb +13 -0
  33. data/lib/whitespace/instructions/io/putc.rb +15 -0
  34. data/lib/whitespace/instructions/io/putn.rb +15 -0
  35. data/lib/whitespace/instructions/io/readc.rb +16 -0
  36. data/lib/whitespace/instructions/io/readn.rb +16 -0
  37. data/lib/whitespace/instructions/stack_manipulation/discard.rb +7 -0
  38. data/lib/whitespace/instructions/stack_manipulation/dup.rb +7 -0
  39. data/lib/whitespace/instructions/stack_manipulation/push.rb +17 -0
  40. data/lib/whitespace/instructions/stack_manipulation/swap.rb +11 -0
  41. data/lib/whitespace/isa.rb +9 -0
  42. data/lib/whitespace/parser.rb +243 -0
  43. data/lib/whitespace/util.rb +37 -0
  44. data/lib/whitespace/version.rb +3 -0
  45. data/lib/whitespace/vm.rb +44 -0
  46. data/test/test_helper.rb +4 -0
  47. data/test/whitespace/data_structures/console_test.rb +113 -0
  48. data/test/whitespace/data_structures/counter_test.rb +37 -0
  49. data/test/whitespace/data_structures/memory_test.rb +25 -0
  50. data/test/whitespace/data_structures/stack_test.rb +63 -0
  51. data/test/whitespace/instructions/arithmetic/add_test.rb +43 -0
  52. data/test/whitespace/instructions/arithmetic/div_test.rb +52 -0
  53. data/test/whitespace/instructions/arithmetic/mod_test.rb +52 -0
  54. data/test/whitespace/instructions/arithmetic/mul_test.rb +43 -0
  55. data/test/whitespace/instructions/arithmetic/sub_test.rb +43 -0
  56. data/test/whitespace/instructions/flow_control/call_test.rb +50 -0
  57. data/test/whitespace/instructions/flow_control/end_test.rb +15 -0
  58. data/test/whitespace/instructions/flow_control/label_test.rb +24 -0
  59. data/test/whitespace/instructions/flow_control/njmp_test.rb +94 -0
  60. data/test/whitespace/instructions/flow_control/return_test.rb +33 -0
  61. data/test/whitespace/instructions/flow_control/ujmp_test.rb +44 -0
  62. data/test/whitespace/instructions/flow_control/zjmp_test.rb +94 -0
  63. data/test/whitespace/instructions/heap_access/retrieve_test.rb +49 -0
  64. data/test/whitespace/instructions/heap_access/store_test.rb +44 -0
  65. data/test/whitespace/instructions/instruction_test.rb +11 -0
  66. data/test/whitespace/instructions/io/putc_test.rb +42 -0
  67. data/test/whitespace/instructions/io/putn_test.rb +42 -0
  68. data/test/whitespace/instructions/io/readc_test.rb +71 -0
  69. data/test/whitespace/instructions/io/readn_test.rb +79 -0
  70. data/test/whitespace/instructions/stack_manipulation/discard_test.rb +30 -0
  71. data/test/whitespace/instructions/stack_manipulation/dup_test.rb +32 -0
  72. data/test/whitespace/instructions/stack_manipulation/push_test.rb +30 -0
  73. data/test/whitespace/instructions/stack_manipulation/swap_test.rb +43 -0
  74. data/test/whitespace/parser_test.rb +362 -0
  75. data/test/whitespace/util_test.rb +87 -0
  76. data/test/whitespace/vm_test.rb +80 -0
  77. data/whitespace-ruby.gemspec +30 -0
  78. metadata +178 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ff8b72c2baeb5ad2c69f31e1a31f3e541f039c8
4
+ data.tar.gz: 2bfffd2243ccaffd7ff0250eb24d672707acc1d8
5
+ SHA512:
6
+ metadata.gz: 3de7c8d3f2719f595fd7f9c1a11ea862c59d68012989ce1b7d4babf1d84e2357ed7c8be5e69e537af0cea2e49e1cb66c6c2787214a2927779072b7d086e3d158
7
+ data.tar.gz: 551eafc5ab4054218fdd83b22d417e37112475b567f4d9977d017ff6661931ada2a522714940718679799d9272804f0034c6b4dc57175979f14bd6960db1b92c
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .ruby-version
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Dwayne Crooks
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,52 @@
1
+ ## About
2
+
3
+ An interpreter written in [Ruby](https://www.ruby-lang.org) for the imperative, stack based language called [Whitespace][1].
4
+
5
+ ## Installation
6
+
7
+ Install it yourself using:
8
+
9
+ ```bash
10
+ $ gem install whitespace-ruby
11
+ ```
12
+
13
+ You would now have access to an executable called `whitespace`. Type
14
+
15
+ ```bash
16
+ $ whitespace --help
17
+ ```
18
+
19
+ to learn more.
20
+
21
+ ## Usage
22
+
23
+ Let's say you've written a [Whitespace][1] program and stored it in the file `program.ws`. Then, to execute that program, type:
24
+
25
+ ```bash
26
+ $ whitespace program.ws
27
+ ```
28
+
29
+ This gem comes with example [Whitespace][1] programs that you can check out at [examples](/examples). Be sure to run them to see what they do.
30
+
31
+ For example, here's the [factorial program](/examples/fact.ws) and a sample execution (assuming you're in the examples directory):
32
+
33
+ ```bash
34
+ $ whitespace fact.ws
35
+ Enter a number: 40
36
+ 40! = 20397882081197443358640281739902897356800000000
37
+ ```
38
+
39
+ ## References
40
+
41
+ - [Whitespace tutorial](http://compsoc.dur.ac.uk/whitespace/tutorial.html)
42
+
43
+ ## Credits
44
+
45
+ Thanks to Edwin Brady and Chris Morris for developing this programming language (also developers of the [Idris][2] programming language). I've had lots of fun playing with it and writing interpreters (in [Racket](https://github.com/dwayne/whitespace-racket), [Haskell](https://github.com/dwayne/whitespace-haskell) and now [Ruby](https://www.ruby-lang.org)) for it.
46
+
47
+ ## Copyright
48
+
49
+ Copyright (c) 2016 Dwayne Crooks. See [LICENSE](/LICENSE.md) for further details.
50
+
51
+ [1]: https://en.wikipedia.org/wiki/Whitespace_\(programming_language\)
52
+ [2]: https://en.wikipedia.org/wiki/Idris_(programming_language)
@@ -0,0 +1,10 @@
1
+ require "bundler/setup"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.pattern = "test/**/*_test.rb"
7
+ t.warning = false
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+
5
+ require_relative "../lib/whitespace"
6
+
7
+ def main
8
+ require "optparse"
9
+ options = {}
10
+ opts = OptionParser.new do |opts|
11
+ executable_name = File.basename($PROGRAM_NAME)
12
+ opts.banner = <<-BANNER
13
+ A Whitespace interpreter written in Ruby.
14
+
15
+ Usage: #{executable_name} [options] filename
16
+
17
+ BANNER
18
+
19
+ opts.on("-v", "--version", "Show version information, then exit") do
20
+ puts Whitespace::VERSION
21
+ exit
22
+ end
23
+
24
+ opts.on("-h", "--help", "Show this help, then exit") do
25
+ puts opts
26
+ exit
27
+ end
28
+ end
29
+ opts.parse!
30
+
31
+ nargs = ARGV.length
32
+ if nargs == 0 || nargs > 1
33
+ puts opts
34
+ exit 1
35
+ end
36
+
37
+ abs_filename = File.expand_path(ARGV[0])
38
+ src = File.new(abs_filename).read
39
+
40
+ vm = Whitespace::VM.new
41
+ console = Whitespace::Console.new
42
+ parser = Whitespace::Parser.new(vm, console)
43
+
44
+ begin
45
+ vm.load(parser.parse(src))
46
+ vm.run
47
+ rescue => e
48
+ raise Whitespace::Error
49
+ end
50
+ end
51
+
52
+ begin
53
+ main
54
+ rescue Whitespace::Error => e
55
+ STDERR.puts "A problem occurred while executing your Whitespace program"
56
+ STDERR.puts "Error: #{e.cause.message}"
57
+ exit 1
58
+ rescue ArgumentError => e
59
+ STDERR.puts "Please check your options/arguments"
60
+ STDERR.puts "Error: #{e.message}"
61
+ exit 1
62
+ rescue => e
63
+ STDERR.puts "Sorry, we encountered an unrecoverable error"
64
+ STDERR.puts e.message
65
+ STDERR.puts e.backtrace.join("\n")
66
+ exit 1
67
+ end