1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # print AST in an human form
22 private import literal
24 private class ASTPrinterVisitor
28 node
.accept_printer
(self)
31 var out
= new List[String]
38 if has_eol
then return
40 for x
in [0..indent_level
[ do out
.add
("\t")
44 var last_current
: nullable ANode = null
48 if last_current
!= current_node
then
49 last_current
= current_node
55 fun indent
do indent_level
+= 1
56 fun unindent
do indent_level
-= 1
60 # print the tree (using the semantic information) on screen
61 # This method is used to debug AST transformations
64 var v
= new ASTPrinterVisitor
72 private fun accept_printer
(v
: ASTPrinterVisitor)
83 redef class ABlockExpr
84 redef fun accept_printer
(v
)
93 redef class AIntegerExpr
94 redef fun accept_printer
(v
)
101 redef fun accept_printer
(v
)
103 v
.write
("new {mtype.as(not null)}.{callsite.mproperty}")
104 if not n_args
.n_exprs
.is_empty
then
108 for a
in n_args
.n_exprs
do
109 if is_first
then is_first
= false else v
.write
(",")
118 redef class ASendExpr
119 redef fun accept_printer
(v
)
121 v
.enter_visit
(n_expr
)
122 v
.write
(".{callsite.mproperty.name}")
123 if not raw_arguments
.is_empty
then
127 for a
in raw_arguments
do
128 if is_first
then is_first
= false else v
.write
(",")
138 redef fun accept_printer
(v
)
140 var name
= variable
.name
141 if name
== "" then name
= "t{variable.object_id}"
146 redef class AVarAssignExpr
147 redef fun accept_printer
(v
)
149 var name
= variable
.name
150 if name
== "" then name
= "t{variable.object_id}"
153 v
.enter_visit
(n_value
)