Skip to content

Compile-time code generation issue #1140

@onionhammer

Description

@onionhammer

This is a boiled down version of the issue i'm experiencing w/ my templates library.

I'm getting an 'undeclared identifier' error when I run the below code; however if line 8 is changed from "if false" to "when false" (or remove the entire if block), the code magically compiles and runs. Another mystery (to me) is that if I remove the second proc at the bottom (proc another), the code magically compiles and runs.

lib/core/macros.nim(211, 21) Error: undeclared identifier: '    <p>Test!</p>
    '
import parseutils, macros

proc parse_until_symbol(node: PNimrodNode, value: string, index: var int): bool {.compiletime.} =
    var splitValue: string
    var read = value.parseUntil(splitValue, '$', index)

    # when false:
    if false:
        var identifier: string
        read = value.parseWhile(identifier, {}, index)
        node.add newCall("add", ident("result"), newCall("$", ident(identifier)))

    if splitValue.len > 0:
        node.insert node.len, newCall("add", ident("result"), newStrLitNode(splitValue))

proc parse_template(node: PNimrodNode, value: string) {.compiletime.}  =
    var index = 0
    while index < value.len and
        parse_until_symbol(node, value, index): nil

macro tmpli*(body: expr): stmt =
    result = newStmtList()
    result.add parseExpr("result = \"\"")
    result.parse_template body[1].strVal


proc actual: string = tmpli html"""
    <p>Test!</p>
    """

proc another: string = tmpli html"""
    <p>what</p>
    """

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions