Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework numeric types #86

Merged
merged 11 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 48 additions & 62 deletions SparrowImplicitLib/bitOper.spr
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,58 @@ using oper_precedence_!<<! = bitOperPrecedence+30
using oper_precedence_!>>! = bitOperPrecedence+30

[autoCt]
[native("_bit_neg_8")] fun pre_!~!(n: Byte): Byte
[native("_bit_shl_8")] fun !<<!(n, k: Byte): Byte
[native("_bit_ashr_8")] fun !>>!(n, k: Byte): Byte
[native("_bit_xor_8")] fun !^!(n1, n2: Byte): Byte
[native("_bit_or_8")] fun !|!(n1, n2: Byte): Byte
[native("_bit_and_8")] fun !&!(n1, n2: Byte): Byte
[native("_bit_neg_8")] fun pre_!~!(n: Int8): Int8
[native("_bit_shl_8")] fun !<<!(n, k: Int8): Int8
[native("_bit_ashr_8")] fun !>>!(n, k: Int8): Int8
[native("_bit_xor_8")] fun !^!(n1, n2: Int8): Int8
[native("_bit_or_8")] fun !|!(n1, n2: Int8): Int8
[native("_bit_and_8")] fun !&!(n1, n2: Int8): Int8

[native("_bit_neg_8")] fun pre_!~!(n: UByte): UByte
[native("_bit_shl_8")] fun !<<!(n, k: UByte): UByte
[native("_bit_lshr_8")] fun !>>!(n, k: UByte): UByte
[native("_bit_xor_8")] fun !^!(n1, n2: UByte): UByte
[native("_bit_or_8")] fun !|!(n1, n2: UByte): UByte
[native("_bit_and_8")] fun !&!(n1, n2: UByte): UByte
[native("_bit_neg_8")] fun pre_!~!(n: UInt8): UInt8
[native("_bit_shl_8")] fun !<<!(n, k: UInt8): UInt8
[native("_bit_lshr_8")] fun !>>!(n, k: UInt8): UInt8
[native("_bit_xor_8")] fun !^!(n1, n2: UInt8): UInt8
[native("_bit_or_8")] fun !|!(n1, n2: UInt8): UInt8
[native("_bit_and_8")] fun !&!(n1, n2: UInt8): UInt8

[native("_bit_neg_16")] fun pre_!~!(n: Short): Short
[native("_bit_shl_16")] fun !<<!(n, k: Short): Short
[native("_bit_ashr_16")] fun !>>!(n, k: Short): Short
[native("_bit_xor_16")] fun !^!(n1, n2: Short): Short
[native("_bit_or_16")] fun !|!(n1, n2: Short): Short
[native("_bit_and_16")] fun !&!(n1, n2: Short): Short
[native("_bit_neg_16")] fun pre_!~!(n: Int16): Int16
[native("_bit_shl_16")] fun !<<!(n, k: Int16): Int16
[native("_bit_ashr_16")] fun !>>!(n, k: Int16): Int16
[native("_bit_xor_16")] fun !^!(n1, n2: Int16): Int16
[native("_bit_or_16")] fun !|!(n1, n2: Int16): Int16
[native("_bit_and_16")] fun !&!(n1, n2: Int16): Int16

[native("_bit_neg_16")] fun pre_!~!(n: UShort): UShort
[native("_bit_shl_16")] fun !<<!(n, k: UShort): UShort
[native("_bit_lshr_16")] fun !>>!(n, k: UShort): UShort
[native("_bit_xor_16")] fun !^!(n1, n2: UShort): UShort
[native("_bit_or_16")] fun !|!(n1, n2: UShort): UShort
[native("_bit_and_16")] fun !&!(n1, n2: UShort): UShort
[native("_bit_neg_16")] fun pre_!~!(n: UInt16): UInt16
[native("_bit_shl_16")] fun !<<!(n, k: UInt16): UInt16
[native("_bit_lshr_16")] fun !>>!(n, k: UInt16): UInt16
[native("_bit_xor_16")] fun !^!(n1, n2: UInt16): UInt16
[native("_bit_or_16")] fun !|!(n1, n2: UInt16): UInt16
[native("_bit_and_16")] fun !&!(n1, n2: UInt16): UInt16

[native("_bit_neg_32")] fun pre_!~!(n: Int): Int
[native("_bit_shl_32")] fun !<<!(n, k: Int): Int
[native("_bit_ashr_32")] fun !>>!(n, k: Int): Int
[native("_bit_xor_32")] fun !^!(n1, n2: Int): Int
[native("_bit_or_32")] fun !|!(n1, n2: Int): Int
[native("_bit_and_32")] fun !&!(n1, n2: Int): Int
[native("_bit_neg_32")] fun pre_!~!(n: Int32): Int32
[native("_bit_shl_32")] fun !<<!(n, k: Int32): Int32
[native("_bit_ashr_32")] fun !>>!(n, k: Int32): Int32
[native("_bit_xor_32")] fun !^!(n1, n2: Int32): Int32
[native("_bit_or_32")] fun !|!(n1, n2: Int32): Int32
[native("_bit_and_32")] fun !&!(n1, n2: Int32): Int32

[native("_bit_neg_32")] fun pre_!~!(n: UInt): UInt
[native("_bit_shl_32")] fun !<<!(n, k: UInt): UInt
[native("_bit_lshr_32")] fun !>>!(n, k: UInt): UInt
[native("_bit_xor_32")] fun !^!(n1, n2: UInt): UInt
[native("_bit_or_32")] fun !|!(n1, n2: UInt): UInt
[native("_bit_and_32")] fun !&!(n1, n2: UInt): UInt
[native("_bit_neg_32")] fun pre_!~!(n: UInt32): UInt32
[native("_bit_shl_32")] fun !<<!(n, k: UInt32): UInt32
[native("_bit_lshr_32")] fun !>>!(n, k: UInt32): UInt32
[native("_bit_xor_32")] fun !^!(n1, n2: UInt32): UInt32
[native("_bit_or_32")] fun !|!(n1, n2: UInt32): UInt32
[native("_bit_and_32")] fun !&!(n1, n2: UInt32): UInt32

[native("_bit_neg_64")] fun pre_!~!(n: Long): Long
[native("_bit_shl_64")] fun !<<!(n, k: Long): Long
[native("_bit_ashr_64")] fun !>>!(n, k: Long): Long
[native("_bit_xor_64")] fun !^!(n1, n2: Long): Long
[native("_bit_or_64")] fun !|!(n1, n2: Long): Long
[native("_bit_and_64")] fun !&!(n1, n2: Long): Long
[native("_bit_neg_64")] fun pre_!~!(n: Int64): Int64
[native("_bit_shl_64")] fun !<<!(n, k: Int64): Int64
[native("_bit_ashr_64")] fun !>>!(n, k: Int64): Int64
[native("_bit_xor_64")] fun !^!(n1, n2: Int64): Int64
[native("_bit_or_64")] fun !|!(n1, n2: Int64): Int64
[native("_bit_and_64")] fun !&!(n1, n2: Int64): Int64

[native("_bit_neg_64")] fun pre_!~!(n: ULong): ULong
[native("_bit_shl_64")] fun !<<!(n, k: ULong): ULong
[native("_bit_lshr_64")] fun !>>!(n, k: ULong): ULong
[native("_bit_xor_64")] fun !^!(n1, n2: ULong): ULong
[native("_bit_or_64")] fun !|!(n1, n2: ULong): ULong
[native("_bit_and_64")] fun !&!(n1, n2: ULong): ULong

[native("_bit_neg_64")] fun pre_!~!(n: SizeType): SizeType
[native("_bit_shl_64")] fun !<<!(n, k: SizeType): SizeType
[native("_bit_ashr_64")] fun !>>!(n, k: SizeType): SizeType
[native("_bit_xor_64")] fun !^!(n1, n2: SizeType): SizeType
[native("_bit_or_64")] fun !|!(n1, n2: SizeType): SizeType
[native("_bit_and_64")] fun !&!(n1, n2: SizeType): SizeType

[native("_bit_neg_64")] fun pre_!~!(n: DiffType): DiffType
[native("_bit_shl_64")] fun !<<!(n, k: DiffType): DiffType
[native("_bit_lshr_64")] fun !>>!(n, k: DiffType): DiffType
[native("_bit_xor_64")] fun !^!(n1, n2: DiffType): DiffType
[native("_bit_or_64")] fun !|!(n1, n2: DiffType): DiffType
[native("_bit_and_64")] fun !&!(n1, n2: DiffType): DiffType
[native("_bit_neg_64")] fun pre_!~!(n: UInt64): UInt64
[native("_bit_shl_64")] fun !<<!(n, k: UInt64): UInt64
[native("_bit_lshr_64")] fun !>>!(n, k: UInt64): UInt64
[native("_bit_xor_64")] fun !^!(n1, n2: UInt64): UInt64
[native("_bit_or_64")] fun !|!(n1, n2: UInt64): UInt64
[native("_bit_and_64")] fun !&!(n1, n2: UInt64): UInt64
36 changes: 17 additions & 19 deletions SparrowImplicitLib/check.spr
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ import std.function(Function)
[initCtor]
datatype Gen(t: Type)
using ValueType = t
_generator: Function(t, UInt)
using _isGenerator = true
_generator: Function(t, Int)

concept _Generator(x) if isValid(x(UInt(0)))
concept _Generator(x) if isValid(x(0))

fun mkGen(t: Type, generator: _Generator): Gen(t)
using FunType = Function(t, UInt)
using FunType = Function(t, Int)
return Gen(t)(FunType(generator))

fun ()(this: !Gen, sizeHint: UInt): ValueType = _generator(sizeHint)
fun ()(this: !Gen, sizeHint: Int): ValueType = _generator(sizeHint)

fun isValid(this: Gen): Bool = _generator isSet

concept GenType(x) \
if typeOf(x.ValueType) == Type \
&& typeOf(x.isValid) == Bool \
&& typeOf(x(0)) == x.ValueType
concept GenType(x) if x._isGenerator

//! Arbitrary prototype
//fun arbitrary(t: Type): t Gen
Expand All @@ -35,7 +33,7 @@ fun arbitrary(t: Type): t Gen if Integer(#$t)
//! Configuration options for our check function
[initCtor]
datatype CheckConfig
numTests, maxAttempts: UInt
numTests, maxAttempts: Int
isVerbose: Bool
fun ctor(this: !CheckConfig)
numTests = 100
Expand Down Expand Up @@ -76,7 +74,7 @@ fun check(values: TestableValuesRange, predMsg: AnyType): Bool \
if isValid(predMsg.v1)
fun check(values: TestableValuesRange, pred: AnyType, msg: StringRef, config: CheckConfig = CheckConfig()): Bool
cout << "* Checking " << msg << endl
var classes: Map(String, UInt)
var classes: Map(String, Int)

var numTrivial = 0
var n = 0
Expand Down Expand Up @@ -140,7 +138,7 @@ concept FreqGenType(x) if(
)

//! Returns a generator that generates elements from the range
fun elements(r: Range): r.RetType Gen = _Impl.ElementsGen(typeOf(r))(r).gen
fun elements(r: Range): r.RetType Gen = tmpToMut(_Impl.ElementsGen(typeOf(r))(r)).gen

//! Given a set of generators, this will randomly chose one of them
fun oneOf(gens: Range): gens.RetType if GenType(#$gens.RetType)
Expand Down Expand Up @@ -171,19 +169,19 @@ fun frequency(gens: Range): typeOf((#$gens.RetType).v2) if FreqGenType(#$gens.Re

fun randBetween(min, max: Integer): commonType(typeOf(min), typeOf(max))
using T = commonType(typeOf(min), typeOf(max))
using RAND_MAX = 0x7fffffff
let offset: T = T(Double(_Impl.rand()) / RAND_MAX * Double(max - min))
using RAND_MAX = Float(0x7fffffff)
let offset: T = T(Float(_Impl.rand()) / RAND_MAX * Float(max - min))
return T(min) + offset

package _Impl
[native("rand")]
fun rand(): UInt
fun rand(): UInt32

package NumericGen(t: Type) if UnsignedInteger(#$t)
fun gen(sizeHint: UInt) = t(randBetween(0, 6 + sizeHint))
fun gen(sizeHint: Int) = t(randBetween(0, 6 + sizeHint))

package NumericGen(t: Type) if SignedInteger(#$t)
fun gen(sizeHint: UInt): t
fun gen(sizeHint: Int): t
let h: Int = sizeHint + 3
return t(randBetween(-h, h+1))

Expand All @@ -192,7 +190,7 @@ package _Impl
using _T = t

_gen: Gen(t)
_count: UInt
_count: Int

fun ctor(this, other: !ForAllGenerator)
this._gen ctor other._gen
Expand Down Expand Up @@ -278,13 +276,13 @@ package _Impl
using _RangeType = rangeType

_range: rangeType
_count: SizeType
_count: Int

fun ctor(this: !ElementsGen, range: this._RangeType)
this._range ctor range
this._count ctor (range rangeSize)

fun ()(this: !ElementsGen, sizeHint: UInt): ElementType
fun ()(this: !ElementsGen, sizeHint: Int): ElementType
let r = _range
r advance randBetween(0, Int(_count))
return r.front
Expand Down
2 changes: 1 addition & 1 deletion SparrowImplicitLib/logic/lRef.spr
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fun reset(this: !LRef, value: !this.ValueType)
cout << "LRef.reset " << this << "\n"

fun isUnique(this: !LRef) = _pptr.isUnique
fun useCount(this: !LRef): UInt = _pptr.useCount
fun useCount(this: !LRef): Int = _pptr.useCount

fun isNull(this: !LRef) = _pptr.get().isNull
fun isSet(this: !LRef) = _pptr.get().isSet
Expand Down
2 changes: 1 addition & 1 deletion SparrowImplicitLib/logic/prologImpl/errorReporter.spr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fun _printLocation(this: !ConsoleErrorReporter, os: !OutStream, loc: Location)
os << '>'
var endCol = loc.end.col
if ( loc.start.line != loc.end.line )
endCol = UInt(line.size()+1);
endCol = line.size()+1;
if ( loc.start.col == endCol )
++endCol;
for i = 1..loc.start.col
Expand Down
8 changes: 4 additions & 4 deletions SparrowImplicitLib/logic/prologImpl/ir.spr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using allowNativeCodeGen = true
for pptr: @PredicateClausePtr = program.get().predicates.all
let p: @PredicateClause = pptr.get
let name: @String = p.name
let arity: UInt = p.args.size
let arity = p.args.size

// Search for an existing predicate with same name and same arity
let r = findIf(predicates.all,
Expand All @@ -45,7 +45,7 @@ using allowNativeCodeGen = true

datatype SparrowPredicate
name: String
arity: UInt
arity: Int
firstClause: PredicateClausePtr
paramVars: Vector(Parameter)
localVars: Vector(ExpressionPtr)
Expand All @@ -55,7 +55,7 @@ using allowNativeCodeGen = true

fun ctor(this: !SparrowPredicate, firstClause: !PredicateClausePtr)
this.name ctor firstClause.get().name
this.arity ctor UInt(firstClause.get().args.size)
this.arity ctor firstClause.get().args.size
this.firstClause ctor firstClause
this.paramVars ctor
this.localVars ctor
Expand Down Expand Up @@ -207,7 +207,7 @@ using allowNativeCodeGen = true

// A name (param / local var) cannot be twice in an 'is' condition
let varName: @String = left.data
let idx: DiffType = indexOf(isVars.all, left.data)
let idx = indexOf(isVars.all, left.data)
if ( idx >= 0 )
canBeNative = false
return
Expand Down
64 changes: 32 additions & 32 deletions SparrowImplicitLib/math.spr
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
module math

[native("llvm.sqrt.f32")] fun sqrt(x: Float): Float
[native("llvm.sin.f32")] fun sin(x: Float): Float
[native("llvm.cos.f32")] fun cos(x: Float): Float
[native("llvm.powi.f32")] fun powi(x: Float, y: Int): Float
[native("llvm.pow.f32")] fun pow(x, y: Float): Float
[native("llvm.exp.f32")] fun exp(x: Float): Float
[native("llvm.exp2.f32")] fun exp2(x: Float): Float
[native("llvm.log.f32")] fun log(x: Float): Float
[native("llvm.log10.f32")] fun log10(x: Float): Float
[native("llvm.log2.f32")] fun log2(x: Float): Float
[native("llvm.fabs.f32")] fun fabs(x: Float): Float
[native("llvm.floor.f32")] fun floor(x: Float): Float
[native("llvm.ceil.f32")] fun ceil(x: Float): Float
[native("llvm.truc.f32")] fun truc(x: Float): Float
[native("llvm.rint.f32")] fun rint(x: Float): Float
[native("llvm.rint.f32")] fun nearbyint(x: Float): Float
[native("llvm.sqrt.f32")] fun sqrt(x: Float32): Float32
[native("llvm.sin.f32")] fun sin(x: Float32): Float32
[native("llvm.cos.f32")] fun cos(x: Float32): Float32
[native("llvm.powi.f32")] fun powi(x: Float32, y: Int): Float32
[native("llvm.pow.f32")] fun pow(x, y: Float32): Float32
[native("llvm.exp.f32")] fun exp(x: Float32): Float32
[native("llvm.exp2.f32")] fun exp2(x: Float32): Float32
[native("llvm.log.f32")] fun log(x: Float32): Float32
[native("llvm.log10.f32")] fun log10(x: Float32): Float32
[native("llvm.log2.f32")] fun log2(x: Float32): Float32
[native("llvm.fabs.f32")] fun fabs(x: Float32): Float32
[native("llvm.floor.f32")] fun floor(x: Float32): Float32
[native("llvm.ceil.f32")] fun ceil(x: Float32): Float32
[native("llvm.truc.f32")] fun truc(x: Float32): Float32
[native("llvm.rint.f32")] fun rint(x: Float32): Float32
[native("llvm.rint.f32")] fun nearbyint(x: Float32): Float32

[native("llvm.sqrt.f64")] fun sqrt(x: Double): Double
[native("llvm.sin.f64")] fun sin(x: Double): Double
[native("llvm.cos.f64")] fun cos(x: Double): Double
[native("llvm.powi.f64")] fun powi(x: Double, y: Int): Double
[native("llvm.pow.f64")] fun pow(x, y: Double): Double
[native("llvm.exp.f64")] fun exp(x: Double): Double
[native("llvm.exp2.f64")] fun exp2(x: Double): Double
[native("llvm.log.f64")] fun log(x: Double): Double
[native("llvm.log10.f64")] fun log10(x: Double): Double
[native("llvm.log2.f64")] fun log2(x: Double): Double
[native("llvm.fabs.f64")] fun fabs(x: Double): Double
[native("llvm.floor.f64")] fun floor(x: Double): Double
[native("llvm.ceil.f64")] fun ceil(x: Double): Double
[native("llvm.truc.f64")] fun truc(x: Double): Double
[native("llvm.rint.f64")] fun rint(x: Double): Double
[native("llvm.rint.f64")] fun nearbyint(x: Double): Double
[native("llvm.sqrt.f64")] fun sqrt(x: Float64): Float64
[native("llvm.sin.f64")] fun sin(x: Float64): Float64
[native("llvm.cos.f64")] fun cos(x: Float64): Float64
[native("llvm.powi.f64")] fun powi(x: Float64, y: Int): Float64
[native("llvm.pow.f64")] fun pow(x, y: Float64): Float64
[native("llvm.exp.f64")] fun exp(x: Float64): Float64
[native("llvm.exp2.f64")] fun exp2(x: Float64): Float64
[native("llvm.log.f64")] fun log(x: Float64): Float64
[native("llvm.log10.f64")] fun log10(x: Float64): Float64
[native("llvm.log2.f64")] fun log2(x: Float64): Float64
[native("llvm.fabs.f64")] fun fabs(x: Float64): Float64
[native("llvm.floor.f64")] fun floor(x: Float64): Float64
[native("llvm.ceil.f64")] fun ceil(x: Float64): Float64
[native("llvm.truc.f64")] fun truc(x: Float64): Float64
[native("llvm.rint.f64")] fun rint(x: Float64): Float64
[native("llvm.rint.f64")] fun nearbyint(x: Float64): Float64

fun abs(x: Integer) = ife(x<0, -x, typeOf(x)(x))
8 changes: 4 additions & 4 deletions SparrowImplicitLib/meta/location.spr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module meta.location
/// A line + column pair, indicating a position in a file
[bitcopiable]
datatype LineCol
line, col: UInt
line, col: Int

/// Type describing a source code; for now we just use an untyped Ptr
[bitcopiable]
Expand All @@ -22,7 +22,7 @@ datatype Location
//fun ctor(this: @Location)
// setOne(this)

fun mkLineCol(line, col: UInt): LineCol
fun mkLineCol(line, col: Int): LineCol
let res: !LineCol
res.line = line
res.col = col
Expand Down Expand Up @@ -58,11 +58,11 @@ fun stepOver(l: !Location)
l.start = l.end

/// Add the given number of columns to the end position; start position remains unchanged
fun addColumns(l: !Location, count: UInt)
fun addColumns(l: !Location, count: Int)
l.end.col += count

/// Add the given number of lines to the end position; start position remains unchanged
fun addLines(l: !Location, count: UInt)
fun addLines(l: !Location, count: Int)
l.end.line += count
l.end.col = 1

Expand Down
Loading