changeset 396:ba8b0aae6453

work on manual; add LuanJavaFunction.ARG_COLLECTION;
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 26 Apr 2015 21:19:10 -0600
parents a7cb58532846
children 5e0acdeaea93
files core/src/luan/LuanJavaFunction.java website/src/manual.html.luan
diffstat 2 files changed, 150 insertions(+), 174 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanJavaFunction.java	Fri Apr 24 17:42:22 2015 -0600
+++ b/core/src/luan/LuanJavaFunction.java	Sun Apr 26 21:19:10 2015 -0600
@@ -10,6 +10,7 @@
 import java.util.HashMap;
 import java.util.Set;
 import java.util.Arrays;
+import java.util.Collection;
 
 
 public final class LuanJavaFunction extends LuanFunction {
@@ -461,6 +462,22 @@
 		}
 	};
 
+	private static final ArgConverter ARG_COLLECTION = new ArgConverter() {
+		public Object convert(Object obj) {
+			if( obj instanceof LuanTableImpl ) {
+				LuanTableImpl t = (LuanTableImpl)obj;
+				if( t.isList() )
+					return t.asList();
+				if( t.isSet() )
+					return t.asSet();
+			}
+			return obj;
+		}
+		@Override public String toString() {
+			return "ARG_COLLECTION";
+		}
+	};
+
 	private static class ArgArray implements ArgConverter {
 		private final Object[] a;
 
@@ -529,6 +546,8 @@
 			return ARG_LIST;
 		if( cls.equals(Set.class) )
 			return ARG_SET;
+		if( cls.equals(Collection.class) )
+			return ARG_COLLECTION;
 		if( cls.isArray() && !cls.getComponentType().isPrimitive() )
 			return new ArgArray(cls);
 		return ARG_SAME;
--- a/website/src/manual.html.luan	Fri Apr 24 17:42:22 2015 -0600
+++ b/website/src/manual.html.luan	Sun Apr 26 21:19:10 2015 -0600
@@ -66,6 +66,13 @@
 			<a href="#expressions">Expressions</a>
 			<ul>
 				<li><a href="#arithmetic">Arithmetic Operators</a></li>
+				<li><a href="#conversions">Coercions and Conversions</a></li>
+				<li><a href="#relational">Relational Operators</a></li>
+				<li><a href="#logical_ops">Logical Operators</a></li>
+				<li><a href="#concatenation">Concatenation</a></li>
+				<li><a href="#length">The Length Operator</a></li>
+				<li><a href="#precedence">Precedence</a></li>
+				<li><a href="#constructors">Table Constructors</a></li>
 			</ul>
 		</li>
 	</ul>
@@ -1210,143 +1217,95 @@
 
 
 
-<h3>3.4.3 &ndash; <a name="3.4.3">Coercions and Conversions</a></h3><p>
-Lua provides some automatic conversions between some
+<h4 margin-top="1em"><a name="conversions">Coercions and Conversions</a></h4>
+
+<p>
+Luan provides some automatic conversions between some
 types and representations at run time.
-Bitwise operators always convert float operands to integers.
-Exponentiation and float division
-always convert integer operands to floats.
-All other arithmetic operations applied to mixed numbers
-(integers and floats) convert the integer operand to a float;
-this is called the <em>usual rule</em>.
-The C API also converts both integers to floats and
-floats to integers, as needed.
-Moreover, string concatenation accepts numbers as arguments,
-besides strings. 
-
-
-<p>
-Lua also converts strings to numbers,
+String concatenation converts all of its arguments to strings.
+
+Luan also converts strings to numbers
 whenever a number is expected.
 
 
-<p>
-In a conversion from integer to float,
-if the integer value has an exact representation as a float,
-that is the result.
-Otherwise,
-the conversion gets the nearest higher or
-the nearest lower representable value.
-This kind of conversion never fails.
-
-
-<p>
-The conversion from float to integer
-checks whether the float has an exact representation as an integer
-(that is, the float has an integral value and
-it is in the range of integer representation).
-If it does, that representation is the result.
-Otherwise, the conversion fails.
-
-
-<p>
-The conversion from strings to numbers goes as follows:
-First, the string is converted to an integer or a float,
-following its syntax and the rules of the Lua lexer.
-(The string may have also leading and trailing spaces and a sign.)
-Then, the resulting number is converted to the required type
-(float or integer) according to the previous rules.
-
-
-<p>
-The conversion from numbers to strings uses a
-non-specified human-readable format.
-For complete control over how numbers are converted to strings,
-use the <code>format</code> function from the string library
-(see <a href="#pdf-string.format"><code>string.format</code></a>).
-
-
-
-
-
-<h3>3.4.4 &ndash; <a name="3.4.4">Relational Operators</a></h3><p>
-Lua supports the following relational operators:
+
+
+
+<h4 margin-top="1em"><a name="relational">Relational Operators</a></h4>
+
+<p>
+Luan supports the following relational operators:
 
 <ul>
-<li><b><code>==</code>: </b>equality</li>
-<li><b><code>~=</code>: </b>inequality</li>
-<li><b><code>&lt;</code>: </b>less than</li>
-<li><b><code>&gt;</code>: </b>greater than</li>
-<li><b><code>&lt;=</code>: </b>less or equal</li>
-<li><b><code>&gt;=</code>: </b>greater or equal</li>
+<li><b><tt>==</tt>: </b>equality</li>
+<li><b><tt>~=</tt>: </b>inequality</li>
+<li><b><tt>&lt;</tt>: </b>less than</li>
+<li><b><tt>&gt;</tt>: </b>greater than</li>
+<li><b><tt>&lt;=</tt>: </b>less or equal</li>
+<li><b><tt>&gt;=</tt>: </b>greater or equal</li>
 </ul><p>
 These operators always result in <b>false</b> or <b>true</b>.
 
 
 <p>
-Equality (<code>==</code>) first compares the type of its operands.
+Equality (<tt>==</tt>) first compares the type of its operands.
 If the types are different, then the result is <b>false</b>.
 Otherwise, the values of the operands are compared.
-Strings are compared in the obvious way.
-Numbers follow the usual rule for binary operations:
-if both operands are integers,
-they are compared as integers;
-otherwise, they are converted to floats
-and compared as such.
-
-
-<p>
-Tables, userdata, and threads
+Strings, numbers, and binary values are compared in the obvious way (by value).
+
+<p>
+Tables
 are compared by reference:
 two objects are considered equal only if they are the same object.
-Every time you create a new object
-(a table, userdata, or thread),
-this new object is different from any previously existing object.
-Closures with the same reference are always equal.
-Closures with any detectable difference
-(different behavior, different definition) are always different.
-
-
-<p>
-You can change the way that Lua compares tables and userdata
+Every time you create a new table,
+it is different from any previously existing table.
+Closures are also compared by reference.
+
+<p>
+You can change the way that Lua compares tables
 by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>).
 
+<p>
+Userdata are Java objects.  They are compared for equality with the Java <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)"><tt>equals</tt></a> method.
 
 <p>
 Equality comparisons do not convert strings to numbers
 or vice versa.
-Thus, <code>"0"==0</code> evaluates to <b>false</b>,
-and <code>t[0]</code> and <code>t["0"]</code> denote different
+Thus, <tt>"0"==0</tt> evaluates to <b>false</b>,
+and <tt>t[0]</tt> and <tt>t["0"]</tt> denote different
 entries in a table.
 
 
 <p>
-The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).
+The operator <tt>~=</tt> is exactly the negation of equality (<tt>==</tt>).
 
 
 <p>
 The order operators work as follows.
+
 If both arguments are numbers,
 then they are compared following
 the usual rule for binary operations.
 Otherwise, if both arguments are strings,
 then their values are compared according to the current locale.
-Otherwise, Lua tries to call the "lt" or the "le"
+Otherwise, Luan tries to call the "lt" or the "le"
 metamethod (see <a href="#2.4">&sect;2.4</a>).
-A comparison <code>a &gt; b</code> is translated to <code>b &lt; a</code>
-and <code>a &gt;= b</code> is translated to <code>b &lt;= a</code>.
-
-
-
-
-
-<h3>3.4.5 &ndash; <a name="3.4.5">Logical Operators</a></h3><p>
-The logical operators in Lua are
+A comparison <tt>a &gt; b</tt> is translated to <tt>b &lt; a</tt>
+and <tt>a &gt;= b</tt> is translated to <tt>b &lt;= a</tt>.
+
+
+
+
+
+<h4 margin-top="1em"><a name="logical_ops">Logical Operators</a></h4>
+
+<p>
+The logical operators in Luan are
 <b>and</b>, <b>or</b>, and <b>not</b>.
+The <b>and</b> and <b>or</b> operators consider both <b>false</b> and <b>nil</b> as false
+and anything else as true.
 Like the control structures (see <a href="#3.3.4">&sect;3.3.4</a>),
-all logical operators consider both <b>false</b> and <b>nil</b> as false
-and anything else as true.
-
+the <b>not</b> operator requires a boolean value.
 
 <p>
 The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.
@@ -1361,7 +1320,7 @@
 the second operand is evaluated only if necessary.
 Here are some examples:
 
-<pre>
+<p><tt><pre>
      10 or 20            --&gt; 10
      10 or error()       --&gt; 10
      nil or "a"          --&gt; "a"
@@ -1370,84 +1329,78 @@
      false and nil       --&gt; false
      false or nil        --&gt; nil
      10 and 20           --&gt; 20
-</pre><p>
+</pre></tt></p>
+
+<p>
 (In this manual,
-<code>--&gt;</code> indicates the result of the preceding expression.)
-
-
-
-
-
-<h3>3.4.6 &ndash; <a name="3.4.6">Concatenation</a></h3><p>
-The string concatenation operator in Lua is
-denoted by two dots ('<code>..</code>').
-If both operands are strings or numbers, then they are converted to
-strings according to the rules described in <a href="#3.4.3">&sect;3.4.3</a>.
-Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">&sect;2.4</a>).
-
-
-
-
-
-<h3>3.4.7 &ndash; <a name="3.4.7">The Length Operator</a></h3>
-
-<p>
-The length operator is denoted by the unary prefix operator <code>#</code>.
-The length of a string is its number of bytes
-(that is, the usual meaning of string length when each
-character is one byte).
+<tt>--&gt;</tt> indicates the result of the preceding expression.)
+
+
+
+<h4 margin-top="1em"><a name="concatenation">Concatenation</a></h4>
+
+<p>
+The string concatenation operator in Luan is
+denoted by two dots ('<tt>..</tt>').
+All operands are converted to strings.
+
+
+
+<h4 margin-top="1em"><a name="length">The Length Operator</a></h4>
+
+<p>
+The length operator is denoted by the unary prefix operator <tt>#</tt>.
+The length of a string is its number of characters.
+The length of a binary is its number of bytes.
 
 
 <p>
 A program can modify the behavior of the length operator for
-any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">&sect;2.4</a>).
-
-
-<p>
-Unless a <code>__len</code> metamethod is given,
-the length of a table <code>t</code> is only defined if the
-table is a <em>sequence</em>,
+any table through the <tt>__len</tt> metamethod (see <a href="#2.4">&sect;2.4</a>).
+
+
+<p>
+Unless a <tt>__len</tt> metamethod is given,
+the length of a table <tt>t</tt> is defined 
+as the number of elements in <em>sequence</em>,
 that is,
-the set of its positive numeric keys is equal to <em>{1..n}</em>
+the size of the set of its positive numeric keys is equal to <em>{1..n}</em>
 for some non-negative integer <em>n</em>.
 In that case, <em>n</em> is its length.
 Note that a table like
 
-<pre>
+<p><tt><pre>
      {10, 20, nil, 40}
-</pre><p>
-is not a sequence, because it has the key <code>4</code>
-but does not have the key <code>3</code>.
-(So, there is no <em>n</em> such that the set <em>{1..n}</em> is equal
-to the set of positive numeric keys of that table.)
-Note, however, that non-numeric keys do not interfere
-with whether a table is a sequence.
-
-
-
-
-
-<h3>3.4.8 &ndash; <a name="3.4.8">Precedence</a></h3><p>
-Operator precedence in Lua follows the table below,
+</pre></tt></p>
+
+<p>
+has a length of <tt>2</tt>, because that is the last key in sequence.
+
+
+
+
+
+<h4 margin-top="1em"><a name="precedence">Precedence</a></h4>
+
+<p>
+Operator precedence in Luan follows the table below,
 from lower to higher priority:
 
-<pre>
+<p><tt><pre>
      or
      and
      &lt;     &gt;     &lt;=    &gt;=    ~=    ==
-     |
-     ~
-     &amp;
-     &lt;&lt;    &gt;&gt;
      ..
      +     -
-     *     /     //    %
-     unary operators (not   #     -     ~)
+     *     /     %
+     unary operators (not   #     -)
      ^
-</pre><p>
+</pre></tt></p>
+
+<p>
 As usual,
 you can use parentheses to change the precedences of an expression.
-The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>')
+The concatenation ('<tt>..</tt>') and exponentiation ('<tt>^</tt>')
 operators are right associative.
 All other binary operators are left associative.
 
@@ -1455,37 +1408,41 @@
 
 
 
-<h3>3.4.9 &ndash; <a name="3.4.9">Table Constructors</a></h3><p>
+<h4 margin-top="1em"><a name="constructors">Table Constructors</a></h4>
+
+<p>
 Table constructors are expressions that create tables.
 Every time a constructor is evaluated, a new table is created.
 A constructor can be used to create an empty table
 or to create a table and initialize some of its fields.
 The general syntax for constructors is
 
-<pre>
+<p><tt><pre>
 	tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo;
 	fieldlist ::= field {fieldsep field} [fieldsep]
 	field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
 	fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
-</pre>
-
-<p>
-Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry
-with key <code>exp1</code> and value <code>exp2</code>.
-A field of the form <code>name = exp</code> is equivalent to
-<code>["name"] = exp</code>.
-Finally, fields of the form <code>exp</code> are equivalent to
-<code>[i] = exp</code>, where <code>i</code> are consecutive integers
+</pre></tt></p>
+
+<p>
+Each field of the form <tt>[exp1] = exp2</tt> adds to the new table an entry
+with key <tt>exp1</tt> and value <tt>exp2</tt>.
+A field of the form <tt>name = exp</tt> is equivalent to
+<tt>["name"] = exp</tt>.
+Finally, fields of the form <tt>exp</tt> are equivalent to
+<tt>[i] = exp</tt>, where <tt>i</tt> are consecutive integers
 starting with 1.
 Fields in the other formats do not affect this counting.
 For example,
 
-<pre>
+<p><tt><pre>
      a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
-</pre><p>
+</pre></tt></p>
+
+<p>
 is equivalent to
 
-<pre>
+<p><tt><pre>
      do
        local t = {}
        t[f(1)] = g
@@ -1497,7 +1454,7 @@
        t[4] = 45          -- 4th exp
        a = t
      end
-</pre>
+</pre></tt></p>
 
 <p>
 The order of the assignments in a constructor is undefined.
@@ -1505,7 +1462,7 @@
 
 
 <p>
-If the last field in the list has the form <code>exp</code>
+If the last field in the list has the form <tt>exp</tt>
 and the expression is a function call or a vararg expression,
 then all values returned by this expression enter the list consecutively
 (see <a href="#3.4.10">&sect;3.4.10</a>).