Skip to content

Instantly share code, notes, and snippets.

@Yardanico
Created October 11, 2017 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yardanico/d98fd78eb23bf51e76cbe1149899ea0a to your computer and use it in GitHub Desktop.
Save Yardanico/d98fd78eb23bf51e76cbe1149899ea0a to your computer and use it in GitHub Desktop.
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