I'm not exactly sure what you mean but I assumed that:

  * by context you mean scope, if so you can use a block statement
  * for compile-tie printing you can use `static`
  * You wanted to see 84 printed and not 48


    
    
    import macros
    
    # this macro evaluates function body in a different context
    # and prints result at compile time
    macro eval1(fun: untyped): untyped  =
      fun.expectKind nnkProcDef
      let body = fun.body
      let stmt = body[0]
      echo stmt.treeRepr
      let x = ident"x" # Avoid capturing an inexisting ident x
      result =
        quote do:
          block:    # Create a new scope
            static: # i'd like to see value 48 printed at compile time,
              let `x` = 42
              echo `stmt`
    
    proc stmt1 {.eval1.}=
      2*x
    
    # Returns 84
    
    
    Run

Reply via email to