Remove expr/stmt (#5857)

This commit is contained in:
Arne Döring 2017-07-25 09:28:23 +02:00 • committed by Andreas Rumpf
commit 000b8afd26
168 changed files with 497 additions and 529 deletions

View file

@ -1,7 +1,7 @@
import macros, parseutils
# Generate tags
macro make(names: openarray[expr]): stmt {.immediate.} =
macro make(names: untyped{nkBracket}): untyped =
result = newStmtList()
for i in 0 .. names.len-1:

View file

@ -5,5 +5,4 @@ type
proc createFoo*(a, b: int): Foo = Foo(fooa: a, foob: b)
template geta*(f: Foo): expr = f.fooa
template geta*(f: Foo): untyped = f.fooa

View file

@ -7,7 +7,7 @@ discard """
proc mpf_get_d(x: int): float = float(x)
proc mpf_cmp_d(a: int; b: float): int = 0
template toFloatHelper(result: expr; tooSmall, tooLarge: stmt) {.immediate.} =
template toFloatHelper(result, tooSmall, tooLarge: untyped) =
result = mpf_get_d(a)
if result == 0.0 and mpf_cmp_d(a,0.0) != 0:
tooSmall

View file

@ -126,7 +126,7 @@ iterator parse_compound_statements(value, identifier: string, index: int): strin
## and returns the initialization of each as an empty statement
## i.e. if x == 5 { ... } becomes if x == 5: nil.
template get_next_ident(expected): stmt =
template get_next_ident(expected) =
var nextIdent: string
discard value.parseWhile(nextIdent, {'$'} + identChars, i)
@ -316,10 +316,10 @@ proc parse_template(node: NimNode, value: string) =
## Nim code into the input `node` AST.
var index = 0
while index < value.len and
parse_until_symbol(node, value, index): nil
parse_until_symbol(node, value, index): discard
macro tmpli*(body: expr): stmt =
macro tmpli*(body: untyped): untyped =
result = newStmtList()
result.add parseExpr("result = \"\"")
@ -330,7 +330,7 @@ macro tmpli*(body: expr): stmt =
parse_template(result, reindent(value))
macro tmpl*(body: expr): stmt =
macro tmpl*(body: untyped): untyped =
result = newStmtList()
var value = if body.kind in nnkStrLit..nnkTripleStrLit: body.strVal

View file

@ -2,7 +2,7 @@
# bug #2629
import sequtils, os
template glob_rst(basedir: string = nil): expr =
template glob_rst(basedir: string = nil): untyped =
if baseDir.isNil:
to_seq(walk_files("*.rst"))
else:

View file

@ -2,7 +2,7 @@
var
e = "abc"
raise newException(EIO, e & "ha!")
raise newException(IOError, e & "ha!")
template t() = echo(foo)
@ -10,7 +10,7 @@ var foo = 12
t()
template test_in(a, b, c: expr): bool {.immediate, dirty.} =
template test_in(a, b, c: untyped): bool {.dirty.} =
var result {.gensym.}: bool = false
false

View file

@ -8,7 +8,7 @@ template baz() =
var y = foo
discard y()
macro test(): stmt =
macro test(): untyped =
result = getAst(baz())
echo(treeRepr(result))

View file

@ -1,15 +1,15 @@
type PNode* = ref object of RootObj
template litNode (name, ty): stmt =
template litNode(name, ty) =
type name* = ref object of PNode
val*: ty
litNode PIntNode, int
import json
template withKey*(j: JsonNode; key: string; varname: expr;
body:stmt): stmt {.immediate.} =
template withKey*(j: JsonNode; key: string; varname,
body: untyped): typed =
if j.hasKey(key):
let varname{.inject.}= j[key]
block:
@ -18,4 +18,3 @@ template withKey*(j: JsonNode; key: string; varname: expr;
var j = parsejson("{\"zzz\":1}")
withkey(j, "foo", x):
echo(x)

View file

@ -1,7 +1,7 @@
# bug #1337
template someIt(a, pred: expr): expr =
template someIt(a, pred): untyped =
var it {.inject.} = 0
pred

View file

@ -43,7 +43,7 @@ template forStatic(index, slice, predicate: untyped) =
block:
const index = i
predicate
template iterateStartingFrom(i: int): stmt =
template iterateStartingFrom(i: int) =
when i <= b:
iteration i
iterateStartingFrom i + 1

View file

@ -4,14 +4,12 @@ discard """
# Test that immediate templates are preferred over non-immediate templates
template foo(a, b: expr) = echo "foo expr"
template foo(a, b: untyped) = echo "foo expr"
template foo(a, b: int) = echo "foo int"
template foo(a, b: float) = echo "foo float"
template foo(a, b: string) = echo "foo string"
template foo(a, b: expr) {.immediate.} = echo "immediate"
template foo(a, b: untyped) {.immediate.} = echo "immediate"
template foo(a, b: bool) = echo "foo bool"
template foo(a, b: char) = echo "foo char"
foo(undeclaredIdentifier, undeclaredIdentifier2)

View file

@ -3,7 +3,7 @@ discard """
"""
var x = 1
template quantity(): stmt {.immediate.} =
template quantity() =
# Causes internal error in compiler/sem.nim
proc unit*(x = 1.0): float = 12
# Throws the correct error: redefinition of 'x'

View file

@ -13,7 +13,7 @@ type
Vector2[T] = T
Pixels=int
template use*(fb: int, st: stmt) : stmt =
template use*(fb: int, st: untyped): untyped =
echo "a ", $fb
st
echo "a ", $fb

View file

@ -3,10 +3,9 @@ type Foo = object
var f = Foo(len: 40)
template getLen(f: Foo): expr = f.len
template getLen(f: Foo): int = f.len
echo f.getLen
# This fails, because `len` gets the nkOpenSymChoice
# treatment inside the template early pass and then
# it can't be recognized as a field anymore

View file

@ -1,6 +1,6 @@
template withOpenFile(f: expr, filename: string, mode: TFileMode,
actions: stmt): stmt {.immediate.} =
template withOpenFile(f: untyped, filename: string, mode: TFileMode,
actions: untyped): untyped =
block:
# test that 'f' is implicitly 'injecting':
var f: TFile
@ -20,20 +20,20 @@ var
myVar: array[0..1, int]
# Test zero argument template:
template ha: expr = myVar[0]
template ha: untyped = myVar[0]
ha = 1
echo(ha)
# Test identifier generation:
template prefix(name: expr): expr {.immediate.} = `"hu" name`
template prefix(name): untyped = `"hu" name`
var `hu "XYZ"` = "yay"
echo prefix(XYZ)
template typedef(name: expr, typ: typeDesc) {.immediate, dirty.} =
template typedef(name: untyped, typ: typeDesc) {.immediate, dirty.} =
type
`T name`* = typ
`P name`* = ref `T name`
@ -51,7 +51,7 @@ type
proc initFoo(arg: int): Foo =
result.arg = arg
template create(typ: typeDesc, arg: expr): expr = `init typ`(arg)
template create(typ: typeDesc, arg: untyped): untyped = `init typ`(arg)
var ff = Foo.create(12)

View file

@ -1,8 +1,7 @@
template `:=`(name, val: expr): stmt {.immediate.} =
template `:=`(name, val: untyped): typed =
var name = val
ha := 1 * 4
hu := "ta-da" == "ta-da"
echo ha, hu

View file

@ -9,7 +9,7 @@ proc parse_to_close(value: string, index: int, open='(', close=')'): int =
discard
# Call parse_to_close
template get_next_ident: stmt =
template get_next_ident: typed =
discard "{something}".parse_to_close(0, open = '{', close = '}')
get_next_ident()
@ -19,11 +19,10 @@ get_next_ident()
#bug #880 (also example in the manual!)
template typedef(name: expr, typ: typedesc) {.immediate.} =
template typedef(name: untyped, typ: typedesc) =
type
`T name`* {.inject.} = typ
`P name`* {.inject.} = ref `T name`
typedef(myint, int)
var x: PMyInt

View file

@ -3,7 +3,7 @@ discard """
"""
# bug #2670
template testTemplate(b: bool): stmt =
template testTemplate(b: bool): typed =
when b:
var a = "hi"
else:

View file

@ -8,7 +8,7 @@ discard """
type Foo* {.pure, final.} = object
elt: float
template defineOpAssign(T: expr, op: expr) {.immediate.} =
template defineOpAssign(T, op: untyped) {.immediate.} =
proc op*(v: var T, w: T) {.inline.} =
for i in 0..1:
op(v.elt, w.elt)
@ -18,7 +18,7 @@ const ATTEMPT = 0
when ATTEMPT == 0:
# FAILS: defining `/=` with template calling template
# ERROR about sem.nim line 144
template defineOpAssigns(T: expr) {.immediate.} =
template defineOpAssigns(T: untyped) =
mixin `/=`
defineOpAssign(T, `/=`)

View file

@ -9,7 +9,7 @@ type
MyData = object
x: int
template newDataWindow(data: ref MyData): stmt =
template newDataWindow(data: ref MyData): untyped =
proc testProc(data: ref MyData) =
echo "Hello, ", data.x
testProc(data)

View file

@ -1,32 +1,31 @@
import unittest
template t(a: int): expr = "int"
template t(a: string): expr = "string"
template t(a: int): string = "int"
template t(a: string): string = "string"
test "templates can be overloaded":
check t(10) == "int"
check t("test") == "string"
test "previous definitions can be further overloaded or hidden in local scopes":
template t(a: bool): expr = "bool"
template t(a: bool): string = "bool"
check t(true) == "bool"
check t(10) == "int"
template t(a: int): expr = "inner int"
template t(a: int): string = "inner int"
check t(10) == "inner int"
check t("test") == "string"
test "templates can be redefined multiple times":
template customAssert(cond: bool, msg: string): stmt {.immediate, dirty.} =
template customAssert(cond: bool, msg: string): typed {.immediate, dirty.} =
if not cond: fail(msg)
template assertion_failed(body: stmt) {.immediate, dirty.} =
template fail(msg: string): stmt = body
template assertion_failed(body: typed) {.immediate, dirty.} =
template fail(msg: string): typed = body
assertion_failed: check msg == "first fail path"
customAssert false, "first fail path"
assertion_failed: check msg == "second fail path"
customAssert false, "second fail path"