Class: Heist::Runtime::Character

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/heist/runtime/data/character.rb

Overview

The Character class is used to represent Scheme’s notion of characters, objects that represent single ASCII glyphs. They are written in Scheme code as the symbols #\ followed by the desired glyph.

Characters can be mapped back and forth to integer character codes and are thus sortable. This class uses Ruby’s Comparable mixin to sort characters by their ASCII code.

In addition to single glyphs, there are a few named characters that represent whitespace characters: see SPECIAL for named characters supported by Heist.

Constant Summary collapse

SPECIAL =

List of special character names and the strings they represent

{
  "space"   => " ",
  "newline" => "\n",
  "tab"     => "\t"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glyph) ⇒ Character

A Character is initialized using either a single-character string, or a string containing the name of a special whitespace character. If a name is used, it must appear as a key in SPECIAL otherwise an exception is thrown.

Raises:



31
32
33
34
# File 'lib/heist/runtime/data/character.rb', line 31

def initialize(glyph)
  raise SyntaxError.new("There is no character named '#{glyph}'") if glyph.size > 1 and SPECIAL[glyph].nil?
  @glyph = glyph
end

Instance Attribute Details

#glyphObject (readonly)

Returns the value of attribute glyph.



18
19
20
# File 'lib/heist/runtime/data/character.rb', line 18

def glyph
  @glyph
end

Instance Method Details

#<=>(other) ⇒ Object

Returns the difference between the receiver’s charcode and the argument’s charcode, used for the Comparable operations.



54
55
56
# File 'lib/heist/runtime/data/character.rb', line 54

def <=>(other)
  char_code - other.char_code
end

#==(other) ⇒ Object

Returns true iff the two characters represent the same glyph.



45
46
47
48
49
50
# File 'lib/heist/runtime/data/character.rb', line 45

def ==(other)
  Character === other and
  ( @glyph               == other.glyph or
    SPECIAL[@glyph]      == other.glyph or
    SPECIAL[other.glyph] == @glyph )
end

#char_codeObject Also known as: to_i

Returns the ASCII code for the Character as an integer.



37
38
39
40
41
# File 'lib/heist/runtime/data/character.rb', line 37

def char_code
  code, char = nil, to_s
  char.each_byte { |x| code = x }
  code
end

#downcaseObject

Returns a new Character representing the lowercase version of the receiver.



66
67
68
# File 'lib/heist/runtime/data/character.rb', line 66

def downcase
  self.class.new(to_s.downcase)
end

#inspectObject

Returns a Scheme-style string representation of the Character.



76
77
78
# File 'lib/heist/runtime/data/character.rb', line 76

def inspect
  "#\\#{ @glyph }"
end

#to_sObject

Returns a String of unit length containing only the Character.



71
72
73
# File 'lib/heist/runtime/data/character.rb', line 71

def to_s
  SPECIAL[@glyph] || @glyph
end

#upcaseObject

Returns a new Character representing the uppercase version of the receiver.



60
61
62
# File 'lib/heist/runtime/data/character.rb', line 60

def upcase
  self.class.new(to_s.upcase)
end