Created
October 11, 2017 16:00
-
-
Save Yardanico/d98fd78eb23bf51e76cbe1149899ea0a to your computer and use it in GitHub Desktop.
This file contains 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 macros, strutils | |
export strutils | |
template parsestring(data): untyped = data | |
macro readLn*(args: varargs[typed]): untyped = | |
let data = genSym(nskLet, "input") | |
result = newTree( | |
nnkStmtList, | |
quote do: | |
let `data` = stdin.readLine().split() | |
) | |
var i = 0 | |
for arg in args: | |
# parseType proc, e.g.: parseint or parsefloat | |
let parseProc = !("parse" & $arg.getTypeImpl()) | |
result.add quote do: | |
`arg` = `parseProc`(`data`[`i`]) | |
inc i | |
echo result.toStrLit | |
var | |
a: int | |
b: float | |
c: bool | |
d: string | |
e: uint | |
# try to enter: | |
# 5 3.12431 false helloworldhowareyou 15991991921 | |
readLn(a, b, c, d, e) | |
echo a | |
echo b | |
echo c | |
echo d | |
echo e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment