Skip to content

Commit 4648e41

Browse files
committed
feat: improve interpreter
1 parent 65277b1 commit 4648e41

14 files changed

Lines changed: 753 additions & 73 deletions

File tree

internal/interpreter/builtin.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package interpreter
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hulo-lang/hulo/internal/object"
7+
)
8+
9+
func echo(args ...object.Value) object.Value {
10+
for i, arg := range args {
11+
if i > 0 {
12+
fmt.Print(" ")
13+
}
14+
fmt.Print(arg.Text())
15+
}
16+
fmt.Println()
17+
return nil
18+
}
19+
20+
var builtin = map[string]object.Value{
21+
"echo": object.NewFunctionValue(object.NewFunctionBuilder("echo").
22+
WithBuiltin(echo).
23+
WithVariadicParameter("args", object.GetAnyType()).
24+
WithReturnType(object.GetVoidType()).
25+
Build()),
26+
"print": object.NewFunctionValue(object.NewFunctionBuilder("print").
27+
WithBuiltin(echo).
28+
WithVariadicParameter("args", object.GetAnyType()).
29+
WithReturnType(object.GetVoidType()).
30+
Build()),
31+
"nop": object.NewFunctionValue(object.NewFunctionBuilder("nop").
32+
WithBuiltin(func(args ...object.Value) object.Value { return nil }).
33+
WithReturnType(object.GetVoidType()).
34+
Build()),
35+
}

internal/interpreter/env.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func (e *Environment) Fork() *Environment {
3939
return env
4040
}
4141

42-
// Get 获取变量值,按优先级查找:const -> let -> var
43-
func (e *Environment) Get(name string) (object.Value, bool) {
42+
// GetValue 获取变量值,按优先级查找:const -> let -> var
43+
func (e *Environment) GetValue(name string) (object.Value, bool) {
4444
// 先查找 const
4545
if info, ok := e.consts[name]; ok {
4646
return info.Value, true
@@ -55,7 +55,7 @@ func (e *Environment) Get(name string) (object.Value, bool) {
5555
}
5656
// 如果当前环境没有,查找外层环境
5757
if e.outer != nil {
58-
return e.outer.Get(name)
58+
return e.outer.GetValue(name)
5959
}
6060
return nil, false
6161
}

internal/interpreter/env_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ func TestEnvironment_DeclareAndGet(t *testing.T) {
3333
}
3434

3535
// 测试获取变量
36-
if value, ok := env.Get("PI"); !ok || value != constValue {
36+
if value, ok := env.GetValue("PI"); !ok || value != constValue {
3737
t.Errorf("Failed to get const variable PI")
3838
}
3939

40-
if value, ok := env.Get("message"); !ok || value != letValue {
40+
if value, ok := env.GetValue("message"); !ok || value != letValue {
4141
t.Errorf("Failed to get let variable message")
4242
}
4343

44-
if value, ok := env.Get("flag"); !ok || value != varValue {
44+
if value, ok := env.GetValue("flag"); !ok || value != varValue {
4545
t.Errorf("Failed to get var variable flag")
4646
}
4747

@@ -92,7 +92,7 @@ func TestEnvironment_Reassign(t *testing.T) {
9292
}
9393

9494
// 验证新值
95-
if value, ok := env.Get("x"); !ok || value != newValue {
95+
if value, ok := env.GetValue("x"); !ok || value != newValue {
9696
t.Errorf("Failed to get reassigned value")
9797
}
9898
}
@@ -117,7 +117,7 @@ func TestEnvironment_ConstReassign(t *testing.T) {
117117
}
118118

119119
// 验证原值未改变
120-
if value, ok := env.Get("PI"); !ok || value != constValue {
120+
if value, ok := env.GetValue("PI"); !ok || value != constValue {
121121
t.Errorf("Const variable value should not change")
122122
}
123123
}
@@ -142,7 +142,7 @@ func TestEnvironment_DuplicateDeclaration(t *testing.T) {
142142
}
143143

144144
// 验证原值未改变
145-
if value, ok := env.Get("x"); !ok || value != value1 {
145+
if value, ok := env.GetValue("x"); !ok || value != value1 {
146146
t.Errorf("Original variable value should not change")
147147
}
148148
}
@@ -159,7 +159,7 @@ func TestEnvironment_OuterScope(t *testing.T) {
159159
}
160160

161161
// 在内层获取变量
162-
if value, ok := inner.Get("x"); !ok || value != outerValue {
162+
if value, ok := inner.GetValue("x"); !ok || value != outerValue {
163163
t.Errorf("Failed to get variable from outer scope")
164164
}
165165

@@ -171,12 +171,12 @@ func TestEnvironment_OuterScope(t *testing.T) {
171171
}
172172

173173
// 验证内层的新值
174-
if value, ok := inner.Get("x"); !ok || value != innerValue {
174+
if value, ok := inner.GetValue("x"); !ok || value != innerValue {
175175
t.Errorf("Failed to get reassigned value in inner scope")
176176
}
177177

178178
// 验证外层值也改变了(因为内层修改了外层的变量)
179-
if value, ok := outer.Get("x"); !ok || value != innerValue {
179+
if value, ok := outer.GetValue("x"); !ok || value != innerValue {
180180
t.Errorf("Outer scope value should be updated when inner scope modifies it")
181181
}
182182
}

internal/interpreter/function_evaluator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (fe *FunctionEvaluatorImpl) convertResultToInterface(result ast.Node) (inte
9292
return fe.convertBasicLitToInterface(result)
9393
case *ast.Ident:
9494
// 从环境中获取变量值
95-
if value, found := fe.interpreter.env.Get(result.Name); found {
95+
if value, found := fe.interpreter.env.GetValue(result.Name); found {
9696
return value.Interface(), nil
9797
}
9898
return nil, nil

0 commit comments

Comments
 (0)