Skip to content

Commit

Permalink
Make -d, -da, and -dt print to stdout and exit (#155)
Browse files Browse the repository at this point in the history
Move debug arguments to separate section in -h/--help output.

Remove unused fields from parser struct.

Make -d, -da, and -dt args print to stdout and stderr, and make goawk
exit if any of them are specified.
  • Loading branch information
xonixx committed Oct 20, 2022
1 parent 0b251f1 commit f2c16f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
23 changes: 15 additions & 8 deletions goawk.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ const (
-f progfile load AWK source from progfile (multiple allowed)
-v var=value variable assignment (multiple allowed)
Additional GoAWK arguments:
-cpuprofile file write CPU profile to file
-d print parsed syntax tree to stderr (debug mode)
-da print virtual machine assembly instructions to stderr
-dt print variable type information to stderr
Additional GoAWK features:
-E progfile load program, treat as last option, disable var=value args
-H parse header row and enable @"field" in CSV input mode
-h, --help show this help message
Expand All @@ -65,6 +61,13 @@ Additional GoAWK arguments:
-o mode use CSV output for print with args (ignore OFS and ORS)
'csv|tsv [separator=<char>]'
-version show GoAWK version and exit
GoAWK debugging arguments:
-cpuprofile file write CPU profile to file
-d print parsed syntax tree to stdout and exit
-da print VM assembly instructions to stdout and exit
-dt print variable type information to stdout and exit
-memprofile file write memory profile to file
`
)

Expand Down Expand Up @@ -231,7 +234,7 @@ argsLoop:
// Parse source code and setup interpreter
parserConfig := &parser.ParserConfig{
DebugTypes: debugTypes,
DebugWriter: os.Stderr,
DebugWriter: os.Stdout,
}
prog, err := parser.ParseProgram(fileReader.Source(), parserConfig)
if err != nil {
Expand All @@ -246,16 +249,20 @@ argsLoop:
}

if debug {
fmt.Fprintln(os.Stderr, prog)
fmt.Fprintln(os.Stdout, prog)
}

if debugAsm {
err := prog.Disassemble(os.Stderr)
err := prog.Disassemble(os.Stdout)
if err != nil {
errorExitf("could not disassemble program: %v", err)
}
}

if debug || debugAsm || debugTypes {
os.Exit(0)
}

if header {
if inputMode == "" {
errorExitf("-H only allowed together with -i")
Expand Down
8 changes: 0 additions & 8 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ func ParseProgram(src []byte, config *ParserConfig) (prog *Program, err error) {
}()
lexer := NewLexer(src)
p := parser{lexer: lexer}
if config != nil {
p.debugTypes = config.DebugTypes
p.debugWriter = config.DebugWriter
}
p.multiExprs = make(map[*ast.MultiExpr]Position, 3)

p.next() // initialize p.tok
Expand Down Expand Up @@ -136,10 +132,6 @@ type parser struct {

// Variable tracking and resolving
multiExprs map[*ast.MultiExpr]Position // tracks comma-separated expressions

// Configuration and debugging
debugTypes bool // show variable types for debugging
debugWriter io.Writer // where the debug output goes
}

// Parse an entire AWK program.
Expand Down

0 comments on commit f2c16f0

Please sign in to comment.