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
all:
mkdir -p bin
- nitc --semi-global nitin_readline.nit -o bin/nitin
+ nitc --semi-global nitin.nit -m readline -o bin/nitin
# The Nit interactive interpreter
module nitin
+import prompt
+
import nitc::interpreter
import nitc::frontend
import nitc::parser_util
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
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
end
last_line = n.location.file.line_starts.length - 1
- readline_add_history(text.chomp)
+ prompt_add_history(text.chomp)
return n
end
end
+++ /dev/null
-# 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
--- /dev/null
+[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
--- /dev/null
+# 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
# GNU readline library wrapper
module readline is ldflags "-lreadline"
+import prompt
+
in "C" `{
#include <readline/readline.h>
#include <readline/history.h>
# 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)
import frontend
import doc::term
-import readline
+import prompt
redef class ToolContext
# 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