myco 0.1.1 → 0.1.2

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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/lib/myco/code_loader.rb +1 -1
  3. data/lib/myco/parser/ast/ToRuby.my +400 -0
  4. data/lib/myco/parser/ast/ToRuby.my.rb +574 -0
  5. data/lib/myco/parser/ast.rb +1 -1
  6. data/lib/myco/version.rb +1 -1
  7. data/lib/myco.rb +24 -2
  8. metadata +3 -40
  9. data/lib/myco/parser/ast/to_ruby/array_assembly.rb +0 -15
  10. data/lib/myco/parser/ast/to_ruby/block.rb +0 -14
  11. data/lib/myco/parser/ast/to_ruby/block_pass.rb +0 -6
  12. data/lib/myco/parser/ast/to_ruby/branch_operator.rb +0 -9
  13. data/lib/myco/parser/ast/to_ruby/constant_access.rb +0 -10
  14. data/lib/myco/parser/ast/to_ruby/constant_assignment.rb +0 -6
  15. data/lib/myco/parser/ast/to_ruby/constant_define.rb +0 -9
  16. data/lib/myco/parser/ast/to_ruby/constant_reopen.rb +0 -6
  17. data/lib/myco/parser/ast/to_ruby/declare_category.rb +0 -7
  18. data/lib/myco/parser/ast/to_ruby/declare_decorator.rb +0 -6
  19. data/lib/myco/parser/ast/to_ruby/declare_file.rb +0 -6
  20. data/lib/myco/parser/ast/to_ruby/declare_meme.rb +0 -16
  21. data/lib/myco/parser/ast/to_ruby/declare_object.rb +0 -8
  22. data/lib/myco/parser/ast/to_ruby/declare_string.rb +0 -6
  23. data/lib/myco/parser/ast/to_ruby/dynamic_string.rb +0 -14
  24. data/lib/myco/parser/ast/to_ruby/dynamic_symbol.rb +0 -7
  25. data/lib/myco/parser/ast/to_ruby/eval_expression.rb +0 -6
  26. data/lib/myco/parser/ast/to_ruby/false_literal.rb +0 -6
  27. data/lib/myco/parser/ast/to_ruby/hash_literal.rb +0 -16
  28. data/lib/myco/parser/ast/to_ruby/invoke.rb +0 -6
  29. data/lib/myco/parser/ast/to_ruby/invoke_method.rb +0 -35
  30. data/lib/myco/parser/ast/to_ruby/iter.rb +0 -10
  31. data/lib/myco/parser/ast/to_ruby/local_variable_access_ambiguous.rb +0 -16
  32. data/lib/myco/parser/ast/to_ruby/local_variable_assignment.rb +0 -8
  33. data/lib/myco/parser/ast/to_ruby/myco_module_scope.rb +0 -8
  34. data/lib/myco/parser/ast/to_ruby/null_literal.rb +0 -6
  35. data/lib/myco/parser/ast/to_ruby/parameters.rb +0 -60
  36. data/lib/myco/parser/ast/to_ruby/quest.rb +0 -13
  37. data/lib/myco/parser/ast/to_ruby/return.rb +0 -7
  38. data/lib/myco/parser/ast/to_ruby/scoped_constant.rb +0 -11
  39. data/lib/myco/parser/ast/to_ruby/self.rb +0 -6
  40. data/lib/myco/parser/ast/to_ruby/splat_value.rb +0 -33
  41. data/lib/myco/parser/ast/to_ruby/string_literal.rb +0 -6
  42. data/lib/myco/parser/ast/to_ruby/symbol_literal.rb +0 -6
  43. data/lib/myco/parser/ast/to_ruby/toplevel_constant.rb +0 -11
  44. data/lib/myco/parser/ast/to_ruby/true_literal.rb +0 -6
  45. data/lib/myco/parser/ast/to_ruby/void_literal.rb +0 -6
  46. data/lib/myco/parser/ast/to_ruby.rb +0 -138
  47. data/lib/myco/toolset.rb +0 -13
@@ -1,15 +0,0 @@
1
-
2
- class CodeTools::AST::ArrayAssembly
3
- def to_ruby g
4
- if @body.empty?
5
- g.add("[]")
6
- else
7
- g.add("[")
8
- g.push_indent
9
- @body[0...-1].each { |item| g.line(item); g.add(",") }
10
- @body.last.tap { |item| g.line(item) }
11
- g.pop_indent
12
- g.line("]")
13
- end
14
- end
15
- end
@@ -1,14 +0,0 @@
1
-
2
- class CodeTools::AST::Block
3
- def to_ruby g
4
- if @array.empty?
5
- g.add("()")
6
- else
7
- g.add("(")
8
- g.push_indent
9
- @array.each { |item| g.line(item) }
10
- g.pop_indent
11
- g.line(")")
12
- end
13
- end
14
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::BlockPass
3
- def to_ruby g
4
- g.add("&"); g.add(@body)
5
- end
6
- end
@@ -1,9 +0,0 @@
1
-
2
- class CodeTools::AST::BranchOperator
3
- def to_ruby g
4
- g.add('::Myco.branch_op(');
5
- g.add(@type.inspect); g.add(', ');
6
- g.add(@left); g.add(') {');
7
- g.add(@right); g.add('}')
8
- end
9
- end
@@ -1,10 +0,0 @@
1
-
2
- class CodeTools::AST::ConstantAccess
3
- def to_ruby g
4
- g.add("::Myco.find_constant(#{@name.inspect})")
5
- end
6
-
7
- def to_ruby_assign g, value
8
- g.add("::Myco.cscope.for_method_definition.const_set(#{@name.inspect}, "); g.add(value); g.add(")")
9
- end
10
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::ConstantAssignment
3
- def to_ruby g
4
- @constant.to_ruby_assign(g, @value)
5
- end
6
- end
@@ -1,9 +0,0 @@
1
-
2
- class CodeTools::AST::ConstantDefine
3
- def to_ruby g
4
- g.add(implementation)
5
- g.line(".tap { |__c__| __c__.__name__ = ")
6
- g.add(@name.name.inspect)
7
- g.add(" }")
8
- end
9
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::ConstantReopen
3
- def to_ruby g
4
- g.add(@name); g.add(".component_eval {"); g.add(@body); g.add("}")
5
- end
6
- end
@@ -1,7 +0,0 @@
1
-
2
- class CodeTools::AST::DeclareCategory
3
- def to_ruby g
4
- g.add("__category__(#{@name.value.inspect})")
5
- g.add(".component_eval {"); g.add(@body); g.add("}")
6
- end
7
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::DeclareDecorator
3
- def to_ruby g
4
- g.add("[#{@name.value.inspect}, "); g.add(@arguments); g.add("]")
5
- end
6
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::DeclareFile
3
- def to_ruby g
4
- implementation.to_ruby(g)
5
- end
6
- end
@@ -1,16 +0,0 @@
1
-
2
- class CodeTools::AST::DeclareMeme
3
- def to_ruby g
4
- g.with_nested_var_scope(body_implementation) {
5
- g.var_scope_declare_locals(@arguments.names)
6
-
7
- g.add("declare_meme(")
8
- g.add("#{@name.inspect}, ")
9
- g.add(@decorations); g.add(", nil, ::Myco.cscope.dup)")
10
- g.add(" { ");
11
- g.add(@arguments); g.add(" ")
12
- g.add(@body);
13
- g.add("}")
14
- }
15
- end
16
- end
@@ -1,8 +0,0 @@
1
-
2
- class CodeTools::AST::DeclareObject
3
- def to_ruby g
4
- g.add("::Myco::Component.new("); g.add(@types); g.add(", ::Myco.cscope.for_method_definition, __FILE__, __LINE__)")
5
- g.line(".tap { |__c__| __c__.__last__ = __c__.component_eval {"); g.add(scope_implementation); g.add("}}")
6
- g.add(".instance") if @create
7
- end
8
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::DeclareString
3
- def to_ruby g
4
- g.add(implementation)
5
- end
6
- end
@@ -1,14 +0,0 @@
1
-
2
- class CodeTools::AST::DynamicString
3
- def to_ruby g
4
- inspect_escape = Proc.new { |str| str.inspect[1...-1] }
5
-
6
- g.add('"')
7
- g.add(inspect_escape.call(@string))
8
- @array.each_slice(2) { |interpolated, inner_string|
9
- g.add('#{'); g.add(interpolated.value); g.add('}')
10
- g.add(inspect_escape.call(inner_string.value))
11
- }
12
- g.add('"')
13
- end
14
- end
@@ -1,7 +0,0 @@
1
-
2
- class CodeTools::AST::DynamicSymbol
3
- def to_ruby g
4
- g.add(':') # prefix with ':', but...
5
- super # delegate to DynamicString implementation
6
- end
7
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::EvalExpression
3
- def to_ruby g
4
- g.add(@body)
5
- end
6
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::FalseLiteral
3
- def to_ruby g
4
- g.add("false")
5
- end
6
- end
@@ -1,16 +0,0 @@
1
-
2
- class CodeTools::AST::HashLiteral
3
- def to_ruby g
4
- list = @array.each_slice(2).to_a
5
- if list.empty?
6
- g.add("{}")
7
- else
8
- g.add("{")
9
- g.push_indent
10
- list[0...-1].each { |key, value| g.line(key); g.add(" => "); g.add(value); g.add(",") }
11
- list.last.tap { |key, value| g.line(key); g.add(" => "); g.add(value) }
12
- g.pop_indent
13
- g.line("}")
14
- end
15
- end
16
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::Invoke
3
- def to_ruby g
4
- g.add(implementation)
5
- end
6
- end
@@ -1,35 +0,0 @@
1
-
2
- module CodeTools::AST
3
- class InvokeMethod
4
- def to_ruby g
5
- list = @arguments ? @arguments.body.dup : []
6
- list.push(@arguments.block) if @arguments.block.is_a?(BlockPass)
7
-
8
- g.add(@receiver)
9
-
10
- if g.easy_ident?(@name)
11
- g.add(".#{@name}")
12
- else
13
- g.add(".__send__")
14
- list.unshift(SymbolLiteral.new(@line, @name))
15
- end
16
-
17
- if list.empty?
18
- g.add("")
19
- elsif list.size == 1
20
- g.add("("); g.add(list.first); g.add(")")
21
- else
22
- g.add("(")
23
- g.push_indent
24
- list[0...-1].each { |item| g.line(item); g.add(",") }
25
- list.last.tap { |item| g.line(item) }
26
- g.pop_indent
27
- g.line(")")
28
- end
29
-
30
- if @arguments.block.is_a?(Iter)
31
- (g.add(" {"); g.add(@arguments.block); g.add("}"))
32
- end
33
- end
34
- end
35
- end
@@ -1,10 +0,0 @@
1
-
2
- class CodeTools::AST::Iter
3
- def to_ruby g
4
- g.with_nested_var_scope(self) {
5
- g.var_scope_declare_locals(@arguments.names)
6
-
7
- g.add(" "); g.add(@arguments); g.add(" "); g.add(@body)
8
- }
9
- end
10
- end
@@ -1,16 +0,0 @@
1
-
2
- class CodeTools::AST::LocalVariableAccessAmbiguous
3
- def to_ruby g
4
- if g.var_scope_has_local?(@name)
5
- g.add(@name.to_s)
6
- else
7
- g.add("self")
8
-
9
- if g.easy_ident?(@name)
10
- g.add(".#{@name}")
11
- else
12
- g.add(".__send__(#{name.inspect})")
13
- end
14
- end
15
- end
16
- end
@@ -1,8 +0,0 @@
1
-
2
- class CodeTools::AST::LocalVariableAssignment
3
- def to_ruby g
4
- g.var_scope_declare_local(@name)
5
- g.var_scope.variables[@name] = true
6
- g.add(@name.to_s); g.add(" = "); g.add(value)
7
- end
8
- end
@@ -1,8 +0,0 @@
1
-
2
- class CodeTools::AST::MycoModuleScope
3
- def to_ruby g
4
- g.with_nested_var_scope(self) {
5
- g.add(@body)
6
- }
7
- end
8
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::NullLiteral
3
- def to_ruby g
4
- g.add("nil")
5
- end
6
- end
@@ -1,60 +0,0 @@
1
-
2
- module CodeTools::AST
3
- class Parameters
4
- def to_ruby g
5
- list = []
6
- list_add = Proc.new { |&blk| list.push(Proc.new(&blk)) }
7
-
8
- @required.each { |item| list_add.call { g.add(item.to_s) } }
9
-
10
- if @defaults
11
- @defaults.arguments.each { |asgn|
12
- name = asgn.name
13
- value = asgn.value
14
- list_add.call {
15
- g.add("#{name}=")
16
- g.add(value) unless value.is_a?(SymbolLiteral) and value.value==:*
17
- }
18
- }
19
- end
20
-
21
- if @splat == :*
22
- list_add.call { g.add("*") }
23
- elsif @splat
24
- list_add.call { g.add("*#{@splat}") }
25
- end
26
-
27
- if @keywords
28
- @keywords.arguments.each { |asgn|
29
- name = asgn.name
30
- value = asgn.value
31
- list_add.call {
32
- g.add("#{name}:")
33
- g.add(value) unless value.is_a?(SymbolLiteral) and value.value==:*
34
- }
35
- }
36
-
37
- if @keywords.kwrest == true
38
- list_add.call { g.add("**") }
39
- elsif @keywords.kwrest
40
- list_add.call { g.add("**#{@keywords.kwrest.name}") }
41
- end
42
- end
43
-
44
- if @block_arg
45
- list_add.call { g.add("&#{@block_arg.name}") }
46
- end
47
-
48
- if list.empty?
49
- g.add("||")
50
- else
51
- g.add("|")
52
- g.push_indent
53
- list[0...-1].each { |proc| proc.call; g.add(", ") }
54
- list.last.tap { |proc| proc.call }
55
- g.pop_indent
56
- g.add("|")
57
- end
58
- end
59
- end
60
- end
@@ -1,13 +0,0 @@
1
-
2
- class CodeTools::AST::Quest
3
- def to_ruby g
4
- associated_questable = @questable.dup
5
- associated_questable.receiver = @receiver
6
-
7
- g.add("(")
8
- g.add(@receiver); g.add(".respond_to?(#{@questable.name.inspect}).false?")
9
- g.add(" ? ::Myco::Void : ")
10
- g.add(associated_questable)
11
- g.add(")")
12
- end
13
- end
@@ -1,7 +0,0 @@
1
-
2
- class CodeTools::AST::Return
3
- def to_ruby g
4
- g.add("return ")
5
- g.add(@value)
6
- end
7
- end
@@ -1,11 +0,0 @@
1
-
2
- class CodeTools::AST::ScopedConstant
3
- def to_ruby g
4
- g.add(@parent); g.add("::#{@name}")
5
- end
6
-
7
- def to_ruby_assign g, value
8
- to_ruby(g)
9
- g.add(" = "); g.add(value)
10
- end
11
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::Self
3
- def to_ruby g
4
- g.add("self")
5
- end
6
- end
@@ -1,33 +0,0 @@
1
-
2
- class CodeTools::AST::SplatValue
3
- def to_ruby g
4
- if @value.is_a?(self.class)
5
- @value.to_ruby(g)
6
- else
7
- g.add("*")
8
- @value.to_ruby(g)
9
- end
10
- end
11
- end
12
-
13
- module CodeTools::AST
14
- class CollectSplat
15
- def to_ruby g
16
- # TODO
17
- end
18
- end
19
- end
20
-
21
- module CodeTools::AST
22
- class ConcatArgs
23
- def to_ruby g
24
- # TODO
25
- end
26
- end
27
- end
28
-
29
- class CodeTools::AST::PushArgs
30
- def to_ruby g
31
- # TODO
32
- end
33
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::StringLiteral
3
- def to_ruby g
4
- g.add(@string.inspect)
5
- end
6
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::SymbolLiteral
3
- def to_ruby g
4
- g.add(@value.inspect)
5
- end
6
- end
@@ -1,11 +0,0 @@
1
-
2
- class CodeTools::AST::ToplevelConstant
3
- def to_ruby g
4
- g.add("::#{@name}")
5
- end
6
-
7
- def to_ruby_assign g, value
8
- to_ruby(g)
9
- g.add(" = "); g.add(value)
10
- end
11
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::TrueLiteral
3
- def to_ruby g
4
- g.add("true")
5
- end
6
- end
@@ -1,6 +0,0 @@
1
-
2
- class CodeTools::AST::VoidLiteral
3
- def to_ruby g
4
- g.add("::Myco::Void")
5
- end
6
- end
@@ -1,138 +0,0 @@
1
-
2
- module CodeTools::AST
3
- class ToRuby
4
- def initialize
5
- @lines = []
6
- @indents = [""]
7
-
8
- @var_scopes = []
9
- end
10
-
11
- # The currently held string of generated ruby code
12
- def to_s
13
- @lines.join("\n")
14
- end
15
-
16
- # Start a new line, optionally adding a string
17
- # or an object that responds to :to_ruby
18
- def line string=""
19
- if string.is_a?(String)
20
- @lines.push(@indents.last + string)
21
- else
22
- line
23
- string.to_ruby(self)
24
- end
25
- end
26
-
27
- # Add to the current line a string
28
- # or an object that responds to :to_ruby
29
- def add string
30
- if string.is_a?(String)
31
- (@lines.last || @lines[0]="").concat(string)
32
- else
33
- string.to_ruby(self)
34
- end
35
- end
36
-
37
- ##
38
- # Stack of indent levels (as strings to be prefixed)
39
- #
40
- def push_indent amount=2
41
- @indents.push(@indents.last + " "*2)
42
- end
43
-
44
- def pop_indent
45
- @indents.pop
46
- end
47
-
48
- ##
49
- # Stack of every AST node in hierarchy that can hold local variables
50
- #
51
- def var_scope
52
- @var_scopes.last
53
- end
54
-
55
- def with_nested_var_scope node
56
- @var_scopes.push(node)
57
- yield
58
- ensure
59
- @var_scopes.pop
60
- end
61
-
62
- def var_scope_has_local? name
63
- @var_scopes.reverse.each { |scope|
64
- return true if scope.variables.has_key?(name)
65
- }
66
- return false
67
- end
68
-
69
- def var_scope_declare_local name
70
- var_scope.variables[name] = true
71
- end
72
-
73
- def var_scope_declare_locals names
74
- names.each { |name| var_scope_declare_local(name) }
75
- end
76
-
77
- ##
78
- # Helper methods
79
-
80
- # TODO: make comprehensive
81
- # Return false if the given indent might be problematic in generated Ruby
82
- def easy_ident? sym
83
- str = sym.to_s
84
- return false if !(str=~/^[a-z_][A-Za-z_]*$/)
85
- return false if %i(
86
- if unless and or def do class module end break begin rescue ensure
87
- ).include?(sym)
88
- return true
89
- end
90
- end
91
-
92
- class Node
93
- def to_ruby_code
94
- g = ToRuby.new
95
- to_ruby(g)
96
- g.to_s
97
- end
98
- end
99
- end
100
-
101
-
102
- require_relative "to_ruby/self"
103
- require_relative "to_ruby/null_literal"
104
- require_relative "to_ruby/void_literal"
105
- require_relative "to_ruby/true_literal"
106
- require_relative "to_ruby/false_literal"
107
- require_relative "to_ruby/string_literal"
108
- require_relative "to_ruby/symbol_literal"
109
- require_relative "to_ruby/hash_literal"
110
- require_relative "to_ruby/dynamic_string"
111
- require_relative "to_ruby/dynamic_symbol"
112
- require_relative "to_ruby/scoped_constant"
113
- require_relative "to_ruby/toplevel_constant"
114
- require_relative "to_ruby/constant_access"
115
- require_relative "to_ruby/constant_assignment"
116
- require_relative "to_ruby/constant_define"
117
- require_relative "to_ruby/constant_reopen"
118
- require_relative "to_ruby/myco_module_scope"
119
- require_relative "to_ruby/declare_file"
120
- require_relative "to_ruby/declare_object"
121
- require_relative "to_ruby/declare_string"
122
- require_relative "to_ruby/declare_category"
123
- require_relative "to_ruby/declare_meme"
124
- require_relative "to_ruby/declare_decorator"
125
- require_relative "to_ruby/parameters"
126
- require_relative "to_ruby/block"
127
- require_relative "to_ruby/invoke"
128
- require_relative "to_ruby/invoke_method"
129
- require_relative "to_ruby/local_variable_assignment"
130
- require_relative "to_ruby/local_variable_access_ambiguous"
131
- require_relative "to_ruby/splat_value"
132
- require_relative "to_ruby/block_pass"
133
- require_relative "to_ruby/iter"
134
- require_relative "to_ruby/quest"
135
- require_relative "to_ruby/branch_operator"
136
- require_relative "to_ruby/return"
137
- require_relative "to_ruby/array_assembly"
138
- require_relative "to_ruby/eval_expression"
data/lib/myco/toolset.rb DELETED
@@ -1,13 +0,0 @@
1
-
2
- require "rubinius/toolset"
3
-
4
- module Myco
5
- ToolSet = Rubinius::ToolSets.create :myco do
6
- require "rubinius/melbourne"
7
- require "rubinius/processor"
8
- require "rubinius/compiler"
9
- require "rubinius/ast"
10
-
11
- require_relative 'parser'
12
- end
13
- end