forked from Skythrill256/The-sex-programming-language-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
50 lines (43 loc) · 1.01 KB
/
parser.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
package main
type ASTNode struct {
Token Token
Left *ASTNode
Right *ASTNode
Children []*ASTNode // For holding statements inside blocks like if
}
func parser(tokens []Token) *ASTNode {
if len(tokens) < 2 {
return nil
}
if tokens[0].Type == SEX && tokens[1].Type == VAR && tokens[2].Type == EQUAL {
return &ASTNode{
Token: tokens[1], // variable name
Left: nil,
Right: &ASTNode{Token: tokens[3]}, // value
}
}
// Handle if condition
if tokens[0].Type == IF {
return &ASTNode{
Token: tokens[0], // if keyword
Left: &ASTNode{ // condition
Token: tokens[1], // variable name
Right: &ASTNode{Token: tokens[3]}, // comparison value
},
}
}
// Handle when statement
if tokens[0].Type == WHEN {
return &ASTNode{
Token: tokens[0], // when keyword
Left: &ASTNode{Token: tokens[1]}, // variable name
}
}
if tokens[0].Type == FUCK {
return &ASTNode{
Token: tokens[0], // Fack keyword
Left: &ASTNode{Token: tokens[1]}, // expression to print
}
}
return nil
}