StdInt.icl 3.83 KB
Newer Older
1 2 3
implementation module StdInt

// ****************************************************************************************
4 5
//	Concurrent Clean Standard Library Module Version 3.0
//	Copyright 2019 University of Nijmegen
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
// ****************************************************************************************

import StdOverloaded
from StdMisc import abort
from StdBool import &&
import StdChar // import <
from StdClass import <=,not,class Ord
import StdArray

instance + Int
where
	(+) :: !Int !Int -> Int
	(+) a b
		= code inline {
			addI
		}

instance - Int
where
	(-) :: !Int !Int -> Int
	(-) a b
		= code inline {
			subI
		}

instance zero Int
where
 zero:: Int
 zero
	= code inline {
			pushI 0
	}

instance * Int
where
 (*) :: !Int !Int -> Int
 (*) a b
	= code inline {
			mulI
	}

instance / Int
where
 (/) :: !Int !Int -> Int
 (/) a b
	= code inline {
			divI
	}

instance one Int
where
 one :: Int
 one
	= code inline {
			pushI 1
	}

instance ^ Int
where
 (^) :: !Int !Int -> Int
 (^) x n
	|	n < 0
		= abort "^ (Int) called with negative power argument"
	// otherwise
		= pow x n
	where
		pow :: !Int !Int -> Int
		pow _ 0
			=	1
		pow a b
			| isEven b
				=	x * x
			// otherwise
				=	a * x * x
			where
				x
					= pow a (b >> 1)

 

instance abs Int
where
 abs::!Int -> Int
 abs x
 	| x<0
 		=	0 - x
 	// otherwise
 		=	x

instance sign Int
where
 sign::!Int -> Int
 sign x | x == 0	= 0
		| x < 0		= -1
					= 1


instance ~ Int
where
 ~ :: !Int -> Int
 ~ x
	= code inline {
			negI
	}

instance ==	Int
where
 (==) :: !Int !Int -> Bool
 (==) a b
	= code inline {
			eqI
	}

instance < Int
where
 (<) :: !Int !Int -> Bool
 (<) a b
	= code inline {
			ltI
	}

instance toInt Int
where
 toInt :: !Int -> Int //	dummy
 toInt a
	= code inline {
			no_op
	}

instance toInt Char
where
 toInt::!Char -> Int
 toInt a
	= code inline {
			CtoI
	}

instance toInt Real
where
 toInt :: !Real -> Int
 toInt a
	= code inline {
			 RtoI
	}

instance fromInt Int
where
 fromInt :: !Int -> Int
 fromInt a
	= code inline {
			no_op
	}

instance fromInt Char
where
 fromInt :: !Int -> Char
 fromInt a
	= code inline {
			ItoC
	}

instance fromInt Real
where
 fromInt :: !Int -> Real
 fromInt a
	= code inline {
			ItoR
	}

instance fromInt {#Char}
where
 fromInt :: !Int -> {#Char}
 fromInt a
	= code inline {
		.d 0 1 i
			jsr ItoAC
		.o 1 0
	}

// Additional functions for integer arithmetic: 

instance rem Int
where
189
	(rem) :: !Int !Int -> Int
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	(rem) a b
	= code inline {
			remI
	  }

instance gcd Int
where
	gcd x y    = gcdnat (abs x) (abs y)
	where
		gcdnat x 0 = x
	    gcdnat x y = gcdnat y (x rem y)


instance lcm Int
where
	lcm _ 0    = 0
	lcm 0 _    = 0
	lcm x y    = abs ((x / gcd x y) * y)

//	Test on Integers:

instance isEven Int
where
213
	isEven :: !Int -> Bool
214 215 216 217 218 219 220 221 222 223
	isEven a
		= code inline {
			pushI 1
			and%
			pushI 0
			eqI
		}

instance isOdd Int
where
224
	isOdd :: !Int -> Bool
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	isOdd a
		= code inline {
			pushI 1
			and%
			pushI 0
			eqI
			notB
		}

//	Operators on Bits:

(bitor) infixl 6 :: !Int !Int -> Int
(bitor) a b
	= code inline {
			or%
	}

(bitand) infixl 6 ::	!Int !Int -> Int
(bitand) a b
	= code inline {
			and%
	}

(bitxor) infixl 6 :: !Int !Int -> Int
(bitxor) a b
	= code inline {
			xor%
	}

(<<) infix 7 :: !Int !Int -> Int
(<<) a b
	= code inline {
			shiftl%
	}

(>>) infix 7 :: !Int !Int -> Int
(>>) a b
	= code inline {
			shiftr%
	}

instance toInt {#Char}
where
 toInt::!{#Char} -> Int
 toInt s
	| len==0
		=	0
	| neg
		=	~signedval
	| pos
		=	signedval
	// otherwise
		=	other
 where
	len	=	size s
	neg	=	s.[0] == '-'
	pos	=	s.[0] == '+'

	signedval
			=	toInt2 1 0 s
	other
			=	toInt2 0 0 s

	toInt2:: !Int !Int !{#Char} -> Int
	toInt2 posn val s
		|	len==posn		=	val
		|	isdigit			=	toInt2 (posn+1) (n+val*10) s
		//	otherwise
							=	0
	where
		n 		=	toInt (s.[posn]) - toInt '0'
		isdigit	=	0<=n && n<= 9

bitnot :: !Int -> Int
bitnot a
	= code inline {
			not%
	}