Class: Lisp::Format::Directives::Character

Inherits:
Directive show all
Defined in:
lib/carat/lisp-format.rb

Overview

Represents the ~C (Character) directive. It outputs a character argument in one of several different representations depending on given modifiers. TODO: UNICODE this shit.

Instance Attribute Summary

Attributes inherited from Directive

#pos

Instance Method Summary collapse

Methods inherited from Directive

#initialize, #join

Constructor Details

This class inherits a constructor from Lisp::Format::Directives::Directive

Instance Method Details

#execute(state) ⇒ Object

Outputs the given argument in one of the ways described in the table below depending on given modifiers:

no modifiers

simply output the character as a normal character,

:

spells out control-bits on the input character, such as Control-Meta-X,

@

outputs the character in such as way that it can be read in by the Ruby interpreter again as input, using the ?char syntax,

:@

has the same effect as : only. The CLTL2 suggests that this outputs unusual shift keys in a manner to make it easy to locate them on a keyboard, but since no such standard has arisen, this is not done.



937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'lib/carat/lisp-format.rb', line 937

def execute(state)
  arg = state.next_arg
  arg_error 'argument not a Fixnum' unless arg.is_a? Fixnum
  ch = arg & 0xff
  if colon_mod?
    state.output('Control-') if (ch & 0x7f) < 0x1f
    state.output('Meta-')    if ch >= 0x7f
    state.output(ch & 0x7f | 0x60)
  elsif at_mod?
    state.output('?' + convert_char(ch))
  else
    state.output ch
  end
end