# Register a new variable with its name
fun add(v: Variable)
do
+ var old_var = self[v.name]
+ if old_var != null then
+ _visitor.error(v.decl, "Error: '{v}' already defined at {old_var.decl.location.relative_to(v.decl.location)}.")
+ end
_dico[v.name] = v
_all_variables.add(v)
end
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.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.
+
+import array
+
+fun foo(p1: Int)
+do
+ var a1: Int = p1 + 1
+ #alt1#var p1: Int = a1 + 2
+ p1.output
+ a1.output
+end
+
+#alt9#fun bar(p1: Int, p1: Int) do p1.output
+
+foo(0)
+var a1: Int = 10
+do
+ var a2: Int = 11
+ #alt2#var a1: Int = 12
+ a1.output
+ a2.output
+end
+#alt3#var a1: Int = 13
+a1.output
+for a3 in [30, 31] do
+ var a2: Int = 20
+ #alt4#var a1: Int = 1
+ #alt5#var a3: Int = 1
+ a1.output
+ a2.output
+ a3.output
+ #alt6#for a2 in [400, 500] do a2.output
+end
+var a2: Int = 40
+a1.output
+a2.output
+#alt7#a3.output
+#alt8#p1.output
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.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.
+
+import kernel
+
+fun foo(p1: Int)
+ !bar(p2: Int) do (p1+p2).output
+ #alt1#!bar(p2: Int) do (p1*p2).output
+ #alt2#!p1 do abort
+do
+ bar(p1*10)
+ #alt3#var bar: Int = p1
+ #alt4#p2.output
+end
+
+foo(0)
+var a1: Int = 1
+foo(2) !bar(a2) do
+ #alt5#var a1: Int = 3
+ #alt6#var a2: Int = 3
+ #alt7#foo(3) !bar(a1) do a1.output
+ #alt8#foo(4) !bar(a2) do a1.output
+ a1.output
+ a2.output
+end
+var a2: Int = 5
+a1.output
+a2.output
--- /dev/null
+0
+1
+10
+11
+10
+10
+20
+30
+10
+20
+31
+10
+40
--- /dev/null
+alt/base_var2_alt1.nit:22,6--7: Error: 'p1' already defined at 19,9--10.
--- /dev/null
+alt/base_var2_alt2.nit:33,6--7: Error: 'a1' already defined at 30,5--6.
--- /dev/null
+alt/base_var2_alt3.nit:37,5--6: Error: 'a1' already defined at 30,5--6.
--- /dev/null
+alt/base_var2_alt4.nit:41,6--7: Error: 'a1' already defined at 30,5--6.
--- /dev/null
+alt/base_var2_alt5.nit:42,6--7: Error: 'a3' already defined at 39,5--6.
--- /dev/null
+alt/base_var2_alt6.nit:46,6--7: Error: 'a2' already defined at 40,6--7.
--- /dev/null
+alt/base_var2_alt7.nit:51,1--2: Error: Method or variable 'a3' unknown in Sys.
--- /dev/null
+alt/base_var2_alt8.nit:52,1--2: Error: Method or variable 'p1' unknown in Sys.
--- /dev/null
+alt/base_var2_alt9.nit:27,18--19: Error: 'p1' already defined at 27,9--10.
--- /dev/null
+0
+1
+20
+1
+5
--- /dev/null
+alt/base_var_closure_alt1.nit:21,3--5: A closure '!bar' already defined at 20,3--5.
--- /dev/null
+alt/base_var_closure_alt2.nit:22,3--4: Error: 'p1' already defined at 19,9--10.
+alt/base_var_closure_alt2.nit:24,6--7: Type error: expected expression.
--- /dev/null
+alt/base_var_closure_alt3.nit:25,6--8: Error: 'bar' already defined at 20,3--5.
--- /dev/null
+alt/base_var_closure_alt4.nit:26,2--3: Error: Method or variable 'p2' unknown in Object.
--- /dev/null
+alt/base_var_closure_alt5.nit:32,6--7: Error: 'a1' already defined at 30,5--6.
--- /dev/null
+alt/base_var_closure_alt6.nit:33,6--7: Error: 'a2' already defined at 31,13--14.
--- /dev/null
+alt/base_var_closure_alt7.nit:34,14--15: Error: 'a1' already defined at 30,5--6.
--- /dev/null
+alt/base_var_closure_alt8.nit:35,14--15: Error: 'a2' already defined at 31,13--14.