spaghetti_stack 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cbfd21862a7b85f0cfd86d9ca6c495e9e771a8c9697b647fa259a07071f6092d
4
+ data.tar.gz: 0c552ab02d78aade82bbae148d1a641faae114f193a858cb3c8b1613648c52b8
5
+ SHA512:
6
+ metadata.gz: 3c87174f585aba57f8abf06d0a0bb3dc83486ec1dd2c1ac5e3a1af5b3070ade33f8a93e117ac48f12d30371ac01580fe20e34b700acdb776272c05dabfab294c
7
+ data.tar.gz: 95d19e559bc1104a8be43008fd08bd56e40ff36cbbb2ab411472710f79abf22c326f3122bc8da2dcfdba4564af08e52bd08a506f84efd72d257f129bcbefa68d
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.2
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in spaghetti_stack.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "pry"
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ spaghetti_stack (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.3)
10
+ method_source (1.0.0)
11
+ minitest (5.14.2)
12
+ pry (0.13.1)
13
+ coderay (~> 1.1)
14
+ method_source (~> 1.0)
15
+ rake (13.0.3)
16
+
17
+ PLATFORMS
18
+ x86_64-linux
19
+
20
+ DEPENDENCIES
21
+ minitest (~> 5.0)
22
+ pry
23
+ rake (~> 13.0)
24
+ spaghetti_stack!
25
+
26
+ BUNDLED WITH
27
+ 2.2.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 mansakondo
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.
@@ -0,0 +1,56 @@
1
+ # SpaghettiStack
2
+
3
+ This is a Ruby implementation of a [SpaghettiStack](https://en.wikipedia.org/wiki/Parent_pointer_tree)(also called *parent pointer tree* or *cactus stack*). A SpaghettiStack can be thought as a linked list where each element (call nodes) keeps a reference pointing to their parent (possibly null), but not to their children. This means that a node can only access to it's parent, but not it's children.
4
+ Another particularity of this kind of stack is that when nodes are popped, they're kept in-memory instead of being destroyed, thus preserving the link to their parent. That's why this data structure is used by compilers to create symbol tables for each lexical scopes, because it allows them to revisit previous scopes one scope at the time to resolve references.
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'spaghetti_stack'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install spaghetti_stack
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "spaghetti_stack"
25
+
26
+ scopes = SpaghettiStack.new(:class)
27
+
28
+ p scopes.root.data == :class # => true
29
+
30
+ scopes.push(:def)
31
+ scopes.push(:block)
32
+ p scopes.top.data == :block # => true
33
+
34
+ p scopes # => (class <~ def <~ block)
35
+
36
+ scopes.pop
37
+ scopes.pop
38
+ p scopes.root == scopes.top # => true
39
+
40
+ p scopes.visited_nodes.map(&:data) # => [:block, :def]
41
+ p scopes # => (class)
42
+ ```
43
+
44
+ ## Development
45
+
46
+ 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.
47
+
48
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/spaghetti_stack.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "spaghetti_stack"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require "pry"
12
+ Pry.start
13
+
14
+ #require "irb"
15
+ #IRB.start(__FILE__)
@@ -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
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "spaghetti_stack/version"
4
+
5
+ class SpaghettiStack
6
+
7
+ class Node
8
+ attr_reader :data, :parent
9
+
10
+ def initialize(data, parent = nil)
11
+ @data = data
12
+ @parent = parent
13
+ end
14
+ end
15
+
16
+ attr_reader :root, :top
17
+
18
+ def initialize(data)
19
+ @root = Node.new(data)
20
+ @top = root
21
+ end
22
+
23
+ def push(data)
24
+ node = Node.new(data, top)
25
+ @top = node
26
+ end
27
+
28
+ def pop
29
+ node = top
30
+ parent = node.parent
31
+
32
+ if parent
33
+ @top = parent
34
+ visited_nodes << node
35
+ end
36
+ end
37
+
38
+ def visited_nodes
39
+ @visited_nodes ||= []
40
+ end
41
+
42
+ def each
43
+ return self.to_enum unless block_given?
44
+
45
+ node = top
46
+ until node == root
47
+ yield node
48
+ node = node.parent
49
+ end
50
+ yield node
51
+
52
+ visited_nodes.each { |visited_node| yield visited_node }
53
+
54
+ self
55
+ end
56
+
57
+ def inspect
58
+ output = ""
59
+
60
+ each do |node|
61
+ data = node.data
62
+
63
+ if node == root
64
+ output = "(#{data}#{output})"
65
+ return output
66
+ else
67
+ output = " <~ #{data}" + output
68
+ end
69
+ end
70
+
71
+ super
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SpaghettiStack
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/spaghetti_stack/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "spaghetti_stack"
7
+ spec.version = SpaghettiStack::VERSION
8
+ spec.authors = ["mansakondo"]
9
+ spec.email = ["mansakondo22@gmail.com"]
10
+
11
+ spec.summary = "A Ruby implementation of a SpaghettiStack"
12
+ spec.homepage = "https://github.com/mansakondo/spaghetti_stack"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ # Uncomment to register a new dependency of your gem
29
+ # spec.add_dependency "example-gem", "~> 1.0"
30
+
31
+ # For more information and examples about making a new gem, checkout our
32
+ # guide at: https://bundler.io/guides/creating_gem.html
33
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spaghetti_stack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - mansakondo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - mansakondo22@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - lib/spaghetti_stack.rb
30
+ - lib/spaghetti_stack/version.rb
31
+ - spaghetti_stack.gemspec
32
+ homepage: https://github.com/mansakondo/spaghetti_stack
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ homepage_uri: https://github.com/mansakondo/spaghetti_stack
37
+ source_code_uri: https://github.com/mansakondo/spaghetti_stack
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.3.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.1.4
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: A Ruby implementation of a SpaghettiStack
57
+ test_files: []