Class: Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/nendo.rb

Instance Method Summary collapse

Constructor Details

#initialize(debug = false) ⇒ Printer

Returns a new instance of Printer.



1142
1143
1144
# File 'lib/nendo.rb', line 1142

def initialize( debug = false )
  @debug    = debug
end

Instance Method Details

#__write(sexp, readable) ⇒ Object



1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
# File 'lib/nendo.rb', line 1146

def __write( sexp, readable )
  getQuoteKeyword = lambda { |x|
    case x
    when :quote
      "'"
    when :quasiquote
      "`"
    when :unquote
      ","
    when :unquote_splicing
      ",@"
    when :dot_operator
      "."
    else
      false
    end
  }
  case sexp
  when Cell
    arr = sexp.map { |x| __write( x.car, readable ) }
    lastAtom = sexp.lastAtom
    lastAtom = __write( lastAtom, readable )  if lastAtom
    keyword = getQuoteKeyword.call( sexp.car )
    if keyword
      keyword + arr[1..-1].join( " " ) + (lastAtom ? " . " + lastAtom : "")
    else
      "(" +  arr.join( " " ) + (lastAtom ? " . " + lastAtom : "") + ")"
    end
  when Symbol
    keyword = getQuoteKeyword.call( sexp )
    if keyword
      keyword
    else
      sprintf( "%s", sexp.to_s )
    end
  when String
    if readable
      sprintf( "\"%s\"", sexp.to_s )
    else
      sexp.to_s
    end
  when Nil
    "()"
  when nil
    "nil"
  else
    sprintf( "%s", sexp )
  end
end

#_print(sexp) ⇒ Object



1196
1197
1198
# File 'lib/nendo.rb', line 1196

def _print( sexp )
  self.__write( sexp, false )
end

#_write(sexp) ⇒ Object



1199
1200
1201
# File 'lib/nendo.rb', line 1199

def _write( sexp )
  self.__write( sexp, true  )
end