Merge: Infer more attribute types
authorJean Privat <jean@pryen.org>
Fri, 16 Feb 2018 16:45:45 +0000 (11:45 -0500)
committerJean Privat <jean@pryen.org>
Fri, 16 Feb 2018 16:45:45 +0000 (11:45 -0500)
Extend the detection of the static type of attributes from their literal values to support three new cases:

* Simple arrays like `[0, 1, 2]` and `[new Set[Int], new Set[Int]]`. However, it does not accept arrays with an explicit type because we can't subtype/anchor at that point, as far as I know.

* Negative integers and floats. This cheats a bit as the return type of the unary - is defined in the core libary. However this should help 99.9% of the time, in particular for Nit beginners, and a workaround is to declare the attribute static type when defining a different kernel library.

* The `once` keyword.

~~~
class A
       # Now detected
       var i = -1
       var f = -1.0
       var a = [0, 1]
       var o = once [0, 1]

       # These are refused
       var a1 = [0, 1.0, "a"] # Different types
       var a2 = [0, 1: Int] # Can't reliably check subtypes
       var a4 = [1+1] # Expression
       var o1 = once [0, "a"] # Forwarded error
end
~~~

---

You may want to review commit by commit as the first commit is a small refactoring.

Pull-Request: #2614

contrib/nitin/Makefile
contrib/nitin/nitin.nit
contrib/nitin/nitin_readline.nit [deleted file]
lib/prompt.ini [new file with mode: 0644]
lib/prompt.nit [new file with mode: 0644]
lib/readline.nit
src/nitx.nit

index c505bcd..940cee6 100644 (file)
@@ -1,3 +1,3 @@
 all:
        mkdir -p bin
-       nitc --semi-global nitin_readline.nit -o bin/nitin
+       nitc --semi-global nitin.nit -m readline -o bin/nitin
index 2497411..6651636 100644 (file)
@@ -19,6 +19,8 @@
 # The Nit interactive interpreter
 module nitin
 
+import prompt
+
 import nitc::interpreter
 import nitc::frontend
 import nitc::parser_util
@@ -56,21 +58,6 @@ redef class ToolContext
                return tree.n_base.as(not null)
        end
 
-       # Read an user-line with a given `prompt`
-       #
-       # Return `null` if end of file
-       fun readline(prompt: String): nullable String do
-               printn prompt
-               var res = stdin.read_line
-               if res == "" and stdin.eof then return null
-               return res
-       end
-
-       # Add `text` in the history for `readline`.
-       #
-       # With the default implementation, the history is dropped
-       fun readline_add_history(text: String) do end
-
        # The last line number read by `i_parse`
        var last_line = 0
 
@@ -85,7 +72,7 @@ redef class ToolContext
                                s = stdin.read_line
                                if s == "" and stdin.eof then s = null
                        else
-                               s = readline(prompt)
+                               s = sys.prompt(prompt)
                        end
                        if s == null then return null
                        if s == "" then
@@ -115,7 +102,7 @@ redef class ToolContext
                        end
 
                        last_line = n.location.file.line_starts.length - 1
-                       readline_add_history(text.chomp)
+                       prompt_add_history(text.chomp)
                        return n
                end
        end
diff --git a/contrib/nitin/nitin_readline.nit b/contrib/nitin/nitin_readline.nit
deleted file mode 100644 (file)
index d193059..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-# This file is part of NIT ( http://www.nitlanguage.org ).
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-# Add GNU readline capabilities to nitin
-module nitin_readline
-
-import nitin
-import readline
-
-redef class ToolContext
-       redef fun readline(prompt) do return sys.readline(prompt)
-       redef fun readline_add_history(text) do sys.add_history(text)
-end
diff --git a/lib/prompt.ini b/lib/prompt.ini
new file mode 100644 (file)
index 0000000..815bd3b
--- /dev/null
@@ -0,0 +1,11 @@
+[package]
+name=prompt
+tags=console,lib
+maintainer=Alexis Laferrière <alexis.laf@xymus.net>
+license=Apache-2.0
+[upstream]
+browse=https://github.com/nitlang/nit/tree/master/lib/prompt.nit
+git=https://github.com/nitlang/nit.git
+git.directory=lib/prompt.nit
+homepage=http://nitlanguage.org
+issues=https://github.com/nitlang/nit/issues
diff --git a/lib/prompt.nit b/lib/prompt.nit
new file mode 100644 (file)
index 0000000..56c8c2d
--- /dev/null
@@ -0,0 +1,34 @@
+# 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.
+
+# Basic services to display a prompt
+module prompt
+
+# Display `prompt` in the console and wait for a response
+#
+# Return the user response, or `null` if EOF.
+#
+# If `add_history`, the user response is added to the history.
+fun prompt(prompt: Text, add_history: nullable Bool): nullable String
+do
+       printn prompt
+       var res = stdin.read_line
+       if res == "" and stdin.eof then return null
+       return res
+end
+
+# Add `line` to the shell history
+#
+# The default implementation is a noop, but other packages can refine it.
+fun prompt_add_history(line: String) do end
index 00f920c..6532cb4 100644 (file)
@@ -17,6 +17,8 @@
 # GNU readline library wrapper
 module readline is ldflags "-lreadline"
 
+import prompt
+
 in "C" `{
        #include <readline/readline.h>
        #include <readline/history.h>
@@ -56,3 +58,7 @@ end
 
 # Adds the data String to the history no matter what it contains
 fun add_history(data: String) do native_add_history data.to_cstring
+
+redef fun prompt(prompt, add_history) do return readline(prompt.to_s, add_history)
+
+redef fun prompt_add_history(line) do native_add_history(line.to_cstring)
index 061b44e..8442ff7 100644 (file)
@@ -25,7 +25,7 @@ module nitx
 
 import frontend
 import doc::term
-import readline
+import prompt
 
 redef class ToolContext
 
@@ -90,9 +90,12 @@ class Nitx
 
        # Prompts the user for a query.
        fun prompt do
-               var line = readline(">> ", true)
+               var line = sys.prompt(">> ", true)
                if line != null then
                        do_command(line)
+               else
+                       # EOF
+                       exit 0
                end
                prompt
        end