generated from codecrafters-io/tester-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun_api.go
More file actions
43 lines (35 loc) · 1.03 KB
/
run_api.go
File metadata and controls
43 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package loxapi
import (
"bytes"
"github.com/codecrafters-io/interpreter-tester/internal/lox"
)
func Run(source string) (string, int, string) {
lox.ClearErrorFlags()
mockStdout := &bytes.Buffer{}
mockStderr := &bytes.Buffer{}
scanner := lox.NewScanner(source)
tokens := scanner.ScanTokens(mockStdout, mockStderr)
parser := lox.NewParser(tokens)
statements := parser.Parse(mockStdout, mockStderr)
locals, err := lox.Resolve(statements)
if err != nil && lox.HadSemanticError {
return "", 65, err.Error()
}
env := lox.NewGlobal()
lox.Interpret(statements, env, locals, mockStdout, mockStderr)
exitCode := 0
if lox.HadParseError || lox.HadSemanticError {
exitCode = 65
} else if lox.HadRuntimeError {
exitCode = 70
}
capturedStdout := mockStdout.String()
if len(capturedStdout) > 0 {
capturedStdout = capturedStdout[:len(capturedStdout)-1]
}
capturedStderr := mockStderr.String()
if len(capturedStderr) > 0 {
capturedStderr = capturedStderr[:len(capturedStderr)-1]
}
return capturedStdout, exitCode, capturedStderr
}