-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.go
126 lines (118 loc) · 4.08 KB
/
interpreter.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// interpreter.go
package taskwrappr
import (
"fmt"
"regexp"
)
type TokenType int
const (
NewLineSymbol = '\n'
TabSymbol = '\t'
ReturnSymbol = '\r'
EscapeSymbol = '\\'
StringSymbol = '"'
SpaceSymbol = ' '
CommentSymbol = '#'
CodeBlockOpenSymbol = '{'
CodeBlockCloseSymbol = '}'
AssignmentSymbol = '='
InvalidTokenSymbol = -1
)
const (
ParenOpenSymbol = '('
ParenCloseSymbol = ')'
BracketOpenSymbol = '['
BracketCloseSymbol = ']'
DelimiterSymbol = ','
DecimalSymbol = '.'
AdditionSymbol = '+'
SubtractionSymbol = '-'
MultiplicationSymbol = '*'
DivisionSymbol = '/'
ModulusSymbol = '%'
ExponentSymbol = '^'
SelfReferenceSymbol = '~'
DeclarationSymbol = ':'
)
const (
InvalidToken TokenType = iota
ActionToken
AssignmentToken
DeclarationToken
AugmentedAssignmentToken
VariableToken
LiteralToken
CodeBlockOpenToken
CodeBlockCloseToken
OperatorAddToken
OperatorSubtractToken
OperatorUnaryMinusToken
OperatorExponentToken
OperatorMultiplyToken
OperatorDivideToken
OperatorModuloToken
ParenOpenToken
ParenCloseToken
DelimiterToken
DecimalToken
LogicalAndToken
LogicalOrToken
LogicalNotToken
LogicalXorToken
EqualityToken
InequalityToken
LessThanToken
LessThanOrEqualToken
GreaterThanToken
GreaterThanOrEqualToken
IgnoreToken
)
const (
TrueString = "true"
FalseString = "false"
NilString = "nil"
LogicalAndString = "&&"
LogicalOrString = "||"
LogicalNotString = "!"
LogicalXorString = "^^"
EqualityString = "=="
InequalityString = "!="
LessThanString = "<"
LessThanOrEqualString = "<="
GreaterThanString = ">"
GreaterThanOrEqualString = ">="
DeclarationString = string(DeclarationSymbol) + string(AssignmentSymbol)
AugmentedAdditionString = string(AdditionSymbol) + string(AssignmentSymbol)
AugmentedSubtractionString = string(SubtractionSymbol) + string(AssignmentSymbol)
AugmentedMultiplicationString = string(MultiplicationSymbol) + string(AssignmentSymbol)
AugmentedDivisionString = string(DivisionSymbol) + string(AssignmentSymbol)
AugmentedModulusString = string(ModulusSymbol) + string(AssignmentSymbol)
AugmentedExponentString = string(ExponentSymbol) + string(AssignmentSymbol)
)
var (
ActionCallPattern = regexp.MustCompile(fmt.Sprintf(`\w+\%c[^%c]*\%c`, ParenOpenSymbol, ParenCloseSymbol, ParenCloseSymbol))
ActionArgumentsPattern = regexp.MustCompile(fmt.Sprintf(`^(\w+)\%c(.*)\%c$`, ParenOpenSymbol, ParenCloseSymbol))
AssignmentPattern = regexp.MustCompile(fmt.Sprintf(`^\s*([a-zA-Z_]\w*)\s*%c\s*(.+)\s*$`, AssignmentSymbol))
DeclarationPattern = regexp.MustCompile(fmt.Sprintf(`^\s*([a-zA-Z_]\w*)\s*%s\s*(.+)\s*$`, DeclarationString))
VariableNamePattern = regexp.MustCompile(fmt.Sprintf(`^[a-zA-Z_][a-zA-Z0-9_]*[^%c]$`, ParenOpenSymbol))
IntegerPattern = regexp.MustCompile(`^-?\d+$`)
FloatPattern = regexp.MustCompile(`^-?\d*\.\d+$`)
BooleanPattern = regexp.MustCompile(fmt.Sprintf(`^(%s|%s)$`, TrueString, FalseString))
StringPattern = regexp.MustCompile(fmt.Sprintf(`^%c.*%c$`, StringSymbol, StringSymbol))
AugmentedAssignementPattern = regexp.MustCompile(fmt.Sprintf(
`^\s*(\w+)\s*([\%c\%c\%c\%c\%c\%c]%c)\s*(.*)\s*$`,
AdditionSymbol, SubtractionSymbol, MultiplicationSymbol, DivisionSymbol, ModulusSymbol, ExponentSymbol, AssignmentSymbol,
))
LogicalOperatorsPattern = regexp.MustCompile(fmt.Sprintf(
`^(%s|%s|%s|%s|%s|%s|%s|%s|%s|%s)$`,
EqualityString, InequalityString, LessThanString, LessThanOrEqualString, GreaterThanString, GreaterThanOrEqualString, LogicalAndString, LogicalOrString, LogicalNotString, LogicalXorString,
))
)
var Operators = string([]rune{
AdditionSymbol,
SubtractionSymbol,
MultiplicationSymbol,
DivisionSymbol,
ModulusSymbol,
ExponentSymbol,
})