summaryrefslogtreecommitdiff
path: root/src/expr2c.mac
blob: 1522169d5a164c21aa13d46a05c2efda9e574800 (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
25
26
27
28
/*
 * Copyright 2006-2009 Johannes Hofmann <Johannes.Hofmann@gmx.de>
 */

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("}", "
"))$