summaryrefslogtreecommitdiff
path: root/src/expr2c.mac
blob: b1fe8d120a4bf7d81e1c91e5536098012e922d12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
load("stringproc")$

varsubst(v) := ssubst("_", "%", string(v))$

printdecl(vars) := (
	sprint("double", varsubst(first(vars))),
	for v in rest(vars) do sprint(",", varsubst(v)),
	sprint(";"))$

printdef(expr) := sprint(varsubst(first(expr)), "=", varsubst(second(expr)), ";")$

block2c(expr) := 
	if ?equal(op(expr), block) then (
		printdecl(first(expr)),
		for d in reverse(rest(reverse(rest(expr)))) do printdef(d),
		sprint("return", varsubst(last(expr)), ";"))
	else
		sprint("return", varsubst(expr), ";")$

expr2c(funcname, argstr, expr) := (
	sprint("double", funcname, "(", argstr, ") {"),
	block2c(subst(pow, "^", optimize(expr))),
	sprint("}", "
"))$