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
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
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
../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/
nit_args8
nitvm_args1
nitvm_args3
+nitin
nitc_args1
nitc_args3
nitc_args5
--- /dev/null
+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)
+
+%$^&
nit_args8
nitvm_args1
nitvm_args3
+nitin
nitc_args1
nitc_args3
nitc_args5
--- /dev/null
+\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
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
done
enginebinname=$engine
isinterpret=
+isinteractive=
case $engine in
nitc|nitg)
engine=nitcs;
OPT="--vm $OPT"
savdirs="sav/niti/"
;;
+ nitin)
+ enginebinname=nitin
+ isinteractive=true
+ ;;
nitj)
engine=nitj;
OPT="--compile-dir $compdir --ant"
> "$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"