-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
233 lines (202 loc) · 4.86 KB
/
main.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"bytes"
"flag"
"fmt"
"go/format"
"go/token"
"io/ioutil"
"os"
"os/exec"
"regexp"
"runtime"
"runtime/debug"
"strings"
"github.com/Konstantin8105/f4go/fortran"
)
var (
packageFlag *string
simplifyFlag *bool
parallelFlag *int
verboseFlag *int
)
func init() {
p := 1
parallelFlag = &p
v := 1
verboseFlag = &v
}
func main() {
packageFlag = flag.String("p",
"main", "set the name of the generated package")
simplifyFlag = flag.Bool("s",
false, "simplify from (*I) to I of Golang file. NOT guaranteed to work correctly")
parallelFlag = flag.Int("m",
1, "enable parallelism in file processing. Default is only one core")
verboseFlag = flag.Int("v",
1, "0: error output, 1: print log, 2: print info, 3: print debug")
run()
}
func run() {
if packageFlag == nil {
packageFlag = new(string)
}
// free 1 CPU for other computer stuff
runtime.GOMAXPROCS(*parallelFlag)
flag.Parse()
fortran.Verbose = *verboseFlag
if flag.NArg() < 1 {
fortran.Errorf("Please run: f4go -h")
return
}
if packageFlag == nil {
var s string
packageFlag = &s
}
var es []errorRow
if *parallelFlag > 1 {
es = parseParallel(flag.Args(), *packageFlag)
} else {
for _, s := range flag.Args() {
fortran.Logf("parsing file %s\n", s)
er := parse(s, *packageFlag, "")
es = append(es, er...)
}
}
for _, e := range es {
fortran.Logf("%20s : %s\n", e.filename, e.err.Error())
}
}
type errorRow struct {
err error
filename string
}
func (err errorRow) Error() string {
return fmt.Sprintf("%s : %v", err.filename, err.err)
}
// parsing to Go code
func parse(filename, packageName, goFilename string) (errR []errorRow) {
defer func() {
err := recover()
if err != nil {
fortran.Errorf("panic parsing %s: %s\n%s", filename, err, string(debug.Stack()))
os.Exit(1)
}
}()
if packageName == "" {
packageName = "main"
}
// read fortran source
dat, err := ioutil.ReadFile(filename)
if err != nil {
return []errorRow{
{
err: fmt.Errorf("Cannot fortran source: %v", err),
filename: filename,
},
}
}
// remove some end line symbols
dat = bytes.Replace(dat, []byte{'\r'}, []byte{}, -1)
dat = bytes.Replace(dat, []byte{'\015'}, []byte{}, -1)
// parse fortran to go/ast
ast, errs := fortran.Parse(dat, packageName)
if len(errs) > 0 {
for _, er := range errs {
errR = append(errR, errorRow{
err: fmt.Errorf("Parsing error : %v", er.Error()),
filename: filename,
})
}
}
// convert ast tree to string
var buf bytes.Buffer
if err = format.Node(&buf, token.NewFileSet(), &ast); err != nil {
return []errorRow{{err: fmt.Errorf("Error go/format : %v", err), filename: filename}}
}
// generate filename of result
if goFilename == "" {
index := strings.LastIndex(filename, ".")
goFilename = filename + ".go"
if index > 0 {
goFilename = filename[:index] + ".go"
}
}
// save go source
if err = ioutil.WriteFile(goFilename, buf.Bytes(), 0644); err != nil {
return []errorRow{{err: fmt.Errorf("Cannot write Go source: %v", err), filename: filename}}
}
// gofmt simplification
_, _ = exec.Command("gofmt", "-s", "-w", goFilename).CombinedOutput()
// goimports
_, _ = exec.Command("goimport", "-w", goFilename).CombinedOutput()
if simplifyFlag == nil {
simplifyFlag = new(bool)
}
if *simplifyFlag {
simplify(goFilename)
}
return
}
func parseParallel(filenames []string, packageName string) (ess []errorRow) {
var (
jobs = make(chan string, len(filenames))
results = make(chan []errorRow, len(filenames))
)
for w := 1; w <= 2*runtime.NumCPU(); w++ {
go func(jobs <-chan string, results chan<- []errorRow) {
for job := range jobs {
results <- parse(job, packageName, "")
}
}(jobs, results)
}
for _, f := range filenames {
jobs <- f
}
close(jobs)
for range filenames {
ess = append(ess, <-results...)
}
return
}
func simplify(filename string) {
// read file
content, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
{
// from : (*(L)) -> L
re := regexp.MustCompile(`\(\*\((?P<name>[[:word:]]*)\)\)`)
content = re.ReplaceAll(content, []byte("$1"))
}
{
// from : (*K) -> K
re := regexp.MustCompile(`\(\*(?P<name>[[:word:]]*)\)`)
content = re.ReplaceAll(content, []byte("$1"))
}
{
// from: A := new(int) -> var A int
re := regexp.MustCompile(`(?P<name>[[:word:]]*) := new\((?P<typ>[[:word:]]*)\)`)
content = re.ReplaceAll(content, []byte("var $1 $2"))
}
{
// from: (123) -> 123
re := regexp.MustCompile(`\((?P<name>[[:digit:]]*)\)`)
content = re.ReplaceAll(content, []byte("$1"))
}
{
// *int
content = bytes.ReplaceAll(content, []byte("*int"), []byte("int"))
content = bytes.ReplaceAll(content, []byte("*float"), []byte("float"))
}
{
// *[]int
content = bytes.ReplaceAll(content, []byte("*[]"), []byte("[]"))
}
// write file
err = ioutil.WriteFile(filename, content, 0644)
if err != nil {
panic(err)
}
}