mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-17 06:21:18 +00:00
Merge remote-tracking branch 'nim-lang/devel' into emscripten-support
This commit is contained in:
@@ -1588,6 +1588,14 @@ proc makeStmtList*(n: PNode): PNode =
|
||||
result = newNodeI(nkStmtList, n.info)
|
||||
result.add n
|
||||
|
||||
proc skipStmtList*(n: PNode): PNode =
|
||||
if n.kind in {nkStmtList, nkStmtListExpr}:
|
||||
for i in 0 .. n.len-2:
|
||||
if n[i].kind notin {nkEmpty, nkCommentStmt}: return n
|
||||
result = n.lastSon
|
||||
else:
|
||||
result = n
|
||||
|
||||
proc createMagic*(name: string, m: TMagic): PSym =
|
||||
result = newSym(skProc, getIdent(name), nil, unknownLineInfo())
|
||||
result.magic = m
|
||||
|
||||
@@ -62,7 +62,7 @@ proc evalTemplateArgs(n: PNode, s: PSym): PNode =
|
||||
# if the template has zero arguments, it can be called without ``()``
|
||||
# `n` is then a nkSym or something similar
|
||||
var totalParams = case n.kind
|
||||
of nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkCallStrLit: <n.len
|
||||
of nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkCallStrLit: n.len-1
|
||||
else: 0
|
||||
|
||||
var
|
||||
@@ -90,8 +90,7 @@ proc evalTemplateArgs(n: PNode, s: PSym): PNode =
|
||||
# not supplied by the user
|
||||
for i in givenRegularParams+1 .. expectedRegularParams:
|
||||
let default = s.typ.n.sons[i].sym.ast
|
||||
internalAssert default != nil
|
||||
if default.kind == nkEmpty:
|
||||
if default.isNil or default.kind == nkEmpty:
|
||||
localError(n.info, errWrongNumberOfArguments)
|
||||
addSon(result, ast.emptyNode)
|
||||
else:
|
||||
|
||||
@@ -37,6 +37,7 @@ const
|
||||
someMod = {mModI}
|
||||
someMax = {mMaxI, mMaxF64}
|
||||
someMin = {mMinI, mMinF64}
|
||||
someBinaryOp = someAdd+someSub+someMul+someMax+someMin
|
||||
|
||||
proc isValue(n: PNode): bool = n.kind in {nkCharLit..nkNilLit}
|
||||
proc isLocation(n: PNode): bool = not n.isValue
|
||||
@@ -165,11 +166,21 @@ proc `|+|`(a, b: PNode): PNode =
|
||||
if a.kind in {nkCharLit..nkUInt64Lit}: result.intVal = a.intVal |+| b.intVal
|
||||
else: result.floatVal = a.floatVal + b.floatVal
|
||||
|
||||
proc `|-|`(a, b: PNode): PNode =
|
||||
result = copyNode(a)
|
||||
if a.kind in {nkCharLit..nkUInt64Lit}: result.intVal = a.intVal |-| b.intVal
|
||||
else: result.floatVal = a.floatVal - b.floatVal
|
||||
|
||||
proc `|*|`(a, b: PNode): PNode =
|
||||
result = copyNode(a)
|
||||
if a.kind in {nkCharLit..nkUInt64Lit}: result.intVal = a.intVal |*| b.intVal
|
||||
else: result.floatVal = a.floatVal * b.floatVal
|
||||
|
||||
proc `|div|`(a, b: PNode): PNode =
|
||||
result = copyNode(a)
|
||||
if a.kind in {nkCharLit..nkUInt64Lit}: result.intVal = a.intVal div b.intVal
|
||||
else: result.floatVal = a.floatVal / b.floatVal
|
||||
|
||||
proc negate(a, b, res: PNode): PNode =
|
||||
if b.kind in {nkCharLit..nkUInt64Lit} and b.intVal != low(BiggestInt):
|
||||
var b = copyNode(b)
|
||||
@@ -213,10 +224,16 @@ proc reassociation(n: PNode): PNode =
|
||||
if result[2].isValue and
|
||||
result[1].getMagic in someAdd and result[1][2].isValue:
|
||||
result = opAdd.buildCall(result[1][1], result[1][2] |+| result[2])
|
||||
if result[2].intVal == 0:
|
||||
result = result[1]
|
||||
of someMul:
|
||||
if result[2].isValue and
|
||||
result[1].getMagic in someMul and result[1][2].isValue:
|
||||
result = opAdd.buildCall(result[1][1], result[1][2] |*| result[2])
|
||||
result = opMul.buildCall(result[1][1], result[1][2] |*| result[2])
|
||||
if result[2].intVal == 1:
|
||||
result = result[1]
|
||||
elif result[2].intVal == 0:
|
||||
result = zero()
|
||||
else: discard
|
||||
|
||||
proc pred(n: PNode): PNode =
|
||||
@@ -234,7 +251,7 @@ proc canon*(n: PNode): PNode =
|
||||
result.sons[i] = canon(n.sons[i])
|
||||
elif n.kind == nkSym and n.sym.kind == skLet and
|
||||
n.sym.ast.getMagic in (someEq + someAdd + someMul + someMin +
|
||||
someMax + someHigh + {mUnaryLt} + someSub + someLen):
|
||||
someMax + someHigh + {mUnaryLt} + someSub + someLen + someDiv):
|
||||
result = n.sym.ast.copyTree
|
||||
else:
|
||||
result = n
|
||||
@@ -248,7 +265,7 @@ proc canon*(n: PNode): PNode =
|
||||
# high == len+(-1)
|
||||
result = opAdd.buildCall(opLen.buildCall(result[1]), minusOne())
|
||||
of mUnaryLt:
|
||||
result = buildCall(opAdd, result[1], newIntNode(nkIntLit, -1))
|
||||
result = buildCall(opAdd, result[1], minusOne())
|
||||
of someSub:
|
||||
# x - 4 --> x + (-4)
|
||||
result = negate(result[1], result[2], result)
|
||||
@@ -294,6 +311,16 @@ proc canon*(n: PNode): PNode =
|
||||
if plus != nil and not isLetLocation(x, true):
|
||||
result = buildCall(result[0].sym, plus, y[1])
|
||||
else: discard
|
||||
elif x.isValue and y.getMagic in someAdd and y[2].isValue:
|
||||
# 0 <= a.len + 3
|
||||
# -3 <= a.len
|
||||
result.sons[1] = x |-| y[2]
|
||||
result.sons[2] = y[1]
|
||||
elif x.isValue and y.getMagic in someSub and y[2].isValue:
|
||||
# 0 <= a.len - 3
|
||||
# 3 <= a.len
|
||||
result.sons[1] = x |+| y[2]
|
||||
result.sons[2] = y[1]
|
||||
else: discard
|
||||
|
||||
proc `+@`*(a: PNode; b: BiggestInt): PNode =
|
||||
@@ -313,6 +340,9 @@ proc usefulFact(n: PNode): PNode =
|
||||
if isLetLocation(n.sons[1], true) or isLetLocation(n.sons[2], true):
|
||||
# XXX algebraic simplifications! 'i-1 < a.len' --> 'i < a.len+1'
|
||||
result = n
|
||||
elif n[1].getMagic in someLen or n[2].getMagic in someLen:
|
||||
# XXX Rethink this whole idea of 'usefulFact' for semparallel
|
||||
result = n
|
||||
of mIsNil:
|
||||
if isLetLocation(n.sons[1], false) or isVar(n.sons[1]):
|
||||
result = n
|
||||
@@ -366,8 +396,8 @@ proc usefulFact(n: PNode): PNode =
|
||||
type
|
||||
TModel* = seq[PNode] # the "knowledge base"
|
||||
|
||||
proc addFact*(m: var TModel, n: PNode) =
|
||||
let n = usefulFact(n)
|
||||
proc addFact*(m: var TModel, nn: PNode) =
|
||||
let n = usefulFact(nn)
|
||||
if n != nil: m.add n
|
||||
|
||||
proc addFactNeg*(m: var TModel, n: PNode) =
|
||||
@@ -697,10 +727,57 @@ proc simpleSlice*(a, b: PNode): BiggestInt =
|
||||
else:
|
||||
result = -1
|
||||
|
||||
|
||||
template isMul(x): expr = x.getMagic in someMul
|
||||
template isDiv(x): expr = x.getMagic in someDiv
|
||||
template isAdd(x): expr = x.getMagic in someAdd
|
||||
template isSub(x): expr = x.getMagic in someSub
|
||||
template isVal(x): expr = x.kind in {nkCharLit..nkUInt64Lit}
|
||||
template isIntVal(x, y): expr = x.intVal == y
|
||||
|
||||
import macros
|
||||
|
||||
macro `=~`(x: PNode, pat: untyped): bool =
|
||||
proc m(x, pat, conds: NimNode) =
|
||||
case pat.kind
|
||||
of nnkInfix:
|
||||
case $pat[0]
|
||||
of "*": conds.add getAst(isMul(x))
|
||||
of "/": conds.add getAst(isDiv(x))
|
||||
of "+": conds.add getAst(isAdd(x))
|
||||
of "-": conds.add getAst(isSub(x))
|
||||
else:
|
||||
error("invalid pattern")
|
||||
m(newTree(nnkBracketExpr, x, newLit(1)), pat[1], conds)
|
||||
m(newTree(nnkBracketExpr, x, newLit(2)), pat[2], conds)
|
||||
of nnkPar:
|
||||
if pat.len == 1:
|
||||
m(x, pat[0], conds)
|
||||
else:
|
||||
error("invalid pattern")
|
||||
of nnkIdent:
|
||||
let c = newTree(nnkStmtListExpr, newLetStmt(pat, x))
|
||||
conds.add c
|
||||
if ($pat)[^1] == 'c': c.add(getAst(isVal(pat)))
|
||||
else: c.add bindSym"true"
|
||||
of nnkIntLit:
|
||||
conds.add(getAst(isIntVal(pat.intVal)))
|
||||
else:
|
||||
error("invalid pattern")
|
||||
|
||||
var conds = newTree(nnkBracket)
|
||||
m(x, pat, conds)
|
||||
result = nestList(!"and", conds)
|
||||
|
||||
|
||||
proc isMinusOne(n: PNode): bool =
|
||||
n.kind in {nkCharLit..nkUInt64Lit} and n.intVal == -1
|
||||
|
||||
proc pleViaModel(model: TModel; aa, bb: PNode): TImplication
|
||||
|
||||
proc ple(m: TModel; a, b: PNode): TImplication =
|
||||
template `<=?`(a,b): expr = ple(m,a,b) == impYes
|
||||
template `>=?`(a,b): expr = ple(m, nkIntLit.newIntNode(b), a) == impYes
|
||||
|
||||
# 0 <= 3
|
||||
if a.isValue and b.isValue:
|
||||
@@ -721,6 +798,7 @@ proc ple(m: TModel; a, b: PNode): TImplication =
|
||||
if a.intVal <= 0: return impYes
|
||||
|
||||
# x <= y+c if 0 <= c and x <= y
|
||||
# x <= y+(-c) if c <= 0 and y >= x
|
||||
if b.getMagic in someAdd and zero() <=? b[2] and a <=? b[1]: return impYes
|
||||
|
||||
# x+c <= y if c <= 0 and x <= y
|
||||
@@ -730,10 +808,44 @@ proc ple(m: TModel; a, b: PNode): TImplication =
|
||||
if b.getMagic in someMul:
|
||||
if a <=? b[1] and one() <=? b[2] and zero() <=? b[1]: return impYes
|
||||
|
||||
|
||||
if a.getMagic in someMul and a[2].isValue and a[1].getMagic in someDiv and
|
||||
a[1][2].isValue:
|
||||
# simplify (x div 4) * 2 <= y to x div (c div d) <= y
|
||||
if ple(m, buildCall(opDiv, a[1][1], `|div|`(a[1][2], a[2])), b) == impYes:
|
||||
return impYes
|
||||
|
||||
# x*3 + x == x*4. It follows that:
|
||||
# x*3 + y <= x*4 if y <= x and 3 <= 4
|
||||
if a =~ x*dc + y and b =~ x2*ec:
|
||||
if sameTree(x, x2):
|
||||
let ec1 = opAdd.buildCall(ec, minusOne())
|
||||
if x >=? 1 and ec >=? 1 and dc >=? 1 and dc <=? ec1 and y <=? x:
|
||||
return impYes
|
||||
elif a =~ x*dc and b =~ x2*ec + y:
|
||||
#echo "BUG cam ehrer e ", a, " <=? ", b
|
||||
if sameTree(x, x2):
|
||||
let ec1 = opAdd.buildCall(ec, minusOne())
|
||||
if x >=? 1 and ec >=? 1 and dc >=? 1 and dc <=? ec1 and y <=? zero():
|
||||
return impYes
|
||||
|
||||
# x+c <= x+d if c <= d. Same for *, - etc.
|
||||
if a.getMagic in someBinaryOp and a.getMagic == b.getMagic:
|
||||
if sameTree(a[1], b[1]) and a[2] <=? b[2]: return impYes
|
||||
elif sameTree(a[2], b[2]) and a[1] <=? b[1]: return impYes
|
||||
|
||||
# x div c <= y if 1 <= c and 0 <= y and x <= y:
|
||||
if a.getMagic in someDiv:
|
||||
if one() <=? a[2] and zero() <=? b and a[1] <=? b: return impYes
|
||||
|
||||
# x div c <= x div d if d <= c
|
||||
if b.getMagic in someDiv:
|
||||
if sameTree(a[1], b[1]) and b[2] <=? a[2]: return impYes
|
||||
|
||||
# x div z <= x - 1 if z <= x
|
||||
if a[2].isValue and b.getMagic in someAdd and b[2].isMinusOne:
|
||||
if a[2] <=? a[1] and sameTree(a[1], b[1]): return impYes
|
||||
|
||||
# slightly subtle:
|
||||
# x <= max(y, z) iff x <= y or x <= z
|
||||
# note that 'x <= max(x, z)' is a special case of the above rule
|
||||
@@ -769,11 +881,19 @@ proc pleViaModelRec(m: var TModel; a, b: PNode): TImplication =
|
||||
for i in 0..m.high:
|
||||
let fact = m[i]
|
||||
if fact != nil and fact.getMagic in someLe:
|
||||
# x <= y implies a <= b if a <= x and y <= b
|
||||
let x = fact[1]
|
||||
let y = fact[2]
|
||||
# mark as used:
|
||||
m[i] = nil
|
||||
# i <= len-100
|
||||
# i <=? len-1
|
||||
# --> true if (len-100) <= (len-1)
|
||||
let x = fact[1]
|
||||
let y = fact[2]
|
||||
if sameTree(x, a) and y.getMagic in someAdd and b.getMagic in someAdd and
|
||||
sameTree(y[1], b[1]):
|
||||
if ple(m, b[2], y[2]) == impYes:
|
||||
return impYes
|
||||
|
||||
# x <= y implies a <= b if a <= x and y <= b
|
||||
if ple(m, a, x) == impYes:
|
||||
if ple(m, y, b) == impYes:
|
||||
return impYes
|
||||
|
||||
@@ -859,11 +859,17 @@ proc transformOuterProc(o: POuterContext, n: PNode; it: TIter): PNode =
|
||||
return indirectAccess(newSymNode(it.closureParam), local, n.info)
|
||||
|
||||
if local.kind == skClosureIterator:
|
||||
# bug #3354; allow for
|
||||
#iterator iter(): int {.closure.}=
|
||||
# s.add(iter)
|
||||
# yield 1
|
||||
|
||||
#if local == o.fn or local == it.fn:
|
||||
# message(n.info, errRecursiveDependencyX, local.name.s)
|
||||
|
||||
# consider: [i1, i2, i1] Since we merged the iterator's closure
|
||||
# with the captured owning variables, we need to generate the
|
||||
# closure generation code again:
|
||||
if local == o.fn or local == it.fn:
|
||||
message(n.info, errRecursiveDependencyX, local.name.s)
|
||||
# XXX why doesn't this work?
|
||||
var closure = PEnv(idTableGet(o.lambdasToEnv, local))
|
||||
if closure.isNil:
|
||||
|
||||
@@ -128,10 +128,10 @@ template `?`(x): expr = x.renderTree
|
||||
proc checkLe(c: AnalysisCtx; a, b: PNode) =
|
||||
case proveLe(c.guards, a, b)
|
||||
of impUnknown:
|
||||
localError(a.info, "cannot prove: " & ?a & " <= " & ?b)
|
||||
localError(a.info, "cannot prove: " & ?a & " <= " & ?b & " (bounds check)")
|
||||
of impYes: discard
|
||||
of impNo:
|
||||
localError(a.info, "can prove: " & ?a & " > " & ?b)
|
||||
localError(a.info, "can prove: " & ?a & " > " & ?b & " (bounds check)")
|
||||
|
||||
proc checkBounds(c: AnalysisCtx; arr, idx: PNode) =
|
||||
checkLe(c, arr.lowBound, idx)
|
||||
@@ -156,19 +156,23 @@ proc addSlice(c: var AnalysisCtx; n: PNode; x, le, ri: PNode) =
|
||||
|
||||
proc overlap(m: TModel; x,y,c,d: PNode) =
|
||||
# X..Y and C..D overlap iff (X <= D and C <= Y)
|
||||
case proveLe(m, x, d)
|
||||
case proveLe(m, c, y)
|
||||
of impUnknown:
|
||||
localError(x.info,
|
||||
"cannot prove: $# > $#; required for ($#)..($#) disjoint from ($#)..($#)" %
|
||||
[?x, ?d, ?x, ?y, ?c, ?d])
|
||||
case proveLe(m, x, d)
|
||||
of impNo: discard
|
||||
of impUnknown, impYes:
|
||||
localError(x.info,
|
||||
"cannot prove: $# > $#; required for ($#)..($#) disjoint from ($#)..($#)" %
|
||||
[?c, ?y, ?x, ?y, ?c, ?d])
|
||||
of impYes:
|
||||
case proveLe(m, c, y)
|
||||
case proveLe(m, x, d)
|
||||
of impUnknown:
|
||||
localError(x.info,
|
||||
"cannot prove: $# > $#; required for ($#)..($#) disjoint from ($#)..($#)" %
|
||||
[?c, ?y, ?x, ?y, ?c, ?d])
|
||||
[?x, ?d, ?x, ?y, ?c, ?d])
|
||||
of impYes:
|
||||
localError(x.info, "($#)..($#) not disjoint from ($#)..($#)" % [?x, ?y, ?c, ?d])
|
||||
localError(x.info, "($#)..($#) not disjoint from ($#)..($#)" %
|
||||
[?c, ?y, ?x, ?y, ?c, ?d])
|
||||
of impNo: discard
|
||||
of impNo: discard
|
||||
|
||||
@@ -278,10 +282,12 @@ proc analyseCall(c: var AnalysisCtx; n: PNode; op: PSym) =
|
||||
slot.stride = min(slot.stride, incr)
|
||||
analyseSons(c, n)
|
||||
elif op.name.s == "[]" and op.fromSystem:
|
||||
c.addSlice(n, n[1], n[2][1], n[2][2])
|
||||
let slice = n[2].skipStmtList
|
||||
c.addSlice(n, n[1], slice[1], slice[2])
|
||||
analyseSons(c, n)
|
||||
elif op.name.s == "[]=" and op.fromSystem:
|
||||
c.addSlice(n, n[1], n[2][1], n[2][2])
|
||||
let slice = n[2].skipStmtList
|
||||
c.addSlice(n, n[1], slice[1], slice[2])
|
||||
analyseSons(c, n)
|
||||
else:
|
||||
analyseSons(c, n)
|
||||
@@ -395,8 +401,9 @@ proc transformSlices(n: PNode): PNode =
|
||||
result = copyNode(n)
|
||||
result.add opSlice.newSymNode
|
||||
result.add n[1]
|
||||
result.add n[2][1]
|
||||
result.add n[2][2]
|
||||
let slice = n[2].skipStmtList
|
||||
result.add slice[1]
|
||||
result.add slice[2]
|
||||
return result
|
||||
if n.safeLen > 0:
|
||||
result = shallowCopy(n)
|
||||
|
||||
@@ -855,6 +855,8 @@ proc initEffects(effects: PNode; s: PSym; t: var TEffects) =
|
||||
newSeq(effects.sons, effectListLen)
|
||||
effects.sons[exceptionEffects] = newNodeI(nkArgList, s.info)
|
||||
effects.sons[tagEffects] = newNodeI(nkArgList, s.info)
|
||||
effects.sons[usesEffects] = ast.emptyNode
|
||||
effects.sons[writeEffects] = ast.emptyNode
|
||||
|
||||
t.exc = effects.sons[exceptionEffects]
|
||||
t.tags = effects.sons[tagEffects]
|
||||
|
||||
@@ -1265,13 +1265,16 @@ proc computeSizeAux(typ: PType, a: var BiggestInt): BiggestInt =
|
||||
else: result = 8
|
||||
a = result
|
||||
of tySet:
|
||||
length = lengthOrd(typ.sons[0])
|
||||
if length <= 8: result = 1
|
||||
elif length <= 16: result = 2
|
||||
elif length <= 32: result = 4
|
||||
elif length <= 64: result = 8
|
||||
elif align(length, 8) mod 8 == 0: result = align(length, 8) div 8
|
||||
else: result = align(length, 8) div 8 + 1
|
||||
if typ.sons[0].kind == tyGenericParam:
|
||||
result = szUnknownSize
|
||||
else:
|
||||
length = lengthOrd(typ.sons[0])
|
||||
if length <= 8: result = 1
|
||||
elif length <= 16: result = 2
|
||||
elif length <= 32: result = 4
|
||||
elif length <= 64: result = 8
|
||||
elif align(length, 8) mod 8 == 0: result = align(length, 8) div 8
|
||||
else: result = align(length, 8) div 8 + 1
|
||||
a = result
|
||||
of tyRange:
|
||||
result = computeSizeAux(typ.sons[0], a)
|
||||
|
||||
@@ -1377,10 +1377,13 @@ proc genCheckedObjAccess(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags) =
|
||||
genObjAccess(c, n.sons[0], dest, flags)
|
||||
|
||||
proc genArrAccess(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags) =
|
||||
if n.sons[0].typ.skipTypes(abstractVarRange-{tyTypeDesc}).kind in {
|
||||
tyString, tyCString}:
|
||||
let arrayType = n.sons[0].typ.skipTypes(abstractVarRange-{tyTypeDesc}).kind
|
||||
if arrayType in {tyString, tyCString}:
|
||||
genArrAccess2(c, n, dest, opcLdStrIdx, {})
|
||||
elif arrayType == tyTypeDesc:
|
||||
c.genTypeLit(n.typ, dest)
|
||||
else:
|
||||
echo renderTree(n)
|
||||
genArrAccess2(c, n, dest, opcLdArr, flags)
|
||||
|
||||
proc getNullValueAux(obj: PNode, result: PNode) =
|
||||
|
||||
@@ -1,577 +0,0 @@
|
||||
#
|
||||
#
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf, Dominik Picheta
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## This module implements graphical output for Nim; the current
|
||||
## implementation uses SDL but the interface is meant to support multiple
|
||||
## backends some day. There is no need to init SDL as this module does that
|
||||
## implicitly.
|
||||
|
||||
import colors, math
|
||||
from sdl import PSurface # Bug
|
||||
from sdl_ttf import openFont, closeFont
|
||||
|
||||
type
|
||||
Rect* = tuple[x, y, width, height: int]
|
||||
Point* = tuple[x, y: int]
|
||||
|
||||
PSurface* = ref Surface ## a surface to draw onto
|
||||
Surface* {.pure, final.} = object
|
||||
w*, h*: Natural
|
||||
s*: sdl.PSurface
|
||||
|
||||
EGraphics* = object of IOError
|
||||
|
||||
Font {.pure, final.} = object
|
||||
f: sdl_ttf.PFont
|
||||
color: sdl.Color
|
||||
PFont* = ref Font ## represents a font
|
||||
{.deprecated: [TSurface: Surface, TFont: Font, TRect: Rect, TPoint: Point].}
|
||||
|
||||
proc toSdlColor*(c: Color): sdl.Color =
|
||||
## Convert colors.Color to sdl.Color
|
||||
var x = c.extractRGB
|
||||
result.r = x.r and 0xff
|
||||
result.g = x.g and 0xff
|
||||
result.b = x.b and 0xff
|
||||
|
||||
proc createSdlColor*(sur: PSurface, c: Color, alpha: int = 0): int32 =
|
||||
## Creates a color using ``sdl.MapRGBA``.
|
||||
var x = c.extractRGB
|
||||
return sdl.mapRGBA(sur.s.format, x.r and 0xff, x.g and 0xff,
|
||||
x.b and 0xff, alpha and 0xff)
|
||||
|
||||
proc toSdlRect*(r: Rect): sdl.Rect =
|
||||
## Convert ``graphics.Rect`` to ``sdl.Rect``.
|
||||
result.x = int16(r.x)
|
||||
result.y = int16(r.y)
|
||||
result.w = uint16(r.width)
|
||||
result.h = uint16(r.height)
|
||||
|
||||
proc raiseEGraphics =
|
||||
raise newException(EGraphics, $sdl.getError())
|
||||
|
||||
proc surfaceFinalizer(s: PSurface) = sdl.freeSurface(s.s)
|
||||
|
||||
proc newSurface*(width, height: int): PSurface =
|
||||
## creates a new surface.
|
||||
new(result, surfaceFinalizer)
|
||||
result.w = width
|
||||
result.h = height
|
||||
result.s = sdl.createRGBSurface(sdl.SWSURFACE, width, height,
|
||||
32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0)
|
||||
if result.s == nil:
|
||||
raiseEGraphics()
|
||||
|
||||
assert(not sdl.mustLock(result.s))
|
||||
|
||||
proc fontFinalizer(f: PFont) = closeFont(f.f)
|
||||
|
||||
proc newFont*(name = "VeraMono.ttf", size = 9, color = colBlack): PFont =
|
||||
## Creates a new font object. Raises ``EIO`` if the font cannot be loaded.
|
||||
new(result, fontFinalizer)
|
||||
result.f = openFont(name, size.cint)
|
||||
if result.f == nil:
|
||||
raise newException(IOError, "Could not open font file: " & name)
|
||||
result.color = toSdlColor(color)
|
||||
|
||||
var
|
||||
defaultFont*: PFont ## default font that is used; this needs to initialized
|
||||
## by the client!
|
||||
|
||||
proc initDefaultFont*(name = "VeraMono.ttf", size = 9, color = colBlack) =
|
||||
## initializes the `defaultFont` var.
|
||||
defaultFont = newFont(name, size, color)
|
||||
|
||||
proc newScreenSurface*(width, height: int): PSurface =
|
||||
## Creates a new screen surface
|
||||
new(result, surfaceFinalizer)
|
||||
result.w = width
|
||||
result.h = height
|
||||
result.s = sdl.setVideoMode(width, height, 0, 0)
|
||||
if result.s == nil:
|
||||
raiseEGraphics()
|
||||
|
||||
proc writeToBMP*(sur: PSurface, filename: string) =
|
||||
## Saves the contents of the surface `sur` to the file `filename` as a
|
||||
## BMP file.
|
||||
if sdl.saveBMP(sur.s, filename) != 0:
|
||||
raise newException(IOError, "cannot write: " & filename)
|
||||
|
||||
type
|
||||
Pixels = array[0..1000_000-1, int32]
|
||||
PPixels = ptr Pixels
|
||||
{.deprecated: [TPixels: Pixels].}
|
||||
|
||||
template setPix(video, pitch, x, y, col: expr): stmt =
|
||||
video[y * pitch + x] = int32(col)
|
||||
|
||||
template getPix(video, pitch, x, y: expr): expr =
|
||||
colors.Color(video[y * pitch + x])
|
||||
|
||||
const
|
||||
ColSize = 4
|
||||
|
||||
proc getPixel(sur: PSurface, x, y: Natural): colors.Color {.inline.} =
|
||||
assert x <% sur.w
|
||||
assert y <% sur.h
|
||||
result = getPix(cast[PPixels](sur.s.pixels), sur.s.pitch.int div ColSize,
|
||||
x, y)
|
||||
|
||||
proc setPixel(sur: PSurface, x, y: Natural, col: colors.Color) {.inline.} =
|
||||
assert x <% sur.w
|
||||
assert y <% sur.h
|
||||
var pixs = cast[PPixels](sur.s.pixels)
|
||||
#pixs[y * (sur.s.pitch div colSize) + x] = int(col)
|
||||
setPix(pixs, sur.s.pitch.int div ColSize, x, y, col)
|
||||
|
||||
proc `[]`*(sur: PSurface, p: Point): Color =
|
||||
## get pixel at position `p`. No range checking is done!
|
||||
result = getPixel(sur, p.x, p.y)
|
||||
|
||||
proc `[]`*(sur: PSurface, x, y: int): Color =
|
||||
## get pixel at position ``(x, y)``. No range checking is done!
|
||||
result = getPixel(sur, x, y)
|
||||
|
||||
proc `[]=`*(sur: PSurface, p: Point, col: Color) =
|
||||
## set the pixel at position `p`. No range checking is done!
|
||||
setPixel(sur, p.x, p.y, col)
|
||||
|
||||
proc `[]=`*(sur: PSurface, x, y: int, col: Color) =
|
||||
## set the pixel at position ``(x, y)``. No range checking is done!
|
||||
setPixel(sur, x, y, col)
|
||||
|
||||
proc blit*(destSurf: PSurface, destRect: Rect, srcSurf: PSurface,
|
||||
srcRect: Rect) =
|
||||
## Copies ``srcSurf`` into ``destSurf``
|
||||
var destTRect, srcTRect: sdl.Rect
|
||||
|
||||
destTRect.x = int16(destRect.x)
|
||||
destTRect.y = int16(destRect.y)
|
||||
destTRect.w = uint16(destRect.width)
|
||||
destTRect.h = uint16(destRect.height)
|
||||
|
||||
srcTRect.x = int16(srcRect.x)
|
||||
srcTRect.y = int16(srcRect.y)
|
||||
srcTRect.w = uint16(srcRect.width)
|
||||
srcTRect.h = uint16(srcRect.height)
|
||||
|
||||
if sdl.blitSurface(srcSurf.s, addr(srcTRect), destSurf.s, addr(destTRect)) != 0:
|
||||
raiseEGraphics()
|
||||
|
||||
proc textBounds*(text: string, font = defaultFont): tuple[width, height: int] =
|
||||
var w, h: cint
|
||||
if sdl_ttf.sizeUTF8(font.f, text, w, h) < 0: raiseEGraphics()
|
||||
result.width = int(w)
|
||||
result.height = int(h)
|
||||
|
||||
proc drawText*(sur: PSurface, p: Point, text: string, font = defaultFont) =
|
||||
## Draws text with a transparent background, at location ``p`` with the given
|
||||
## font.
|
||||
var textSur: PSurface # This surface will have the text drawn on it
|
||||
new(textSur, surfaceFinalizer)
|
||||
|
||||
# Render the text
|
||||
textSur.s = sdl_ttf.renderTextBlended(font.f, text, font.color)
|
||||
# Merge the text surface with sur
|
||||
sur.blit((p.x, p.y, sur.w, sur.h), textSur, (0, 0, sur.w, sur.h))
|
||||
|
||||
proc drawText*(sur: PSurface, p: Point, text: string,
|
||||
bg: Color, font = defaultFont) =
|
||||
## Draws text, at location ``p`` with font ``font``. ``bg``
|
||||
## is the background color.
|
||||
var textSur: PSurface # This surface will have the text drawn on it
|
||||
new(textSur, surfaceFinalizer)
|
||||
textSur.s = sdl_ttf.renderTextShaded(font.f, text, font.color, toSdlColor(bg))
|
||||
# Merge the text surface with sur
|
||||
sur.blit((p.x, p.y, sur.w, sur.h), textSur, (0, 0, sur.w, sur.h))
|
||||
|
||||
proc drawCircle*(sur: PSurface, p: Point, r: Natural, color: Color) =
|
||||
## draws a circle with center `p` and radius `r` with the given color
|
||||
## onto the surface `sur`.
|
||||
var video = cast[PPixels](sur.s.pixels)
|
||||
var pitch = sur.s.pitch.int div ColSize
|
||||
var a = 1 - r
|
||||
var py = r
|
||||
var px = 0
|
||||
var x = p.x
|
||||
var y = p.y
|
||||
while px <= py + 1:
|
||||
if x+px <% sur.w:
|
||||
if y+py <% sur.h: setPix(video, pitch, x+px, y+py, color)
|
||||
if y-py <% sur.h: setPix(video, pitch, x+px, y-py, color)
|
||||
|
||||
if x-px <% sur.w:
|
||||
if y+py <% sur.h: setPix(video, pitch, x-px, y+py, color)
|
||||
if y-py <% sur.h: setPix(video, pitch, x-px, y-py, color)
|
||||
|
||||
if x+py <% sur.w:
|
||||
if y+px <% sur.h: setPix(video, pitch, x+py, y+px, color)
|
||||
if y-px <% sur.h: setPix(video, pitch, x+py, y-px, color)
|
||||
|
||||
if x-py <% sur.w:
|
||||
if y+px <% sur.h: setPix(video, pitch, x-py, y+px, color)
|
||||
if y-px <% sur.h: setPix(video, pitch, x-py, y-px, color)
|
||||
|
||||
if a < 0:
|
||||
a = a + (2 * px + 3)
|
||||
else:
|
||||
a = a + (2 * (px - py) + 5)
|
||||
py = py - 1
|
||||
px = px + 1
|
||||
|
||||
proc `>-<`(val: int, s: PSurface): int {.inline.} =
|
||||
return if val < 0: 0 elif val >= s.w: s.w-1 else: val
|
||||
|
||||
proc `>|<`(val: int, s: PSurface): int {.inline.} =
|
||||
return if val < 0: 0 elif val >= s.h: s.h-1 else: val
|
||||
|
||||
proc drawLine*(sur: PSurface, p1, p2: Point, color: Color) =
|
||||
## draws a line between the two points `p1` and `p2` with the given color
|
||||
## onto the surface `sur`.
|
||||
var stepx, stepy: int = 0
|
||||
var x0 = p1.x >-< sur
|
||||
var x1 = p2.x >-< sur
|
||||
var y0 = p1.y >|< sur
|
||||
var y1 = p2.y >|< sur
|
||||
var dy = y1 - y0
|
||||
var dx = x1 - x0
|
||||
if dy < 0:
|
||||
dy = -dy
|
||||
stepy = -1
|
||||
else:
|
||||
stepy = 1
|
||||
if dx < 0:
|
||||
dx = -dx
|
||||
stepx = -1
|
||||
else:
|
||||
stepx = 1
|
||||
dy = dy * 2
|
||||
dx = dx * 2
|
||||
var video = cast[PPixels](sur.s.pixels)
|
||||
var pitch = sur.s.pitch.int div ColSize
|
||||
setPix(video, pitch, x0, y0, color)
|
||||
if dx > dy:
|
||||
var fraction = dy - (dx div 2)
|
||||
while x0 != x1:
|
||||
if fraction >= 0:
|
||||
y0 = y0 + stepy
|
||||
fraction = fraction - dx
|
||||
x0 = x0 + stepx
|
||||
fraction = fraction + dy
|
||||
setPix(video, pitch, x0, y0, color)
|
||||
else:
|
||||
var fraction = dx - (dy div 2)
|
||||
while y0 != y1:
|
||||
if fraction >= 0:
|
||||
x0 = x0 + stepx
|
||||
fraction = fraction - dy
|
||||
y0 = y0 + stepy
|
||||
fraction = fraction + dx
|
||||
setPix(video, pitch, x0, y0, color)
|
||||
|
||||
proc drawHorLine*(sur: PSurface, x, y, w: Natural, color: Color) =
|
||||
## draws a horizontal line from (x,y) to (x+w-1, y).
|
||||
var video = cast[PPixels](sur.s.pixels)
|
||||
var pitch = sur.s.pitch.int div ColSize
|
||||
|
||||
if y >= 0 and y <= sur.s.h:
|
||||
for i in 0 .. min(sur.s.w-x, w)-1:
|
||||
setPix(video, pitch, x + i, y, color)
|
||||
|
||||
proc drawVerLine*(sur: PSurface, x, y, h: Natural, color: Color) =
|
||||
## draws a vertical line from (x,y) to (x, y+h-1).
|
||||
var video = cast[PPixels](sur.s.pixels)
|
||||
var pitch = sur.s.pitch.int div ColSize
|
||||
|
||||
if x >= 0 and x <= sur.s.w:
|
||||
for i in 0 .. min(sur.s.h-y, h)-1:
|
||||
setPix(video, pitch, x, y + i, color)
|
||||
|
||||
proc fillCircle*(s: PSurface, p: Point, r: Natural, color: Color) =
|
||||
## draws a circle with center `p` and radius `r` with the given color
|
||||
## onto the surface `sur` and fills it.
|
||||
var a = 1 - r
|
||||
var py: int = r
|
||||
var px = 0
|
||||
var x = p.x
|
||||
var y = p.y
|
||||
while px <= py:
|
||||
# Fill up the middle half of the circle
|
||||
drawVerLine(s, x + px, y, py + 1, color)
|
||||
drawVerLine(s, x + px, y - py, py, color)
|
||||
if px != 0:
|
||||
drawVerLine(s, x - px, y, py + 1, color)
|
||||
drawVerLine(s, x - px, y - py, py, color)
|
||||
if a < 0:
|
||||
a = a + (2 * px + 3)
|
||||
else:
|
||||
a = a + (2 * (px - py) + 5)
|
||||
py = py - 1
|
||||
# Fill up the left/right half of the circle
|
||||
if py >= px:
|
||||
drawVerLine(s, x + py + 1, y, px + 1, color)
|
||||
drawVerLine(s, x + py + 1, y - px, px, color)
|
||||
drawVerLine(s, x - py - 1, y, px + 1, color)
|
||||
drawVerLine(s, x - py - 1, y - px, px, color)
|
||||
px = px + 1
|
||||
|
||||
proc drawRect*(sur: PSurface, r: Rect, color: Color) =
|
||||
## draws a rectangle.
|
||||
var video = cast[PPixels](sur.s.pixels)
|
||||
var pitch = sur.s.pitch.int div ColSize
|
||||
if (r.x >= 0 and r.x <= sur.s.w) and (r.y >= 0 and r.y <= sur.s.h):
|
||||
var minW = min(sur.s.w - r.x, r.width)
|
||||
var minH = min(sur.s.h - r.y, r.height)
|
||||
|
||||
# Draw Top
|
||||
for i in 0 .. minW - 1:
|
||||
setPix(video, pitch, r.x + i, r.y, color)
|
||||
setPix(video, pitch, r.x + i, r.y + minH - 1, color) # Draw bottom
|
||||
|
||||
# Draw left side
|
||||
for i in 0 .. minH - 1:
|
||||
setPix(video, pitch, r.x, r.y + i, color)
|
||||
setPix(video, pitch, r.x + minW - 1, r.y + i, color) # Draw right side
|
||||
|
||||
proc fillRect*(sur: PSurface, r: Rect, col: Color) =
|
||||
## Fills a rectangle using sdl's ``FillRect`` function.
|
||||
var rect = toSdlRect(r)
|
||||
if sdl.fillRect(sur.s, addr(rect), sur.createSdlColor(col)) == -1:
|
||||
raiseEGraphics()
|
||||
|
||||
proc plot4EllipsePoints(sur: PSurface, cx, cy, x, y: Natural, col: Color) =
|
||||
var video = cast[PPixels](sur.s.pixels)
|
||||
var pitch = sur.s.pitch.int div ColSize
|
||||
if cx+x <= sur.s.w-1:
|
||||
if cy+y <= sur.s.h-1: setPix(video, pitch, cx+x, cy+y, col)
|
||||
if cy-y <= sur.s.h-1: setPix(video, pitch, cx+x, cy-y, col)
|
||||
if cx-x <= sur.s.w-1:
|
||||
if cy+y <= sur.s.h-1: setPix(video, pitch, cx-x, cy+y, col)
|
||||
if cy-y <= sur.s.h-1: setPix(video, pitch, cx-x, cy-y, col)
|
||||
|
||||
proc drawEllipse*(sur: PSurface, cx, cy, xRadius, yRadius: Natural,
|
||||
col: Color) =
|
||||
## Draws an ellipse, ``CX`` and ``CY`` specify the center X and Y of the
|
||||
## ellipse, ``XRadius`` and ``YRadius`` specify half the width and height
|
||||
## of the ellipse.
|
||||
var
|
||||
x, y: Natural
|
||||
xChange, yChange: int
|
||||
ellipseError: Natural
|
||||
twoASquare, twoBSquare: Natural
|
||||
stoppingX, stoppingY: Natural
|
||||
|
||||
twoASquare = 2 * xRadius * xRadius
|
||||
twoBSquare = 2 * yRadius * yRadius
|
||||
x = xRadius
|
||||
y = 0
|
||||
xChange = yRadius * yRadius * (1 - 2 * xRadius)
|
||||
yChange = xRadius * xRadius
|
||||
ellipseError = 0
|
||||
stoppingX = twoBSquare * xRadius
|
||||
stoppingY = 0
|
||||
|
||||
while stoppingX >= stoppingY: # 1st set of points, y` > - 1
|
||||
sur.plot4EllipsePoints(cx, cy, x, y, col)
|
||||
inc(y)
|
||||
inc(stoppingY, twoASquare)
|
||||
inc(ellipseError, yChange)
|
||||
inc(yChange, twoASquare)
|
||||
if (2 * ellipseError + xChange) > 0 :
|
||||
dec(x)
|
||||
dec(stoppingX, twoBSquare)
|
||||
inc(ellipseError, xChange)
|
||||
inc(xChange, twoBSquare)
|
||||
|
||||
# 1st point set is done; start the 2nd set of points
|
||||
x = 0
|
||||
y = yRadius
|
||||
xChange = yRadius * yRadius
|
||||
yChange = xRadius * xRadius * (1 - 2 * yRadius)
|
||||
ellipseError = 0
|
||||
stoppingX = 0
|
||||
stoppingY = twoASquare * yRadius
|
||||
while stoppingX <= stoppingY:
|
||||
sur.plot4EllipsePoints(cx, cy, x, y, col)
|
||||
inc(x)
|
||||
inc(stoppingX, twoBSquare)
|
||||
inc(ellipseError, xChange)
|
||||
inc(xChange,twoBSquare)
|
||||
if (2 * ellipseError + yChange) > 0:
|
||||
dec(y)
|
||||
dec(stoppingY, twoASquare)
|
||||
inc(ellipseError, yChange)
|
||||
inc(yChange,twoASquare)
|
||||
|
||||
|
||||
proc plotAA(sur: PSurface, x, y: int, c: float, color: Color) =
|
||||
if (x > 0 and x < sur.s.w) and (y > 0 and y < sur.s.h):
|
||||
var video = cast[PPixels](sur.s.pixels)
|
||||
var pitch = sur.s.pitch.int div ColSize
|
||||
|
||||
var pixColor = getPix(video, pitch, x, y)
|
||||
|
||||
setPix(video, pitch, x, y,
|
||||
pixColor.intensity(1.0 - c) + color.intensity(c))
|
||||
|
||||
|
||||
template ipart(x: expr): expr = floor(x)
|
||||
template cround(x: expr): expr = ipart(x + 0.5)
|
||||
template fpart(x: expr): expr = x - ipart(x)
|
||||
template rfpart(x: expr): expr = 1.0 - fpart(x)
|
||||
|
||||
proc drawLineAA*(sur: PSurface, p1, p2: Point, color: Color) =
|
||||
## Draws a anti-aliased line from ``p1`` to ``p2``, using Xiaolin Wu's
|
||||
## line algorithm
|
||||
var (x1, x2, y1, y2) = (p1.x.toFloat(), p2.x.toFloat(),
|
||||
p1.y.toFloat(), p2.y.toFloat())
|
||||
var dx = x2 - x1
|
||||
var dy = y2 - y1
|
||||
|
||||
var ax = dx
|
||||
if ax < 0'f64:
|
||||
ax = 0'f64 - ax
|
||||
var ay = dy
|
||||
if ay < 0'f64:
|
||||
ay = 0'f64 - ay
|
||||
|
||||
if ax < ay:
|
||||
swap(x1, y1)
|
||||
swap(x2, y2)
|
||||
swap(dx, dy)
|
||||
|
||||
template doPlot(x, y: int, c: float, color: Color): stmt =
|
||||
if ax < ay:
|
||||
sur.plotAA(y, x, c, color)
|
||||
else:
|
||||
sur.plotAA(x, y, c, color)
|
||||
|
||||
if x2 < x1:
|
||||
swap(x1, x2)
|
||||
swap(y1, y2)
|
||||
|
||||
var gradient = dy / dx
|
||||
# handle first endpoint
|
||||
var xend = cround(x1)
|
||||
var yend = y1 + gradient * (xend - x1)
|
||||
var xgap = rfpart(x1 + 0.5)
|
||||
var xpxl1 = int(xend) # this will be used in the main loop
|
||||
var ypxl1 = int(ipart(yend))
|
||||
doPlot(xpxl1, ypxl1, rfpart(yend)*xgap, color)
|
||||
doPlot(xpxl1, ypxl1 + 1, fpart(yend)*xgap, color)
|
||||
var intery = yend + gradient # first y-intersection for the main loop
|
||||
|
||||
# handle second endpoint
|
||||
xend = cround(x2)
|
||||
yend = y2 + gradient * (xend - x2)
|
||||
xgap = fpart(x2 + 0.5)
|
||||
var xpxl2 = int(xend) # this will be used in the main loop
|
||||
var ypxl2 = int(ipart(yend))
|
||||
doPlot(xpxl2, ypxl2, rfpart(yend) * xgap, color)
|
||||
doPlot(xpxl2, ypxl2 + 1, fpart(yend) * xgap, color)
|
||||
|
||||
# main loop
|
||||
var x = xpxl1 + 1
|
||||
while x <= xpxl2-1:
|
||||
doPlot(x, int(ipart(intery)), rfpart(intery), color)
|
||||
doPlot(x, int(ipart(intery)) + 1, fpart(intery), color)
|
||||
intery = intery + gradient
|
||||
inc(x)
|
||||
|
||||
proc fillSurface*(sur: PSurface, color: Color) =
|
||||
## Fills the entire surface with ``color``.
|
||||
if sdl.fillRect(sur.s, nil, sur.createSdlColor(color)) == -1:
|
||||
raiseEGraphics()
|
||||
|
||||
template withEvents*(surf: PSurface, event: expr, actions: stmt): stmt {.
|
||||
immediate.} =
|
||||
## Simple template which creates an event loop. ``Event`` is the name of the
|
||||
## variable containing the Event object.
|
||||
while true:
|
||||
var event: sdl.Event
|
||||
if sdl.waitEvent(addr(event)) == 1:
|
||||
actions
|
||||
|
||||
if sdl.init(sdl.INIT_VIDEO) < 0: raiseEGraphics()
|
||||
if sdl_ttf.init() < 0: raiseEGraphics()
|
||||
|
||||
when not defined(testing) and isMainModule:
|
||||
var surf = newScreenSurface(800, 600)
|
||||
|
||||
surf.fillSurface(colWhite)
|
||||
|
||||
# Draw the shapes
|
||||
surf.drawLineAA((150, 170), (400, 471), colTan)
|
||||
surf.drawLine((100, 170), (400, 471), colRed)
|
||||
|
||||
surf.drawEllipse(200, 300, 200, 30, colSeaGreen)
|
||||
surf.drawHorLine(1, 300, 400, colViolet)
|
||||
# Check if the ellipse is the size it's suppose to be.
|
||||
surf.drawVerLine(200, 300 - 30 + 1, 60, colViolet) # ^^ | i suppose it is
|
||||
|
||||
surf.drawEllipse(400, 300, 300, 300, colOrange)
|
||||
surf.drawEllipse(5, 5, 5, 5, colGreen)
|
||||
|
||||
surf.drawHorLine(5, 5, 900, colRed)
|
||||
surf.drawVerLine(5, 60, 800, colRed)
|
||||
surf.drawCircle((600, 500), 60, colRed)
|
||||
|
||||
surf.fillRect((50, 50, 100, 100), colFuchsia)
|
||||
surf.fillRect((150, 50, 100, 100), colGreen)
|
||||
surf.drawRect((50, 150, 100, 100), colGreen)
|
||||
surf.drawRect((150, 150, 100, 100), colAqua)
|
||||
surf.drawRect((250, 150, 100, 100), colBlue)
|
||||
surf.drawHorLine(250, 150, 100, colRed)
|
||||
|
||||
surf.drawLineAA((592, 160), (592, 280), colPurple)
|
||||
|
||||
#surf.drawText((300, 300), "TEST", colMidnightBlue)
|
||||
#var textSize = textBounds("TEST")
|
||||
#surf.drawText((300, 300 + textSize.height), $textSize.width & ", " &
|
||||
# $textSize.height, colDarkGreen)
|
||||
|
||||
var mouseStartX = -1
|
||||
var mouseStartY = -1
|
||||
withEvents(surf, event):
|
||||
var eventp = addr(event)
|
||||
case event.kind:
|
||||
of sdl.QUITEV:
|
||||
break
|
||||
of sdl.KEYDOWN:
|
||||
var evk = sdl.evKeyboard(eventp)
|
||||
if evk.keysym.sym == sdl.K_LEFT:
|
||||
surf.drawHorLine(395, 300, 50, colBlack)
|
||||
echo("Drawing")
|
||||
elif evk.keysym.sym == sdl.K_ESCAPE:
|
||||
break
|
||||
else:
|
||||
echo(evk.keysym.sym)
|
||||
of sdl.MOUSEBUTTONDOWN:
|
||||
var mbd = sdl.evMouseButton(eventp)
|
||||
if mouseStartX == -1 or mouseStartY == -1:
|
||||
mouseStartX = int(mbd.x)
|
||||
mouseStartY = int(mbd.y)
|
||||
else:
|
||||
surf.drawLineAA((mouseStartX, mouseStartY), (int(mbd.x), int(mbd.y)), colPurple)
|
||||
mouseStartX = -1
|
||||
mouseStartY = -1
|
||||
|
||||
of sdl.MOUSEMOTION:
|
||||
var mm = sdl.evMouseMotion(eventp)
|
||||
if mouseStartX != -1 and mouseStartY != -1:
|
||||
surf.drawLineAA((mouseStartX, mouseStartY), (int(mm.x), int(mm.y)), colPurple)
|
||||
#echo(mm.x, " ", mm.y, " ", mm.yrel)
|
||||
|
||||
else:
|
||||
discard "echo(event.kind)"
|
||||
|
||||
sdl.updateRect(surf.s, 0, 0, 800, 600)
|
||||
|
||||
surf.writeToBMP("test.bmp")
|
||||
sdl.quit()
|
||||
@@ -1468,16 +1468,25 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
|
||||
hint("Processing " & prc[0].getName & " as an async proc.")
|
||||
|
||||
let returnType = prc[3][0]
|
||||
var baseType: NimNode
|
||||
# Verify that the return type is a Future[T]
|
||||
if returnType.kind == nnkIdent:
|
||||
error("Expected return type of 'Future' got '" & $returnType & "'")
|
||||
elif returnType.kind == nnkBracketExpr:
|
||||
if $returnType[0] != "Future":
|
||||
error("Expected return type of 'Future' got '" & $returnType[0] & "'")
|
||||
if returnType.kind == nnkBracketExpr:
|
||||
let fut = repr(returnType[0])
|
||||
if fut != "Future":
|
||||
error("Expected return type of 'Future' got '" & fut & "'")
|
||||
baseType = returnType[1]
|
||||
elif returnType.kind in nnkCallKinds and $returnType[0] == "[]":
|
||||
let fut = repr(returnType[1])
|
||||
if fut != "Future":
|
||||
error("Expected return type of 'Future' got '" & fut & "'")
|
||||
baseType = returnType[2]
|
||||
elif returnType.kind == nnkEmpty:
|
||||
baseType = returnType
|
||||
else:
|
||||
error("Expected return type of 'Future' got '" & repr(returnType) & "'")
|
||||
|
||||
let subtypeIsVoid = returnType.kind == nnkEmpty or
|
||||
(returnType.kind == nnkBracketExpr and
|
||||
returnType[1].kind == nnkIdent and returnType[1].ident == !"void")
|
||||
(baseType.kind == nnkIdent and returnType[1].ident == !"void")
|
||||
|
||||
var outerProcBody = newNimNode(nnkStmtList, prc[6])
|
||||
|
||||
@@ -1485,7 +1494,7 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
|
||||
var retFutureSym = genSym(nskVar, "retFuture")
|
||||
var subRetType =
|
||||
if returnType.kind == nnkEmpty: newIdentNode("void")
|
||||
else: returnType[1]
|
||||
else: baseType
|
||||
outerProcBody.add(
|
||||
newVarStmt(retFutureSym,
|
||||
newCall(
|
||||
@@ -1509,7 +1518,7 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
|
||||
newIdentNode("off")))) # -> {.push warning[resultshadowed]: off.}
|
||||
|
||||
procBody.insert(1, newNimNode(nnkVarSection, prc[6]).add(
|
||||
newIdentDefs(newIdentNode("result"), returnType[1]))) # -> var result: T
|
||||
newIdentDefs(newIdentNode("result"), baseType))) # -> var result: T
|
||||
|
||||
procBody.insert(2, newNimNode(nnkPragma).add(
|
||||
newIdentNode("pop"))) # -> {.pop.})
|
||||
@@ -1550,8 +1559,8 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
|
||||
result[6] = outerProcBody
|
||||
|
||||
#echo(treeRepr(result))
|
||||
if prc[0].getName == "hubConnectionLoop":
|
||||
echo(toStrLit(result))
|
||||
#if prc[0].getName == "hubConnectionLoop":
|
||||
# echo(toStrLit(result))
|
||||
|
||||
macro async*(prc: stmt): stmt {.immediate.} =
|
||||
## Macro which processes async procedures into the appropriate
|
||||
|
||||
@@ -32,8 +32,9 @@ type
|
||||
size*: int ## size of the memory mapped file
|
||||
|
||||
when defined(windows):
|
||||
fHandle: int
|
||||
mapHandle: int
|
||||
fHandle: Handle
|
||||
mapHandle: Handle
|
||||
wasOpened: bool ## only close if wasOpened
|
||||
else:
|
||||
handle: cint
|
||||
|
||||
@@ -115,7 +116,8 @@ proc open*(filename: string, mode: FileMode = fmRead,
|
||||
template callCreateFile(winApiProc, filename: expr): expr =
|
||||
winApiProc(
|
||||
filename,
|
||||
if readonly: GENERIC_READ else: GENERIC_ALL,
|
||||
# GENERIC_ALL != (GENERIC_READ or GENERIC_WRITE)
|
||||
if readonly: GENERIC_READ else: GENERIC_READ or GENERIC_WRITE,
|
||||
FILE_SHARE_READ,
|
||||
nil,
|
||||
if newFileSize != -1: CREATE_ALWAYS else: OPEN_EXISTING,
|
||||
@@ -172,6 +174,8 @@ proc open*(filename: string, mode: FileMode = fmRead,
|
||||
if mappedSize != -1: result.size = min(fileSize, mappedSize).int
|
||||
else: result.size = fileSize.int
|
||||
|
||||
result.wasOpened = true
|
||||
|
||||
else:
|
||||
template fail(errCode: OSErrorCode, msg: expr) =
|
||||
rollback()
|
||||
@@ -226,7 +230,7 @@ proc close*(f: var MemFile) =
|
||||
var lastErr: OSErrorCode
|
||||
|
||||
when defined(windows):
|
||||
if f.fHandle != INVALID_HANDLE_VALUE:
|
||||
if f.fHandle != INVALID_HANDLE_VALUE and f.wasOpened:
|
||||
error = unmapViewOfFile(f.mem) == 0
|
||||
lastErr = osLastError()
|
||||
error = (closeHandle(f.mapHandle) == 0) or error
|
||||
@@ -243,6 +247,7 @@ proc close*(f: var MemFile) =
|
||||
when defined(windows):
|
||||
f.fHandle = 0
|
||||
f.mapHandle = 0
|
||||
f.wasOpened = false
|
||||
else:
|
||||
f.handle = 0
|
||||
|
||||
|
||||
@@ -223,10 +223,7 @@ when defined(ssl):
|
||||
of protSSLv23:
|
||||
newCTX = SSL_CTX_new(SSLv23_method()) # SSlv2,3 and TLS1 support.
|
||||
of protSSLv2:
|
||||
when not defined(linux):
|
||||
newCTX = SSL_CTX_new(SSLv2_method())
|
||||
else:
|
||||
raiseSslError()
|
||||
raiseSslError("SSLv2 is no longer secure and has been deprecated, use protSSLv3")
|
||||
of protSSLv3:
|
||||
newCTX = SSL_CTX_new(SSLv3_method())
|
||||
of protTLSv1:
|
||||
|
||||
@@ -108,6 +108,36 @@ proc get*[T](self: Option[T]): T =
|
||||
raise UnpackError(msg : "Can't obtain a value from a `none`")
|
||||
self.val
|
||||
|
||||
proc get*[T](self: Option[T], otherwise: T): T =
|
||||
## Returns the contents of this option or `otherwise` if the option is none.
|
||||
if self.isSome:
|
||||
self.val
|
||||
else:
|
||||
otherwise
|
||||
|
||||
|
||||
proc map*[T](self: Option[T], callback: proc (input: T)) =
|
||||
## Applies a callback to the value in this Option
|
||||
if self.has:
|
||||
callback(self.val)
|
||||
|
||||
proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] =
|
||||
## Applies a callback to the value in this Option and returns an option
|
||||
## containing the new value. If this option is None, None will be returned
|
||||
if self.has:
|
||||
some[R]( callback(self.val) )
|
||||
else:
|
||||
none(R)
|
||||
|
||||
proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] =
|
||||
## Applies a callback to the value in this Option. If the callback returns
|
||||
## `true`, the option is returned as a Some. If it returns false, it is
|
||||
## returned as a None.
|
||||
if self.has and not callback(self.val):
|
||||
none(T)
|
||||
else:
|
||||
self
|
||||
|
||||
|
||||
proc `==`*(a, b: Option): bool =
|
||||
## Returns ``true`` if both ``Option``s are ``none``,
|
||||
@@ -115,8 +145,16 @@ proc `==`*(a, b: Option): bool =
|
||||
(a.has and b.has and a.val == b.val) or (not a.has and not b.has)
|
||||
|
||||
|
||||
proc `$`*[T]( self: Option[T] ): string =
|
||||
## Returns the contents of this option or `otherwise` if the option is none.
|
||||
if self.has:
|
||||
"Some(" & $self.val & ")"
|
||||
else:
|
||||
"None[" & T.name & "]"
|
||||
|
||||
|
||||
when isMainModule:
|
||||
import unittest
|
||||
import unittest, sequtils
|
||||
|
||||
suite "optionals":
|
||||
# work around a bug in unittest
|
||||
@@ -158,3 +196,27 @@ when isMainModule:
|
||||
check false
|
||||
when compiles(none(string) == none(int)):
|
||||
check false
|
||||
|
||||
test "get with a default value":
|
||||
check( some("Correct").get("Wrong") == "Correct" )
|
||||
check( stringNone.get("Correct") == "Correct" )
|
||||
|
||||
test "$":
|
||||
check( $(some("Correct")) == "Some(Correct)" )
|
||||
check( $(stringNone) == "None[string]" )
|
||||
|
||||
test "map with a void result":
|
||||
var procRan = 0
|
||||
some(123).map(proc (v: int) = procRan = v)
|
||||
check procRan == 123
|
||||
intNone.map(proc (v: int) = check false)
|
||||
|
||||
test "map":
|
||||
check( some(123).map(proc (v: int): int = v * 2) == some(246) )
|
||||
check( intNone.map(proc (v: int): int = v * 2).isNone )
|
||||
|
||||
test "filter":
|
||||
check( some(123).filter(proc (v: int): bool = v == 123) == some(123) )
|
||||
check( some(456).filter(proc (v: int): bool = v == 123).isNone )
|
||||
check( intNone.filter(proc (v: int): bool = check false).isNone )
|
||||
|
||||
|
||||
@@ -314,10 +314,7 @@ when defined(ssl):
|
||||
of protSSLv23:
|
||||
newCTX = SSL_CTX_new(SSLv23_method()) # SSlv2,3 and TLS1 support.
|
||||
of protSSLv2:
|
||||
when not defined(linux) and not defined(OpenBSD):
|
||||
newCTX = SSL_CTX_new(SSLv2_method())
|
||||
else:
|
||||
raiseSslError()
|
||||
raiseSslError("SSLv2 is no longer secure and has been deprecated, use protSSLv3")
|
||||
of protSSLv3:
|
||||
newCTX = SSL_CTX_new(SSLv3_method())
|
||||
of protTLSv1:
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
# Nim Compiler
|
||||
|
||||
[](https://gitter.im/nim-lang/Nim?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
|
||||
|
||||
This repo contains the Nim compiler, Nim's stdlib, tools and
|
||||
documentation.
|
||||
|
||||
|
||||
28
tests/parallel/tparfind.nim
Normal file
28
tests/parallel/tparfind.nim
Normal file
@@ -0,0 +1,28 @@
|
||||
discard """
|
||||
output: "500"
|
||||
"""
|
||||
|
||||
import threadpool, sequtils
|
||||
|
||||
{.experimental.}
|
||||
|
||||
proc linearFind(a: openArray[int]; x, offset: int): int =
|
||||
for i, y in a:
|
||||
if y == x: return i+offset
|
||||
result = -1
|
||||
|
||||
proc parFind(a: seq[int]; x: int): int =
|
||||
var results: array[4, int]
|
||||
parallel:
|
||||
if a.len >= 4:
|
||||
let chunk = a.len div 4
|
||||
results[0] = spawn linearFind(a[0 ..< chunk], x, 0)
|
||||
results[1] = spawn linearFind(a[chunk ..< chunk*2], x, chunk)
|
||||
results[2] = spawn linearFind(a[chunk*2 ..< chunk*3], x, chunk*2)
|
||||
results[3] = spawn linearFind(a[chunk*3 ..< a.len], x, chunk*3)
|
||||
result = max(results)
|
||||
|
||||
|
||||
let data = toSeq(0..1000)
|
||||
echo parFind(data, 500)
|
||||
|
||||
13
tests/stdlib/tmemfiles1.nim
Normal file
13
tests/stdlib/tmemfiles1.nim
Normal file
@@ -0,0 +1,13 @@
|
||||
discard """
|
||||
test that closing a closed file is ignored (no error raised)
|
||||
file: "tmemfiles1.nim"
|
||||
"""
|
||||
import memfiles, os
|
||||
var
|
||||
mm: MemFile
|
||||
fn = "test.mmap"
|
||||
# Create a new file
|
||||
mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 20)
|
||||
mm.close()
|
||||
mm.close()
|
||||
if fileExists(fn): removeFile(fn)
|
||||
38
tests/stdlib/tmemfiles2.nim
Normal file
38
tests/stdlib/tmemfiles2.nim
Normal file
@@ -0,0 +1,38 @@
|
||||
discard """
|
||||
test creating/reading/writing/changing memfiles
|
||||
file: "tmemfiles2.nim"
|
||||
output: '''Full read size: 20
|
||||
Half read size: 10 Data: Hello'''
|
||||
"""
|
||||
import memfiles, os
|
||||
var
|
||||
mm, mm_full, mm_half: MemFile
|
||||
fn = "test.mmap"
|
||||
p: pointer
|
||||
|
||||
if fileExists(fn): removeFile(fn)
|
||||
|
||||
# Create a new file, data all zeros
|
||||
mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 20)
|
||||
mm.close()
|
||||
|
||||
# read, change
|
||||
mm_full = memfiles.open(fn, mode = fmWrite, mappedSize = -1)
|
||||
echo "Full read size: ",mm_full.size
|
||||
p = mm_full.mapMem(fmReadWrite, 20, 0)
|
||||
var p2 = cast[cstring](p)
|
||||
p2[0] = 'H'
|
||||
p2[1] = 'e'
|
||||
p2[2] = 'l'
|
||||
p2[3] = 'l'
|
||||
p2[4] = 'o'
|
||||
p2[5] = '\0'
|
||||
mm_full.unmapMem(p, 20)
|
||||
mm_full.close()
|
||||
|
||||
# read half, and verify data change
|
||||
mm_half = memfiles.open(fn, mode = fmRead, mappedSize = 10)
|
||||
echo "Half read size: ",mm_half.size, " Data: ", cast[cstring](mm_half.mem)
|
||||
mm_half.close()
|
||||
|
||||
if fileExists(fn): removeFile(fn)
|
||||
21
tests/vm/tyaytypedesc.nim
Normal file
21
tests/vm/tyaytypedesc.nim
Normal file
@@ -0,0 +1,21 @@
|
||||
discard """
|
||||
output: "ntWhitespace"
|
||||
"""
|
||||
|
||||
# bug #3357
|
||||
|
||||
type NodeType* = enum
|
||||
ntWhitespace
|
||||
|
||||
type TokenType* = enum
|
||||
ttWhitespace
|
||||
|
||||
proc enumTable*[A, B, C](a: openarray[tuple[key: A, val: B]], ret: typedesc[C]): C =
|
||||
for item in a:
|
||||
result[item.key] = item.val
|
||||
|
||||
const tokenTypeToNodeType = {
|
||||
ttWhitespace: ntWhitespace,
|
||||
}.enumTable(array[ttWhitespace..ttWhitespace, NodeType])
|
||||
|
||||
echo tokenTypeToNodeType[ttWhitespace]
|
||||
Reference in New Issue
Block a user