-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
109 lines (93 loc) · 2.43 KB
/
Copy pathtypes.go
File metadata and controls
109 lines (93 loc) · 2.43 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
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
package main
import "fmt"
// TypeKind represents the type of a value in Deer.
type TypeKind int
const (
TypeVoid TypeKind = iota
TypeBool
TypeByte // uint8
TypeShort // uint16
TypeRune // int32
TypeInt // int64
TypeFloat // float64
TypeStr // string
TypeStruct
)
var typeKindNames = map[TypeKind]string{
TypeVoid: "void",
TypeBool: "bool",
TypeByte: "byte",
TypeShort: "short",
TypeRune: "rune",
TypeInt: "int",
TypeFloat: "float",
TypeStr: "str",
TypeStruct: "struct",
}
func (t TypeKind) String() string {
if name, ok := typeKindNames[t]; ok {
return name
}
return fmt.Sprintf("TypeKind(%d)", int(t))
}
// builtinTypeMap maps type name keywords to their TypeKind.
var builtinTypeMap = map[string]TypeKind{
"void": TypeVoid,
"bool": TypeBool,
"byte": TypeByte,
"short": TypeShort,
"rune": TypeRune,
"int": TypeInt,
"float": TypeFloat,
"str": TypeStr,
}
// builtinType returns the TypeKind for a builtin type name, or false if not found.
func builtinType(name string) (TypeKind, bool) {
k, ok := builtinTypeMap[name]
return k, ok
}
// isTypeKeyword returns true if the name is a builtin type keyword.
func isTypeKeyword(name string) bool {
_, ok := builtinTypeMap[name]
return ok
}
// isIntegerType returns true for bool, byte, short, rune, int.
func isIntegerType(k TypeKind) bool {
return k >= TypeBool && k <= TypeInt
}
// isNumericType returns true for integer types and float.
func isNumericType(k TypeKind) bool {
return k >= TypeBool && k <= TypeFloat
}
// StructField describes a single field in a struct definition.
type StructField struct {
Name string
Type TypeKind
Private bool // _ prefix
}
// StructDef describes a user-defined struct type.
type StructDef struct {
Name string
Parent string // inherited parent type name (empty if none)
Fields []StructField
Methods map[string]*fnPrototypeNode
}
// structRegistry holds all defined struct types.
var structRegistry = map[string]*StructDef{}
// registerStruct adds a struct definition to the registry.
func registerStruct(def *StructDef) {
structRegistry[def.Name] = def
}
// lookupStruct returns the struct definition for a name, or nil.
func lookupStruct(name string) *StructDef {
return structRegistry[name]
}
// fieldIndex returns the index of a field by name, or -1 if not found.
func (s *StructDef) fieldIndex(name string) int {
for i, f := range s.Fields {
if f.Name == name {
return i
}
}
return -1
}