How to parse optional arguments without keyval?
\documentclass{minimal}
\parindent=0pt
\usepackage{pstricks}
\usepackage[nomessages]{fp}
\def\LoadConstants{}
\makeatletter
\def\const{\@ifnextchar[\const@i{\const@i[r]}}
\def\const@i[#1]{\expandafter\const@ii#1,,\@nil}
\def\const@ii#1,#2,#3\@nil#4#5{%
\if\relax\detokenize{#2}\relax
\if\relax\detokenize{#1}\relax \expandafter\const@iii\expandafter{#5}{#4}%
\else
\pst@isnum{#1}%
{>>> Wrong Parameter '#1' \@namedef{#4}{}}
{\ifx#1c \expandafter\const@iii\expandafter{clip(#5)}{#4}%
\else \expandafter\const@iii\expandafter{#5}{#4}%
\fi}%
\fi%
\else%
\ifx#2t \expandafter\const@iii\expandafter{trunc(#5:#1)}{#4}%
\else
\ifx#2c\expandafter\const@iii\expandafter{clip(#5}{#4}%
\else \expandafter\const@iii\expandafter{round(#5:#1)}{#4}%
\fi%
\fi%
\fi%
}
\def\const@iii#1#2{%
\expandafter\FPeval\csname#2\expandafter\endcsname\expandafter{\expandafter{#1}}%
\toks0=\expandafter{\LoadConstants\pstVerb}
\edef\LoadConstants{\the\toks0 {/#2 \csname#2\endcsname\space def}}}
\makeatother
\begin{document}
\const{Side}{root(2,17)} \Side \\
\const[c]{Side}{root(2,17)} \Side \\
\const[4]{Side}{root(2,17)} \Side \\
\const[r]{Side}{root(2,17)} \Side\\
\const[6,r]{Side}{root(2,17)} \Side\\
\const[6,t]{Side}{root(2,17)} \Side
\end{document}
An empty argument like \const[]{...
is also possible.
And here the same with the key/value syntax:
\documentclass{minimal}
\parindent=0pt
\usepackage{pstricks}
\usepackage[nomessages]{fp}
\def\LoadConstants{}
\makeatletter
\define@key[psset]{}{Roundtype}[round]{\expandafter\pst@@Roundtype#1\@nil}%
\def\pst@@Roundtype#1#2\@nil{%
\ifx#1t \def\psk@Roundtype{trunc}%
\else\ifx#1c \def\psk@Roundtype{clip}\else\def\psk@Roundtype{round}%
\fi\fi}
\define@key[psset]{}{Decimals}[-1]{\pst@getint{#1}\psk@Decimals}%
\psset{Roundtype,Decimals}
\def\pst@RT@c{clip}
\def\const{\def\pst@par{}\pst@object{const}}
\def\const@i#1#2{%
\use@par%
\ifx\psk@Roundtype\pst@RT@c
\expandafter\const@ii\expandafter{clip(#2)}{#1}
\else
\ifnum\psk@Decimals<1\relax
\expandafter\const@ii\expandafter{#2}{#1}%
\else
\expandafter\const@ii\expandafter{\psk@Roundtype(#2:\psk@Decimals)}{#1}%
\fi%
\fi%
}
\def\const@ii#1#2{%
\expandafter\FPeval\csname#2\expandafter\endcsname\expandafter{\expandafter{#1}}%
\toks0=\expandafter{\LoadConstants\pstVerb}
\edef\LoadConstants{\the\toks0 {/#2 \csname#2\endcsname\space def}}}
\makeatother
\begin{document}
\const{Side}{root(2,17)} \Side \\
\const[Roundtype=c]{Side}{root(2,17)} \Side \\
\const[Decimals=4]{Side}{root(2,17)} \Side \\
\const[Roundtype=r]{Side}{root(2,17)} \Side\\
\const[Decimals=6,Roundtype=r]{Side}{root(2,17)} \Side\\
\const[Decimals=6,Roundtype=t]{Side}{root(2,17)} \Side\\
\const[]{Side}{root(2,17)} \Side
\end{document}
\def\zzA#1,#2#3\relax#4{
\if t#2 trunc(#4:#1)%
\else\if r#2 round(#4:#1)%
\else clip(#4)%
\fi\fi}
% Constant declarator invoked in the preamble
\newcommand\const[3][]{%
\if\relax\detokenize{#1}\relax
\edef\temporary{#3}%
\else
\edef\temporary{\zzA#1,c\relax{#3}}%
\wlog{::\temporary}%
\fi
\expandafter\FPeval\csname#2\expandafter\endcsname
\expandafter{\expandafter{\temporary}}%
\toks0=\expandafter{\LoadConstants\pstVerb}
\edef\LoadConstants{\the\toks0 {/#2 \csname#2\endcsname\space def}}}
produces a log of
:: trunc(root(2,17):4)
:: round(root(2,17):4)
:: clip(root(2,17))
from
\const{Side}{root(2,17)}% the square root of 17
\const[4,t]{Side}{root(2,17)}% the square root of 17
\const[4,r]{Side}{root(2,17)}% the square root of 17
\const[c]{Side}{root(2,17)}% the square root of 17