Skip to content

Commit 3ff2f9d

Browse files
committed
feat: pass Locals to UserFunction for name resolution
- Update UserFunction to store Locals for resolving variable scope - Modify Call method to pass Locals to Eval during function execution - Update Run API to pass resolved locals to Interpret method - Add semantic error handling in Run API
1 parent 645ec59 commit 3ff2f9d

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

internal/lox/api/run_api.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ func Run(source string) (string, int, string) {
1515
tokens := scanner.ScanTokens(mockStdout, mockStderr)
1616
parser := lox.NewParser(tokens)
1717
statements := parser.Parse(mockStdout, mockStderr)
18-
lox.Interpret(statements, mockStdout, mockStderr)
18+
locals, err := lox.Resolve(statements)
19+
if err != nil || lox.HadSemanticError {
20+
return "", 65, err.Error()
21+
}
22+
lox.Interpret(statements, locals, mockStdout, mockStderr)
1923

2024
exitCode := 0
21-
if lox.HadParseError {
25+
if lox.HadParseError || lox.HadSemanticError {
2226
exitCode = 65
2327
} else if lox.HadRuntimeError {
2428
exitCode = 70

internal/lox/functions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type UserFunction struct {
3737
Callable
3838
Declaration *Function
3939
Closure *Environment
40+
Locals Locals // TODO: Pass pointer to Locals
4041
}
4142

4243
// NewUserFunction creates a new UserFunction
@@ -54,7 +55,7 @@ func (u *UserFunction) Call(arguments []interface{}, globalEnv *Environment, std
5455
}
5556

5657
for _, stmt := range u.Declaration.Body {
57-
_, err := Eval(stmt, env, stdout, stderr)
58+
_, err := Eval(stmt, env, u.Locals, stdout, stderr)
5859

5960
if err != nil {
6061
if r, ok := err.(ReturnError); ok {

0 commit comments

Comments
 (0)