befunger 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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/.yardopts +2 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +85 -0
- data/Rakefile +10 -0
- data/befunger.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/befunger.rb +18 -0
- data/lib/befunger/interpreter.rb +250 -0
- data/lib/befunger/version.rb +3 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 13f8c6222f3f991b2316f1af4dfd93fbab419da6
|
4
|
+
data.tar.gz: 7445278203eb52b1bed7bad1b4802261f70ff2a2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3859a68f657555bae5791d901d19c1f064a254048cc814920ef906725a4a9c8fc5b8a930d8fe0c336cfa45cdb73231b83103cbde889883bce162e3823e68614
|
7
|
+
data.tar.gz: 7d0115f877f145a2bede98c73f2a4e449beaf960099cd001e712d315035dce0bfe69784fdb60b5a408b8a001f706fc42cdb1bc7de42d66fedfd6a0af7ae7cea5
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 rohitpaulk@live.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
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Paul Kuruvilla
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# Befunger
|
2
|
+
|
3
|
+
Befunger is an interpreter for [Befunge](https://esolangs.org/wiki/Befunge) written in Ruby. I wrote this as an answer to [this Kata on codewars](https://www.codewars.com/kata/befunge-interpreter).
|
4
|
+
|
5
|
+
### Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'befunger'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install befunger
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
require 'befunger'
|
25
|
+
|
26
|
+
Befunge.run(code)
|
27
|
+
```
|
28
|
+
|
29
|
+
## Specification
|
30
|
+
|
31
|
+
This is the [original problem statement](https://www.codewars.com/kata/befunge-interpreter) I wrote this for:
|
32
|
+
|
33
|
+
Your task is to write a method which will interpret Befunge-93 code! Befunge-93 is a language in which the code is presented not as a series of instructions, but as instructions scattered on a 2D plane; your pointer starts at the top-left corner and defaults to moving right through the code. Note that the instruction pointer wraps around the screen! There is a singular stack which we will assume is unbounded and only contain integers. While Befunge-93 code is supposed to be restricted to 80x25, you need not be concerned with code size. Befunge-93 supports the following instructions
|
34
|
+
|
35
|
+
- `0-9` Push this number onto the stack.
|
36
|
+
- `+` Addition: Pop a and b, then push a+b.
|
37
|
+
- `-` Subtraction: Pop a and b, then push b-a.
|
38
|
+
- `*` Multiplication: Pop a and b, then push a*b.
|
39
|
+
- `/` Integer division: Pop a and b, then push b/a, rounded down. If a is zero, push zero.
|
40
|
+
- `%` Modulo: Pop a and b, then push the b%a. If a is zero, push zero.
|
41
|
+
- `!` Logical NOT: Pop a value. If the value is zero, push 1; otherwise, push zero.
|
42
|
+
- `` ` `` Greater than: Pop a and b, then push 1 if b>a, otherwise push zero.
|
43
|
+
- `>` Start moving right.
|
44
|
+
- `<` Start moving left.
|
45
|
+
- `^` Start moving up.
|
46
|
+
- `v` Start moving down.
|
47
|
+
- `?` Start moving in a random cardinal direction.
|
48
|
+
- `_` Pop a value; move right if value = 0, left otherwise.
|
49
|
+
- `|` Pop a value; move down if value = 0, up otherwise.
|
50
|
+
- `"` Start string mode: push each character's ASCII value all the way up to the next ".
|
51
|
+
- `:` Duplicate value on top of the stack. If there is nothing on top of the stack, push a 0.
|
52
|
+
- `\` Swap two values on top of the stack. If there is only one value, pretend there is an extra 0 on bottom of the stack.
|
53
|
+
- `$` Pop value from the stack and discard it.
|
54
|
+
- `.` Pop value and output as an integer.
|
55
|
+
- `,` Pop value and output the ASCII character represented by the integer code that is stored in the value.
|
56
|
+
- `#` Trampoline: Skip next cell.
|
57
|
+
- `p` A "put" call (a way to store a value for later use). Pop y, x and v, then change the character at the position (x,y) in the program to the character with ASCII value v.
|
58
|
+
- `g` A "get" call (a way to retrieve data in storage). Pop y and x, then push ASCII value of the character at that position in the program.
|
59
|
+
- `@` End program.
|
60
|
+
- ` ` (i.e. a space) No-op. Does nothing.
|
61
|
+
|
62
|
+
Here's an example:
|
63
|
+
|
64
|
+
```
|
65
|
+
>987v>.v
|
66
|
+
v456< :
|
67
|
+
>321 ^ _@
|
68
|
+
```
|
69
|
+
|
70
|
+
will create the output `123456789`.
|
71
|
+
|
72
|
+
## Development
|
73
|
+
|
74
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
75
|
+
|
76
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rohitpaulk/befunge-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
81
|
+
|
82
|
+
## License
|
83
|
+
|
84
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
85
|
+
|
data/Rakefile
ADDED
data/befunger.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'befunger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "befunger"
|
8
|
+
spec.version = Befunger::VERSION
|
9
|
+
spec.authors = ["Paul Kuruvilla"]
|
10
|
+
spec.email = ["rohitpaulk@live.com"]
|
11
|
+
|
12
|
+
spec.summary = "A befunge interpreter"
|
13
|
+
spec.homepage = "https://github.com/rohitpaulk/befunge-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "befunger"
|
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
data/lib/befunger.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "befunger/interpreter"
|
2
|
+
require "befunger/version"
|
3
|
+
|
4
|
+
# Example usage:
|
5
|
+
#
|
6
|
+
# ```
|
7
|
+
# code = ">987v>.v\nv456< :\n>321 ^ _@\n"
|
8
|
+
# output = Befunger.run(code)
|
9
|
+
# ```
|
10
|
+
module Befunger
|
11
|
+
# Runs befunge code and returns the output
|
12
|
+
#
|
13
|
+
# @param code [String] the code to be run, lines separated with newline characters.
|
14
|
+
# @return [String] output of the program
|
15
|
+
def self.run(code)
|
16
|
+
Befunger::Interpreter.new(code).run
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,250 @@
|
|
1
|
+
module Befunger
|
2
|
+
# {Befunger::Interpreter} is responsible for running Befunge code.
|
3
|
+
#
|
4
|
+
# The only input to the Interpreter is the code to be executed,
|
5
|
+
# passed in as a string. Lines are to be separated with the `\n`
|
6
|
+
# character.
|
7
|
+
#
|
8
|
+
# Example usage:
|
9
|
+
#
|
10
|
+
# ```
|
11
|
+
# output = Befunger::Interpreter.new(code_string).run
|
12
|
+
# ```
|
13
|
+
class Interpreter
|
14
|
+
# The Befunge code to be run, stored as a 2D array of characters.
|
15
|
+
#
|
16
|
+
# For a program like this:
|
17
|
+
#
|
18
|
+
# ```
|
19
|
+
# >987v>.v
|
20
|
+
# v456< :
|
21
|
+
# >321 ^ _@
|
22
|
+
# ```
|
23
|
+
#
|
24
|
+
# `code_array` will look like:
|
25
|
+
#
|
26
|
+
# ```
|
27
|
+
# [
|
28
|
+
# [">", "9", "8", "7", "v", ">", ".", "v"],
|
29
|
+
# ["v", "4", "5", "6", "<", " ", " ", ":"],
|
30
|
+
# [">", "3", "2", "1", " ", "^", " ", "_", "@"]
|
31
|
+
# ]
|
32
|
+
# ```
|
33
|
+
#
|
34
|
+
attr_reader :code_array
|
35
|
+
|
36
|
+
# Human friendly constants for the direction to move a code_pointer in.
|
37
|
+
DIRECTIONS = {
|
38
|
+
right: { x: 1, y: 0 },
|
39
|
+
down: { x: 0, y: 1 },
|
40
|
+
left: { x: -1, y: 0 },
|
41
|
+
up: { x: 0, y: -1 }
|
42
|
+
}
|
43
|
+
|
44
|
+
# Default interpreter state.
|
45
|
+
#
|
46
|
+
# This gets copied and then modified and passed from instruction to instruction
|
47
|
+
# in the interpreter loop.
|
48
|
+
#
|
49
|
+
# - ## `code_pointer`
|
50
|
+
#
|
51
|
+
# Denotes what the position of the current instruction is.
|
52
|
+
#
|
53
|
+
# `x` and `y` are positive values starting at 0, calculated like this:
|
54
|
+
#
|
55
|
+
# ```
|
56
|
+
# .----------------------------------------------> x
|
57
|
+
# |
|
58
|
+
# |
|
59
|
+
# |
|
60
|
+
# | (code_array) --------> {x: 5, y: 0}
|
61
|
+
# | |
|
62
|
+
# | [ |
|
63
|
+
# |
|
64
|
+
# | [">", "9", "8", "7", "v", ">", ".", "v"],
|
65
|
+
# |
|
66
|
+
# | ["v", "4", "5", "6", "<", " ", " ", ":"],
|
67
|
+
# |
|
68
|
+
# | [">", "3", "2", "1", " ", "^", " ", "_", "@"]
|
69
|
+
# |
|
70
|
+
# | ] |
|
71
|
+
# | |
|
72
|
+
# | ------> {x: 8, y: 2}
|
73
|
+
# v
|
74
|
+
#
|
75
|
+
# y
|
76
|
+
#
|
77
|
+
# ```
|
78
|
+
#
|
79
|
+
# ## `code_direction`
|
80
|
+
#
|
81
|
+
# The next direction to move `code_pointer` in.
|
82
|
+
#
|
83
|
+
# `x` and `y` are either 0, 1, or -1.
|
84
|
+
#
|
85
|
+
# - (x: 1, y: 0) means the next move is towards the right
|
86
|
+
# - (x: 0, y: 1) means the next move is downwards
|
87
|
+
# - ... so on and so forth.
|
88
|
+
#
|
89
|
+
# Human-friendly constants available in {Interpreter::DIRECTIONS}.
|
90
|
+
#
|
91
|
+
# ## `stack`
|
92
|
+
#
|
93
|
+
# A stack to store values, manipulated by the befunge code being run.
|
94
|
+
#
|
95
|
+
# ## `output`
|
96
|
+
#
|
97
|
+
# Output returned from program, represented as an array of characters.
|
98
|
+
INITIAL_STATE = {
|
99
|
+
code_pointer: {x: 0, y: 0}, # Instructions are evaluated from the top-left
|
100
|
+
code_direction: Interpreter::DIRECTIONS[:right],
|
101
|
+
stack: [],
|
102
|
+
output: []
|
103
|
+
}
|
104
|
+
|
105
|
+
# @param code [String] the befunge code to be run. Lines must be separated with newline characters
|
106
|
+
def initialize(code)
|
107
|
+
@code_array = code.split("\n").map { |line| line.chars }
|
108
|
+
end
|
109
|
+
|
110
|
+
# @return [String] output of program execution
|
111
|
+
def run
|
112
|
+
state = Interpreter::INITIAL_STATE # TODO: `deep_dup`?
|
113
|
+
|
114
|
+
loop do
|
115
|
+
instruction = get_instruction(state[:code_pointer])
|
116
|
+
|
117
|
+
break if instruction == '@'
|
118
|
+
|
119
|
+
state = handle_instruction(instruction, state)
|
120
|
+
end
|
121
|
+
|
122
|
+
state[:output].join('')
|
123
|
+
end
|
124
|
+
|
125
|
+
private
|
126
|
+
|
127
|
+
# Returns the next instruction to be processed
|
128
|
+
#
|
129
|
+
# @param code_pointer [Hash] the current +code_pointer+, in the form: `{x: 0, y: 3}`
|
130
|
+
# @return [String] the next instruction to be processed
|
131
|
+
def get_instruction(code_pointer)
|
132
|
+
@code_array[code_pointer[:y]][code_pointer[:x]]
|
133
|
+
end
|
134
|
+
|
135
|
+
# Moves the `code_pointer` in the specified `code_direction`, and returns the modified
|
136
|
+
# `code_pointer`
|
137
|
+
#
|
138
|
+
# @param code_pointer [Hash] the current +code_pointer+, in the form: `{x: 0, y: 3}`
|
139
|
+
# @param code_direction [Hash] the current +code_direction+, in the form: `{x: 0, y: 1}`
|
140
|
+
# @return [Hash] the modified `code_pointer`
|
141
|
+
def move_pointer(code_pointer, code_direction)
|
142
|
+
code_pointer[:x] += code_direction[:x]
|
143
|
+
code_pointer[:y] += code_direction[:y]
|
144
|
+
|
145
|
+
code_pointer
|
146
|
+
end
|
147
|
+
|
148
|
+
# Given an `instruction` and `state` - processes the instruction and returns
|
149
|
+
# the modified `state`.
|
150
|
+
#
|
151
|
+
# @param instruction [String] the instruction to be executed
|
152
|
+
# @param state [Hash] the current interpreter state
|
153
|
+
# @return [Hash] the modified `state`
|
154
|
+
def handle_instruction(instruction, state)
|
155
|
+
output = state[:output].dup
|
156
|
+
stack = state[:stack].dup
|
157
|
+
code_pointer = state[:code_pointer].dup
|
158
|
+
code_direction = state[:code_direction].dup
|
159
|
+
|
160
|
+
# Numbers
|
161
|
+
if ('0'..'9').include? instruction
|
162
|
+
stack.push(instruction.to_i)
|
163
|
+
|
164
|
+
# Binary Stack Operations
|
165
|
+
elsif ['+', '-', '*', '/', '%', '`'].include? instruction
|
166
|
+
a = stack.pop
|
167
|
+
b = stack.pop
|
168
|
+
|
169
|
+
value = case instruction
|
170
|
+
when '+' then b + a
|
171
|
+
when '*' then b * a
|
172
|
+
when '-' then b - a
|
173
|
+
when '/' then b / a
|
174
|
+
when '%' then b % a
|
175
|
+
when '`' then b > a ? 1 : 0
|
176
|
+
end
|
177
|
+
|
178
|
+
stack.push(value)
|
179
|
+
|
180
|
+
# Setter, Getter
|
181
|
+
elsif ['p', 'g'].include? instruction
|
182
|
+
y = stack.pop
|
183
|
+
x = stack.pop
|
184
|
+
case instruction
|
185
|
+
when 'p' then @code_array[y][x] = stack.pop.chr
|
186
|
+
when 'g' then stack.push @code_array[y][x].ord
|
187
|
+
end
|
188
|
+
|
189
|
+
# Operations with one stack value
|
190
|
+
elsif ['!', '$', '.', ','].include? instruction
|
191
|
+
a = stack.pop
|
192
|
+
|
193
|
+
case instruction
|
194
|
+
when '!' then stack.push(a == 0 ? 1 : 0)
|
195
|
+
when '.' then output.push(a)
|
196
|
+
when ',' then output.push(a.chr)
|
197
|
+
end
|
198
|
+
|
199
|
+
# Direction changes
|
200
|
+
elsif ['>', '<', 'v', '^', '?', '_', '|'].include? instruction
|
201
|
+
code_direction = case instruction
|
202
|
+
when '>' then DIRECTIONS[:right]
|
203
|
+
when '<' then DIRECTIONS[:left]
|
204
|
+
when 'v' then DIRECTIONS[:down]
|
205
|
+
when '^' then DIRECTIONS[:up]
|
206
|
+
when '?' then DIRECTIONS.values.sample
|
207
|
+
when '_' then DIRECTIONS[stack.pop == 0 ? :right : :left]
|
208
|
+
when '|' then DIRECTIONS[stack.pop == 0 ? :down : :up]
|
209
|
+
end
|
210
|
+
|
211
|
+
# String Mode
|
212
|
+
elsif instruction == '"'
|
213
|
+
code_pointer = move_pointer(code_pointer, code_direction) # Skip the first quote
|
214
|
+
|
215
|
+
loop do
|
216
|
+
instruction = get_instruction(code_pointer)
|
217
|
+
break if instruction == '"'
|
218
|
+
stack.push(instruction.ord)
|
219
|
+
code_pointer = move_pointer(code_pointer, code_direction)
|
220
|
+
end
|
221
|
+
|
222
|
+
# Swap
|
223
|
+
elsif instruction == '\\'
|
224
|
+
if stack.size == 1
|
225
|
+
stack.push(0)
|
226
|
+
else
|
227
|
+
stack[-1], stack[-2] = stack[-2], stack[-1]
|
228
|
+
end
|
229
|
+
|
230
|
+
# Duplicate
|
231
|
+
elsif instruction == ":"
|
232
|
+
stack.push(stack.empty? ? 0 : stack.last)
|
233
|
+
|
234
|
+
# Skip
|
235
|
+
elsif instruction == '#'
|
236
|
+
code_pointer = move_pointer(code_pointer, code_direction)
|
237
|
+
|
238
|
+
elsif instruction != ' '
|
239
|
+
raise "Invalid Instruction '#{instruction}'"
|
240
|
+
end
|
241
|
+
|
242
|
+
{
|
243
|
+
code_pointer: move_pointer(code_pointer, code_direction),
|
244
|
+
code_direction: code_direction,
|
245
|
+
stack: stack,
|
246
|
+
output: output
|
247
|
+
}
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: befunger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Kuruvilla
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- rohitpaulk@live.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- ".yardopts"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- befunger.gemspec
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/befunger.rb
|
74
|
+
- lib/befunger/interpreter.rb
|
75
|
+
- lib/befunger/version.rb
|
76
|
+
homepage: https://github.com/rohitpaulk/befunge-ruby
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.6.11
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: A befunge interpreter
|
100
|
+
test_files: []
|