Better over- and underflow handling in the optimizer
This commit is contained in:
parent
89f57b4c07
commit
3fcdb3ec0a
|
@ -138,6 +138,7 @@ proc optimizeBinary(self: Optimizer, node: BinaryExpr): ASTNode =
|
||||||
var x, y, z: int
|
var x, y, z: int
|
||||||
discard parseInt(IntExpr(a).literal.lexeme, x)
|
discard parseInt(IntExpr(a).literal.lexeme, x)
|
||||||
discard parseInt(IntExpr(b).literal.lexeme, y)
|
discard parseInt(IntExpr(b).literal.lexeme, y)
|
||||||
|
try:
|
||||||
case node.operator.kind:
|
case node.operator.kind:
|
||||||
of Plus:
|
of Plus:
|
||||||
z = x + y
|
z = x + y
|
||||||
|
@ -161,7 +162,10 @@ proc optimizeBinary(self: Optimizer, node: BinaryExpr): ASTNode =
|
||||||
# Special case, yields a float
|
# Special case, yields a float
|
||||||
return ASTNode(FloatExpr(kind: intExpr, literal: Token(kind: Float, lexeme: $(x / y), line: IntExpr(a).literal.line, pos: (start: -1, stop: -1))))
|
return ASTNode(FloatExpr(kind: intExpr, literal: Token(kind: Float, lexeme: $(x / y), line: IntExpr(a).literal.line, pos: (start: -1, stop: -1))))
|
||||||
else:
|
else:
|
||||||
discard
|
discard # Unreachable
|
||||||
|
except OverflowDefect:
|
||||||
|
self.newWarning(valueOverflow, node)
|
||||||
|
return ASTNode(BinaryExpr(kind: binaryExpr, a: a, b: b, operator: node.operator))
|
||||||
result = ASTNode(IntExpr(kind: intExpr, literal: Token(kind: Integer, lexeme: $z, line: IntExpr(a).literal.line, pos: (start: -1, stop: -1))))
|
result = ASTNode(IntExpr(kind: intExpr, literal: Token(kind: Integer, lexeme: $z, line: IntExpr(a).literal.line, pos: (start: -1, stop: -1))))
|
||||||
elif a.kind == floatExpr or b.kind == floatExpr:
|
elif a.kind == floatExpr or b.kind == floatExpr:
|
||||||
# Optimizes float operations
|
# Optimizes float operations
|
||||||
|
|
Loading…
Reference in New Issue