| Bytes | Lang | Time | Link |
|---|---|---|---|
| 170 | Wolfram Language Mathematica | 201009T043254Z | att |
| 567 | Python 3 | 201008T153826Z | hyper-ne |
Wolfram Language (Mathematica), 267 246 190 172 170 bytes
R[ToString[""@ToExpression@R[#,{"("->"~In~##&[",")"->"]",a:(WordCharacter|"_")..:>q<>a<>q}]/.#2/.$...[[0]]|List->(##&)/.In->(#@##2&),CForm],q->""]&
R=StringReplace
q="\""
Input ["expr", packs]. packs can be in any format accepted by ReplaceAll.
There unfortunately seems to be no straightforward way to simultaneously expand heads (Through, Comap14.0+) and arguments (Thread).
R=StringReplace;q="\""
ToExpression@R[#,{ parse input:
"("->"~In~##&[",")"->"]", convert f(args...) to In[f,args...]
a:(WordCharacter|"_")..:>q<>a<>q convert symbols to strings
}]
""@ % wrap in ""
/.#2 insert packs
/.$...[[0]]|List->(##&) expand packs
/.In->(#@##2&) convert In[f,args...] to f[args...]
ToString[ % , stringify,
CForm converting [] to ()
]
R[ % ,q->""] unquote names
Python 3, 567 bytes
lambda v,q:w(g(v,p(q),-1))
def c(s):
a=[];b=0;l=""
for x in s:
if x!=","or b:l+=x;b+=~-")(".find(x)%3-1
else:a+=[p(l)];l=""
return a+[p(l)]
p=lambda s:(0,p(s[:-3]))if"..."==s[-3:]else(1,(2,s.split("(")[0]),c(s[s.find("(")+1:-1]))if")"==s[-1]else(2,s)
def g(v,q,i):
if q[0]>1:return q[1]if q[1]not in v or i<0else v[q[1]][i]
if q[0]:return(g(v,q[1],i),[g(v,k,i)for k in q[2]])
a=[];i=0
while 1:
try:a+=[g(v,q[1],i)];i+=1
except:return a
w=lambda k:",".join(q for q in[w(x)for x in k]if q)if type(k)==list else k if type(k)==str else k[0]+"("+w(k[1])+")"
Could use a lot of golfing but I'm pretty happy with this answer. Tested on all 10 example cases.
-9 bytes thanks to Neil