Skip to content

Commit 9f5e67d

Browse files
committed
Improve output formatting, add timeouts for NS compilation and roq decompilation
1 parent 5b84340 commit 9f5e67d

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

cmd/ns/ns.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os/exec"
1414
"path/filepath"
1515
"strings"
16+
"time"
1617
)
1718

1819
const (
@@ -111,23 +112,46 @@ func RunNeverscript(arguments CommandLineArguments) error {
111112
return errors.New("ERROR - Target game must be thps3/thps4/thug1/thug2")
112113
}
113114

114-
compilationError := compiler.Compile(*arguments.FileToCompile, outputFilename, &lexer, &parser, &bytecodeCompiler)
115+
compilationChannel := make(chan compiler.Error, 1)
116+
go func() {
117+
compilationError := compiler.Compile(*arguments.FileToCompile, outputFilename, &lexer, &parser, &bytecodeCompiler)
118+
compilationChannel <- compilationError
119+
}()
120+
var compilationError compiler.Error
121+
select {
122+
case result := <-compilationChannel:
123+
compilationError = result
124+
case <-time.After(3 * time.Second):
125+
return errors.New("ERROR - Compiler took too long. It probably went into an infinite loop because of a bug or an unimplemented feature")
126+
fmt.Println("\nWARNING - Roq decompiler froze. Some QB cannot be decompiled, e.g. adjacent line ending bytes (0x01 0x01)")
127+
}
115128
if compilationError != nil {
116129
return errors.New(fmt.Sprintf("ERROR %s(line %d) - %s", filepath.Base(*arguments.FileToCompile), compilationError.GetLineNumber(), compilationError.GetMessage()))
117130
}
118-
fmt.Printf(" Created '%s'.\n", outputFilename)
131+
fmt.Printf("\n Created '%s'.\n", outputFilename)
119132

120133
if *arguments.ShowHexDump {
121-
fmt.Printf("\n%s\n", hex.Dump(bytecodeCompiler.Bytes))
122-
} else {
123-
fmt.Println()
134+
fmt.Printf("\n%s", hex.Dump(bytecodeCompiler.Bytes))
124135
}
125136

126137
if *arguments.ShowDecompiledRoq {
127-
fmt.Println("Roq decompiler output (may freeze):")
128-
roqCmd := exec.Command("roq.exe", "-d", outputFilename)
129-
decompiledCode, _ := roqCmd.Output()
130-
fmt.Println(string(decompiledCode))
138+
fmt.Println("\nRoq decompiler output:")
139+
140+
roqChannel := make(chan string, 1)
141+
142+
go func() {
143+
roqCmd := exec.Command("roq.exe", "-d", outputFilename)
144+
decompiledCode, _ := roqCmd.Output()
145+
roqChannel <- "\n" + strings.TrimSpace(string(decompiledCode))
146+
}()
147+
148+
select {
149+
case decompiledRoq := <-roqChannel:
150+
fmt.Println(decompiledRoq)
151+
case <-time.After(3 * time.Second):
152+
fmt.Println("\nWARNING - Roq decompiler froze. Some QB cannot be decompiled, e.g. adjacent line ending bytes (0x01 0x01)")
153+
}
154+
131155
}
132156
} else if *arguments.FileToDecompile != "" {
133157
argumentsWereSupplied = true

compiler/tests/verify_error_messages.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func main() {
3838
check(IncompleteStruct)
3939
check(IncompleteArray)
4040
check(IncompleteScript)
41+
// check(ExtraCurlyBrace)
4142
check(ExtraParenthesis)
4243
check(IncompleteVector)
4344
check(IncompletePair)

0 commit comments

Comments
 (0)