Merge branch 'devel' of https://github.com/Araq/Nimrod into devel
This commit is contained in:
commit
5d93957048
5 changed files with 25 additions and 24 deletions
|
|
@ -1376,7 +1376,7 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
|
||||||
getTemp(p, getSysType(tyInt), i) # our counter
|
getTemp(p, getSysType(tyInt), i) # our counter
|
||||||
initLocExpr(p, e.sons[1], a)
|
initLocExpr(p, e.sons[1], a)
|
||||||
initLocExpr(p, e.sons[2], b)
|
initLocExpr(p, e.sons[2], b)
|
||||||
if d.k == locNone: getTemp(p, a.t, d)
|
if d.k == locNone: getTemp(p, getSysType(tyBool), d)
|
||||||
lineF(p, cpsStmts, lookupOpr[op],
|
lineF(p, cpsStmts, lookupOpr[op],
|
||||||
[rdLoc(i), toRope(size), rdLoc(d), rdLoc(a), rdLoc(b)])
|
[rdLoc(i), toRope(size), rdLoc(d), rdLoc(a), rdLoc(b)])
|
||||||
of mEqSet:
|
of mEqSet:
|
||||||
|
|
|
||||||
|
|
@ -1701,11 +1701,11 @@ algorithm returns true:
|
||||||
result = isOrdinal(t) or t.kind in {float, float32, float64}
|
result = isOrdinal(t) or t.kind in {float, float32, float64}
|
||||||
|
|
||||||
proc isExplicitlyConvertible(a, b: PType): bool =
|
proc isExplicitlyConvertible(a, b: PType): bool =
|
||||||
|
result = false
|
||||||
if isImplicitlyConvertible(a, b): return true
|
if isImplicitlyConvertible(a, b): return true
|
||||||
if typeEqualsOrDistinct(a, b): return true
|
if typeEqualsOrDistinct(a, b): return true
|
||||||
if isIntegralType(a) and isIntegralType(b): return true
|
if isIntegralType(a) and isIntegralType(b): return true
|
||||||
if isSubtype(a, b) or isSubtype(b, a): return true
|
if isSubtype(a, b) or isSubtype(b, a): return true
|
||||||
return false
|
|
||||||
|
|
||||||
The convertible relation can be relaxed by a user-defined type
|
The convertible relation can be relaxed by a user-defined type
|
||||||
`converter`:idx:.
|
`converter`:idx:.
|
||||||
|
|
@ -1774,7 +1774,7 @@ Example:
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
proc p(x, y: int): int =
|
proc p(x, y: int): int =
|
||||||
return x + y
|
result = x + y
|
||||||
|
|
||||||
discard p(3, 4) # discard the return value of `p`
|
discard p(3, 4) # discard the return value of `p`
|
||||||
|
|
||||||
|
|
@ -1789,7 +1789,7 @@ been declared with the `discardable`:idx: pragma:
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
proc p(x, y: int): int {.discardable.} =
|
proc p(x, y: int): int {.discardable.} =
|
||||||
return x + y
|
result = x + y
|
||||||
|
|
||||||
p(3, 4) # now valid
|
p(3, 4) # now valid
|
||||||
|
|
||||||
|
|
@ -2440,7 +2440,7 @@ A procedure cannot modify its parameters (unless the parameters have the type
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
proc `$` (x: int): string =
|
proc `$` (x: int): string =
|
||||||
# converts an integer to a string; this is a prefix operator.
|
# converts an integer to a string; this is a prefix operator.
|
||||||
return intToStr(x)
|
result = intToStr(x)
|
||||||
|
|
||||||
Operators with one parameter are prefix operators, operators with two
|
Operators with one parameter are prefix operators, operators with two
|
||||||
parameters are infix operators. (However, the parser distinguishes these from
|
parameters are infix operators. (However, the parser distinguishes these from
|
||||||
|
|
@ -2454,7 +2454,7 @@ notation. (Thus an operator can have more than two parameters):
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
proc `*+` (a, b, c: int): int =
|
proc `*+` (a, b, c: int): int =
|
||||||
# Multiply and add
|
# Multiply and add
|
||||||
return a * b + c
|
result = a * b + c
|
||||||
|
|
||||||
assert `*+`(3, 4, 6) == `*`(a, `+`(b, c))
|
assert `*+`(3, 4, 6) == `*`(a, `+`(b, c))
|
||||||
|
|
||||||
|
|
@ -2500,7 +2500,7 @@ different; for this a special setter syntax is needed:
|
||||||
|
|
||||||
proc host*(s: TSocket): int {.inline.} =
|
proc host*(s: TSocket): int {.inline.} =
|
||||||
## getter of hostAddr
|
## getter of hostAddr
|
||||||
return s.FHost
|
s.FHost
|
||||||
|
|
||||||
var
|
var
|
||||||
s: TSocket
|
s: TSocket
|
||||||
|
|
@ -2650,11 +2650,12 @@ return values. This can be done in a cleaner way by returning a tuple:
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
proc divmod(a, b: int): tuple[res, remainder: int] =
|
proc divmod(a, b: int): tuple[res, remainder: int] =
|
||||||
return (a div b, a mod b)
|
(a div b, a mod b)
|
||||||
|
|
||||||
var t = divmod(8, 5)
|
var t = divmod(8, 5)
|
||||||
|
|
||||||
assert t.res == 1
|
assert t.res == 1
|
||||||
assert t.remainder = 3
|
assert t.remainder == 3
|
||||||
|
|
||||||
One can use `tuple unpacking`:idx: to access the tuple's fields:
|
One can use `tuple unpacking`:idx: to access the tuple's fields:
|
||||||
|
|
||||||
|
|
@ -2726,7 +2727,7 @@ dispatch.
|
||||||
|
|
||||||
method eval(e: ref TPlusExpr): int =
|
method eval(e: ref TPlusExpr): int =
|
||||||
# watch out: relies on dynamic binding
|
# watch out: relies on dynamic binding
|
||||||
return eval(e.a) + eval(e.b)
|
result = eval(e.a) + eval(e.b)
|
||||||
|
|
||||||
proc newLit(x: int): ref TLiteral =
|
proc newLit(x: int): ref TLiteral =
|
||||||
new(result)
|
new(result)
|
||||||
|
|
@ -2925,7 +2926,7 @@ parameters of an outer factory proc:
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
proc mycount(a, b: int): iterator (): int =
|
proc mycount(a, b: int): iterator (): int =
|
||||||
return iterator (): int =
|
result = iterator (): int =
|
||||||
var x = a
|
var x = a
|
||||||
while x <= b:
|
while x <= b:
|
||||||
yield x
|
yield x
|
||||||
|
|
@ -3375,9 +3376,9 @@ module to illustrate this:
|
||||||
## requires `x` and `y` to be of the same tuple type
|
## requires `x` and `y` to be of the same tuple type
|
||||||
## generic ``==`` operator for tuples that is lifted from the components
|
## generic ``==`` operator for tuples that is lifted from the components
|
||||||
## of `x` and `y`.
|
## of `x` and `y`.
|
||||||
|
result = true
|
||||||
for a, b in fields(x, y):
|
for a, b in fields(x, y):
|
||||||
if a != b: return false
|
if a != b: result = false
|
||||||
return true
|
|
||||||
|
|
||||||
Alternatively, the ``distinct`` type modifier can be applied to the type class
|
Alternatively, the ``distinct`` type modifier can be applied to the type class
|
||||||
to allow each param matching the type class to bind to a different type.
|
to allow each param matching the type class to bind to a different type.
|
||||||
|
|
@ -3999,9 +4000,9 @@ predicate:
|
||||||
|
|
||||||
proc re(pattern: semistatic[string]): TRegEx =
|
proc re(pattern: semistatic[string]): TRegEx =
|
||||||
when isStatic(pattern):
|
when isStatic(pattern):
|
||||||
return precompiledRegex(pattern)
|
result = precompiledRegex(pattern)
|
||||||
else:
|
else:
|
||||||
return compile(pattern)
|
result = compile(pattern)
|
||||||
|
|
||||||
Static params can also appear in the signatures of generic types:
|
Static params can also appear in the signatures of generic types:
|
||||||
|
|
||||||
|
|
@ -4508,7 +4509,7 @@ This is best illustrated by an example:
|
||||||
proc p*(x: A.T1): A.T1 =
|
proc p*(x: A.T1): A.T1 =
|
||||||
# this works because the compiler has already
|
# this works because the compiler has already
|
||||||
# added T1 to A's interface symbol table
|
# added T1 to A's interface symbol table
|
||||||
return x + 1
|
result = x + 1
|
||||||
|
|
||||||
|
|
||||||
Import statement
|
Import statement
|
||||||
|
|
@ -5202,7 +5203,7 @@ Example:
|
||||||
{.pragma: rtl, importc, dynlib: "client.dll", cdecl.}
|
{.pragma: rtl, importc, dynlib: "client.dll", cdecl.}
|
||||||
|
|
||||||
proc p*(a, b: int): int {.rtl.} =
|
proc p*(a, b: int): int {.rtl.} =
|
||||||
return a+b
|
result = a+b
|
||||||
|
|
||||||
In the example a new pragma named ``rtl`` is introduced that either imports
|
In the example a new pragma named ``rtl`` is introduced that either imports
|
||||||
a symbol from a dynamic library or exports the symbol for dynamic library
|
a symbol from a dynamic library or exports the symbol for dynamic library
|
||||||
|
|
|
||||||
10
doc/tut1.txt
10
doc/tut1.txt
|
|
@ -690,8 +690,8 @@ Nimrod provides the ability to overload procedures similar to C++:
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
proc toString(x: int): string = ...
|
proc toString(x: int): string = ...
|
||||||
proc toString(x: bool): string =
|
proc toString(x: bool): string =
|
||||||
if x: return "true"
|
if x: result = "true"
|
||||||
else: return "false"
|
else: result = "false"
|
||||||
|
|
||||||
echo(toString(13)) # calls the toString(x: int) proc
|
echo(toString(13)) # calls the toString(x: int) proc
|
||||||
echo(toString(true)) # calls the toString(x: bool) proc
|
echo(toString(true)) # calls the toString(x: bool) proc
|
||||||
|
|
@ -1569,7 +1569,7 @@ This is best illustrated by an example:
|
||||||
proc p*(x: A.T1): A.T1 =
|
proc p*(x: A.T1): A.T1 =
|
||||||
# this works because the compiler has already
|
# this works because the compiler has already
|
||||||
# added T1 to A's interface symbol table
|
# added T1 to A's interface symbol table
|
||||||
return x + 1
|
result = x + 1
|
||||||
|
|
||||||
|
|
||||||
A symbol of a module *can* be *qualified* with the ``module.symbol`` syntax. If
|
A symbol of a module *can* be *qualified* with the ``module.symbol`` syntax. If
|
||||||
|
|
@ -1600,11 +1600,11 @@ rules apply:
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
# Module A
|
# Module A
|
||||||
proc x*(a: int): string = return $a
|
proc x*(a: int): string = result = $a
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
# Module B
|
# Module B
|
||||||
proc x*(a: string): string = return $a
|
proc x*(a: string): string = result = $a
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
# Module C
|
# Module C
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,7 @@ The syntax for type conversions is ``destination_type(expression_to_convert)``
|
||||||
|
|
||||||
.. code-block:: nimrod
|
.. code-block:: nimrod
|
||||||
proc getID(x: TPerson): int =
|
proc getID(x: TPerson): int =
|
||||||
return TStudent(x).id
|
TStudent(x).id
|
||||||
|
|
||||||
The ``EInvalidObjectConversion`` exception is raised if ``x`` is not a
|
The ``EInvalidObjectConversion`` exception is raised if ``x`` is not a
|
||||||
``TStudent``.
|
``TStudent``.
|
||||||
|
|
@ -238,7 +238,7 @@ is needed:
|
||||||
|
|
||||||
proc host*(s: TSocket): int {.inline.} =
|
proc host*(s: TSocket): int {.inline.} =
|
||||||
## getter of hostAddr
|
## getter of hostAddr
|
||||||
return s.FHost
|
s.FHost
|
||||||
|
|
||||||
var
|
var
|
||||||
s: TSocket
|
s: TSocket
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue