mlc 0.0.1

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: 7f5180891d736d0301e0d224c4a69a838d27ec01
4
+ data.tar.gz: bf916019b350baf452630f349f35ded24dd4d8ba
5
+ SHA512:
6
+ metadata.gz: 7c1ffa0d1997363b7938e9b62e941d1a847dd9ead1830212ec7c2dedb14480400e03a30808e3927bcc4971de26700ed7ef6b10da1ecd9262cce090b28192711a
7
+ data.tar.gz: 77c4773a513d5cdcfb3c8789c37af63355ebd2a520c084134966bb014b3f12e7466d531c5ae1649a9cc6255419497baaf716eee6bb6ce6f8c9e85283afc30193
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mlc.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kilobyte
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
+ # Mlc
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mlc'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mlc
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,28 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+
4
+ nogem = false
5
+ OptionParser.new do |opts|
6
+ opts.banner = 'Usage: mlc [flags] input... output'
7
+ opts.on('-g', '-nogem', 'Do not run as gem') do |v|
8
+ nogem = true
9
+ end
10
+ end#.parse!
11
+
12
+ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '../lib'))) if defined? $DEBUG && $DEBUG
13
+
14
+ require 'mlc'
15
+
16
+ if ARGV.length < 2
17
+ $stderr.puts 'Usage: input... output'
18
+ $stderr.puts 'Example: hello.rb world.rb out.lua'
19
+ exit 1
20
+ end
21
+
22
+ out = ARGV.pop
23
+ outdata = []
24
+ ARGV.each do |file|
25
+ outdata << Mlc::Compiler.new(File.read(ARGV[0])).parse!.to_lua
26
+ end
27
+
28
+ File.write(out, outdata.join("\n"))
@@ -0,0 +1,14 @@
1
+ require 'mlc/version'
2
+ require 'mlc/abstract'
3
+ require 'mlc/compiler'
4
+ require 'mlc/parserstate'
5
+
6
+ module Mlc
7
+ def self.escape_str(str)
8
+ str.gsub('\\', '\\\\').gsub("'", "\\'")
9
+ end
10
+
11
+ def self.prepare_args(indent, options, state, args)
12
+ "(#{args.map {|el| el.to_lua(indent + 1, options, state)}.join ', '})"
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module Mlc
2
+ module Abstract
3
+
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ module Mlc
2
+ module Abstract
3
+ class Block
4
+ def initialize
5
+ @children = []
6
+ end
7
+
8
+ def <<(child)
9
+ @children << child
10
+ end
11
+
12
+ def to_lua(indent, options, state)
13
+ i = ' ' * indent
14
+ i + @children.map {|el| el.to_lua(indent + 1, options, state)}.join("\n#{i}")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module Mlc
2
+ module Abstract
3
+ class Call
4
+ attr_accessor :object, :name
5
+ attr_reader :args
6
+ def initialize
7
+ @args = []
8
+ end
9
+
10
+ def to_lua(indent, options, state)
11
+ obj = @object.to_lua(indent + 1, options, state)
12
+ args = @args.dup.unshift Raw.new('nil')
13
+ name = "m#{@name}"
14
+ if name =~ /^[A-Za-z][A-Za-z0-1_]*$/
15
+ "#{obj}:#{name}#{Mlc.prepare_args(indent, options, state, args)}"
16
+ else
17
+ args.unshift Raw.new("'#{Mlc.escape_str(name)}'")
18
+ "#{obj}:_call#{Mlc.prepare_args(indent, options, state, args)}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Mlc
2
+ module Abstract
3
+ class Const
4
+ def initialize(name)
5
+ @name = name
6
+ end
7
+
8
+ def to_lua(indent, options, state)
9
+ "_const.#{@name}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module Mlc
2
+ module Abstract
3
+ class LAsgn
4
+ def initialize(name, value)
5
+ @name = name
6
+ @value = value
7
+ end
8
+
9
+ def to_lua(indent, options, state)
10
+ ret = "l#{@name.to_s} = #{@value.to_lua(indent + 1, options, state)}"
11
+ state.add_lvar @name
12
+ ret
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module Mlc
2
+ module Abstract
3
+ class Literal
4
+ def initialize(value)
5
+ @value = value
6
+ end
7
+
8
+ def to_lua(indent, options, state)
9
+ if @value.is_a? Symbol
10
+ "_.sym('#{Mlc.escape_str(@value.to_s)}')"
11
+ elsif @value.is_a? String
12
+ "_.wrap('#{Mlc.escape_str(@value)}')"
13
+ else
14
+ "_.wrap(#{@value.to_s})"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ module Mlc
2
+ module Abstract
3
+ class LVar
4
+ def initialize(name)
5
+ @name = name
6
+ end
7
+
8
+ def to_lua(indent, options, state)
9
+ "l#{@name.to_s}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Mlc
2
+ module Abstract
3
+ class Raw
4
+ def initialize(text)
5
+ @text = text
6
+ end
7
+
8
+ def to_lua(indent, options, state)
9
+ @text
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module Mlc
2
+ module Abstract
3
+ class Snippet
4
+ def initialize
5
+ @children = []
6
+ end
7
+
8
+ def to_lua(opts = {})
9
+ template = <<URMOM
10
+ (function (mlc)
11
+ local _scope, _global, _const, _ = mlc.scope, mlc.global, mlc.const, mlc.helpers
12
+ LVARS
13
+ STUFF
14
+
15
+ end)(Mlc)
16
+ URMOM
17
+ state = Mlc::ParserState.new
18
+ (opts[:require] ? "local Mlc = require('mlc')\n" : '') +
19
+ template.gsub('STUFF', @children.first.to_lua(1, opts, state)).
20
+ gsub('LVARS', state.lvars.empty? ? '' : "\n #{state.lvars_define}")
21
+
22
+ end
23
+
24
+ def <<(child)
25
+ @children << child
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,80 @@
1
+ require 'ruby_parser'
2
+ require 'mlc/abstract/snippet'
3
+ require 'mlc/abstract/call'
4
+ require 'mlc/abstract/raw'
5
+ require 'mlc/abstract/block'
6
+ require 'mlc/abstract/literal'
7
+ require 'mlc/abstract/const'
8
+ require 'mlc/abstract/lasgn'
9
+ require 'mlc/abstract/lvar'
10
+
11
+ module Mlc
12
+ class Compiler
13
+ def initialize(code)
14
+ @tree = RubyParser.new.parse(code)
15
+ end
16
+
17
+ def parse!
18
+ puts (tree = @tree).inspect
19
+ snip = Mlc::Abstract::Snippet.new
20
+ if tree
21
+ snip << ensure_block(tree)
22
+ end
23
+ snip
24
+ end
25
+
26
+ def ensure_block(sexp)
27
+ unless sexp[0] == :block
28
+ sexp = Sexp.new :block, sexp
29
+ end
30
+ parse_block(sexp)
31
+ end
32
+
33
+ def parse(sexp)
34
+ if sexp.nil?
35
+ nil
36
+ else
37
+ send(('parse_' + sexp[0].to_s).to_sym, sexp)
38
+ end
39
+ end
40
+
41
+ def parse_block(sexp)
42
+ block = Abstract::Block.new
43
+ sexp[1..sexp.length].each do |el|
44
+ block << parse(el)
45
+ end
46
+ block
47
+ end
48
+
49
+ def parse_call(sexp)
50
+ call = Abstract::Call.new
51
+ call.object = parse(sexp[1]) || Abstract::Raw.new('self')
52
+ call.name = sexp[2]
53
+ sexp[3..sexp.length].each do |arg|
54
+ call.args << parse(arg)
55
+ end
56
+ call
57
+ end
58
+
59
+ def parse_str(sexp)
60
+ Abstract::Literal.new(sexp[1])
61
+ end
62
+
63
+ def parse_lit(sexp)
64
+ Abstract::Literal.new(sexp[1])
65
+ end
66
+
67
+ def parse_const(sexp)
68
+ Abstract::Const.new(sexp[1].to_s)
69
+ end
70
+
71
+ def parse_lasgn(sexp)
72
+ Abstract::LAsgn.new(sexp[1], parse(sexp[2]))
73
+ end
74
+
75
+ def parse_lvar(sexp)
76
+ Abstract::LVar.new(sexp[1])
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,32 @@
1
+ module Mlc
2
+ class ParserState
3
+
4
+ attr_reader :parent, :lvars
5
+
6
+ def initialize(parent = nil, inherit = true)
7
+ @tmpid = -1
8
+ @lvars = []
9
+ @parent = parent
10
+ @inherit = inherit
11
+ end
12
+
13
+ def tmpvar
14
+ "__tmp_#{@tmpid += 1}"
15
+ end
16
+
17
+ def add_lvar(name)
18
+ unless knows_lvar? name
19
+ @lvars << name
20
+ end
21
+ end
22
+
23
+ def knows_lvar?(name)
24
+ @lvars.include? name || (@inherit && @parent && @parent.knows_lvar?(name))
25
+ end
26
+
27
+ def lvars_define
28
+ @lvars.map{|el| 'l' + el.to_s}.join(', ')
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Mlc
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mlc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mlc'
8
+ spec.version = Mlc::VERSION
9
+ spec.authors = ['Kilobyte']
10
+ spec.email = ['stiepen22@gmx.de']
11
+ spec.description = %q{Moonstone Lua Compiler}
12
+ spec.summary = %q{I can compile ruby to lua}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+
24
+ spec.add_dependency 'ruby_parser'
25
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mlc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kilobyte
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-23 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby_parser
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Moonstone Lua Compiler
56
+ email:
57
+ - stiepen22@gmx.de
58
+ executables:
59
+ - mlc.rb
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/mlc.rb
69
+ - lib/mlc.rb
70
+ - lib/mlc/abstract.rb
71
+ - lib/mlc/abstract/block.rb
72
+ - lib/mlc/abstract/call.rb
73
+ - lib/mlc/abstract/const.rb
74
+ - lib/mlc/abstract/lasgn.rb
75
+ - lib/mlc/abstract/literal.rb
76
+ - lib/mlc/abstract/lvar.rb
77
+ - lib/mlc/abstract/raw.rb
78
+ - lib/mlc/abstract/snippet.rb
79
+ - lib/mlc/compiler.rb
80
+ - lib/mlc/parserstate.rb
81
+ - lib/mlc/version.rb
82
+ - mlc.gemspec
83
+ homepage: ''
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.2.2
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: I can compile ruby to lua
107
+ test_files: []