Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import macros
- import tables
- # Delete later
- import streams
- import ./nimja
- macro nimjaGo1(strTemplate, resultString: typed, body: untyped) =
- result = newStmtList()
- var identifiers = newTable[string, NimNode]()
- for node in body:
- if node.kind != nnkIdent:
- error("Macro only accepts identifiers!", node)
- identifiers.add(node.strVal, node)
- for k,v in identifiers:
- result.add quote do:
- if `k` == `strTemplate`:
- echo `v`
- macro nimjaGo2(head: string, body: untyped) =
- result = newStmtList()
- var identifiers = newTable[string, NimNode]()
- for node in body:
- if node.kind != nnkIdent:
- error("Macro only accepts identifiers!", node)
- identifiers.add(node.strVal, node)
- var
- parserSym = genSym(nskVar)
- tokensSym = genSym(nskVar)
- analysisSym = genSym(nskVar)
- result.add quote do:
- var
- `parserSym` = new NimjaParser
- `tokensSym`: seq[Token]
- `analysisSym`: tuple[correct: bool, location: Location, msg: string]
- `parserSym`.s = newStringStream(`head`)
- `tokensSym` = `parserSym`.tokenize()
- `analysisSym` = checkTokens(`tokensSym`)
- if `analysisSym`[0] == false:
- raise newException(NimjaParseError, `analysisSym`[2])
- var mainBody = nnkStmtList.newTree(nnkCommand.newTree())
- for k,v in identifiers:
- mainBody[0].add quote do:
- if token.val == `k`:
- echo `v`
- var forStmt = newNimNode(nnkForStmt)
- forStmt.add(newIdentNode("token"), tokensSym)
- var ifStmt = nnkIfStmt.newTree(
- nnkElifBranch.newTree(
- nnkInfix.newTree(
- newIdentNode("=="),
- nnkDotExpr.newTree(
- newIdentNode("token"),
- newIdentNode("tokenType")
- ),
- newIdentNode("tIdent")
- ),
- mainBody)
- )
- forStmt.add(ifStmt)
- result.add(forStmt)
- echo lispRepr(result, true)
- var x = 3
- nimjaGo2 "{{ x }}":
- x
Advertisement
Add Comment
Please, Sign In to add comment