# Copyright 2007-2008 Kevin Reid, under the terms of the MIT X license # found at http://www.opensource.org/licenses/mit-license.html ................ pragma.syntax("0.9") def unsignedIntegerCoding :DeepFrozen := def signedIntegerCoding implements DeepFrozen, ExitViaHere { to get(bitCount :(int >= 0)) { def bitCountD :int := bitCount # XXX interim def unsignedIntegerSpecificCoding :DeepFrozen := unsignedIntegerCoding[bitCount] def valueBitCount :int := bitCount - 1 def SignedIntegerSpecific :DeepFrozen := ((-(2**valueBitCount))..!(2**valueBitCount)) return def signedIntegerSpecificCoding implements DeepFrozen { # implements Binary to asType() { return SignedIntegerSpecific } to writingTo(out) { # , schedule def writeU := unsignedIntegerSpecificCoding.writingTo(out) return def writer(value :SignedIntegerSpecific) { writeU(value & (2**bitCountD-1)) } } to takingFrom(ins, schedule) { def takeU := unsignedIntegerSpecificCoding.takingFrom(ins, schedule) def signMask := (1 << (bitCountD-1)) return def taker() :(schedule.result(SignedIntegerSpecific)) { return schedule.whenResolved(takeU(), fn u { u - ((u & signMask) << 1) }) } } } } }