Merge: Nitin tests
authorJean Privat <jean@pryen.org>
Wed, 26 Apr 2017 14:38:25 +0000 (10:38 -0400)
committerJean Privat <jean@pryen.org>
Wed, 26 Apr 2017 14:38:25 +0000 (10:38 -0400)
Add a basic test for the nit interactive interpreter.

I also added a new engine but most test fails because:

Local variables in main are too much local, so the following fail because `a` is unknown in the second line. I plan to solve this problem.

~~~
var a = 1
print a
~~~

Classes and top-level methods are analysed one after the other so the following fails because `B` is unknown while processing the first class. I do not plan to solve this problem as I think this is the expected behavior when interactive.

~~~
class A
   var b: B
end
class B
end
~~~

Pull-Request: #2410

contrib/nitin/nitin.nit
tests/listfull.sh
tests/niti.skip
tests/nitin.inputs [new file with mode: 0644]
tests/nitvm.skip
tests/sav/nitin.res [new file with mode: 0644]
tests/tests.sh

index 0d4484d..641644f 100644 (file)
@@ -24,6 +24,15 @@ import nitc::frontend
 import nitc::parser_util
 
 redef class ToolContext
+
+       # --no-prompt
+       var opt_no_prompt = new OptionBool("Disable writing a prompt.", "--no-prompt")
+
+       redef init do
+               super
+               option_context.add_option(opt_no_prompt)
+       end
+
        # Parse a full module given as a string
        #
        # Return a AModule or a AError
@@ -62,7 +71,13 @@ redef class ToolContext
                var oldtext = ""
 
                loop
-                       var s = readline(prompt)
+                       var s
+                       if opt_no_prompt.value then
+                               s = stdin.read_line
+                               if s == "" and stdin.eof then s = null
+                       else
+                               s = readline(prompt)
+                       end
                        if s == null then return null
                        if s == "" then continue
 
index 841fdad..edbc37e 100755 (executable)
@@ -14,6 +14,7 @@ ls -1 -- "$@" \
        ../contrib/friendz/src/solver_cmd.nit \
        ../contrib/neo_doxygen/src/tests/neo_doxygen_*.nit \
        ../contrib/pep8analysis/src/pep8analysis.nit \
+       ../contrib/nitin/nitin.nit \
        ../contrib/nitiwiki/src/nitiwiki.nit \
        *.nit \
        | grep -v ../lib/popcorn/examples/
index d93ce27..b3a9b20 100644 (file)
@@ -7,6 +7,7 @@ nit_args6
 nit_args8
 nitvm_args1
 nitvm_args3
+nitin
 nitc_args1
 nitc_args3
 nitc_args5
diff --git a/tests/nitin.inputs b/tests/nitin.inputs
new file mode 100644 (file)
index 0000000..266b05c
--- /dev/null
@@ -0,0 +1,47 @@
+print 5+2
+
+for i in [0..5[ do
+print i
+end
+
+do
+var sum = 0
+for i in [0..50[ do
+sum += i
+end
+print sum
+end
+
+class A
+fun foo do
+print "hello"
+end
+end
+(new A).foo
+
+class
+end
+class A
+end
+redef class A
+redef fun foo do print "Bye"
+end
+(new A).foo
+
+foo
+fun foo do
+print "I'm sys"
+end
+foo
+
+redef class Sys
+var my_int: Int is writable
+end
+my_int = 5
+print my_int
+
+print([0..10[.to_a.to_json)
+import json
+print([0..10[.to_a.to_json)
+
+%$^&
index b8ae2ec..b312543 100644 (file)
@@ -7,6 +7,7 @@ nit_args6
 nit_args8
 nitvm_args1
 nitvm_args3
+nitin
 nitc_args1
 nitc_args3
 nitc_args5
diff --git a/tests/sav/nitin.res b/tests/sav/nitin.res
new file mode 100644 (file)
index 0000000..0e83a4c
--- /dev/null
@@ -0,0 +1,26 @@
+\e[0;33m1,7\e[0m: Redef Error: `A` is an imported class. Add the `redef` keyword to refine it.
+       class \e[1;31mA\e[0m
+             ^
+\e[0;33m1,1--3\e[0m: Error: method or variable `foo` unknown in `Sys`.
+       \e[1;31mfoo\e[0m
+       ^
+\e[0;33m1,20--26\e[0m: Error: method `to_json` does not exists in `Array[Int]`.
+       print([0..10[.to_a.\e[1;31mto_json\e[0m)
+                          ^
+-->7
+-->-->......0
+1
+2
+3
+4
+-->-->..................1225
+-->-->............-->hello
+-->-->...      \e[0;31mend\e[0m
+       ^: Syntax Error: unexpected keyword 'end'.
+-->...-->......-->Bye
+-->-->-->......-->I'm sys
+-->-->......-->-->5
+-->-->-->-->[0,1,2,3,4,5,6,7,8,9]
+-->--> \e[0;31m%\e[0m$^&
+       ^: Syntax Error: unexpected operator '%'.
+-->
\ No newline at end of file
index 2fbab89..e413069 100755 (executable)
@@ -436,7 +436,7 @@ istodo()
 find_nitc()
 {
        local name="$enginebinname"
-       local recent=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../c_src/$name 2>/dev/null | head -1`
+       local recent=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../contrib/nitin/bin/$name ../c_src/$name 2>/dev/null | head -1`
        if [[ "x$recent" == "x" ]]; then
                echo "Could not find binary for engine $engine, aborting"
                exit 1
@@ -470,6 +470,7 @@ while [ $stop = false ]; do
 done
 enginebinname=$engine
 isinterpret=
+isinteractive=
 case $engine in
        nitc|nitg)
                engine=nitcs;
@@ -515,6 +516,10 @@ case $engine in
                OPT="--vm $OPT"
                savdirs="sav/niti/"
                ;;
+       nitin)
+               enginebinname=nitin
+               isinteractive=true
+               ;;
        nitj)
                engine=nitj;
                OPT="--compile-dir $compdir --ant"
@@ -629,6 +634,15 @@ END
                        > "$ff.compile.log"
                        ERR=0
                        echo 0.0 > "$ff.time.out"
+               elif [ -n "$isinteractive" ]; then
+                       cat > "$ff.bin" <<END
+exec $NITC --no-color --no-prompt $OPT $includes < $(printf '%q' "$i") "\$@"
+END
+                       chmod +x "$ff.bin"
+                       > "$ff.cmp.err"
+                       > "$ff.compile.log"
+                       ERR=0
+                       echo 0.0 > "$ff.time.out"
                else
                        if skip_cc "$bf"; then
                                nocc="--no-cc"