Class: Yadriggy::Number

Inherits:
ASTnode show all
Defined in:
lib/yadriggy/ast.rb,
lib/yadriggy/ast_value.rb

Overview

Numeric literal.

Instance Attribute Summary collapse

Attributes inherited from ASTnode

#parent, #usertype

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ASTnode

#add_child, #add_children, #const_value_in_class, #get_context_class, #get_receiver_object, #is_proc?, #pretty_print, #root, #source_location, #source_location_string, #value_in_class

Constructor Details

#initialize(sexp) ⇒ Number

Returns a new instance of Number.



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/yadriggy/ast.rb', line 263

def initialize(sexp)
  @value = case sexp[0]
           when :@int
             if sexp[1].start_with? "0x"
               sexp[1].hex
             else
               sexp[1].to_i
             end
           when :@float
             sexp[1].to_f
           when :@rational
             sexp[1].to_r
           when :@imaginary
             Complex(sexp[1])
           else
             raise "unknown symbol #{sexp[0]}"
           end
  @line_no = sexp[2][0].to_i
  @column = sexp[2][1].to_i
end

Instance Attribute Details

#columnInteger (readonly)

Returns the column.

Returns:

  • (Integer)

    the column.



257
258
259
# File 'lib/yadriggy/ast.rb', line 257

def column
  @column
end

#line_noInteger (readonly)

Returns the line number.

Returns:

  • (Integer)

    the line number.



255
256
257
# File 'lib/yadriggy/ast.rb', line 255

def line_no
  @line_no
end

#valueNumeric (readonly)

Returns the number.

Returns:

  • (Numeric)

    the number.



253
254
255
# File 'lib/yadriggy/ast.rb', line 253

def value
  @value
end

Class Method Details

.tagsObject



259
260
261
# File 'lib/yadriggy/ast.rb', line 259

def self.tags()
  [:@int, :@float, :@rational, :@imaginary]
end

Instance Method Details

#accept(evaluator) ⇒ void

This method returns an undefined value.

A method for Visitor pattern.

Parameters:

  • evaluator (Eval)

    the visitor of Visitor pattern.



287
288
289
# File 'lib/yadriggy/ast.rb', line 287

def accept(evaluator)
  evaluator.number(self)
end

#const_valueObject

This is defined by attr_reader. def value() @value end



267
# File 'lib/yadriggy/ast_value.rb', line 267

def const_value() value end