changeset 1680:9ef19f5ea973

add // operator
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 02 Jun 2022 22:34:55 -0600
parents 39287902fb0c
children 6061be2cd84b
files src/luan/impl/LuanImpl.java src/luan/impl/LuanParser.java website/src/manual.html.luan
diffstat 3 files changed, 44 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
diff -r 39287902fb0c -r 9ef19f5ea973 src/luan/impl/LuanImpl.java
--- a/src/luan/impl/LuanImpl.java	Tue May 31 15:39:58 2022 -0600
+++ b/src/luan/impl/LuanImpl.java	Thu Jun 02 22:34:55 2022 -0600
@@ -67,6 +67,15 @@
 		return arithmetic(luan,"__div",o1,o2);
 	}
 
+	public static Object idiv(Luan luan,Object o1,Object o2) throws LuanException {
+		if( o1 instanceof Number && o2 instanceof Number ) {
+			double d1 = ((Number)o1).doubleValue();
+			double d2 = ((Number)o2).doubleValue();
+			return Math.floor(d1/d2);
+		}
+		return arithmetic(luan,"__idiv",o1,o2);
+	}
+
 	public static Object mod(Luan luan,Object o1,Object o2) throws LuanException {
 		if( o1 instanceof Number && o2 instanceof Number ) {
 			double d1 = ((Number)o1).doubleValue();
diff -r 39287902fb0c -r 9ef19f5ea973 src/luan/impl/LuanParser.java
--- a/src/luan/impl/LuanParser.java	Tue May 31 15:39:58 2022 -0600
+++ b/src/luan/impl/LuanParser.java	Thu Jun 02 22:34:55 2022 -0600
@@ -1030,16 +1030,29 @@
 				newExp.add( ")" );
 				exp = newExp;
 			} else if( parser.match('/') ) {
-				Spaces();
-				exp = exp.single();
-				Expr exp2 = required(UnaryExpr()).single();
-				Expr newExp = new Expr(Val.SINGLE,false);
-				newExp.add( "LuanImpl.div(luan," );
-				newExp.addAll( exp );
-				newExp.add( "," );
-				newExp.addAll( exp2 );
-				newExp.add( ")" );
-				exp = newExp;
+				if( parser.match('/') ) {
+					Spaces();
+					exp = exp.single();
+					Expr exp2 = required(UnaryExpr()).single();
+					Expr newExp = new Expr(Val.SINGLE,false);
+					newExp.add( "LuanImpl.idiv(luan," );
+					newExp.addAll( exp );
+					newExp.add( "," );
+					newExp.addAll( exp2 );
+					newExp.add( ")" );
+					exp = newExp;
+				} else {
+					Spaces();
+					exp = exp.single();
+					Expr exp2 = required(UnaryExpr()).single();
+					Expr newExp = new Expr(Val.SINGLE,false);
+					newExp.add( "LuanImpl.div(luan," );
+					newExp.addAll( exp );
+					newExp.add( "," );
+					newExp.addAll( exp2 );
+					newExp.add( ")" );
+					exp = newExp;
+				}
 			} else if( Mod() ) {
 				Spaces();
 				exp = exp.single();
diff -r 39287902fb0c -r 9ef19f5ea973 website/src/manual.html.luan
--- a/website/src/manual.html.luan	Tue May 31 15:39:58 2022 -0600
+++ b/website/src/manual.html.luan	Thu Jun 02 22:34:55 2022 -0600
@@ -292,6 +292,12 @@
 </p></li>
 
 <li><p>
+<b>"idiv": </b>
+the <code>//</code> operation.
+Behavior similar to the "add" operation.
+</p></li>
+
+<li><p>
 <b>"mod": </b>
 the <code>%</code> operation.
 Behavior similar to the "add" operation.
@@ -1270,7 +1276,8 @@
 <li><b><code>+</code>: </b>addition</li>
 <li><b><code>-</code>: </b>subtraction</li>
 <li><b><code>*</code>: </b>multiplication</li>
-<li><b><code>/</code>: </b>division</li>
+<li><b><code>/</code>: </b>float division</li>
+<li><b><code>//</code>: </b>floor division</li>
 <li><b><code>%</code>: </b>modulo</li>
 <li><b><code>^</code>: </b>exponentiation</li>
 <li><b><code>-</code>: </b>unary minus</li>
@@ -1281,6 +1288,10 @@
 </p>
 
 <p>
+Floor division (//) is a division that rounds the quotient towards minus infinity, that is, the floor of the division of its operands.
+</p>
+
+<p>
 Modulo is defined as the remainder of a division
 that rounds the quotient towards minus infinite (floor division).
 (The Java modulo operator is not used.)