aboutsummaryrefslogtreecommitdiff
path: root/Tools
diff options
context:
space:
mode:
authorIan Hinder <ian.hinder@aei.mpg.de>2010-02-28 18:38:19 +0100
committerIan Hinder <ian.hinder@aei.mpg.de>2010-03-02 08:02:48 -0600
commit90044389629f516e431f6a266b817771ae6b2d7b (patch)
treea1a7cfa68a5a8185b0cdb54016e50e5065a91b7d /Tools
parent87bb301b8625ad9b75b50e8a0589112d1995280b (diff)
CalculationFunction.m: Replace Mathematica's line-breaking with our own
Mathematica's implementation of line-breaking in CForm sometimes splits lines in the middle of tokens and adds continuation backslashes to the end of the split line. In combination with the indentation that we apply in Kranc, this can cause the tokens to be broken by whitespace. This commit inhibits Mathematica's line breaking and replaces it with an algorithm which splits lines only on whitespace.
Diffstat (limited to 'Tools')
-rw-r--r--Tools/CodeGen/CalculationFunction.m25
1 files changed, 24 insertions, 1 deletions
diff --git a/Tools/CodeGen/CalculationFunction.m b/Tools/CodeGen/CalculationFunction.m
index 08678ae..5efc32c 100644
--- a/Tools/CodeGen/CalculationFunction.m
+++ b/Tools/CodeGen/CalculationFunction.m
@@ -125,6 +125,27 @@ replaceDerivatives[x_, derivRules_] :=
replaceCustom = Flatten[derivRules,1];
x /. replaceStandard];
+
+(* Take a string s and break it into separate lines using l as a guide
+ to the line length. If a word (sequence of non-whitespace
+ characters) is longer than l, do not break it. Add two spaces of
+ indentation after each line break (this will push the line length
+ over l). Algorithm essentially taken from
+ http://en.wikipedia.org/wiki/Word_wrap *)
+lineBreak[s_, l_] :=
+ Module[{spaceLeft, words, word, i, lineWidth = l, spaceWidth = 1,
+ len},
+ spaceLeft = l;
+ words = StringSplit[s];
+ Do[
+ word = words[[i]];
+ len = StringLength[word];
+ If[len > spaceLeft,
+ words[[i]] = "\n " <> word;
+ spaceLeft = lineWidth - len,
+ spaceLeft = spaceLeft - (len + spaceWidth)], {i, 1, Length[words]}];
+ Return[StringJoin[Riffle[words, " "]]]];
+
(* Return a CodeGen block which assigns dest by evaluating expr *)
assignVariableFromExpression[dest_, expr_] := Module[{tSym, type, cleanExpr, code},
@@ -135,7 +156,7 @@ assignVariableFromExpression[dest_, expr_] := Module[{tSym, type, cleanExpr, cod
cleanExpr = ReplacePowers[expr] /. sym`t -> tSym;
If[SOURCELANGUAGE == "C",
- code = type <> " const " <> ToString[dest] <> " = " <> ToString[cleanExpr, CForm, PageWidth -> 120] <> ";\n",
+ code = type <> " const " <> ToString[dest] <> " = " <> ToString[cleanExpr, CForm, PageWidth -> Infinity] <> ";\n",
code = ToString@dest <> ".eq." <> ToString[cleanExpr, FortranForm, PageWidth -> 120] <> "\n"
];
@@ -150,6 +171,8 @@ assignVariableFromExpression[dest_, expr_] := Module[{tSym, type, cleanExpr, cod
];
];
+ code = lineBreak[code, 70] <> "\n";
+
(* code = StringReplace[code, "Rule" -> " = "]; *)
code = StringReplace[code, "normal1" -> "normal[0]"];
code = StringReplace[code, "normal2" -> "normal[1]"];