Skip to content

Commit

Permalink
cmd/uclang: Add -o flag to specify output path.
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed Jun 18, 2016
1 parent a62d7fc commit e109ea6
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions cmd/uclang/uclang.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
// disable colors in output
// -no-nested-functions
// disable support for nested functions
// -o string
// output path
package main

import (
"flag"
"fmt"
"io"
"log"
"os"

Expand Down Expand Up @@ -47,11 +50,15 @@ func main() {
// goccLexer specifies whether to use the Gocc generated lexer, instead of
// the hand-written lexer.
goccLexer bool
noColors bool
// noColors specifies whether to disable colors in output.
noColors bool
// outputPath specifies the output path for the generated LLVM IR.
outputPath string
)
flag.BoolVar(&goccLexer, "gocc-lexer", false, "use Gocc generated lexer")
flag.BoolVar(&noColors, "no-colors", false, "disable colors in output")
flag.BoolVar(&semcheck.NoNestedFunctions, "no-nested-functions", false, "disable support for nested functions")
flag.StringVar(&outputPath, "o", "", "output path")
flag.Usage = usage
flag.Parse()
semerrors.UseColor = !noColors
Expand All @@ -64,20 +71,25 @@ func main() {
semcheck.NoNestedFunctions = true

// Parse input.
output := os.Stdout
if len(outputPath) > 0 {
var err error
output, err = os.Create(outputPath)
if err != nil {
log.Fatal(errutil.Err(err))
}
defer output.Close()
}
for _, path := range flag.Args() {
err := compileFile(path, goccLexer)
err := compileFile(path, output, goccLexer)
if err != nil {
if _, ok := err.(*semerrors.Error); ok {
elog.Print(err)
} else {
log.Print(err)
}
log.Fatal(err)
}
}
}

// checkFile performs a static semantic analysis check on the given file.
func compileFile(path string, goccLexer bool) error {
func compileFile(path string, output io.Writer, goccLexer bool) error {
// Lexical analysis
// Syntactic analysis
// Semantic analysis
Expand Down Expand Up @@ -129,12 +141,9 @@ func compileFile(path string, goccLexer bool) error {

// Generate LLVM IR module based on the syntax tree of the given file.
module := irgen.Gen(file, info)
log.Printf("=== [ LLVM IR module ] ===\n\n")
fmt.Println(module)
if _, err := fmt.Fprint(output, module); err != nil {
return errutil.Err(err)
}

return nil
}

// elog represents a logger with no prefix or flags, which logs errors to
// standard error.
var elog = log.New(os.Stderr, "", 0)

0 comments on commit e109ea6

Please sign in to comment.