whitespace-ruby 1.0.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.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/Gemfile +3 -0
- data/LICENSE.md +21 -0
- data/README.md +52 -0
- data/Rakefile +10 -0
- data/bin/whitespace +67 -0
- data/examples/count.ws +76 -0
- data/examples/fact.ws +135 -0
- data/examples/hello.ws +110 -0
- data/examples/name.ws +135 -0
- data/lib/whitespace.rb +14 -0
- data/lib/whitespace/data_structures/console.rb +56 -0
- data/lib/whitespace/data_structures/counter.rb +24 -0
- data/lib/whitespace/data_structures/memory.rb +19 -0
- data/lib/whitespace/data_structures/stack.rb +25 -0
- data/lib/whitespace/instructions/arithmetic/add.rb +9 -0
- data/lib/whitespace/instructions/arithmetic/binop.rb +18 -0
- data/lib/whitespace/instructions/arithmetic/div.rb +9 -0
- data/lib/whitespace/instructions/arithmetic/mod.rb +9 -0
- data/lib/whitespace/instructions/arithmetic/mul.rb +9 -0
- data/lib/whitespace/instructions/arithmetic/sub.rb +9 -0
- data/lib/whitespace/instructions/flow_control/call.rb +19 -0
- data/lib/whitespace/instructions/flow_control/end.rb +7 -0
- data/lib/whitespace/instructions/flow_control/label.rb +16 -0
- data/lib/whitespace/instructions/flow_control/njmp.rb +20 -0
- data/lib/whitespace/instructions/flow_control/return.rb +7 -0
- data/lib/whitespace/instructions/flow_control/ujmp.rb +18 -0
- data/lib/whitespace/instructions/flow_control/zjmp.rb +20 -0
- data/lib/whitespace/instructions/heap_access/retrieve.rb +9 -0
- data/lib/whitespace/instructions/heap_access/store.rb +10 -0
- data/lib/whitespace/instructions/instruction.rb +13 -0
- data/lib/whitespace/instructions/io/putc.rb +15 -0
- data/lib/whitespace/instructions/io/putn.rb +15 -0
- data/lib/whitespace/instructions/io/readc.rb +16 -0
- data/lib/whitespace/instructions/io/readn.rb +16 -0
- data/lib/whitespace/instructions/stack_manipulation/discard.rb +7 -0
- data/lib/whitespace/instructions/stack_manipulation/dup.rb +7 -0
- data/lib/whitespace/instructions/stack_manipulation/push.rb +17 -0
- data/lib/whitespace/instructions/stack_manipulation/swap.rb +11 -0
- data/lib/whitespace/isa.rb +9 -0
- data/lib/whitespace/parser.rb +243 -0
- data/lib/whitespace/util.rb +37 -0
- data/lib/whitespace/version.rb +3 -0
- data/lib/whitespace/vm.rb +44 -0
- data/test/test_helper.rb +4 -0
- data/test/whitespace/data_structures/console_test.rb +113 -0
- data/test/whitespace/data_structures/counter_test.rb +37 -0
- data/test/whitespace/data_structures/memory_test.rb +25 -0
- data/test/whitespace/data_structures/stack_test.rb +63 -0
- data/test/whitespace/instructions/arithmetic/add_test.rb +43 -0
- data/test/whitespace/instructions/arithmetic/div_test.rb +52 -0
- data/test/whitespace/instructions/arithmetic/mod_test.rb +52 -0
- data/test/whitespace/instructions/arithmetic/mul_test.rb +43 -0
- data/test/whitespace/instructions/arithmetic/sub_test.rb +43 -0
- data/test/whitespace/instructions/flow_control/call_test.rb +50 -0
- data/test/whitespace/instructions/flow_control/end_test.rb +15 -0
- data/test/whitespace/instructions/flow_control/label_test.rb +24 -0
- data/test/whitespace/instructions/flow_control/njmp_test.rb +94 -0
- data/test/whitespace/instructions/flow_control/return_test.rb +33 -0
- data/test/whitespace/instructions/flow_control/ujmp_test.rb +44 -0
- data/test/whitespace/instructions/flow_control/zjmp_test.rb +94 -0
- data/test/whitespace/instructions/heap_access/retrieve_test.rb +49 -0
- data/test/whitespace/instructions/heap_access/store_test.rb +44 -0
- data/test/whitespace/instructions/instruction_test.rb +11 -0
- data/test/whitespace/instructions/io/putc_test.rb +42 -0
- data/test/whitespace/instructions/io/putn_test.rb +42 -0
- data/test/whitespace/instructions/io/readc_test.rb +71 -0
- data/test/whitespace/instructions/io/readn_test.rb +79 -0
- data/test/whitespace/instructions/stack_manipulation/discard_test.rb +30 -0
- data/test/whitespace/instructions/stack_manipulation/dup_test.rb +32 -0
- data/test/whitespace/instructions/stack_manipulation/push_test.rb +30 -0
- data/test/whitespace/instructions/stack_manipulation/swap_test.rb +43 -0
- data/test/whitespace/parser_test.rb +362 -0
- data/test/whitespace/util_test.rb +87 -0
- data/test/whitespace/vm_test.rb +80 -0
- data/whitespace-ruby.gemspec +30 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -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
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -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.
|
data/README.md
ADDED
@@ -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)
|
data/Rakefile
ADDED
data/bin/whitespace
ADDED
@@ -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
|
data/examples/count.ws
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
data/examples/fact.ws
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
Ask the user for a
|
2
|
+
number, then calculate its
|
3
|
+
factorial.
|
4
|
+
|
5
|
+
|
6
|
+
This program shows how we
|
7
|
+
handle recursion as well as big numbers.
|
8
|
+
|
9
|
+
Try giving 10000 as an input...
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
data/examples/hello.ws
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
Say hello.
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|