Permalink
Browse files

Add Numbers, and gnu math. Add empty message send, add mixin structur…

…e and comparators
  • Loading branch information...
1 parent 8606326 commit bdaec9e381258d499e96ce0ac2c15412edfb16ef @olabini committed Oct 12, 2008
Showing with 8,181 additions and 64 deletions.
  1. +6 −0 COPYING
  2. +21 −0 LICENSE.kawa
  3. +12 −0 build.xml
  4. +43 −12 src/grammar/ioke.g
  5. +177 −0 src/main/gnu/math/BaseUnit.java
  6. +423 −0 src/main/gnu/math/BitOps.java
  7. +45 −0 src/main/gnu/math/CComplex.java
  8. +50 −0 src/main/gnu/math/CQuantity.java
  9. +251 −0 src/main/gnu/math/Complex.java
  10. +397 −0 src/main/gnu/math/DComplex.java
  11. +341 −0 src/main/gnu/math/DFloNum.java
  12. +131 −0 src/main/gnu/math/DQuantity.java
  13. +738 −0 src/main/gnu/math/DateTime.java
  14. +186 −0 src/main/gnu/math/Dimensions.java
  15. +526 −0 src/main/gnu/math/Duration.java
  16. +318 −0 src/main/gnu/math/ExponentialFormat.java
  17. +206 −0 src/main/gnu/math/FixedRealFormat.java
  18. +164 −0 src/main/gnu/math/IntFraction.java
  19. +1,593 −0 src/main/gnu/math/IntNum.java
  20. +732 −0 src/main/gnu/math/MPN.java
  21. +128 −0 src/main/gnu/math/MulUnit.java
  22. +133 −0 src/main/gnu/math/NamedUnit.java
  23. +124 −0 src/main/gnu/math/Numeric.java
  24. +206 −0 src/main/gnu/math/Quantity.java
  25. +161 −0 src/main/gnu/math/RatNum.java
  26. +365 −0 src/main/gnu/math/RealNum.java
  27. +223 −0 src/main/gnu/math/Unit.java
  28. +15 −2 src/main/ioke/lang/DefaultBehavior.java
  29. +28 −0 src/main/ioke/lang/False.java
  30. +4 −0 src/main/ioke/lang/Ground.java
  31. +9 −4 src/main/ioke/lang/IokeObject.java
  32. +81 −35 src/main/ioke/lang/Message.java
  33. +29 −0 src/main/ioke/lang/Mixins.java
  34. +81 −0 src/main/ioke/lang/Number.java
  35. +8 −0 src/main/ioke/lang/Origin.java
  36. +25 −3 src/main/ioke/lang/Runtime.java
  37. +7 −8 src/main/ioke/lang/Text.java
  38. +28 −0 src/main/ioke/lang/True.java
  39. +16 −0 src/main/ioke/lang/exceptions/IokeException.java
  40. +16 −0 src/main/ioke/lang/exceptions/ObjectIsNotRightType.java
  41. +33 −0 src/main/ioke/lang/mixins/Comparing.java
  42. +35 −0 test/number_spec.rb
  43. +27 −0 test/operators_spec.rb
  44. +17 −0 test/parsing_spec.rb
  45. +4 −0 test/scripts/bench_fib_rec.ik
  46. +11 −0 test/scripts/fib_iter.ik
  47. +7 −0 test/scripts/fib_rec.ik
View
@@ -0,0 +1,6 @@
+Different parts of Ioke is covered by different licences and copyrights:
+
+The package ioke.lang is covered by the file LICENSE.
+The package gnu.math is covered by the file LICENSE.kawa.
+
+Everything else is covered by the file LICENSE, unless noted.
View
@@ -0,0 +1,21 @@
+The software (with related files and documentation) in these packages
+are copyright (C) 1996-2006 Per Bothner.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -61,6 +61,18 @@
</jar>
</target>
+ <target name="jar-notest" depends="compile" description="Create the ioke.jar file">
+ <jar destfile="${lib.dist.dir}/ioke.jar">
+ <fileset dir="${classes.dir}">
+ <include name="**/*.class"/>
+ <include name="**/*.properties"/>
+ </fileset>
+ <manifest>
+ <attribute name="Built-By" value="${user.name}"/>
+ </manifest>
+ </jar>
+ </target>
+
<target name="clean" description="clean almost everything">
<delete dir="${build.dir}"/>
<delete file="${lib.dist.dir}/ioke.jar" quiet="true"/>
View
@@ -2,10 +2,12 @@ grammar ioke;
options {
output = AST;
+ backtrack = true;
}
tokens {
MESSAGE_SEND;
+ MESSAGE_SEND_EMPTY;
}
@lexer::header {
@@ -16,10 +18,28 @@ package ioke.lang.parser;
package ioke.lang.parser;
}
-@lexer::members {
+@members {
+ protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException {
+ throw new MismatchedTokenException(ttype, input);
+ }
+
+ public void recoverFromMismatchedSet(IntStream input, RecognitionException e, BitSet follow) throws RecognitionException {
+ reportError(e);
+ throw e;
+ }
}
-@members {
+@rulecatch {
+ catch(RecognitionException e) {
+ throw e;
+ }
+}
+
+@lexer::members {
+ public void reportError(RecognitionException e) {
+ displayRecognitionError(this.getTokenNames(), e);
+ throw new RuntimeException(e);
+ }
}
messageChain
@@ -34,31 +54,34 @@ commatedExpression
assignmentExpression
:
- expression (Equals expression)?
+ expression+ (binaryOperator expression)?
;
expression
- :
- literal
- | message
- | Terminator
- ;
-
-message
:
Identifier ('(' commatedExpression? ')')? -> ^(MESSAGE_SEND Identifier commatedExpression?)
+ | binaryOperator '(' commatedExpression? ')' -> ^(MESSAGE_SEND binaryOperator commatedExpression?)
+ | '(' commatedExpression? ')' -> ^(MESSAGE_SEND_EMPTY commatedExpression?)
+ | StringLiteral
+ | NumberLiteral
+ | Terminator
;
-literal
+binaryOperator
:
- StringLiteral
+ ComparisonOperator
+ | Equals
;
Identifier
:
Letter (Letter|IDDigit|StrangeChars)*
;
+NumberLiteral
+ : '0'
+ | ('1'..'9') Digit*
+ ;
StringLiteral
: '"' ( EscapeSequence | ~('\\'|'"') )* '"'
@@ -75,6 +98,11 @@ LineComment
: '#' ~('\n'|'\r')* {$channel=HIDDEN;}
;
+ComparisonOperator
+ :
+ '<'
+ ;
+
Equals
:
'='
@@ -104,6 +132,9 @@ UnicodeEscape
: '\\' 'u' HexDigit HexDigit HexDigit HexDigit
;
+fragment
+Digit : '0'..'9' ;
+
fragment
HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;
@@ -0,0 +1,177 @@
+// Copyright (c) 1997 Per M.A. Bothner.
+// This is free software; for terms and warranty disclaimer see ./COPYING.
+
+package gnu.math;
+import java.io.*;
+
+/** A primitive Unit of measurement (such as a meter).
+ * @author Per Bothner
+ */
+
+public class BaseUnit extends NamedUnit implements Externalizable
+{
+ /** The name of the "dimension" this is a base unit for. */
+ String dimension;
+
+ /** BaseUnits are numberd with globally unique indexes. */
+ static int base_count = 0;
+
+ /** This is an index in the bases array.
+ * The index may change if there are insertions in bases. */
+ int index;
+
+ /* Array of all existing BaseUnits.
+ * This array is kept sorted (according to the compareTo method).
+ * The reason is to make it easy to keep the BaseUnits in a Dimensions
+ * array to be sorted "lexicographically". One reason we want to
+ * do that is to have a stable serialization representtion. *
+ // static BaseUnit[] bases = null;
+
+ /** A name for the dimension bing measured.
+ * A meter has the dimension "Length".
+ * BaseUnits are considered equal if their name <em>and</em>
+ * their dimension are equal. (In that case they are also identical.)
+ * We use dimension as a partial guard against accidental name clashes.
+ */
+ public String getDimension()
+ {
+ return dimension;
+ }
+
+ /** Name for Unit.Empty. */
+ private static final String unitName = "(name)";
+
+ /** Should only be used for serialization, and Unit.Empty. */
+ public BaseUnit()
+ {
+ name = unitName;
+ index = 0x7fffffff;
+ dims = Dimensions.Empty;
+ }
+
+ protected void init()
+ {
+ this.base = this;
+ this.scale = 1.0;
+ this.dims = new Dimensions (this);
+ super.init();
+
+ this.index = BaseUnit.base_count++;
+ /*
+ if (bases == null)
+ bases = new BaseUnit[10];
+ else if (index >= bases.length)
+ {
+ BaseUnit[] b = new BaseUnit[2 * index];
+ System.arraycopy(bases, 0, b, 0, bases.length);
+ bases = b;
+ }
+ bases[index] = this;
+ // Make sure bases array is sorted.
+ for (int i = index; --i >= 0; )
+ {
+ BaseUnit old = bases[i];
+ int code = compare(old, this);
+ if (code == 0)
+ throw new Error("internal invariant failure");
+ if (code > 0)
+ break;
+ // Swap old and this, and their index fields.
+ bases[i] = this;
+ bases[index] = old;
+ old.index = index;
+ index = i;
+ }
+ */
+ }
+
+ public BaseUnit (String name)
+ {
+ this.name = name;
+ init();
+ }
+
+ public BaseUnit (String name, String dimension)
+ {
+ this.name = name;
+ this.dimension = dimension;
+ init();
+ }
+
+ public int hashCode () { return name.hashCode(); }
+
+ public Unit unit() { return this; }
+
+ /** Look for an existing matching BaseUnit.
+ * @param name name of desired BaseUnit, such as "m"
+ * @param dimension a name for what the unit measures, such as "Length".
+ */
+ public static BaseUnit lookup(String name, String dimension)
+ {
+ name = name.intern();
+ if (name == unitName && dimension == null)
+ return Unit.Empty;
+ int hash = name.hashCode();
+ int index = (hash & 0x7FFFFFFF) % table.length;
+ for (NamedUnit unit = table[index]; unit != null; unit = unit.chain)
+ {
+ if (unit.name == name && unit instanceof BaseUnit)
+ {
+ BaseUnit bunit = (BaseUnit) unit;
+ if (bunit.dimension == dimension)
+ return bunit;
+ }
+ }
+ return null;
+ }
+
+ public static BaseUnit make(String name, String dimension)
+ {
+ BaseUnit old = lookup(name, dimension);
+ return old == null ? new BaseUnit(name, dimension) : old;
+ }
+
+ public static int compare (BaseUnit unit1, BaseUnit unit2)
+ {
+ int code = unit1.name.compareTo(unit2.name);
+ if (code != 0)
+ return code;
+ String dim1 = unit1.dimension;
+ String dim2 = unit2.dimension;
+ if (dim1 == dim2)
+ return 0;
+ if (dim1 == null)
+ return -1;
+ if (dim2 == null)
+ return 1;
+ return dim1.compareTo(dim2);
+ }
+
+ /**
+ * @serialData Write the unit name (using writeUTF), followed.
+ * followed by the name of the dimension it is a unit for.
+ * The latter is either null or a String and is written with writeObject.
+ */
+
+ public void writeExternal(ObjectOutput out) throws IOException
+ {
+ out.writeUTF(name);
+ out.writeObject(dimension);
+ }
+
+ public void readExternal(ObjectInput in)
+ throws IOException, ClassNotFoundException
+ {
+ name = in.readUTF();
+ dimension = (String) in.readObject();
+ }
+
+ public Object readResolve() throws ObjectStreamException
+ {
+ BaseUnit unit = lookup(name, dimension);
+ if (unit != null)
+ return unit;
+ init();
+ return this;
+ }
+}
Oops, something went wrong.

0 comments on commit bdaec9e

Please sign in to comment.