forked from aichaos/rivescript-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
82 lines (68 loc) · 1.67 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
package rivescript
/* "Abstract Syntax Tree" of parsed RiveScript code.
The tree looks like this (in JSON-style syntax):
{
"begin": {
"global": {} // Global vars
"var_": {} // Bot vars
"sub": {} // Substitutions
"person": {} // Person substitutions
"array": {} // Arrays
}
"topics": {}
"objects" []
}
*/
type astRoot struct {
begin astBegin
topics map[string]*astTopic
objects []*astObject
}
type astBegin struct {
global map[string]string
var_ map[string]string
sub map[string]string
person map[string]string
array map[string][]string // Map of string (names) to arrays-of-strings
}
type astTopic struct {
triggers []*astTrigger
includes map[string]bool
inherits map[string]bool
}
type astTrigger struct {
trigger string
reply []string
condition []string
redirect string
previous string
}
type astObject struct {
name string
language string
code []string
}
func newAST() *astRoot {
ast := new(astRoot)
// Initialize all the structures.
ast.begin.global = map[string]string{}
ast.begin.var_ = map[string]string{}
ast.begin.sub = map[string]string{}
ast.begin.person = map[string]string{}
ast.begin.array = map[string][]string{}
// Initialize the 'random' topic.
ast.topics = map[string]*astTopic{}
ast = initTopic(ast, "random")
// Objects
ast.objects = []*astObject{}
return ast
}
// initTopic sets up the AST tree for a new topic and gets it ready for
// triggers to be added.
func initTopic(ast *astRoot, name string) *astRoot {
ast.topics[name] = new(astTopic)
ast.topics[name].triggers = []*astTrigger{}
ast.topics[name].includes = map[string]bool{}
ast.topics[name].inherits = map[string]bool{}
return ast
}