Skip to content

Commit da6eb5b

Browse files
authored
Merge pull request #1 from microsoft/first-light
Initial port of compiler
2 parents 69d88b2 + 2ab97f0 commit da6eb5b

File tree

13 files changed

+25471
-4
lines changed

13 files changed

+25471
-4
lines changed

cmd/tsgo/main.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,60 @@
22
package main
33

44
import (
5-
"github.com/microsoft/typescript-go/internal/compiler"
5+
"flag"
6+
"fmt"
7+
"runtime"
8+
"strings"
9+
"time"
10+
11+
ts "github.com/microsoft/typescript-go/internal/compiler"
612
)
713

14+
var quiet = false
15+
var singleThreaded = false
16+
var parseAndBindOnly = false
17+
18+
func printDiagnostic(d *ts.Diagnostic, level int) {
19+
file := d.File()
20+
if file != nil {
21+
line, character := ts.GetLineAndCharacterOfPosition(file, d.Loc().Pos())
22+
fmt.Printf("%v%v(%v,%v): error TS%v: %v\n", strings.Repeat(" ", level*2), file.FileName(), line+1, character+1, d.Code(), d.Message())
23+
} else {
24+
fmt.Printf("%verror TS%v: %v\n", strings.Repeat(" ", level*2), d.Code(), d.Message())
25+
}
26+
for _, r := range d.RelatedInformation() {
27+
printDiagnostic(r, level+1)
28+
}
29+
}
30+
831
func main() {
9-
compiler.CreateSourceFile()
32+
flag.BoolVar(&quiet, "q", false, "Quiet output")
33+
flag.BoolVar(&singleThreaded, "s", false, "Single threaded")
34+
flag.BoolVar(&parseAndBindOnly, "p", false, "Parse and bind only")
35+
flag.Parse()
36+
compilerOptions := &ts.CompilerOptions{Target: ts.ScriptTargetESNext, ModuleKind: ts.ModuleKindNodeNext}
37+
programOptions := ts.ProgramOptions{RootPath: flag.Arg(0), Options: compilerOptions, SingleThreaded: singleThreaded}
38+
startTime := time.Now()
39+
program := ts.NewProgram(programOptions)
40+
diagnostics := program.GetSyntacticDiagnostics(nil)
41+
if len(diagnostics) == 0 {
42+
if parseAndBindOnly {
43+
diagnostics = program.GetBindDiagnostics(nil)
44+
} else {
45+
diagnostics = program.GetSemanticDiagnostics(nil)
46+
}
47+
}
48+
compileTime := time.Since(startTime)
49+
var memStats runtime.MemStats
50+
runtime.GC()
51+
runtime.GC()
52+
runtime.ReadMemStats(&memStats)
53+
if !quiet {
54+
for _, diagnostic := range diagnostics {
55+
printDiagnostic(diagnostic, 0)
56+
}
57+
}
58+
fmt.Printf("Files: %v\n", len(program.SourceFiles()))
59+
fmt.Printf("Compile time: %v\n", compileTime)
60+
fmt.Printf("Memory used: %vK\n", memStats.Alloc/1024)
1061
}

0 commit comments

Comments
 (0)