aboutsummaryrefslogtreecommitdiff
path: root/Tools/CodeGen/CodeGen.m
blob: 9bd749c1fca3413bec79340e115456d90435a390 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
(* $Id$ *)

(*  Copyright 2004 Sascha Husa, Ian Hinder, Christiane Lechner

    This file is part of Kranc.

    Kranc is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kranc is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kranc; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)

BeginPackage["CodeGen`", {"Errors`", "Kranc`"}];

FlattenBlock::usage = "FlattenBlock[block] converts 'block' to a string.";
SeparatedBlock::usage = "SeparatedBlock[block] returns a version of 'block' with " <>
  "a newline before it.";
GenerateFile::usage = "GenerateFile[name, block] writes 'block' to a file of the " <>
  "specified 'name'.";
AddToFile::usage = "AddToFile[name, block] appends 'block' to a file of the " <>
    "specified 'name'.";
SpaceSeparated::usage = "";
NewlineSeparated::usage = "";
InfoVariable::usage = "";
CommaNewlineSeparated::usage = "";
CommaSeparated::usage = "";
Stringify::usage = "";
Quote::usage = "Quote[x] returns x surrounded by quotes";
IndentBlock::usage = "";
IndentBlock2::usage = "";
CheckBlock::usage = "";

CodeGenBlock := _String | _?AtomQ | List[(_?(MatchQ[#, CodeGenBlock] &)) ...];
Boolean = (True | False);

Begin["`Private`"];

(* Code generation utilities; not specific to any language *)

DefFn[
  CheckBlock[s_String] := s];

DefFn[
  CheckBlock[a_?AtomQ] := a];

DefFn[
  CheckBlock[l_List] := Map[CheckBlock, l]];

DefFn[
  FlattenBlock[b_] :=
  Module[
    {flattenBlock},
    flattenBlock[x_String] := x;
    flattenBlock[l_List] := StringJoin@@Map[FlattenBlock, l];
    flattenBlock[a_?AtomQ] := ToString[a];

    CheckBlock[b];
    flattenBlock[b]]];

DefFn[
  IndentBlock[block:CodeGenBlock] :=
  StringDrop["  " <> StringReplace[FlattenBlock[block], {"\n" -> "\n  "}],-2]];

(* This should be used everywhere - need to tidy up the newline convention in CodeGen *)
DefFn[
  IndentBlock2[block:CodeGenBlock] :=
  Riffle[Map[StringJoin["  ",#] &,
      StringSplit[FlattenBlock[block],"\n"]],"\n"]];

DefFn[
  SeparatedBlock[block:CodeGenBlock] := {"\n", block}];
ErrorDefinition[SeparatedBlock];

DefFn[
  GenerateFile[filename_String, contents_] :=
  Module[
    {fp = OpenWrite[filename]},
    CheckBlock[contents];
    WriteString[fp, FlattenBlock[contents]];
    Close[fp]]];

DefFn[
  AddToFile[filename_String, contents:CodeGenBlock] :=
  Module[
    {fp = OpenAppend[filename]},
    WriteString[fp, FlattenBlock[contents]];
    Close[fp]]];

DefFn[
  CommaNewlineSeparated[l_List] :=
  Riffle[l, ",\n"]];

DefFn[
  SpaceSeparated[l_List] :=
  Riffle[l, " "]];

DefFn[
  CommaSeparated[l_List] :=
  Riffle[l, ", "]];

DefFn[
  NewlineSeparated[l_List] :=
  Riffle[l, "\n"]];

DefFn[
  CommaInitSeparated[l_List] :=
  Riffle[Map[{#," = INITVALUE"} &, l], ", "]];

(* Turn a section of code into a string:
   1. quote all quotes (replace all quotes with backslash-quote)
   2. break the string into lines to make it readable (replace all newlines
      with quote-newline-quote)
   3. surround the result with quotes *)
DefFn[
  Stringify[x:CodeGenBlock] :=
  "\"" <> StringReplace[StringReplace[FlattenBlock[x], "\"" -> "\\\""],
                        "\n" -> "\\n\"\n\""] <> "\"\n"];

DefFn[
  PartitionVarList[list_List] :=
  Module[
    {partition, split},

    partition[locallist_] :=
    Module[
      {cutoff},
      cutoff = 6;
      If[Length@locallist > cutoff, Partition[locallist, cutoff, cutoff, {1,1}, {}],
         {locallist}]];

    split = Split[list, NameRoot[#1] == NameRoot[#2] &];
    split = Flatten[Map[partition, split], 1];

    split]];

DefFn[
  insertFile[name_String] :=
  Module[
    {istream_, contents_},
    istream = OpenRead[name];
    contents = ReadList[istream, String];
    Close[istream];
    contents]];

DefFn[
  NameRoot[name_Symbol] :=
  Module[
    {dropNumberRule, root},
    dropNumberRule = {"1" -> "", "2" -> "", "3" -> "", "4" -> "", "5" -> "",
                      "6" -> "", "7" -> "", "8" -> "", "9" -> "", "0" -> "", "rhs" -> ""};
    root = StringReplace[ToString@name, dropNumberRule]]];

DefFn[
  Quote[x:CodeGenBlock] :=
  {"\"", x, "\""}];

End[];

EndPackage[];