Skip to content

Commit

Permalink
Improve astprinter #2847 from privat
Browse files Browse the repository at this point in the history
The module `astprinter` is used to print AST in order to debug synthetic statements and expressions.
It was quite limited.

This PR extends it to most Nit expressions and statements. The missing ones will be addressed in futures PR.

The PR also add a simple nit test program `syntax_expr.nit` that exhibits many syntactic constructions. It is used to test the astprinter through a test tool `test_astprinter` that (pretty?)-print given programs.
  • Loading branch information
privat authored Aug 15, 2024
2 parents e8e9cca + ca41660 commit e361242
Show file tree
Hide file tree
Showing 9 changed files with 591 additions and 15 deletions.
357 changes: 342 additions & 15 deletions src/astprinter.nit

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions src/test_astprinter.nit
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Test the AST printer by reading modules and printing the body of their methods
module test_astprinter

import test_phase
import frontend
#import transform # Not yet :)

import astprinter

redef fun do_work(mainmodule, given_mmodules, modelbuilder)
do
# We iterate the model instead of the ast for classes and methods
for m in given_mmodules do
for cd in m.mclassdefs do
for pd in cd.mpropdefs do
print "# {pd}"
var node = modelbuilder.mentity2node(pd)
assert node != null
node.print_tree
end
end
end
end
1 change: 1 addition & 0 deletions tests/nitcg.skip
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ test_loader
get_mclasses
^nit
test_astbuilder
test_astprinter
1 change: 1 addition & 0 deletions tests/niti.skip
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ repeating_key_xor_solve
nitpm
nitdoc
test_astbuilder
test_astprinter
2 changes: 2 additions & 0 deletions tests/sav/syntax_expr.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
1
2 changes: 2 additions & 0 deletions tests/sav/test_astprinter.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Usage: [OPTION]... <file.nit>...
Use --help for help
87 changes: 87 additions & 0 deletions tests/sav/test_astprinter_args1.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# syntax_expr$Sys$main
var a
var b: Bool
var c: Collection[Int] = [1]
var d = 1
a = 1.+(2).-(3.unary -./(4).*(5).%(6.**(2)))
b = true and not false or true or a.<(0) or a.<=(0) or a.==(0) or a.!=(0) or a.>=(0) or a.>(0)
a = 1.&(2).|(3.unary ~.^(4.<<(5).>>(6)))
a = 1.<=>(2)
c = [1..2[
c = [1..2]
c = [1, 2, 3]
a += 1
a -= 1
a *= 1
a /= 1
a %= 1
a **= 1
a &= 1
a |= 1
a ^= 1
a <<= 1
a >>= 1
a = 1
print(a)
print(a)
a.abs
a.max(2)
a.max(2)
a.setbit(0, 1)
c = new Array[Int].defaultinit
c = new Array[Int].with_capacity(10)
a = new Array[Int].with_capacity(10).length
c.add(1)
a = a isa Array[Int]
a = null
if b then a = 1
d = a.as(not null)
d = a.as(Int)
do end
do a = 1
do
a = 1
a = 1
end
if b then end
if b then a = 1
if b then
a = 2
a = 2
end
if b then a = 3
if b then a = 4 else a = 5
if b then a = 6 else
a = 7
a = 7
end
if b then end
if b then end
if b then else a = 8
if b then else
a = 9
a = 9
end
if b then if b then a = 1
if b then
if b then
a = 2
a = 3
end
end
a = if b then 1 else 2
a = if b then if b then 3 else 4 else if b then 5 else 6
for i in c do end
for i in c do
a = 1
a = 1
end
for i in c, j in c do end
var m = new HashMap[String, String].defaultinit
for k, v in m do end
loop
if b then break
if b then continue
end
assert b
if not b then return
118 changes: 118 additions & 0 deletions tests/syntax_expr.nit
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

var a
var b: Bool
var c: Collection[Int] = [1]
var d = 1
a = 1 + 2 - - 3 / 4 * 5 % 6 ** 2
b = true and not false or true or a < 0 or a <= 0 or a == 0 or a != 0 or a >= 0 or a > 0
a = 1 & 2 | ~3 ^ 4 << 5 >> 6
a = 1 <=> 2
c = [1..2[
c = [1..2]
c = [1,2,3]

a += 1
a -= 1
a *= 1
a /= 1
a %= 1
a **= 1

a &= 1
a |= 1
a ^= 1
a <<= 1
a >>= 1

a = 1
print(a)
print a
a.abs
a.max(2)
a.max 2
a.setbit(0, 1)

c = new Array[Int]
c = new Array[Int].with_capacity(10)
a = new Array[Int].with_capacity(10).length
c.add(1)

a = a isa Array[Int]
a = null
if b then a = 1
d = a.as(not null)
d = a.as(Int)


do end
do a = 1
do
a = 1
a = 1
end

if b then end
if b then a = 1
if b then
a = 2
a = 2
end
if b then a = 3 else end
if b then a = 4 else a = 5
if b then a = 6 else
a = 7
a = 7
end
if b then
else
end
if b then else end
if b then else a = 8
if b then else
a = 9
a = 9
end
if b then
if b then
a = 1
end
end
if b then
if b then
a = 2
a = 3
end
end

a = if b then 1 else 2
a = if b then if b then 3 else 4 else if b then 5 else 6

for i in c do end
for i in c do
a = 1
a = 1
end
for i in c, j in c do end
var m = new HashMap[String,String]
for k, v in m do end

loop
if b then break
if b then continue
end

assert b
if not b then return
1 change: 1 addition & 0 deletions tests/test_astprinter.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
syntax_expr.nit

0 comments on commit e361242

Please sign in to comment.