-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
140 lines (125 loc) · 2.61 KB
/
debug.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Copyright (c) 2022-2024 Markku Rossi
//
// All rights reserved.
//
package scheme
import (
"fmt"
"sort"
"github.com/markkurossi/scheme/types"
)
const (
fLambda int = 1 << iota
fNative
fScheme
)
var debugBuiltins = []Builtin{
{
Name: "print-env",
Args: []string{"sym..."},
Return: types.Any,
Native: func(scm *Scheme, args []Value) (Value, error) {
var flags int
if len(args) == 0 {
flags = 0xffff
} else {
for _, arg := range args {
switch flag := arg.(type) {
case *Identifier:
switch flag.Name {
case "native":
flags |= fNative
case "scheme":
flags |= fScheme
default:
return nil, fmt.Errorf("unknown flag: %v", arg)
}
case Keyword:
switch flag {
case KwLambda:
flags |= fLambda
default:
return nil, fmt.Errorf("unknown flag: %v", arg)
}
default:
return nil, fmt.Errorf("invalid flag: %v(%T)", arg, arg)
}
}
}
var names []string
for k, v := range scm.symbols {
if v.Global != nil {
switch val := v.Global.(type) {
case *Lambda:
if flags&fLambda != 0 {
if (flags&(fNative|fScheme)) == 0 ||
((val.Impl.Native != nil) &&
(flags&fNative != 0)) ||
((val.Impl.Native == nil) &&
(flags&fScheme != 0)) {
names = append(names, k)
}
}
default:
if flags&fLambda == 0 {
names = append(names, k)
}
}
}
}
sort.Strings(names)
var max int
for _, name := range names {
if len(name) > max {
max = len(name)
}
}
fmt.Printf("Global symbols:\n")
for _, name := range names {
for i := 0; i+len(name) < max; i++ {
fmt.Print(" ")
}
fmt.Printf("%s : %s\n", name, scm.symbols[name].Global)
}
fmt.Printf("%d symbols matched\n", len(names))
return nil, nil
},
},
{
Name: "disassemble",
Args: []string{"obj"},
Return: types.Any,
Native: func(scm *Scheme, args []Value) (Value, error) {
switch arg := args[0].(type) {
case *Lambda:
scm.Stdout.Printf("lambda: %v\n", arg)
if arg.Impl.Native == nil {
for _, c := range arg.Impl.Code {
scm.Stdout.Printf("%s\n", c)
}
}
default:
scm.Stdout.Printf("value: %v\n", arg)
}
return nil, nil
},
},
{
Name: "type",
Args: []string{"obj"},
Return: types.Nil,
Native: func(scm *Scheme, args []Value) (Value, error) {
if args[0] == nil {
scm.Stdout.Println("nil")
} else {
t := args[0].Type()
if t == nil {
t = types.Any
}
scm.Stdout.Printf("%v\n", t)
}
return nil, nil
},
},
}