lolcode 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +7 -0
- data/Guardfile +27 -0
- data/LICENSE.txt +22 -0
- data/README.md +26 -0
- data/Rakefile +6 -0
- data/VERSION +1 -0
- data/bin/lolcode +11 -0
- data/lib/lolcode.rb +4 -0
- data/lib/lolcode/cli.rb +64 -0
- data/lib/lolcode/translator.rb +59 -0
- data/lib/lolcode/vm.rb +94 -0
- data/lolcode.gemspec +23 -0
- data/test/comma.lol +5 -0
- data/test/comma.txt +0 -0
- data/test/comments.lol +23 -0
- data/test/comments.txt +7 -0
- data/test/hello_world.lol +4 -0
- data/test/hello_world.txt +1 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f5c7315b62253df28ceda0ca3cb7ee7a52a5cc4
|
4
|
+
data.tar.gz: c9d0779db1cc322bcf407bffc02ed0c6e711b279
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 61dd1772c9078d23cefdc94b53d12262a0ef0267b56c7dae2d76154cb3f5ba61d9774410185120cdc250a8c4b9224ca463f15af3b1cb19f0fa6061499989ee30
|
7
|
+
data.tar.gz: a0473d3c61080f4b8e47d7e2731dfa221ac8df1f3292d398bb76945ed2b3148b5ba58e9fbea47236a6d5406c3825b6bf22e8a4dbe7f9e48fef241dbbaf4c3460
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
|
3
|
+
guard :shell do
|
4
|
+
watch(/lib\/(.*)\.rb$/) do |m|
|
5
|
+
all_passed = true
|
6
|
+
Dir['test/*.lol'].each do |test|
|
7
|
+
print "Run #{test}: ".yellow
|
8
|
+
|
9
|
+
res = `bin/lolcode #{test}`
|
10
|
+
ans = File.open(test.gsub('.lol', '.txt')).read
|
11
|
+
|
12
|
+
if res == ans
|
13
|
+
puts "passed".green
|
14
|
+
else
|
15
|
+
puts "\nexpected:".yellow
|
16
|
+
puts ans
|
17
|
+
puts "gotten:".yellow
|
18
|
+
puts res
|
19
|
+
puts "DEBUG:".yellow
|
20
|
+
puts `bin/lolcode -v #{test}`
|
21
|
+
all_passed = false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
n 'LOLCODE', '', all_passed ? :success : :failed
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ranmocy Sheng
|
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,26 @@
|
|
1
|
+
# LOLCODE
|
2
|
+
|
3
|
+
LOLCODE interpreter for English and Chinese version stand on Ruby.
|
4
|
+
|
5
|
+
Currently working on English version.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Probably you will install it yourself as:
|
10
|
+
|
11
|
+
$ gem install lolcode
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Directly run `lolcode` will give you a interpretor,
|
16
|
+
or you can specify a file like `lolcode FILE.lol` to run the lol script.
|
17
|
+
|
18
|
+
For more information, please use `lolcode -h` for help.
|
19
|
+
|
20
|
+
## Contributing
|
21
|
+
|
22
|
+
1. Fork it
|
23
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
24
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
25
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
26
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/bin/lolcode
ADDED
data/lib/lolcode.rb
ADDED
data/lib/lolcode/cli.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require_relative 'vm'
|
3
|
+
|
4
|
+
module Lolcode
|
5
|
+
class CLI
|
6
|
+
attr_accessor :files, :options
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
self.options = {}
|
10
|
+
|
11
|
+
OptionParser.new do |opts|
|
12
|
+
opts.banner =
|
13
|
+
<<-banner
|
14
|
+
Welcome to Lolcode.
|
15
|
+
When file is given it is a script excutor, otherwise it's a interpretor.
|
16
|
+
Usage: lolcode [options] [file]
|
17
|
+
banner
|
18
|
+
|
19
|
+
opts.on("-v", "Run verbosely") do |v|
|
20
|
+
self.options[:verbose] = v
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
24
|
+
puts opts
|
25
|
+
exit 1
|
26
|
+
end
|
27
|
+
|
28
|
+
self.files = opts.permute(ARGV) # rest args in ARGV
|
29
|
+
end.parse!
|
30
|
+
|
31
|
+
if self.files.empty?
|
32
|
+
interpretor
|
33
|
+
else
|
34
|
+
excutor
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def interpretor
|
42
|
+
puts "Welcome to use LOLCODE interpretor."
|
43
|
+
vm = VM.new(self.options)
|
44
|
+
print "(-o-)> "
|
45
|
+
while line = readline
|
46
|
+
puts "=> #{vm.run(line) || 'nil'}"
|
47
|
+
puts "I'm awake!" if line =~ /^HAI\b/
|
48
|
+
puts "Good night!" if line =~ /^KTHXBYE\b/
|
49
|
+
print vm.started? ? "(lol)> " : "(-o-)> "
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def excutor
|
54
|
+
files.each do |filename|
|
55
|
+
vm = VM.new(self.options)
|
56
|
+
File.open(filename).readlines.each do |line|
|
57
|
+
vm.run(line)
|
58
|
+
end
|
59
|
+
vm.flush
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Lolcode
|
2
|
+
module Translator
|
3
|
+
|
4
|
+
# Translate to Ruby
|
5
|
+
def translate(line)
|
6
|
+
line = line.dup
|
7
|
+
|
8
|
+
line.gsub!(/\bBTW\b/, ' # ') # BTW comments
|
9
|
+
# line.gsub!(/\bOBTW\b/, '\n=begin\n') # multiline comments begin
|
10
|
+
# multiline comments begin
|
11
|
+
line.gsub!(/\bOBTW\b/) do |s|
|
12
|
+
self.block_level += 1
|
13
|
+
"\n=begin\n"
|
14
|
+
end
|
15
|
+
# multiline comments end
|
16
|
+
line.gsub!(/\bTLDR\b/) do |s|
|
17
|
+
self.block_level -= 1
|
18
|
+
"\n=end\n"
|
19
|
+
end
|
20
|
+
|
21
|
+
line.gsub!(/\bVISIBLE\s+([\"\w]+)/) do |s|
|
22
|
+
m = /\bVISIBLE\s+([\"\w]+)/.match s
|
23
|
+
case type_of(m[1])
|
24
|
+
when :symbol
|
25
|
+
"puts @#{m[1]}"
|
26
|
+
else
|
27
|
+
"puts #{m[1]}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
line.gsub!(/\bINVISIBLE\s+([\"\w]+)/) do |s|
|
31
|
+
m = /\bINVISIBLE\s+([\"\w]+)/.match s
|
32
|
+
case type_of(m[1])
|
33
|
+
when :symbol
|
34
|
+
"warn @#{m[1]}"
|
35
|
+
else
|
36
|
+
"warn #{m[1]}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Varible assignment
|
41
|
+
line.gsub!(/\bI\s+HAS\s+A\s+(\w+)\s+ITZ\s+([\"\w]+)/) do |s|
|
42
|
+
m = /\bI\s+HAS\s+A\s+(\w+)\s+ITZ\s+([\"\w]+)/.match s
|
43
|
+
case type_of(m[2])
|
44
|
+
when :symbol
|
45
|
+
"@#{m[1]} = @#{m[2]}"
|
46
|
+
else
|
47
|
+
"@#{m[1]} = #{m[2]}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
line.gsub!(/\bI\s+HAS\s+A\s+(\w+)/, '@\1 = nil')
|
51
|
+
|
52
|
+
# TODO: Library
|
53
|
+
line.gsub!(/\bCAN\s+HAS\s+(\w+)/, '# require \'\1\'')
|
54
|
+
|
55
|
+
return line
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
data/lib/lolcode/vm.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require_relative 'translator'
|
2
|
+
|
3
|
+
module Lolcode
|
4
|
+
class VM
|
5
|
+
include Lolcode::Translator
|
6
|
+
|
7
|
+
attr_accessor :buffer, :block_level, :started, :verbose
|
8
|
+
|
9
|
+
def initialize(options={})
|
10
|
+
reset_all
|
11
|
+
$stdout.sync = true
|
12
|
+
$stderr.sync = true
|
13
|
+
self.verbose = options[:verbose] || false
|
14
|
+
end
|
15
|
+
|
16
|
+
def run(line)
|
17
|
+
puts "[INFO] Orig line: #{line}" if self.verbose
|
18
|
+
|
19
|
+
(start; return) if line =~ /^HAI\b.*/ # start VM
|
20
|
+
(halt; return) if line =~ /^KTHXBYE\b.*/ # halt VM
|
21
|
+
return unless started?
|
22
|
+
|
23
|
+
# TODO: comma in the string shouldn't be splited
|
24
|
+
ruby_line = line.split(',').collect do |l|
|
25
|
+
translate(l)
|
26
|
+
end.join(';')
|
27
|
+
|
28
|
+
puts "[INFO] Ruby code: #{ruby_line}" if self.verbose
|
29
|
+
|
30
|
+
self.buffer << ruby_line
|
31
|
+
return if open_block?
|
32
|
+
|
33
|
+
res = nil
|
34
|
+
begin
|
35
|
+
puts "[INFO] Eval code: #{self.buffer}" if self.verbose
|
36
|
+
res = eval self.buffer
|
37
|
+
reset_buffer
|
38
|
+
rescue
|
39
|
+
puts "[ERROR] Exec Error: #{line}"
|
40
|
+
end
|
41
|
+
|
42
|
+
$stdout.flush
|
43
|
+
$stderr.flush
|
44
|
+
|
45
|
+
res
|
46
|
+
end
|
47
|
+
|
48
|
+
# Flush the buffer
|
49
|
+
def flush
|
50
|
+
run('')
|
51
|
+
end
|
52
|
+
|
53
|
+
def started?
|
54
|
+
self.started
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def start
|
61
|
+
self.started = true
|
62
|
+
puts "[INFO] Now LOLCODE VM is started" if self.verbose
|
63
|
+
end
|
64
|
+
|
65
|
+
def halt
|
66
|
+
reset_all
|
67
|
+
puts "[INFO] Now LOLCODE VM is halted" if self.verbose
|
68
|
+
end
|
69
|
+
|
70
|
+
def reset_buffer
|
71
|
+
self.buffer = ""
|
72
|
+
end
|
73
|
+
|
74
|
+
def reset_all
|
75
|
+
instance_variables.each { |v| remove_instance_variable(v) }
|
76
|
+
self.started = false
|
77
|
+
self.block_level = 0
|
78
|
+
reset_buffer
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
def open_block?
|
83
|
+
self.block_level > 0
|
84
|
+
end
|
85
|
+
|
86
|
+
def type_of string
|
87
|
+
return :string if string =~ /^\"(.*)\"$/
|
88
|
+
return :int if string =~ /^[0-9]+$/
|
89
|
+
return :symbol if string =~ /^\w+$/
|
90
|
+
return :untyped
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
data/lolcode.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$LOAD_PATH.push(File.expand_path('../lib', __FILE__)).uniq!
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "lolcode"
|
5
|
+
spec.version = File.open('VERSION') { |f| f.read }
|
6
|
+
spec.authors = ["Ranmocy Sheng"]
|
7
|
+
spec.email = ["ranmocy@gmail.com"]
|
8
|
+
spec.description = %q{LOLCODE interpreter for English and Chinese}
|
9
|
+
spec.summary = %q{LOLCODE interpreter for English and Chinese version stand on Ruby}
|
10
|
+
spec.homepage = "https://github.com/ranmocy/lolcode"
|
11
|
+
spec.license = "MIT"
|
12
|
+
|
13
|
+
spec.files = `git ls-files`.split($/)
|
14
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
15
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
+
spec.require_paths = ["lib"]
|
17
|
+
|
18
|
+
spec.add_dependency "args_parser", ">= 0.2"
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "version"
|
23
|
+
end
|
data/test/comma.lol
ADDED
data/test/comma.txt
ADDED
File without changes
|
data/test/comments.lol
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
HAI
|
2
|
+
I HAS A VAR ITZ 1 BTW VAR = 1
|
3
|
+
VISIBLE VAR
|
4
|
+
I HAS A VAR ITZ 2, BTW VAR = 2
|
5
|
+
VISIBLE VAR
|
6
|
+
I HAS A VAR ITZ 3
|
7
|
+
BTW VAR = 3
|
8
|
+
VISIBLE VAR
|
9
|
+
I HAS A VAR ITZ 4
|
10
|
+
OBTW this is a long comment block
|
11
|
+
see, i have more comments here
|
12
|
+
and here
|
13
|
+
TLDR
|
14
|
+
VISIBLE VAR
|
15
|
+
I HAS A FISH ITZ VAR
|
16
|
+
VISIBLE FISH
|
17
|
+
I HAS A VAR ITZ 5, OBTW this is a long comment block
|
18
|
+
see, i have more comments here
|
19
|
+
and here
|
20
|
+
TLDR, I HAS A FISH ITZ VAR
|
21
|
+
VISIBLE VAR
|
22
|
+
VISIBLE FISH
|
23
|
+
KTHXBYE
|
@@ -0,0 +1 @@
|
|
1
|
+
HAI WORLD!
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lolcode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ranmocy Sheng
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: args_parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: version
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: LOLCODE interpreter for English and Chinese
|
70
|
+
email:
|
71
|
+
- ranmocy@gmail.com
|
72
|
+
executables:
|
73
|
+
- lolcode
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- Gemfile
|
79
|
+
- Guardfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- VERSION
|
84
|
+
- bin/lolcode
|
85
|
+
- lib/lolcode.rb
|
86
|
+
- lib/lolcode/cli.rb
|
87
|
+
- lib/lolcode/translator.rb
|
88
|
+
- lib/lolcode/vm.rb
|
89
|
+
- lolcode.gemspec
|
90
|
+
- test/comma.lol
|
91
|
+
- test/comma.txt
|
92
|
+
- test/comments.lol
|
93
|
+
- test/comments.txt
|
94
|
+
- test/hello_world.lol
|
95
|
+
- test/hello_world.txt
|
96
|
+
homepage: https://github.com/ranmocy/lolcode
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.1.11
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: LOLCODE interpreter for English and Chinese version stand on Ruby
|
120
|
+
test_files:
|
121
|
+
- test/comma.lol
|
122
|
+
- test/comma.txt
|
123
|
+
- test/comments.lol
|
124
|
+
- test/comments.txt
|
125
|
+
- test/hello_world.lol
|
126
|
+
- test/hello_world.txt
|