-
-
Save jots/1de3c8af82f884c1ae1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import strutils, macros | |
| macro `:=` (a,b:expr): expr {.immediate.} = | |
| var | |
| sr = a.toStrLit.strVal # XXX is this even close? | |
| ids = sr.split(",") | |
| source = "var _tmp_arr = " & b.toStrLit.strVal & "\n" | |
| id,typ,typestring:string | |
| echo "IN MACRO" # XXX | |
| for x, idp in ids: | |
| let pair = idp.split(":") | |
| id = pair[0] | |
| typ = pair[1] | |
| case typ | |
| of "int": | |
| typestring = ".parseInt" | |
| of "float": | |
| typestring = ".parseFloat" | |
| else: | |
| typestring = "" # don't need for "string" | |
| source.add("$1 = _tmp_arr[$2]$3 ;" % [id,$x,typestring]) | |
| # how to see "source" while debugging | |
| # how do we REPLACE the line that triggered this macro? | |
| result = parseStmt(source) | |
| var | |
| var1:int | |
| var2:string | |
| var3:float | |
| var line = "1,cool,12.12" | |
| var1:int, var2:string, var3:float := line.split(",") | |
| #echo [var1,var2,var3].join("->") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment