microplane 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7101c87881bf06553225c39f82c7985c3ea1f88c
4
+ data.tar.gz: 9f051d4b24ca374eb422942e5935dff89d63001a
5
+ SHA512:
6
+ metadata.gz: 8bd07ef18b9cb9d8d3b63777fe624f74a05a726daf7867b288eee149accb110bce2f2e4610e1b5525eec760a43127d79f53ec0b369f13aae23a8125d5815001b
7
+ data.tar.gz: 3424432cad9995b98990881b5502ec1b1fe1a6cbc588a8f057a36573091e303736177ddc308e3cb675760836a028b49119e6131109248f952227e59a8a997212
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
11
+ /spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.4.1
@@ -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 carlthuringer@gmail.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
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in microplane.gemspec
6
+ gemspec
@@ -0,0 +1,13 @@
1
+ Copyright 2017 Carl Thuringer
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,43 @@
1
+ # Microplane
2
+
3
+ A microlanguage for general-purpose embedded programming inside your application.
4
+
5
+ Microplane was designed so that untrusted users could be given a limited environment
6
+ in which to define their own programs and execute code inside your application
7
+ without the risks of evaluating full ruby code.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'microplane'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install microplane
24
+
25
+ ## Usage
26
+
27
+ I don't know how this will be used yet.
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ 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).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/microplane. 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.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0).
43
+
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'microplane'
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,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Base module
4
+ module Microplane
5
+ # Your code goes here...
6
+ end
7
+
8
+ require 'microplane/version'
9
+ require 'microplane/vm'
10
+ require 'microplane/lib/std'
11
+ require 'microplane/errors'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StackUnderflow < StandardError
4
+ end
5
+
6
+ class EmptyWord < StandardError; end
7
+ class NestedDefinition < StandardError; end
@@ -0,0 +1,6 @@
1
+ module Microplane
2
+ # Namespace for function libraries
3
+ module Lib; end
4
+ end
5
+
6
+ require_relative './lib/std'
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Microplane
4
+ module Lib
5
+ # Standard library of functions.
6
+ class Std
7
+ # DICTIONARY = .freeze
8
+
9
+ def self.load_words(base)
10
+ base.instance_eval do
11
+ @dictionary.merge!(
12
+ '+' => -> { push(pop + pop) },
13
+ '-' => -> { push(pop - pop) },
14
+ '*' => -> { push(pop * pop) },
15
+ '/' => -> { push(pop / pop) },
16
+ '%' => -> { push(pop % pop) },
17
+ '<' => -> { push(pop < pop) },
18
+ '<=' => -> { push(pop <= pop) },
19
+ '=' => -> { push(pop == pop) },
20
+ '>=' => -> { push(pop >= pop) },
21
+ '>' => -> { push(pop > pop) },
22
+ 'true' => -> { push true },
23
+ 'false' => -> { push false },
24
+ '|' => -> { push(pop || pop) },
25
+ 'not' => -> { push(!pop) },
26
+ 'neg' => -> { push(-pop) },
27
+ 'pop' => -> { pop },
28
+ 'if' => -> { @skip = true },
29
+ 'fi' => -> { @skip = false },
30
+ 'dup' => lambda do
31
+ popped = pop
32
+ push(popped)
33
+ push(popped)
34
+ end,
35
+ 'over' => lambda do
36
+ first = pop
37
+ second = pop
38
+ push(first)
39
+ push(second)
40
+ end
41
+ )
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Microplane
4
+ VERSION = '0.1.0'.freeze
5
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Microplane
4
+ # Implements the basic Microplane VM
5
+ class VM
6
+ def self.run(code)
7
+ new.evaluate(code).pop
8
+ end
9
+
10
+ attr_reader :stack
11
+
12
+ def initialize
13
+ @stack = []
14
+ @skip = false
15
+ @dictionary = {}
16
+ @libraries = []
17
+ add_library Lib::Std
18
+ end
19
+
20
+ def add_library(lib)
21
+ lib.load_words(self)
22
+ end
23
+
24
+ def evaluate(code)
25
+ tokens = code.split
26
+ return define_new_word(tokens) if tokens.first == ':'
27
+ tokens.each do |w|
28
+ next if @skip == true && w != 'fi'
29
+ parse(w)
30
+ end
31
+ self
32
+ end
33
+
34
+ def pop
35
+ stack.pop
36
+ end
37
+
38
+ private
39
+
40
+ def define_new_word(tokens)
41
+ word = tokens[1]
42
+ @dictionary[word] = tokens[2..-2].join(' ')
43
+ end
44
+
45
+ def push(obj)
46
+ stack << obj
47
+ end
48
+
49
+ def parse(w)
50
+ return push(w.to_i) if number?(w)
51
+ word = @dictionary[w]
52
+ raise "Unknown Word #{w}" unless word
53
+ return word.call if word.is_a?(Proc)
54
+ evaluate(word)
55
+ end
56
+
57
+ def number?(w)
58
+ w.getbyte(0) >= 48 && w.getbyte(0) <= 57
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'microplane/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'microplane'
10
+ spec.version = Microplane::VERSION
11
+ spec.authors = ['Carl Thuringer']
12
+ spec.email = ['carlthuringer@gmail.com']
13
+
14
+ spec.summary = 'A stack-oriented, extensible microlanguage'
15
+ spec.description = 'A general-purpose microlanguage for configurable objects.'
16
+ spec.homepage = 'https://www.github.com/carlthuringer'
17
+ spec.license = 'Apache 2.0'
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ 'public gem pushes.'
26
+ end
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.14'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rspec', '~> 3.6.0'
38
+ spec.add_development_dependency 'ruby-prof', '~> 0.16.2'
39
+ spec.add_development_dependency 'benchmark-ips', '~> 2.7.2'
40
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: microplane
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Carl Thuringer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-08 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.6.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.6.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: ruby-prof
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.16.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.16.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: benchmark-ips
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.7.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.7.2
83
+ description: A general-purpose microlanguage for configurable objects.
84
+ email:
85
+ - carlthuringer@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".ruby-version"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/microplane.rb
101
+ - lib/microplane/errors.rb
102
+ - lib/microplane/lib.rb
103
+ - lib/microplane/lib/std.rb
104
+ - lib/microplane/version.rb
105
+ - lib/microplane/vm.rb
106
+ - microplane.gemspec
107
+ homepage: https://www.github.com/carlthuringer
108
+ licenses:
109
+ - Apache 2.0
110
+ metadata:
111
+ allowed_push_host: https://rubygems.org
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.6.11
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: A stack-oriented, extensible microlanguage
132
+ test_files: []