-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
103 lines (78 loc) · 1.24 KB
/
ast.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
package ast
import (
"fmt"
"github.com/elpinal/coco3/extra/token"
"github.com/elpinal/coco3/extra/types"
)
type Command struct {
Name token.Token
Args []Expr
}
// Expressions
type Expr interface {
Expr()
Type() types.Type
}
func (_ *String) Expr() {}
func (_ *Int) Expr() {}
func (_ *Ident) Expr() {}
func (_ *Empty) Expr() {}
func (_ *Cons) Expr() {}
// Simple types
type (
String struct {
Lit string
}
Int struct {
Lit string
}
Ident struct {
Lit string
}
)
func (_ *String) Type() types.Type {
return types.String
}
func (_ *Int) Type() types.Type {
return types.Int
}
func (_ *Ident) Type() types.Type {
return types.Ident
}
func (s *String) String() string {
return fmt.Sprintf("%q", s.Lit)
}
func (i *Int) String() string {
return i.Lit
}
func (id *Ident) String() string {
return fmt.Sprintf("%q", id.Lit)
}
// Lists
type List interface {
Length() int
Expr
}
type (
Cons struct {
Head string
Tail List
}
Empty struct{}
)
func (e *Empty) Type() types.Type {
return types.StringList
}
func (c *Cons) Type() types.Type {
return types.StringList
}
func (e *Empty) Length() int {
return 0
}
func (c *Cons) Length() int {
return 1 + c.Tail.Length()
}
type Def struct {
Name token.Token
Expr Expr
}