obtuse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in obtuse.gemspec
4
+ gemspec
@@ -0,0 +1,3 @@
1
+ guard 'rspec' do
2
+ watch(/^(lib|spec)/) { "spec" }
3
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Utkarsh Kukreti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Obtuse
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'obtuse'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install obtuse
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'obtuse'
3
+
4
+ Obtuse::CLI.new ARGV
@@ -0,0 +1,9 @@
1
+ require "parslet"
2
+
3
+ require "obtuse/parser"
4
+ require "obtuse/transform"
5
+ require "obtuse/evaluator"
6
+
7
+ require "obtuse/cli"
8
+
9
+ require "obtuse/version"
@@ -0,0 +1,42 @@
1
+ module Obtuse
2
+ class CLI
3
+ def initialize(argv)
4
+ repl # Just start the REPL for now
5
+ end
6
+
7
+ def repl
8
+ require "readline"
9
+ puts "Obtuse Programming Language"
10
+ puts 'Type "stack" or "s" to print the complete stack.'
11
+ puts 'Type "exit" or hit Ctrl-D to exit'
12
+ prompt = "> "
13
+
14
+ evaluator = Evaluator.new
15
+ loop do
16
+ line = Readline::readline(prompt)
17
+
18
+ if line.nil? or line == "exit"
19
+ puts if line.nil?
20
+ puts "Bye!"
21
+ exit 0
22
+ end
23
+
24
+ next if line.empty?
25
+
26
+ Readline::HISTORY << line
27
+
28
+ begin
29
+ if line == "s" || line == "stack"
30
+ p evaluator.stack
31
+ else
32
+ evaluator.eval line
33
+ p evaluator.peek
34
+ end
35
+ rescue Exception => e
36
+ puts e.message
37
+ puts e.backtrace
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ module Obtuse
2
+ class Evaluator
3
+ attr_reader :stack
4
+
5
+ def initialize
6
+ @stack = []
7
+ @parser = Parser.new
8
+ @transform = Transform.new
9
+ end
10
+
11
+ def eval(input)
12
+ @transform.apply(@parser.parse(input)).each do |atom|
13
+ case atom
14
+ when Integer, String
15
+ push atom
16
+ when :+, :-, :*, :/, :%, :^
17
+ atom = :** if atom == :^
18
+ y, x = pop, pop
19
+ if Integer === x && Integer === y ||
20
+ String === x && String === y
21
+ push x.send(atom, y)
22
+ else
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def push(object)
29
+ @stack << object
30
+ end
31
+
32
+ def pop
33
+ @stack.pop
34
+ end
35
+
36
+ def peek
37
+ @stack.last
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,28 @@
1
+ module Obtuse
2
+ class Parser < Parslet::Parser
3
+ rule(:spaces) { match['\s'].repeat(1) }
4
+ rule(:spaces?) { spaces.maybe }
5
+ rule(:digit) { match["0-9"] }
6
+ rule(:digits) { match["0-9"].repeat(1) }
7
+
8
+ rule :integer do
9
+ digits.as(:integer) >> spaces?
10
+ end
11
+
12
+ rule :string do
13
+ str('"') >> (str('"').absent? >> (str('\\"') | any)).repeat.as(:string) >>
14
+ str('"') >> spaces?
15
+ end
16
+
17
+ rule :function do
18
+ %w{+ - * / % ^}.map { |name| str name }.reduce(:|).
19
+ as(:function) >> spaces?
20
+ end
21
+
22
+ rule :expression do
23
+ spaces? >> (string | integer | function).repeat >> spaces?
24
+ end
25
+
26
+ root :expression
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ module Obtuse
2
+ class Transform < Parslet::Transform
3
+ rule(integer: simple(:x)) { x.to_i }
4
+ rule(function: simple(:x)) { x.to_s.to_sym }
5
+ rule(string: simple(:x)) do
6
+ x.to_s.gsub('\\t', "\t").gsub('\\n', "\n").gsub('\\"', '"')
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Obtuse
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'obtuse/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "obtuse"
8
+ gem.version = Obtuse::VERSION
9
+ gem.authors = ["Utkarsh Kukreti"]
10
+ gem.email = ["utkarshkukreti@gmail.com"]
11
+ gem.description = "A Stack-oriented programming language, optimized for " +
12
+ "brevity."
13
+ gem.summary = "A Stack-oriented programming language, optimized for " +
14
+ "brevity."
15
+ gem.homepage = "https://github.com/utkarshkukreti/obtuse"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_dependency "parslet"
23
+
24
+ %w{rspec guard-rspec simplecov pry}.each do |name|
25
+ gem.add_development_dependency name
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe Obtuse do
4
+ it "should have a VERSION" do
5
+ Obtuse::VERSION.should be_a String
6
+ end
7
+
8
+ describe "Integer Integer (+|-|*|/|%|^)" do
9
+ e "2 1+", 3
10
+ e "10 20 +", 30
11
+ e "2 3-", -1
12
+ e "10 20 -", -10
13
+ e "2 3*", 6
14
+ e "10 20 *", 200
15
+ e "2 3/", 0
16
+ e "10 20 /", 0
17
+ e "2 3%", 2
18
+ e "10 20 %", 10
19
+ e "2 3^", 8
20
+ e "10 20 ^", 10 ** 20
21
+
22
+ e "1 2+3-", 0
23
+ e "5 1 4 5 2 3 4 +-+-+-", 0
24
+ end
25
+
26
+ describe "String" do
27
+ e %q{"a \n b \n c \t\td\na"}, "a \n b \n c \t\td\na"
28
+ end
29
+
30
+ describe "String String +" do
31
+ e '"foo""bar"+', "foobar"
32
+ e '"foo" "bar"+', "foobar"
33
+ e '"foo\\"" "bar"+', 'foo"bar'
34
+ end
35
+ end
@@ -0,0 +1,16 @@
1
+ require "bundler/setup"
2
+ require "simplecov"
3
+ SimpleCov.start do
4
+ add_group "Libraries", "/lib"
5
+ end
6
+ require "obtuse"
7
+
8
+ RSpec.configure do |config|
9
+ def e(input, output)
10
+ it "should evaluate #{input.inspect} to #{output.inspect}" do
11
+ evaluator = Obtuse::Evaluator.new
12
+ evaluator.eval(input)
13
+ evaluator.peek.should eq output
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: obtuse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Utkarsh Kukreti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: parslet
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard-rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: A Stack-oriented programming language, optimized for brevity.
95
+ email:
96
+ - utkarshkukreti@gmail.com
97
+ executables:
98
+ - obtuse
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - .rspec
104
+ - Gemfile
105
+ - Guardfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - bin/obtuse
110
+ - lib/obtuse.rb
111
+ - lib/obtuse/cli.rb
112
+ - lib/obtuse/evaluator.rb
113
+ - lib/obtuse/parser.rb
114
+ - lib/obtuse/transform.rb
115
+ - lib/obtuse/version.rb
116
+ - obtuse.gemspec
117
+ - spec/obtuse_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: https://github.com/utkarshkukreti/obtuse
120
+ licenses: []
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 1.8.24
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: A Stack-oriented programming language, optimized for brevity.
143
+ test_files:
144
+ - spec/obtuse_spec.rb
145
+ - spec/spec_helper.rb
146
+ has_rdoc: