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
)
94 redef fun accept_printer
(v
)
100 redef class AByteExpr
101 redef fun accept_printer
(v
)
108 redef fun accept_printer
(v
)
110 v
.write
("new {mtype.as(not null)}.{callsite.mproperty}")
111 if not n_args
.n_exprs
.is_empty
then
115 for a
in n_args
.n_exprs
do
116 if is_first
then is_first
= false else v
.write
(",")
125 redef class ASendExpr
126 redef fun accept_printer
(v
)
128 v
.enter_visit
(n_expr
)
129 v
.write
(".{callsite.mproperty.name}")
130 if not raw_arguments
.is_empty
then
134 for a
in raw_arguments
do
135 if is_first
then is_first
= false else v
.write
(",")
145 redef fun accept_printer
(v
)
147 var name
= variable
.name
148 if name
== "" then name
= "t{variable.object_id}"
153 redef class AVarAssignExpr
154 redef fun accept_printer
(v
)
156 var name
= variable
.name
157 if name
== "" then name
= "t{variable.object_id}"
160 v
.enter_visit
(n_value
)