hquby 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.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +1 -0
- data/bin/hquby +174 -0
- data/hquby.gemspec +19 -0
- data/lib/hquby/version.rb +3 -0
- data/lib/hquby.rb +5 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Anton Ovchinnikov
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# hquby
|
2
|
+
|
3
|
+
|
4
|
+
**hquby** is a HQ9+ implementation written in Ruby.
|
5
|
+
|
6
|
+
You can:
|
7
|
+
|
8
|
+
- jump straight to interactive mode
|
9
|
+
|
10
|
+
`$ ./hquby.rb`
|
11
|
+
|
12
|
+
- execute script from the command line
|
13
|
+
|
14
|
+
`$ ./hquby.rb -e 'hqq+'`
|
15
|
+
|
16
|
+
- read commands from the standard input
|
17
|
+
|
18
|
+
`$ echo -n 'hq99+' | ./hquby.rb`
|
19
|
+
|
20
|
+
- read commands from script files
|
21
|
+
|
22
|
+
`$ ./hquby.rb script1.hq script2.hq`
|
23
|
+
|
24
|
+
**hquby** supports Readline library.
|
25
|
+
|
26
|
+
You can also use the following service commands while in interactive mode:
|
27
|
+
|
28
|
+
`:help` - show options summary
|
29
|
+
|
30
|
+
`:setprompt PROMPT` - set new prompt
|
31
|
+
|
32
|
+
`:examine` - print hidden variable value (for debug purposes!)
|
33
|
+
|
34
|
+
`:quit` - terminate the shell
|
35
|
+
|
36
|
+
## Installation
|
37
|
+
|
38
|
+
$ gem install hquby
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/hquby
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
def eval_h
|
6
|
+
puts "Hello, world!"
|
7
|
+
end
|
8
|
+
|
9
|
+
def eval_q(script_string)
|
10
|
+
puts script_string
|
11
|
+
end
|
12
|
+
|
13
|
+
def eval_9
|
14
|
+
|
15
|
+
def multibeer(n)
|
16
|
+
n = n.to_i
|
17
|
+
case n
|
18
|
+
when 0 then 'no more bottles'
|
19
|
+
when 1 then '1 bottle'
|
20
|
+
else "#{n} bottles"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
99.downto(1) do |i|
|
24
|
+
puts <<-BOTTLES
|
25
|
+
#{multibeer(i)} of beer on the wall, #{multibeer(i)} of beer.
|
26
|
+
Take one down and pass it around, #{multibeer(i-1)} of beer on the wall.
|
27
|
+
|
28
|
+
BOTTLES
|
29
|
+
end
|
30
|
+
|
31
|
+
puts <<-BOTTLES_ENDING
|
32
|
+
No more bottles of beer on the wall, no more bottles of beer.
|
33
|
+
Go to the store and buy some more, 99 bottles of beer on the wall.
|
34
|
+
BOTTLES_ENDING
|
35
|
+
end
|
36
|
+
|
37
|
+
$i = 0
|
38
|
+
def eval_plus
|
39
|
+
$i += 1
|
40
|
+
end
|
41
|
+
|
42
|
+
def show_usage
|
43
|
+
puts <<-USAGE
|
44
|
+
Usage:
|
45
|
+
|
46
|
+
-h, --help - show help
|
47
|
+
-e SCRIPT - execute script
|
48
|
+
|
49
|
+
no arguments - fall back to interactive mode
|
50
|
+
USAGE
|
51
|
+
end
|
52
|
+
|
53
|
+
def show_greeting
|
54
|
+
puts <<-GREETING
|
55
|
+
--- Welcome to hquby interactive mode!
|
56
|
+
--- hquby is an implementation of HQ9+ interpreter.
|
57
|
+
--- Type ':help' or '?' for more options.
|
58
|
+
GREETING
|
59
|
+
end
|
60
|
+
|
61
|
+
def show_help
|
62
|
+
puts <<-HELP
|
63
|
+
\nAvailable options:
|
64
|
+
|
65
|
+
:help, ? - show this help
|
66
|
+
:setprompt PROMPT - set new prompt
|
67
|
+
:examine - print hidden variable value for debug purposes
|
68
|
+
:quit - get me out of here!\n
|
69
|
+
HELP
|
70
|
+
end
|
71
|
+
|
72
|
+
def eval_config(str)
|
73
|
+
words = str.split(/\s+/)
|
74
|
+
case words[0]
|
75
|
+
when /\?|:help/ then show_help()
|
76
|
+
when ':setprompt' then $prompt = words[1].nil? ? DEFAULT_PROMPT : "#{words[1]} "
|
77
|
+
when ':quit' then exit(0)
|
78
|
+
when ':examine' then puts "#{$i}"
|
79
|
+
else puts "~ Error! Unknown statement: '#{words[0]}'"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def eval_string(str)
|
84
|
+
str.strip!
|
85
|
+
|
86
|
+
# eval config
|
87
|
+
if $is_interactive and [':', '?'].include?(str[0])
|
88
|
+
eval_config(str)
|
89
|
+
return
|
90
|
+
end
|
91
|
+
|
92
|
+
str.downcase!
|
93
|
+
str.gsub!(/\s+/, '')
|
94
|
+
commands = str.split('')
|
95
|
+
|
96
|
+
# check commands
|
97
|
+
commands.each_index do |c|
|
98
|
+
if not ['h', 'q', '9', '+'].include?(commands[c])
|
99
|
+
$stderr.puts "~ Error! Unknown symbol: '#{commands[c]}' (:#{c})"
|
100
|
+
return
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# exec
|
105
|
+
commands.each do |x|
|
106
|
+
case x
|
107
|
+
when 'h' then eval_h
|
108
|
+
when 'q' then eval_q(str)
|
109
|
+
when '9' then eval_9
|
110
|
+
when '+' then eval_plus
|
111
|
+
else puts "~ Error! This is awkward..."
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
########
|
118
|
+
# MAIN #
|
119
|
+
########
|
120
|
+
|
121
|
+
optparse = OptionParser.new do |opts|
|
122
|
+
opts.on('-h', '--help', 'Display usage') do
|
123
|
+
show_usage()
|
124
|
+
exit(0)
|
125
|
+
end
|
126
|
+
|
127
|
+
opts.on('-e SCRIPT', 'Execute script from command line') do |scr|
|
128
|
+
eval_string(scr)
|
129
|
+
exit(0)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
optparse.parse!
|
133
|
+
|
134
|
+
|
135
|
+
$is_interactive = false
|
136
|
+
# Read from scripts or STDIN
|
137
|
+
if not ARGV.empty? or not $stdin.tty?
|
138
|
+
ARGF.each do |line|
|
139
|
+
eval_string(line.chomp)
|
140
|
+
end
|
141
|
+
exit(0)
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
# Fall back to interactive mode
|
146
|
+
# TODO Check 'Readline' support!
|
147
|
+
require 'readline'
|
148
|
+
$stdout.sync = true
|
149
|
+
|
150
|
+
DEFAULT_PROMPT = '>> '
|
151
|
+
$prompt = DEFAULT_PROMPT
|
152
|
+
$is_interactive = true
|
153
|
+
|
154
|
+
# Catch Ctrl-C
|
155
|
+
trap('INT') do
|
156
|
+
puts "\n~ Interrupt"
|
157
|
+
print $prompt
|
158
|
+
end
|
159
|
+
|
160
|
+
LIST = [
|
161
|
+
':help', ':setprompt',
|
162
|
+
':quit', ':examine',
|
163
|
+
].sort
|
164
|
+
|
165
|
+
comp = proc { |s| LIST.grep( /^#{Regexp.escape(s)}/ ) }
|
166
|
+
|
167
|
+
Readline.completion_append_character = " "
|
168
|
+
Readline.completion_proc = comp
|
169
|
+
|
170
|
+
show_greeting()
|
171
|
+
while line = Readline.readline($prompt, true)
|
172
|
+
eval_string(line.chomp)
|
173
|
+
end
|
174
|
+
|
data/hquby.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hquby/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "hquby"
|
8
|
+
gem.version = Hquby::VERSION
|
9
|
+
gem.authors = ["Anton Ovchinnikov"]
|
10
|
+
gem.email = ["revolver112@gmail.com"]
|
11
|
+
gem.description = %q{HQ9+ interpreter}
|
12
|
+
gem.summary = %q{Simple HQ9+ implementation}
|
13
|
+
gem.homepage = "https://github.com/rev112/hquby"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/hquby.rb
ADDED
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hquby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Anton Ovchinnikov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: HQ9+ interpreter
|
15
|
+
email:
|
16
|
+
- revolver112@gmail.com
|
17
|
+
executables:
|
18
|
+
- hquby
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/hquby
|
28
|
+
- hquby.gemspec
|
29
|
+
- lib/hquby.rb
|
30
|
+
- lib/hquby/version.rb
|
31
|
+
homepage: https://github.com/rev112/hquby
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.25
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Simple HQ9+ implementation
|
55
|
+
test_files: []
|