Class: Grammar::Ruby::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/grammar/ruby/code.rb

Overview

TODO: better list delimiter handling

Direct Known Subclasses

Always, Command, List, Local, Never, Primary, Rescue, Unary, While

Defined Under Namespace

Classes: Always, And, Assign, Begin, Binary, Blocked, Brackets, Call, Command, Conditional, Dedented, Def, Dot, Immediate, List, Local, Method, Never, Not, Or, Primary, Rescue, SharedSteps, Singleton, Steps, Unary, Until, While

Constant Summary collapse

True =
Class.new(Singleton) {
    def _immediate; true; end
    def _inspect(s="", newline="\n")
        s.concat("True")
    end
    def _lines(text, indent=0, op=nil, maxlen=72)
        text.last.concat(true.inspect)
    end
    def _always; true; end
    def _ror (before); before << before.pop._step(self); end
    def _and         ; yield ; end
    def _rand(before); before; end
    def _not         ; False ; end
}.new
FalseTerminator =
Class.new(Singleton) {
    def _inspect(s="", newline="\n")
        s.concat("FalseTerminator")
    end
    def _lines(text, indent=0, op=nil, maxlen=72)
        text.last.concat(false.inspect)
    end
    def _never; false; end
    def _always; true; end
    def _ror (before); before << Always.new(before.pop); end
    def _and         ; raise("#{inspect} &&"); end
    def _or          ; raise("#{inspect} ||"); end
    def _not         ; raise("!#{inspect}"); end
}.new
False =
Class.new(Singleton) {
    def _immediate; true; end
    def _inspect(s="", newline="\n")
        s.concat("False")
    end
    def _lines(text, indent=0, op=nil, maxlen=72)
        text.last.concat(false.inspect)
    end
    def _never; true; end
    def _rand(before); before << before.pop._step(self); end
    def _or          ; yield ; end
    def _ror (before); before; end
    def _not         ; True  ; end
}.new
Nil =
Class.new(Singleton) {
    def _immediate; true; end
    def _inspect(s="", newline="\n")
        s.concat("Nil")
    end
    def _lines(text, indent=0, op=nil, maxlen=72)
        text.last.concat(nil.inspect)
    end
    def _never; true; end
    def _rand(before); before << before.pop._step(self); end
    def _or          ; yield ; end
    def _ror (before); before; end
    def _not         ; True  ; end
}.new
Self =
Class.new(Singleton) {
    def _inspect(s="", newline="\n")
        s.concat("Self")
    end
    def _lines(text, indent=0, op=nil, maxlen=72)
        text.last.concat("self")
    end
    def _always; true; end
    @@keywords = Hash.new
    %w(
        BEGIN END
        begin if unless while until for case def class module
        then elsif else when in do ensure rescue end
        and or
        not defined?
        return yield super undef alias
        break next retry redo
        nil false true self
    ).each { |keyword|
        @@keywords[keyword] = true
    }
    def _dot(method)
        (@@keywords[method] || /^[A-Za-z]\w*[?!]?$/!~method) ? super :
            Code::Command.new(method)
    end
}.new
Implicit =
Class.new(Singleton) {
    def _inspect(s="", newline="\n")
        s.concat("Implicit")
    end
    def _dot(method)
        Code::Command.new(method.to_s)
    end
}.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Code

Returns a new instance of Code.



40
41
42
# File 'lib/grammar/ruby/code.rb', line 40

def initialize(code)
    @code = code
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/grammar/ruby/code.rb', line 172

def method_missing(method, *args, &block)
    if block
        call = args.empty? ? _dot(method.to_s) :
            Method.new(_dot(method.to_s), *args)
        Blocked.new(call, &block)
    else
        case method
        when :**,:*,:/,:%,:+,:-,:<<,:>>,:&,:|,:^,
                :>,:>=,:<,:<=,:<=>,:==,:===,:=~
            Binary.new(" #{method} ", self, args[0])
        when :-@,:+@,:~@
            Unary.new(method.to_s[0..-2], self)
        when :[]
            Brackets.new(self, *args)
        when :[]=
            Brackets.new(self, *args[0..-2])._assign(args[-1])
        else
            if method.to_s[-1] == ?= and args.size==1
                Dot.new(self, method.to_s[0..-2])._assign(args[-1])
            elsif args.empty?
                _dot(method.to_s)
            else
                Method.new(_dot(method.to_s), *args)
            end
        end
    end
end

Class Method Details

.[](object) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/grammar/ruby/code.rb', line 20

def self.[](object)
    case object
    when NilClass
        Nil
    when FalseClass
        False
    when TrueClass
        True
    when Fixnum,Symbol,Module # last 3 not really immediates
        Immediate.new(object)
    else
        s = object.inspect
        begin
            object.eql?(eval(s)) && Primary.new(s)
        rescue
        end or
        Immediate.new(Marshal).load(Primary.new(
            Marshal.dump(object).inspect))
    end
end

Instance Method Details

#_alwaysObject



88
89
90
# File 'lib/grammar/ruby/code.rb', line 88

def _always
    @code._always
end

#_andObject

:yield:



105
106
107
108
# File 'lib/grammar/ruby/code.rb', line 105

def _and # :yield:
    _never ? self :
    And.new(yield._rand(operands = [self]))
end

#_assign(rvalue) ⇒ Object



134
135
136
# File 'lib/grammar/ruby/code.rb', line 134

def _assign(rvalue)
    Assign.new([self, rvalue])
end

#_classnameObject



55
56
57
# File 'lib/grammar/ruby/code.rb', line 55

def _classname
    _class.name.gsub(/.*::/, "")
end

#_code_selfObject



85
86
87
# File 'lib/grammar/ruby/code.rb', line 85

def _code_self
    self
end

#_codesObject



43
44
45
# File 'lib/grammar/ruby/code.rb', line 43

def _codes
    [@code]
end

#_dataObject



46
47
48
# File 'lib/grammar/ruby/code.rb', line 46

def _data
    nil
end

#_def(name, *args) ⇒ Object



163
164
165
# File 'lib/grammar/ruby/code.rb', line 163

def _def(name, *args)
    Def.new(name.to_s, args, self)
end

#_dot(method) ⇒ Object



151
152
153
# File 'lib/grammar/ruby/code.rb', line 151

def _dot(method)
    Dot.new(self, method)
end

#_else(after) ⇒ Object



160
161
162
# File 'lib/grammar/ruby/code.rb', line 160

def _else(after)
    Begin.new(self)._else(after)
end

#_ensure(after) ⇒ Object



157
158
159
# File 'lib/grammar/ruby/code.rb', line 157

def _ensure(after)
    Begin.new(self)._ensure(after)
end

#_equal(other) ⇒ Object



49
50
51
52
53
54
# File 'lib/grammar/ruby/code.rb', line 49

def _equal(other)
    _class.equal?(other._class) && _data==other._data &&
        !_codes.zip(other._codes) { |a,b|
            break(true) unless a._equal(b)
        }
end

#_immediateObject



94
95
96
# File 'lib/grammar/ruby/code.rb', line 94

def _immediate
    false
end

#_inspect(s = "", newline = "\n") ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/grammar/ruby/code.rb', line 58

def _inspect(s="", newline="\n")
    data = _data
    codes = _codes
    s.concat(_classname) << ?\(
    s.concat(data.inspect) if data
    if !codes.empty?
        newline << ?\s
        if codes.size==1
            s.concat(", ") if data
            codes[0]._inspect(s, newline)
            newline[-1,1] = ""
        else
            codes.each { |code|
                s << ?\, if data
                s.concat(newline)
                code._inspect(s, newline)
                data = true
            }
            newline[-1,1] = ""
            s.concat(newline)
        end
    end
    s << ?\)
end

#_lines(text, indent = 0, op = nil, maxlen = 72) ⇒ Object



82
83
84
# File 'lib/grammar/ruby/code.rb', line 82

def _lines(text, indent=0, op=nil, maxlen=72)
    @code._lines(text, indent, op, maxlen)
end

#_locals(n = 0) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/grammar/ruby/code.rb', line 143

def _locals(n=0)
    @locals ||= (
        _codes.each { |code|
            n = code._locals(n)
        }
        n
    )
end

#_mid(before) ⇒ Object



119
120
121
# File 'lib/grammar/ruby/code.rb', line 119

def _mid(before)
    before << self
end

#_neverObject



91
92
93
# File 'lib/grammar/ruby/code.rb', line 91

def _never
    @code._never
end

#_notObject



122
123
124
# File 'lib/grammar/ruby/code.rb', line 122

def _not
    Not.new(self)
end

#_orObject

:yield:



97
98
99
100
# File 'lib/grammar/ruby/code.rb', line 97

def _or # :yield:
    _always ? self :
    Or.new(yield._ror(operands = [self]))
end

#_question(yes, no) ⇒ Object



125
126
127
# File 'lib/grammar/ruby/code.rb', line 125

def _question(yes, no)
    Conditional.new(self, yes, no)
end

#_rand(before) ⇒ Object



109
110
111
112
# File 'lib/grammar/ruby/code.rb', line 109

def _rand(before)
    before <<
        (before.last._always ? before.pop._step(self) : self)
end

#_rcond(before) ⇒ Object



131
132
133
# File 'lib/grammar/ruby/code.rb', line 131

def _rcond(before)
    before << self
end

#_rescue(after, exception = nil, var = nil) ⇒ Object



154
155
156
# File 'lib/grammar/ruby/code.rb', line 154

def _rescue(after, exception=nil, var=nil)
    Begin.new(self)._rescue(after, exception, var)
end

#_ror(before) ⇒ Object



101
102
103
104
# File 'lib/grammar/ruby/code.rb', line 101

def _ror(before)
    before <<
        (before.last._never ? before.pop._step(self) : self)
end

#_rstep(before) ⇒ Object



116
117
118
# File 'lib/grammar/ruby/code.rb', line 116

def _rstep(before)
    before << self
end

#_splatObject



137
138
139
# File 'lib/grammar/ruby/code.rb', line 137

def _splat
    Unary.new("*", self)
end

#_step(after) ⇒ Object



113
114
115
# File 'lib/grammar/ruby/code.rb', line 113

def _step(after)
    Steps.new(after._rstep(_mid([])))
end

#_to_blockObject



140
141
142
# File 'lib/grammar/ruby/code.rb', line 140

def _to_block
    Unary.new("&", self)
end

#_unless(cond, yes) ⇒ Object



128
129
130
# File 'lib/grammar/ruby/code.rb', line 128

def _unless(cond, yes)
    Conditional.new(cond, yes, self)
end

#_until(condition) ⇒ Object



169
170
171
# File 'lib/grammar/ruby/code.rb', line 169

def _until(condition)
    Until.new(condition, self)
end

#_while(condition) ⇒ Object



166
167
168
# File 'lib/grammar/ruby/code.rb', line 166

def _while(condition)
    While.new(condition, self)
end