1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Example of a calculation used nitcc
16 # see `calc.sablecc` for the grammar
17 module calc
is example
19 # Reuse the test program to simplify the code
20 import calc_test_parser
22 # The main evaluator, as a visitiopn that stack values
27 var stack
= new Array[Int]
29 redef fun visit
(n
) do n
.accept_calculator
(self)
33 # Default caculation is to visit the childrens
34 fun accept_calculator
(v
: Calulator) do visit_children
(v
)
38 redef fun accept_calculator
(v
) do v
.stack
.push
(text
.to_i
)
42 redef fun accept_calculator
(v
) do
44 v
.stack
.push
(v
.stack
.pop
+v
.stack
.pop
)
49 redef fun accept_calculator
(v
) do
52 v
.stack
.push
(v
.stack
.pop-n1
)
57 redef fun accept_calculator
(v
) do
59 v
.stack
.push
(-v
.stack
.pop
)
64 redef fun accept_calculator
(v
) do
66 v
.stack
.push
(v
.stack
.pop
*v
.stack
.pop
)
71 redef fun accept_calculator
(v
) do
74 v
.stack
.push
(v
.stack
.pop
/n1
)
78 var t
= new TestParser_calc