Skip to content

Commit 31bb014

Browse files
authored
Merge pull request #3 from strict-lang/feature/diagnostics
Add diagnostics and cross-compile fix
2 parents 0da2bc7 + d7f0086 commit 31bb014

File tree

18 files changed

+297
-179
lines changed

18 files changed

+297
-179
lines changed

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ _bundle: &_bundle
3030
stage: bundle
3131
script:
3232
- EXECUTABLE_PATH=./output/$TRAVIS_BRANCH/$STRICT_PLATFORM/$STRICT_ARCHITECTURE/strict
33-
- go build -o $EXECUTABLE_PATH ./cmd/strict
33+
- GOOS=$COMPILE_OS GOARCH=$COMPILE_ARCH go build -o $EXECUTABLE_PATH ./cmd/strict
3434
- RESULT_DIRECTORY=output/$TRAVIS_BRANCH/artifacts
3535
- strict_sdk make -p $STRICT_PLATFORM -a $STRICT_ARCHITECTURE -b -o $RESULT_DIRECTORY -v $TRAVIS_BRANCH -e $EXECUTABLE_PATH
3636
deploy:
@@ -47,13 +47,13 @@ jobs:
4747
# Bundles a Strict SDK by using the executable built in the 'build' stage and deploys it to
4848
# the Github releases. The $GITHUB_OAUTH_TOKEN has to be defined.
4949
- name: bundle-osx
50-
env: [STRICT_PLATFORM=osx STRICT_ARCHITECTURE=x86_64 GOOS=darwin GOARCH=amd]
50+
env: [STRICT_PLATFORM=osx STRICT_ARCHITECTURE=x86_64 COMPILE_OS=darwin COMPILE_ARCH=amd64]
5151
<<: *_bundle
5252
- name: bundle-linux
53-
env: [STRICT_PLATFORM=windows STRICT_ARCHITECTURE=x86_64 GOOS=windows GOARCH=amd]
53+
env: [STRICT_PLATFORM=windows STRICT_ARCHITECTURE=x86_64 COMPILE_OS=windows COMPILE_ARCH=amd64]
5454
<<: *_bundle
5555
- name: bundle-windows
56-
env: [STRICT_PLATFORM=linux STRICT_ARCHITECTURE=x86_64 GOOS=linux GOARCH=amd]
56+
env: [STRICT_PLATFORM=linux STRICT_ARCHITECTURE=x86_64 COMPILE_OS=linux COMPILE_ARCH=amd64]
5757
<<: *_bundle
5858
# Runs all tests.
5959
- stage: test

cmd/strict/command_build.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/spf13/cobra"
66
"github.com/strict-lang/sdk/pkg/compiler"
77
"github.com/strict-lang/sdk/pkg/compiler/backend"
8+
"github.com/strict-lang/sdk/pkg/compiler/input/linemap"
89
"github.com/strict-lang/sdk/pkg/compiler/report"
910
"io/ioutil"
1011
"log"
@@ -40,18 +41,18 @@ func disableLogging() {
4041
}
4142
}
4243

43-
var reportFormats = map[string] func(report.Report) report.Output {
44+
var reportFormats = map[string] func(report.Report, *linemap.LineMap) report.Output {
4445
"text": report.NewRenderingOutput,
45-
"json": func(input report.Report) report.Output {
46+
"json": func(input report.Report, lineMap *linemap.LineMap) report.Output {
4647
return report.NewSerializingOutput(report.NewJsonSerializationFormat(), input)
4748
},
48-
"pretty-json": func(input report.Report) report.Output {
49+
"pretty-json": func(input report.Report, lineMap *linemap.LineMap) report.Output {
4950
return report.NewSerializingOutput(report.NewPrettyJsonSerializationFormat(), input)
5051
},
51-
"xml": func(input report.Report) report.Output {
52+
"xml": func(input report.Report, lineMap *linemap.LineMap) report.Output {
5253
return report.NewSerializingOutput(report.NewXmlSerializationFormat(), input)
5354
},
54-
"pretty-xml": func(input report.Report) report.Output {
55+
"pretty-xml": func(input report.Report, lineMap *linemap.LineMap) report.Output {
5556
return report.NewSerializingOutput(report.NewPrettyXmlSerializationFormat(), input)
5657
},
5758
}
@@ -69,47 +70,56 @@ func createFailedReport(beginTime time.Time) report.Report {
6970

7071
func RunCompile(command *cobra.Command, arguments []string) error {
7172
disableLogging()
72-
compilationReport := compile(command, arguments)
73-
output := createOutput(compilationReport)
73+
compilationReport, lineMap := compile(command, arguments)
74+
output := createOutput(compilationReport, lineMap)
7475
return output.Print(command.OutOrStdout())
7576
}
7677

77-
func createOutput(compilationReport report.Report) report.Output {
78+
func createOutput(
79+
compilationReport report.Report,
80+
lineMap *linemap.LineMap) report.Output {
81+
7882
if output, ok := reportFormats[buildOptions.reportFormat]; ok {
79-
return output(compilationReport)
83+
return output(compilationReport, lineMap)
8084
}
81-
return report.NewRenderingOutput(compilationReport)
85+
return report.NewRenderingOutput(compilationReport, lineMap)
8286
}
8387

84-
func compile(command *cobra.Command, arguments []string) report.Report {
88+
func compile(
89+
command *cobra.Command, arguments []string) (report.Report, *linemap.LineMap) {
90+
8591
beginTime := time.Now()
8692
file, ok := findSourceFileInArguments(command, arguments)
8793
if !ok {
88-
return createFailedReport(beginTime)
94+
return createFailedReport(beginTime), linemap.Empty()
8995
}
9096
defer file.Close()
9197
name, err := ParseUnitName(file.Name())
9298
if err != nil {
9399
command.Printf("Invalid filename: %s\n", file.Name())
94-
return createFailedReport(beginTime)
100+
return createFailedReport(beginTime), linemap.Empty()
95101
}
96102
return runCompilation(command, name, file)
97103
}
98104

99-
func runCompilation(command *cobra.Command, unitName string, file *os.File) report.Report {
105+
func runCompilation(
106+
command *cobra.Command,
107+
unitName string,
108+
file *os.File) (report.Report, *linemap.LineMap) {
109+
100110
compilation := &compiler.Compilation{
101111
Name: unitName,
102112
Source: &compiler.FileSource{File: file},
103113
}
104114
result := compilation.Compile()
105115
if result.Error != nil {
106-
return result.Report
116+
return result.Report, result.LineMap
107117
}
108118
if err := writeGeneratedSources(result); err != nil {
109119
command.PrintErrf("failed to write generated sources %v\n", err)
110120
result.Report.Success = false
111121
}
112-
return result.Report
122+
return result.Report, result.LineMap
113123
}
114124

115125
func writeGeneratedSources(compilation compiler.Result) (err error) {

cmd/strict/command_tree.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ func createUnitNameFromFileName(name string) string {
3333

3434
func parseAndPrintAst(command *cobra.Command, sourceFile *os.File) {
3535
parseResult := compiler.ParseFile(sourceFile.Name(), sourceFile)
36-
parseResult.Diagnostics.PrintEntries(&cobraDiagnosticPrinter{command})
3736
if parseResult.Error != nil {
3837
return
3938
}

pkg/compiler/analysis/import_pass.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/strict-lang/sdk/pkg/compiler/isolate"
77
passes "github.com/strict-lang/sdk/pkg/compiler/pass"
88
"github.com/strict-lang/sdk/pkg/compiler/scope"
9+
"io/ioutil"
910
"os"
10-
"path/filepath"
1111
"strings"
1212
)
1313

@@ -54,11 +54,15 @@ func (pass *ImportPass) importWorkingDirectory(scope scope.MutableScope) {
5454
pass.importFiles(files, scope)
5555
}
5656

57-
func (pass *ImportPass) shouldImportFile(name string) bool {
58-
return !strings.HasSuffix(name, pass.currentFile+strictFileExtension)
57+
func (pass *ImportPass) shouldImportFile(info os.FileInfo) bool {
58+
name := info.Name()
59+
return strings.HasSuffix(name, pass.currentFile + strictFileExtension)
5960
}
6061

61-
func filterFiles(files []string, filter func(string) bool) (filtered []string) {
62+
func filterFiles(
63+
files []os.FileInfo,
64+
filter func(info os.FileInfo) bool) (filtered []os.FileInfo) {
65+
6266
for _, file := range files {
6367
if filter(file) {
6468
filtered = append(filtered, file)
@@ -76,25 +80,28 @@ func (pass *ImportPass) importDirectory(directory string, scope scope.MutableSco
7680
pass.importFiles(files, scope)
7781
}
7882

79-
func (pass *ImportPass) importFiles(files []string, scope scope.MutableScope) {
80-
importing := NewSourceImporting(files)
83+
func (pass *ImportPass) importFiles(files []os.FileInfo, scope scope.MutableScope) {
84+
names := convertInfosToNames(files)
85+
importing := NewSourceImporting(names)
8186
if err := importing.Import(scope); err != nil {
8287
pass.reportFailedImport(err)
8388
}
8489
}
8590

86-
func listFilesInDirectory(directory string) (files []string, err error) {
87-
err = filepath.Walk(directory, func(path string, info os.FileInfo, walkErr error) error {
88-
if walkErr == nil && isStrictFile(info) {
89-
files = append(files, path)
90-
}
91-
return walkErr
92-
})
93-
return files, err
91+
func convertInfosToNames(infos []os.FileInfo) (names []string) {
92+
for _, info := range infos {
93+
names = append(names, info.Name())
94+
}
95+
return
96+
}
97+
98+
func listFilesInDirectory(directory string) (files []os.FileInfo, err error) {
99+
return ioutil.ReadDir(directory)
94100
}
95101

96102
func listFilesInDirectoryFiltered(
97-
directory string, filter func(string) bool) ([]string, error) {
103+
directory string,
104+
filter func(info os.FileInfo) bool) ([]os.FileInfo, error) {
98105

99106
files, err := listFilesInDirectory(directory)
100107
if err != nil {

pkg/compiler/analysis/source_importing.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package analysis
22

33
import (
44
"fmt"
5-
"github.com/strict-lang/sdk/pkg/compiler/diagnostic"
65
"github.com/strict-lang/sdk/pkg/compiler/grammar/syntax"
76
"github.com/strict-lang/sdk/pkg/compiler/grammar/tree"
87
"github.com/strict-lang/sdk/pkg/compiler/input"
@@ -70,7 +69,6 @@ func (importing *SourceImporting) parseFile(name string) *tree.TranslationUnit {
7069
defer file.Close()
7170
result := syntax.Parse(name, input.NewStreamReader(file))
7271
if result.Error != nil {
73-
result.Diagnostics.PrintEntries(diagnostic.NewFmtPrinter())
7472
importing.reportFailedParse(name, result.Error)
7573
return nil
7674
}

pkg/compiler/analysis/type_resolution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (pass *TypeResolution) resolveCalledMethod(
167167
}
168168

169169
func (pass *TypeResolution) reportFailedInference(node tree.Node) {
170-
panic("Failed to infer node")
170+
// panic("Failed to infer node")
171171
}
172172

173173
func isResolved(expression tree.Expression) bool {

pkg/compiler/compilation.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ package compiler
33
import (
44
"fmt"
55
"github.com/strict-lang/sdk/pkg/compiler/backend"
6+
_ "github.com/strict-lang/sdk/pkg/compiler/backend/arduino"
7+
_ "github.com/strict-lang/sdk/pkg/compiler/backend/cpp"
8+
_ "github.com/strict-lang/sdk/pkg/compiler/backend/ilasm"
9+
_ "github.com/strict-lang/sdk/pkg/compiler/backend/silk"
610
"github.com/strict-lang/sdk/pkg/compiler/diagnostic"
711
"github.com/strict-lang/sdk/pkg/compiler/grammar/syntax"
812
"github.com/strict-lang/sdk/pkg/compiler/grammar/tree"
913
"github.com/strict-lang/sdk/pkg/compiler/input"
14+
"github.com/strict-lang/sdk/pkg/compiler/input/linemap"
1015
isolates "github.com/strict-lang/sdk/pkg/compiler/isolate"
1116
"github.com/strict-lang/sdk/pkg/compiler/lowering"
1217
"github.com/strict-lang/sdk/pkg/compiler/pass"
1318
"github.com/strict-lang/sdk/pkg/compiler/report"
19+
"log"
1420
"time"
1521
)
1622

@@ -29,6 +35,7 @@ type Result struct {
2935
GeneratedFiles []backend.GeneratedFile
3036
Report report.Report
3137
Error error
38+
LineMap *linemap.LineMap
3239
}
3340

3441
type Generated struct {
@@ -104,7 +111,6 @@ func translateDiagnosticKind(kind *diagnostic.Kind) report.DiagnosticKind {
104111
}
105112
}
106113

107-
108114
func (compilation *Compilation) Compile() Result {
109115
compilation.beginTime = time.Now()
110116
parseResult := compilation.parse()
@@ -115,14 +121,18 @@ func (compilation *Compilation) Compile() Result {
115121
Report: compilation.createReport(failure),
116122
Error: parseResult.Error,
117123
UnitName: compilation.Name,
124+
LineMap: parseResult.LineMap,
118125
}
119126
}
120127
compilation.Lower(parseResult.TranslationUnit)
128+
generatedFiles, err := compilation.generateOutput(parseResult.TranslationUnit)
129+
121130
return Result{
122-
GeneratedFiles: compilation.generateOutput(parseResult.TranslationUnit),
131+
GeneratedFiles: generatedFiles,
123132
Report: compilation.createReport(success),
124-
Error: nil,
133+
Error: err,
125134
UnitName: parseResult.TranslationUnit.Name,
135+
LineMap: parseResult.LineMap,
126136
}
127137
}
128138

@@ -140,13 +150,16 @@ func (compilation *Compilation) parse() syntax.Result {
140150
}
141151

142152
func (compilation *Compilation) generateOutput(
143-
unit *tree.TranslationUnit) []backend.GeneratedFile {
153+
unit *tree.TranslationUnit) ([]backend.GeneratedFile, error) {
144154

145-
output, _ := compilation.invokeBackend(backend.Input{
155+
output, err := compilation.invokeBackend(backend.Input{
146156
Unit: unit,
147157
Diagnostics: diagnostic.NewBag(),
148158
})
149-
return output.GeneratedFiles
159+
if err != nil {
160+
return nil, err
161+
}
162+
return output.GeneratedFiles, nil
150163
}
151164

152165
func (compilation *Compilation) invokeBackend(
@@ -156,6 +169,7 @@ func (compilation *Compilation) invokeBackend(
156169
backendId := compilation.Backend
157170
chosenBackend, ok := backend.LookupInIsolate(isolate, backendId)
158171
if ok {
172+
log.Printf("compiling files with %s backend", backendId)
159173
return chosenBackend.Generate(input)
160174
}
161175
return backend.Output{}, fmt.Errorf("no such backend: %s", backendId)

pkg/compiler/diagnostic/diagnostics.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,3 @@ func (diagnostics *Diagnostics) ListEntries() (values []Entry) {
1212
}
1313
return values
1414
}
15-
16-
func (diagnostics *Diagnostics) PrintEntries(printer Printer) {
17-
for _, entries := range diagnostics.entries {
18-
for _, entry := range entries {
19-
entry.PrintColored(printer)
20-
}
21-
printer.PrintLine("")
22-
}
23-
}

0 commit comments

Comments
 (0)