-
Notifications
You must be signed in to change notification settings - Fork 0
/
foresee.go
131 lines (114 loc) · 3.15 KB
/
foresee.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
/*
*
* Copyright 2023 foresee authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses", as
* defined by the Mozilla Public License, v. 2.0.
*
*/
package main
import (
"bufio"
"bytes"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/dvaumoron/foresee/builtins/compile"
"github.com/dvaumoron/foresee/builtins/eval"
"github.com/dvaumoron/foresee/builtins/names"
"github.com/dvaumoron/foresee/infer"
"github.com/dvaumoron/foresee/parser"
"github.com/dvaumoron/foresee/types"
)
const (
fileExt = ".fc"
fileExtLen = len(fileExt)
)
//go:generate gennames -output "builtins/compile/hints.go" -package "compile" -name "standardLibraryHints" -standard -novendor -path "./..."
func main() {
if !loadGoMod() {
return
}
if len(os.Args) > 1 {
for _, filePath := range os.Args[1:] {
processFile(filePath)
}
return
}
fmt.Println("No files listed, walking current directory")
filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
if err == nil && !d.IsDir() && path[len(path)-fileExtLen:] == fileExt {
processFile(path)
}
return err
})
}
func loadGoMod() bool {
goModFile, err := os.Open("go.mod")
if err != nil {
fmt.Println("Error while reading go.mod :", err)
return false
}
defer goModFile.Close()
moduleName, ok := "", false
scanner := bufio.NewScanner(goModFile)
if scanner.Scan() {
if moduleName, ok = strings.CutPrefix(scanner.Text(), "module "); !ok {
fmt.Println("Error while parsing go.mod : should start with module declaration line")
return false
}
}
if err = scanner.Err(); err != nil {
fmt.Println("Error while parsing go.mod :", err)
return false
}
if ok = moduleName != ""; ok {
eval.Builtins.StoreStr(names.HiddenModule, types.String(moduleName))
compile.Builtins.StoreStr(names.HiddenModule, types.String(moduleName))
}
return ok
}
func processFile(filePath string) {
data, err := os.ReadFile(filePath)
if err != nil {
fmt.Println("Error while reading", filePath, ":", err)
return
}
parsed, err := parser.Parse(string(data))
if err != nil {
fmt.Println("Error while parsing", filePath, ":", err)
return
}
expanded, err := eval.ExpandMacro(parsed)
if err != nil {
fmt.Println("Error while expanding", filePath, ":", err)
return
}
// TODO manage inference across multiple file
infered, err := infer.InferTypes(expanded)
if err != nil {
fmt.Println("Error while infering", filePath, ":", err)
return
}
var outputdata bytes.Buffer
outputPath := computeOutputPath(filePath)
if err = compile.Compile(infered).Render(&outputdata); err != nil {
fmt.Println("Error while rendering", outputPath, ":", err)
return
}
if err = os.WriteFile(outputPath, outputdata.Bytes(), 0644); err != nil {
fmt.Println("Error while writing", outputPath, ":", err)
}
}
func computeOutputPath(filePath string) string {
if dotIndex := strings.LastIndexByte(filePath, '.'); dotIndex != -1 {
filePath = filePath[:dotIndex]
}
return filePath + ".go"
}