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:<format-string>"
    
    block:
      let x = "test"
      fmt"x & x:<format-string>"
    

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?

Reply via email to