| 1 | (******************************************************************************* |
|---|
| 2 | Copyright 2009 Sun Microsystems, Inc., |
|---|
| 3 | 4150 Network Circle, Santa Clara, California 95054, U.S.A. |
|---|
| 4 | All rights reserved. |
|---|
| 5 | |
|---|
| 6 | U.S. Government Rights - Commercial software. |
|---|
| 7 | Government users are subject to the Sun Microsystems, Inc. standard |
|---|
| 8 | license agreement and applicable provisions of the FAR and its supplements. |
|---|
| 9 | |
|---|
| 10 | Use is subject to license terms. |
|---|
| 11 | |
|---|
| 12 | This distribution may include materials developed by third parties. |
|---|
| 13 | |
|---|
| 14 | Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered |
|---|
| 15 | trademarks of Sun Microsystems, Inc. in the U.S. and other countries. |
|---|
| 16 | ******************************************************************************) |
|---|
| 17 | |
|---|
| 18 | component CompilerBuiltin |
|---|
| 19 | import java com.sun.fortress.nativeHelpers.{simplePrintln.nativePrintln => jPrintln} |
|---|
| 20 | import java com.sun.fortress.nativeHelpers.{simpleConcatenate.nativeConcatenate => jConcatenate, |
|---|
| 21 | simpleConcatenate.nativeStrlen => jStrlen} |
|---|
| 22 | import java com.sun.fortress.nativeHelpers.{simpleIntArith.intToString => jIntToString, |
|---|
| 23 | simpleIntArith.intAdd => jIntAdd, |
|---|
| 24 | simpleIntArith.intSub => jIntSub, |
|---|
| 25 | simpleIntArith.intMul => jIntMul, |
|---|
| 26 | simpleIntArith.intDiv => jIntDiv, |
|---|
| 27 | simpleIntArith.intLT => jIntLT, |
|---|
| 28 | simpleIntArith.intLE => jIntLE, |
|---|
| 29 | simpleIntArith.intGT => jIntGT, |
|---|
| 30 | simpleIntArith.intGE => jIntGE, |
|---|
| 31 | simpleIntArith.intEQ => jIntEQ, |
|---|
| 32 | simpleIntArith.intNeg => jIntNeg, |
|---|
| 33 | simpleIntArith.parseInt => jParseInt, |
|---|
| 34 | simpleIntArith.intAbs => jIntAbs, |
|---|
| 35 | simpleIntArith.longToInt => jLongToInt} |
|---|
| 36 | import java com.sun.fortress.nativeHelpers.{simpleDoubleArith.doubleToString => jDoubleToString, |
|---|
| 37 | simpleDoubleArith.doubleAdd => jDoubleAdd, |
|---|
| 38 | simpleDoubleArith.doubleSub => jDoubleSub, |
|---|
| 39 | simpleDoubleArith.doubleMul => jDoubleMul, |
|---|
| 40 | simpleDoubleArith.doubleDiv => jDoubleDiv, |
|---|
| 41 | simpleDoubleArith.doubleLT => jDoubleLT, |
|---|
| 42 | simpleDoubleArith.doubleLE => jDoubleLE, |
|---|
| 43 | simpleDoubleArith.doubleGT => jDoubleGT, |
|---|
| 44 | simpleDoubleArith.doubleGE => jDoubleGE, |
|---|
| 45 | simpleDoubleArith.doubleEQ => jDoubleEQ, |
|---|
| 46 | simpleDoubleArith.doubleNeg => jDoubleNeg, |
|---|
| 47 | simpleDoubleArith.parseDouble => jParseDouble, |
|---|
| 48 | simpleDoubleArith.doubleAbs => jDoubleAbs, |
|---|
| 49 | simpleDoubleArith.doublePow => jDoublePow, |
|---|
| 50 | simpleDoubleArith.doubleNanoTime => jNanoTime} |
|---|
| 51 | import java com.sun.fortress.nativeHelpers.{LocalRandom.localRandomDouble => jRandomDouble, |
|---|
| 52 | LocalRandom.localRandomInt => jRandomInt } |
|---|
| 53 | import AnyType.{Any} |
|---|
| 54 | export CompilerBuiltin |
|---|
| 55 | |
|---|
| 56 | nanoTime(): RR64 = jNanoTime() |
|---|
| 57 | |
|---|
| 58 | trait Object extends Any |
|---|
| 59 | end Object |
|---|
| 60 | |
|---|
| 61 | trait String |
|---|
| 62 | getter isEmpty(): Boolean = (jStrlen(self) = 0) |
|---|
| 63 | getter asString(): String = self |
|---|
| 64 | opr |self| : ZZ32 = jStrlen(self) |
|---|
| 65 | opr ||(self, b:String): String = jConcatenate(self, b) |
|---|
| 66 | opr juxtaposition(self, b:String): String = jConcatenate(self, b) |
|---|
| 67 | end |
|---|
| 68 | |
|---|
| 69 | object FlatString extends String |
|---|
| 70 | end FlatString |
|---|
| 71 | |
|---|
| 72 | println(s:String):() = jPrintln(s) |
|---|
| 73 | println(x:ZZ32):() = jPrintln(x.asString) |
|---|
| 74 | println(x:ZZ64):() = jPrintln(x.asString) |
|---|
| 75 | (* println(x:RR32):() = jPrintln(x.asString) *) |
|---|
| 76 | println(x:RR64):() = jPrintln(x.asString) |
|---|
| 77 | |
|---|
| 78 | strToInt(s:String):ZZ32 = jParseInt(s) |
|---|
| 79 | |
|---|
| 80 | trait Number excludes { String } |
|---|
| 81 | getter asString(): String |
|---|
| 82 | end |
|---|
| 83 | |
|---|
| 84 | trait ZZ64 extends Number excludes { RR64 } |
|---|
| 85 | getter asZZ32(): ZZ32 = jLongToInt(self) |
|---|
| 86 | end |
|---|
| 87 | |
|---|
| 88 | trait ZZ32 extends Number excludes { ZZ64, RR64 } |
|---|
| 89 | coerce(x: IntLiteral) = x.asZZ32 |
|---|
| 90 | getter asZZ32(): ZZ32 = self |
|---|
| 91 | getter asString(): String = jIntToString(self) |
|---|
| 92 | opr |self| : ZZ32 = jIntAbs(self) |
|---|
| 93 | opr -(self): ZZ32 = jIntNeg(self) |
|---|
| 94 | opr +(self, other:ZZ32): ZZ32 = jIntAdd(self,other) |
|---|
| 95 | opr -(self, other:ZZ32): ZZ32 = jIntSub(self,other) |
|---|
| 96 | opr <(self, other:ZZ32): Boolean = jIntLT(self,other) |
|---|
| 97 | opr <=(self, other:ZZ32): Boolean = jIntLE(self,other) |
|---|
| 98 | opr >(self, other:ZZ32): Boolean = jIntGT(self,other) |
|---|
| 99 | opr >=(self, other:ZZ32): Boolean = jIntGE(self,other) |
|---|
| 100 | opr =(self, other:ZZ32): Boolean = jIntEQ(self,other) |
|---|
| 101 | opr juxtaposition(self, other:ZZ32): ZZ32 = jIntMul(self,other) |
|---|
| 102 | opr DOT(self, other:ZZ32): ZZ32 = jIntMul(self,other) |
|---|
| 103 | opr DIV(self, other:ZZ32): ZZ32 = jIntDiv(self,other) |
|---|
| 104 | end |
|---|
| 105 | |
|---|
| 106 | trait IntLiteral |
|---|
| 107 | getter asZZ32(): ZZ32 |
|---|
| 108 | getter asZZ64(): ZZ64 |
|---|
| 109 | (* |
|---|
| 110 | getter asNN32(): NN32 |
|---|
| 111 | getter asZZ(): ZZ |
|---|
| 112 | getter asRR32(): RR32 |
|---|
| 113 | *) |
|---|
| 114 | getter asRR64(): RR64 |
|---|
| 115 | end |
|---|
| 116 | |
|---|
| 117 | trait RR64 extends Number |
|---|
| 118 | getter asString(): String = jDoubleToString(self) |
|---|
| 119 | opr |self| : RR64 = jDoubleAbs(self) |
|---|
| 120 | opr -(self): RR64 = jDoubleNeg(self) |
|---|
| 121 | opr +(self, other:RR64): RR64 = jDoubleAdd(self,other) |
|---|
| 122 | opr -(self, other:RR64): RR64 = jDoubleSub(self,other) |
|---|
| 123 | opr <(self, other:RR64): Boolean = jDoubleLT(self,other) |
|---|
| 124 | opr <=(self, other:RR64): Boolean = jDoubleLE(self,other) |
|---|
| 125 | opr >(self, other:RR64): Boolean = jDoubleGT(self,other) |
|---|
| 126 | opr >=(self, other:RR64): Boolean = jDoubleGE(self,other) |
|---|
| 127 | opr =(self, other:RR64): Boolean = jDoubleEQ(self,other) |
|---|
| 128 | opr juxtaposition(self, other:RR64): RR64 = jDoubleMul(self,other) |
|---|
| 129 | opr DOT(self, other:RR64): RR64 = jDoubleMul(self,other) |
|---|
| 130 | opr /(self, other:RR64): RR64 = jDoubleDiv(self,other) |
|---|
| 131 | opr ^(self, other:RR64): RR64 = jDoublePow(self,other) |
|---|
| 132 | opr ^(self, other:ZZ32): RR64 = |
|---|
| 133 | if other > 2 then |
|---|
| 134 | mid = other DIV 2 |
|---|
| 135 | if (mid + mid) = other then |
|---|
| 136 | self2 = self self |
|---|
| 137 | (self2)^mid |
|---|
| 138 | else |
|---|
| 139 | self (self^(other-1)) |
|---|
| 140 | end |
|---|
| 141 | elif other = 2 then self self |
|---|
| 142 | elif other = 1 then self |
|---|
| 143 | elif other = 0 then 1.0 |
|---|
| 144 | else (* other < 0 then *) 1.0 / (self^(-other)) |
|---|
| 145 | end |
|---|
| 146 | end |
|---|
| 147 | |
|---|
| 148 | trait RR32 extends Number |
|---|
| 149 | end |
|---|
| 150 | |
|---|
| 151 | object FloatLiteral extends RR64 |
|---|
| 152 | end |
|---|
| 153 | |
|---|
| 154 | trait Boolean |
|---|
| 155 | end |
|---|
| 156 | |
|---|
| 157 | true : Boolean = (0=0) |
|---|
| 158 | false : Boolean = (0=1) |
|---|
| 159 | |
|---|
| 160 | (************************************************************ |
|---|
| 161 | * Random numbers |
|---|
| 162 | ************************************************************) |
|---|
| 163 | |
|---|
| 164 | random(i:RR64): RR64 = jRandomDouble(i) |
|---|
| 165 | randomZZ32(x:ZZ32): ZZ32 = jRandomInt(0,x) |
|---|
| 166 | |
|---|
| 167 | end |
|---|