Class: Nendo::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.



2265
2266
2267
# File 'lib/nendo.rb', line 2265

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

Instance Method Details

#__write(sexp, readable) ⇒ Object



2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
# File 'lib/nendo.rb', line 2269

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
    lastAtomStr = lastAtom ? __write( sexp.getLastAtom, readable ) : ""
    keyword = getQuoteKeyword.call( sexp.car )
    if keyword
      keyword + arr[1..-1].join( " " ) + (lastAtom ? " . " + lastAtomStr : "")
    else
      "(" +  arr.join( " " ) + (lastAtom ? " . " + lastAtomStr : "") + ")"
    end
  when Array # is a vector in the Nendo world.
    arr = sexp.map { |x| __write( x, readable ) }
    "#(" + arr.join( " " ) + ")"
  when true
    "#t"
  when false
    "#f"
  when Symbol
    keyword = getQuoteKeyword.call( sexp )
    if keyword
      keyword
    else
      sprintf( "%s", sexp.to_s )
    end
  when String, LispString
    if readable
      sprintf( "\"%s\"", LispString.escape( sexp.to_s ))
    else
      sexp.to_s
    end
  when Regexp
    "#/" + sexp.source + "/" + (sexp.casefold? ? "i" : "")
  when LispKeyword
    ":" + sexp.key.to_s
  when Nil
    "()"
  when nil
    "nil"
  else
    sprintf( "%s", sexp )
  end
end

#_print(sexp) ⇒ Object



2330
2331
2332
# File 'lib/nendo.rb', line 2330

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

#_write(sexp) ⇒ Object



2333
2334
2335
# File 'lib/nendo.rb', line 2333

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