Class: Nendo::Printer

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

Instance Method Summary collapse

Constructor Details

#initialize(debug = false) ⇒ Printer

Returns a new instance of Printer.



38
39
40
# File 'lib/nendo/ruby/printer.rb', line 38

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

Instance Method Details

#__write(sexp, readable) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nendo/ruby/printer.rb', line 42

def __write( sexp, readable )
  getQuoteKeyword = lambda { |x|
    case x
    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 SyntacticClosure
    sprintf( "#<SyntacticClosure[%s:%s]>", sexp.originalSymbol, sexp.renamedSymbol )
  when Regexp
    "#/" + sexp.source + "/" + (sexp.casefold? ? "i" : "")
  when LispRegexp
    if readable
      sexp.to_readable
    else
      sexp.to_s
    end
  when LispKeyword
    ":" + sexp.key.to_s
  when LispCoreSyntax
    "#<Nendo::LispCoreSyntax>"
  when LispMacro
    "#<Nendo::LispMacro>"
  when LispSyntax
    "#<Nendo::LispSyntax>"
  when Nil
    "()"
  when nil
    "nil"
  when Hash
    sexp.to_s
  else
    sprintf( "%s", sexp )
  end
end

#_print(sexp) ⇒ Object



111
112
113
# File 'lib/nendo/ruby/printer.rb', line 111

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

#_write(sexp) ⇒ Object



114
115
116
# File 'lib/nendo/ruby/printer.rb', line 114

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