Re: Can I tell Nim to NOT use *reference* for a var parameter?

2017-11-19 Thread Araq
Please give me more details.


Re: Can I tell Nim to NOT use *reference* for a var parameter?

2017-11-19 Thread monster
@Araq I have read it, several times. But I do not see how this helps me, if I 
don't want to directly read or write a volatile, but rather I have to pass a 
pointer to a volatile location to an OS method that expects one.


Re: Macros: How to parse a string and get the type of the expression?

2017-11-19 Thread bluenote
Ah, I think I found a solution:


import macros, strutils

macro typecheckedAst(x: typed): untyped =
  echo "calling typecheck on expression: ", x.repr
  echo "with type: ", x.getType().treeRepr()
  # We can build a type specific AST here:
  let typ = x.getType()
  if sameType(typ, bindSym"int"):
echo "it's an int"
result = newStrLitNode("AST for int")
  elif sameType(typ, bindSym"string"):
echo "it's a string"
result = newStrLitNode("AST for string")
  else:
echo "unknown type"
  echo " => resulting type dependent AST: ", result.repr

macro fmt*(fmt: static[string]): untyped =
  let fields = fmt.split(":")
  assert fields.len == 2
  let lhs = fields[0]
  let rhs = fields[1]
  # We probably can't get the type of `e` here, but we can delegate
  # all type specific AST generation into the `typecheckedAst` macro
  let e = parseExpr(lhs)
  result = newCall(ident"typecheckedAst", e)

block:
  let x = 1
  echo fmt"x + x:"

block:
  let x = "test"
  echo fmt"x & x:"


So the trick is to use `parseExpr` in the outer macro and delegate the type 
specific AST generation into a sub-macro which takes a `typed` expression.


Re: Statement macro applied on proc

2017-11-19 Thread tzumao
Works like charm. Thanks!


Re: Statement macro applied on proc

2017-11-19 Thread Araq
Try instead


macro duplicate(procStmt: untyped): untyped =
  ...



Re: What's wrong with this simple macro?

2017-11-19 Thread Araq
Why should it work? You pass `procA` via an indirection.


Re: Starting point for nimx

2017-11-19 Thread dom96
I'm not aware of any documentation.

You might have better luck creating an issue in the repo. I'm not sure if 
yglukhov comes into the forum often.


Re: Can I tell Nim to NOT use *reference* for a var parameter?

2017-11-19 Thread Araq
`volatile.nim` is a stdlib module, you can read its code to see how it works. 
Don't use the type system to model `volatile`, that does not work well.


Can I tell Nim to NOT use *reference* for a var parameter?

2017-11-19 Thread monster
I'm (still) trying to put the "missing bits" of atomic methods together into a 
cross-platform/cross-backend module. I've now got to the point that the VCC C 
version compiles and run (I only test with one thread, so idk if it really 
gives the expected atomic behaviour yet). When I try to build and run the VCC 
C++ version, I have an issue with C++ references. It seems "(t: var T)" is 
translated to a pointer in C, and a reference in C++. And this doesn't seem to 
play well with volatiles.

How to I make this work in C++?


type
  VolatilePtr*[T] = distinct ptr T

proc toVolatilePtr*[T](t: var T): VolatilePtr[T] =
  cast[VolatilePtr[T]](addr t)

var my_vbyte {.volatile.}: byte = 0'u8
var my_vbyte_ptr: VolatilePtr[byte] = toVolatilePtr[byte](my_vbyte)

echo(cast[pointer](my_vbyte_ptr) == nil)


When trying to use this, I get this kind of error:


error C2664: 'NU8 *toVolatilePtr_WWtk4tL3VWMmQk62xcO0Ew(NU8 &)': cannot 
convert argument 1 from 'volatile NU8' to 'NU8 &'



What's wrong with this simple macro?

2017-11-19 Thread dawkot

import macros

macro procImplRepr(p: proc): string =
p.symbol.getImpl.repr

proc procA = discard
proc procB(p: proc): string = p.procImplRepr

echo procA.procImplRepr #returns proc procA = discard
echo procB(procA) #returns nil


.


Re: Macros: How to parse a string and get the type of the expression?

2017-11-19 Thread bluenote
@planhths: What I'm looking for is the slightly more advanced version of that, 
i.e., a macro that generates a different AST depending on the type of that 
`parseExpr(v)` in your example.


Re: Macros: How to parse a string and get the type of the expression?

2017-11-19 Thread planhths
Here is an example that works: 
[plot_eval.nim](https://gist.github.com/konqoro/185337d656f1a2224a2d55391870b0c4)
 I don't know what macros.strVal is supposed to do though I' m curious...


Re: Unifiing resource management like destructors and finalizers

2017-11-19 Thread Araq
I don't understand your questions. The default GC is pretty aggressive and so 
finalizers end up being called "early enough" so we don't really discourage 
them.


Re: Macros: How to parse a string and get the type of the expression?

2017-11-19 Thread bluenote
Sorry for bumping an old thread, but it's for a good reason (the [string 
interpolation PR](https://github.com/nim-lang/Nim/pull/6507)). I'm still 
struggling to achieve the same from within a macro:


macro parse(x: string): untyped =
  echo "calling parse on: ", x
  result = parseExpr(x.strVal)

macro typecheck(x: typed): untyped =
  echo "calling typecheck on: ", x.getType().repr()
  result = newStmtList()

template stringParseTest(x: string) = typecheck parse x

macro fmt*(fmt: static[string]): untyped =
  let fields = fmt.split(":")
  assert fields.len == 2
  let lhs = fields[0]
  let rhs = fields[1]
  echo "Trying to get type of: ", lhs
  # Neither of the following works, because the ``x.strVal`` produces:
  # Error: field 'strVal' cannot be found
  # Also, it is strange that the value of x within parse is "lhs" and not
  # its actual value.
  when false:
let lhsType = stringParseTest(lhs)
  when false:
let lhsType = typecheck(parse(lhs))
  when false: # in general calling parse doesn't seems to work due to the 
value issue:
parse(lhs)
  # So it looks like I have to call parseExpr directly here. This gives the 
expected
  # result for the expression, but calling typecheck on it does not work, 
because of:
  # Error: expression '' has no type (or is ambiguous)
  when false:
let e = parseExpr(lhs)
echo e.treeRepr
let lhsType = typecheck(e)
  # Similarly any attempt to call getType() directly on the expressions 
fails with
  # node has no type.
  when false:
let e = parseExpr(lhs)
echo e.treeRepr
let lhsType = e.getType()

block:
  let x = 1
  fmt"x + x:"

block:
  let x = "test"
  fmt"x & x:"


I feel like I'm just missing the right kind of nesting. Or maybe it isn't 
possible when the outer-most thing is a macro, and I really need to wrap 
everything into another outer template?


Statement macro applied on proc

2017-11-19 Thread tzumao
Hi,

I am still very new to nim, so what I am trying to do may not be making sense 
at all. The thing I am trying to do with nim macro is simple: create a 
duplication of a proc. Everything remains the same except the name is changed 
to [proc_name]_duplicate (I plan to do some manipulation with the proc but I 
stucked here) The code is: 


import macros

macro duplicate(procStmt: typed): typed =
  procStmt.expectKind nnkStmtList
  let procDef = procStmt[0]
  procDef.expectKind nnkProcDef
  
  let name = newIdentNode($procDef[0] & "_duplicate")
  
  var params: seq[NimNode]
  params = @[]
  for param_id in 0..