Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / test_astbuilder.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Program used to test the `clone` method of the astbuilder tool
16 # Program use the nit interpreter to do his job. It take a nit file into parameter and replace the ast nodes by a clone.
17 # After replace it run a basic interpretation.
18 # When a new method `clone` is implemented you need to add a redefinition to test it.
19 # When the number of method `clone` will increase its more practical to define the nodes to not clone.
20 module test_astbuilder
21
22 import nit
23 import astbuilder
24
25 redef class ModelBuilder
26 redef fun run_naive_interpreter(mainmodule: MModule, arguments: Array[String])
27 do
28 var clone_visitor = new CloneVisitor
29 for nmodule in self.nmodules do
30 clone_visitor.enter_visit(nmodule)
31 end
32 super
33 end
34 end
35
36 private class CloneVisitor
37 super Visitor
38
39 redef fun visit(node)
40 do
41 # return when the node is an annotation to avoid the clonage
42 if node isa AAnnotations then return
43 node.do_cloneable(self)
44 node.visit_all(self)
45 end
46 end
47
48 redef class ANode
49 private fun do_cloneable(v: CloneVisitor)do end
50
51 # Create a new clone of `self`
52 fun replace_clone
53 do
54 var self_clone = self.clone
55 replace_with(self.clone)
56 self_clone.location = location
57 #Call the `validate` method to set correctly the parents and the location
58 self_clone.validate
59 end
60 end
61
62 # Implement the `do_cloeable` method for each Node were the method `clone` is defined in the astbuilder module
63
64 redef class ASignature
65 redef fun do_cloneable(v)
66 do
67 replace_clone
68 end
69 end
70
71 redef class AImplicitSelfExpr
72 redef fun do_cloneable(v)
73 do
74 replace_clone
75 end
76 end
77
78 redef class ASelfExpr
79 redef fun do_cloneable(v)
80 do
81 replace_clone
82 end
83 end
84
85 redef class AIntegerExpr
86 redef fun do_cloneable(v)
87 do
88 replace_clone
89 end
90 end
91
92 redef class ATrueExpr
93 redef fun do_cloneable(v)
94 do
95 replace_clone
96 end
97 end
98
99 redef class AFloatExpr
100 redef fun do_cloneable(v)
101 do
102 replace_clone
103 end
104 end
105
106 redef class AFalseExpr
107 redef fun do_cloneable(v)
108 do
109 replace_clone
110 end
111 end
112
113 redef class ACharExpr
114 redef fun do_cloneable(v)
115 do
116 replace_clone
117 end
118 end