root/trunk/ProjectFortress/LibraryBuiltin/CompilerBuiltin.fss

Revision 4341, 7.0 KB (checked in by jmaessen, 6 days ago)

[libraries, codegen] ZZ32 now coerces IntLiteral? as described in the
spec. Over the next few weeks expect more coercions to come on line
as we gradually migrate to a flat numeric hierarchy.

This checkin in particular was a massive joint effort between myself
and the Austin folks (Eric, Justin, and Scott); everyone had their
fingers on most of the bits of code at some point in the past few
days. And now it actually works!

Line 
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
18component CompilerBuiltin
19import java com.sun.fortress.nativeHelpers.{simplePrintln.nativePrintln => jPrintln}
20import java com.sun.fortress.nativeHelpers.{simpleConcatenate.nativeConcatenate => jConcatenate,
21                                            simpleConcatenate.nativeStrlen => jStrlen}
22import 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}
36import 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}
51import java com.sun.fortress.nativeHelpers.{LocalRandom.localRandomDouble => jRandomDouble,
52                                            LocalRandom.localRandomInt => jRandomInt }
53import AnyType.{Any}
54export CompilerBuiltin
55
56nanoTime(): RR64 = jNanoTime()
57
58trait Object extends Any
59end Object
60
61trait 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)
67end
68
69object FlatString extends String
70end FlatString
71
72println(s:String):() = jPrintln(s)
73println(x:ZZ32):() = jPrintln(x.asString)
74println(x:ZZ64):() = jPrintln(x.asString)
75(* println(x:RR32):() = jPrintln(x.asString) *)
76println(x:RR64):() = jPrintln(x.asString)
77
78strToInt(s:String):ZZ32 = jParseInt(s)
79
80trait Number excludes { String }
81    getter asString(): String
82end
83
84trait ZZ64 extends Number excludes { RR64 }
85    getter asZZ32(): ZZ32 = jLongToInt(self)
86end
87
88trait 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)
104end
105
106trait 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
115end
116
117trait 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
146end
147
148trait RR32 extends Number
149end
150
151object FloatLiteral extends RR64
152end
153
154trait Boolean
155end
156
157true : Boolean = (0=0)
158false : Boolean = (0=1)
159
160(************************************************************
161* Random numbers
162************************************************************)
163
164random(i:RR64): RR64 = jRandomDouble(i)
165randomZZ32(x:ZZ32): ZZ32 = jRandomInt(0,x)
166
167end
Note: See TracBrowser for help on using the browser.