STUDIO HAZE · topaz
Topaz source
Open by house policy. Topaz is our own language.
Topaz 5.21 · 50 files
Compiler sources from the Topaz 5.21.0 release. Compatibility code for earlier language modes is retained.
Publication edits are limited to explanatory comments. Executable code is unchanged.
Rights and source notices · Source manifest
compiler/crates/topaz_self_frontend/topaz/length.tpz
Topaz · 6186 B · Published file
1
import literal { validDecimalLiteral }2
import types { SyntaxNode }3
// Compiler numeric operations. Exact integers/rationals use the existing BigInt4
// runtime. This is shared by K literal formation, constant folding and emission.5
export record LengthGrid { unit: string, ticks: int }6
const OVERFLOW = "Length result is not representable"7
const ZERO = "Length division by zero"8
export function unitName(suffix: string) -> string {9
match suffix { case "pt" => "Pt"; case "mm" => "Mm"; case "cm" => "Cm"; case "in" => "In"; case "em" => "Em"; case _ => "" }10
}11
function bi(n: int) -> BigInt { n.toBigInt() }12
function abs(n: BigInt) -> BigInt { if n < bi(0) { -n } else { n } }13
function powerTwo(n: int) -> BigInt {14
let mut value = bi(1)15
let mut i = 016
while i < n { value = value * bi(2); i += 1 }17
value18
}19
export function roundFraction(n: BigInt, d: BigInt) -> Result<int, string> {20
if d == bi(0) { return Err(ZERO) }21
let negative = (n < bi(0)) != (d < bi(0))22
let numerator = abs(n)23
let denominator = abs(d)24
let mut q = numerator.div(denominator)?25
let r = numerator.mod(denominator)?26
if r * bi(2) > denominator || (r * bi(2) == denominator && q.mod(bi(2))? != bi(0)) { q = q + bi(1) }27
let signed = if negative { -q } else { q }28
match signed.toInt() { case Some(value) => Ok(value); case None => Err(OVERFLOW) }29
}30
function digit(b: int) -> bool { b >= 48 && b <= 57 }31
export function validLiteral(spelling: string) -> bool {32
let bytes = spelling.encodeUtf8()33
if bytes.length() < 3 { return false }34
let suffix = match bytes.slice(bytes.length()-2, bytes.length()).decodeUtf8() { case Ok(s) => s; case Err(_) => return false }35
if unitName(suffix) == "" { return false }36
let number = match bytes.slice(0,bytes.length()-2).decodeUtf8() { case Ok(s) => s; case Err(_) => return false }37
validDecimalLiteral(number)38
}40
export function decodeLength(spelling: string, negative: bool) -> Result<LengthGrid, string> {41
if !validLiteral(spelling) { return Err("malformed Length literal") }42
let size = spelling.encodeUtf8().length()43
let number = spelling.slice(0, size - 2).replace("_", "")44
let unit = unitName(spelling.slice(size - 2, size))45
let mut magnitude = bi(0)46
let mut denominator = bi(1)47
let mut fraction = false48
for b in number.encodeUtf8().toArray() {49
if b == 46 { fraction = true } else {50
magnitude = magnitude * bi(10) + bi(b - 48)51
if fraction { denominator = denominator * bi(10) }52
}53
}54
let multiplier = match unit { case "In" => 4718592; case "Mm" => 23592960; case "Cm" => 235929600; case _ => 65536 }55
if unit == "Mm" || unit == "Cm" { denominator = denominator * bi(127) }56
let signed = if negative { -magnitude } else { magnitude }57
Ok(LengthGrid { unit: unit, ticks: roundFraction(signed * bi(multiplier), denominator)? })58
}59
export function lengthFloatMultiply(ticks: int, scalar: float) -> Result<int, string> {60
if !scalar.isFinite() { return Err(OVERFLOW) }61
if scalar == 0.0 || ticks == 0 { return Ok(0) }62
let mut value = scalar.abs()63
let mut exponent = 064
// Powers of two are exact, including the subnormal-to-normal transition.65
while value < 1.0 { value = value * 2.0; exponent -= 1 }66
while value >= 2.0 { value = value / 2.0; exponent += 1 }67
let mantissa = value * 4503599627370496.068
let high = (mantissa / 4294967296.0).floor().toFloat()69
let low = mantissa - high * 4294967296.070
let highInt = match "{high}".replace(".0", "").toInt() { case Some(n) => n; case None => return Err(OVERFLOW) }71
let lowInt = match "{low}".replace(".0", "").toInt() { case Some(n) => n; case None => return Err(OVERFLOW) }72
let significand = bi(highInt) * bi(4294967296) + bi(lowInt)73
let mut numerator = bi(ticks) * significand74
if scalar < 0.0 { numerator = -numerator }75
let shift = exponent - 5276
if shift >= 0 { roundFraction(numerator * powerTwo(shift), bi(1)) } else { roundFraction(numerator, powerTwo(-shift)) }77
}78
export function lengthRatio(left: int, right: int) -> Result<float, string> {79
if right == 0 { return Err(ZERO) }80
if left == 0 { return Ok(if right < 0 { -0.0 } else { 0.0 }) }81
let mut n = abs(bi(left))82
let mut d = abs(bi(right))83
let mut exponent = 084
while n < d { n = n * bi(2); exponent -= 1 }85
while n >= d * bi(2) { d = d * bi(2); exponent += 1 }86
let significand = roundFraction(n * powerTwo(52), d)?87
let mut result = significand.toFloat()88
let shift = exponent - 5289
let mut i = 090
while i < shift { result = result * 2.0; i += 1 }91
while i > shift { result = result / 2.0; i -= 1 }92
Ok(if (left < 0) != (right < 0) { -result } else { result })93
}94
export function lengthResolve(ticks: int, font: int) -> Result<int, string> {95
if font <= 0 { return Err("Length.resolve font size must be positive") }96
roundFraction(bi(ticks) * bi(font), bi(65536))97
}98
export function fromMilliPoints(value: int) -> Result<int, string> { roundFraction(bi(value) * bi(65536), bi(1000)) }99
export function toMilliPoints(value: int) -> Result<int, string> { roundFraction(bi(value) * bi(1000), bi(65536)) }101
export record LengthLiteralInput { spelling: string, negative: bool }102
function operandOf(node: SyntaxNode) -> Option<SyntaxNode> {103
for value in node.children { if value.field == "operand" { return Some(value) } }104
None105
}106
function operatorOf(node: SyntaxNode) -> string {107
for value in node.attributes { if value.name == "operator" { return value.value } }108
""109
}110
export function signedLengthInput(node: SyntaxNode, source: Bytes) -> Option<LengthLiteralInput> {111
let mut value = node112
while value.kind == "expression/parenthesized" {113
value = match operandOf(value) {case Some(v)=>v;case None=>return None}114
}115
let negative = value.kind == "expression/unary" && operatorOf(value) == "minus"116
if negative {117
value = match operandOf(value) {case Some(v)=>v;case None=>return None}118
while value.kind == "expression/parenthesized" { value = match operandOf(value) {case Some(v)=>v;case None=>return None} }119
}120
if value.kind != "expression/length" { return None }121
let spelling = match source.slice(value.lo,value.hi).decodeUtf8() {case Ok(v)=>v;case Err(_)=>return None}122
Some(LengthLiteralInput {spelling:spelling,negative:negative})123
}SHA256 aa8700ff914f963437abac3cf0b8b39bbd193cb62b32775274ef295b99437e88