To do this you'd need to make a macro that takes a `static string` and operates
on it. I have no clue the requirements of what you're after, but I've written a
very simple 'parser' that simply inserts spaces between operators where it
makes sense then uses Nim's `parseExpr` to just get Nim to handle it(there are
probably too many assumptions to make this valid for your use case though).
import std/[macros, strutils, math]
macro nimify(s: static string): untyped =
type ParserState = enum
inNothing, inOp, inNumber
var
myString = s
i = 0
parserState = inNothing
if s[i] notin {'+', '-'} + Digits:
error("Invalid string")
proc insertSpace() =
if i - 1 > 0 and myString[i - 1] notin WhiteSpace:
myString.insert(" ", i)
inc i
while i < myString.len:
case myString[i]
of Digits:
if parserState == inOp:
insertSpace()
parserState = inNumber
of WhiteSpace:
discard
of {'-', '+'}:
if parserState == inOp:
insertSpace()
parserState = inNumber
else:
insertSpace()
parserState = inOp
else:
if parserState != inOp:
insertSpace()
parserState = inOp
inc i
echo myString
parseExpr(myString)
echo nimify"10+3"
echo nimify"10-3"
echo nimify"7+-167+521"
echo nimify"1<3-55mod2"
echo nimify"10mod3^4*3div3"
Run