-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
write.go
executable file
·129 lines (120 loc) · 3.17 KB
/
write.go
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
// Copyright 2014 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package genome
import (
"bytes"
"fmt"
"io"
"sort"
"strings"
"go/format"
"github.com/gmlewis/gep/v2/functions"
"github.com/gmlewis/gep/v2/grammars"
)
type dump struct {
w io.Writer
gr *grammars.Grammar
fm functions.FuncMap
genome *Genome
subs map[string]string
}
func (g *Genome) Write(w io.Writer, grammar *grammars.Grammar) {
d := &dump{
gr: grammar,
genome: g,
subs: map[string]string{
"CHARX": "X",
},
}
code, err := d.generateCode()
if err != nil {
fmt.Printf("error generating code: %v", err)
}
fmt.Fprintf(w, "%s", code)
}
func (d *dump) generateCode() ([]byte, error) {
var buf bytes.Buffer
d.w = &buf
// d.write("// GML: d.gr.Open\n")
d.write(d.gr.Open)
for _, h := range d.gr.Headers {
if h.Type != "default" {
continue
}
// d.write(fmt.Sprintf("// GML: d.gr.Headers: h=%#v\n", h))
d.write(h.Chardata)
d.write(d.gr.Endline)
}
for _, t := range d.gr.Tempvars {
if t.Type != "default" {
continue
}
// d.write(fmt.Sprintf("// GML: d.gr.Tempvars: t=%#v\n", t))
d.write(t.Chardata)
d.subs["tempvarname"] = t.Varname
d.write(d.gr.Endline)
}
// Generate the expression, keeping track of any helper functions that are needed.
helpers := make(grammars.HelperMap)
s, ok := d.gr.Functions.FuncMap[d.genome.LinkFunc]
if !ok {
return nil, fmt.Errorf("unable to find grammar linking function: %v", s.Symbol())
}
glf, ok := s.(*grammars.Function)
if !ok {
return nil, fmt.Errorf("error casting link function: %v", s.Symbol())
}
exps := []string{""}
for i, e := range d.genome.Genes {
// d.write(fmt.Sprintf("// GML: d.genome.Genes: e=%#v\n", e))
exp, err := e.Expression(d.gr, helpers)
if err != nil {
return nil, err
}
if i > 0 {
// d.write(fmt.Sprintf("// GML: len(d.genome.Genes)=%v\n", len(d.genome.Genes)))
merge := strings.Replace(glf.Uniontype, "{tempvarname}", d.subs["tempvarname"], -1)
merge = strings.Replace(merge, "{member}", exp, -1)
merge = strings.Replace(merge, "{symbol}", glf.SymbolName, -1)
exps = append(exps, merge)
} else {
// d.write(fmt.Sprintf("// GML: len(d.genome.Genes)=%v\n", len(d.genome.Genes)))
exps = append(exps, d.subs["tempvarname"]+" = "+exp)
}
}
exps = append(exps, "") // blank line
fmt.Fprintln(d.w, strings.Join(exps, "\n"))
for _, f := range d.gr.Footers {
if f.Type != "default" {
continue
}
// d.write(fmt.Sprintf("// GML: d.gr.Footers=%#v\n", f))
d.write(f.Chardata)
d.write(d.gr.Endline)
}
if len(helpers) > 0 { // Write out the helpers
keys := make([]string, 0, len(helpers))
for k := range helpers {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
d.write(d.gr.Endline)
d.write(helpers[k])
}
}
clean, err := format.Source(buf.Bytes())
if err != nil {
return buf.Bytes(), err
}
return clean, nil
}
func (d *dump) write(s string) {
s = strings.Replace(s, "{CRLF}", "\n", -1)
s = strings.Replace(s, "{TAB}", "\t", -1)
for k, v := range d.subs {
s = strings.Replace(s, fmt.Sprintf("{%v}", k), v, -1)
}
fmt.Fprint(d.w, s)
}