-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.go
More file actions
178 lines (163 loc) · 3.57 KB
/
Copy pathlex.go
File metadata and controls
178 lines (163 loc) · 3.57 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"strings"
)
//Token represents a term in a line of code
type Token struct {
Type string
Value []byte
}
func isInt(word string) bool {
chars := strings.Split(word, "")
//Loop over characters
for _, c := range chars {
bytechar := []byte(c)
//one byte holds one character
val := bytechar[0]
//Not an integer 0:0x30 - 9:0x39
if val < 48 || val > 57 {
return false
}
}
return true
}
func isOperator(word string) bool {
chars := strings.Split(word, "")
if word == "or" || word == "and" || word == ">" || word == "<" || word == "==" || word == "!=" {
return true
}
//Loop over characters
for _, c := range chars {
bytechar := []byte(c)
//one byte holds one character
val := bytechar[0]
//Check if operator * : 42, + : 43, - : 45, / : 47
if val == 44 || val == 46 || val < 40 || val > 47 {
return false
}
}
return true
}
func isDeclaration(word string) bool {
return word == "Int" || word == "String" || word == "Bool"
}
func isAssignment(word string) bool {
return word == "="
}
func tokenizer(line string) []Token {
words := strings.Fields(line)
tokens := []Token{}
for i, w := range words {
if isInt(w) {
token := Token{
Type: "Int",
Value: []byte(w),
}
tokens = append(tokens, token)
//Check if declaration was made. If it did, means it's a variable
} else if w == "," {
token := Token{
Type: "Comma",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if i > 0 && tokens[i-1].Type == "Declaration" {
token := Token{
Type: "Variable",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if w == "=>" {
//Check if this was a function call or func declaration
if i > 0 && tokens[i-1].Type == "Variable" && tokens[0].Type != "If" {
tokens[i-1].Type = "Function"
}
var token Token
//If this was if declaration
if tokens[0].Type == "If" {
token = Token{
Type: "FunctionParam",
Value: []byte(w),
}
} else {
token = Token{
Type: "FunctionParam",
Value: []byte(w),
}
}
tokens = append(tokens, token)
} else if isOperator(w) {
token := Token{
Type: "Operator",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if w == "{" {
token := Token{
Type: "CurlyRight",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if w == "(" {
token := Token{
Type: "(",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if w == ")" {
token := Token{
Type: ")",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if w == "}" {
token := Token{
Type: "}",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if isDeclaration(w) {
token := Token{
Type: "Declaration",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if isAssignment(w) {
token := Token{
Type: "Assignment",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if w == "return" {
token := Token{
Type: "Return",
Value: []byte(w),
}
tokens = append(tokens, token)
} else if w == "if" {
token := Token{
Type: "If",
Value: []byte(w),
}
tokens = append(tokens, token)
} else {
_, ok := LocalVariable[w]
//w is a local variable
//Note: Need to implement undeclared variable error
if ok {
token := Token{
Type: "Variable",
Value: []byte(w),
}
tokens = append(tokens, token)
} else {
token := Token{
Type: "Function",
Value: []byte(w),
}
tokens = append(tokens, token)
}
}
}
return tokens
}