forked from AplaProject/go-apla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.go
305 lines (286 loc) · 9.16 KB
/
lex.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// MIT License
//
// Copyright (c) 2016-2018 GenesisKernel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package script
import (
"encoding/binary"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/GenesisKernel/go-genesis/packages/consts"
"github.com/shopspring/decimal"
log "github.com/sirupsen/logrus"
)
// The lexical analysis of the incoming program is implemented in this file. It is the first phase of compilation
// where the incoming text is divided into a sequence of lexemes.
const (
// lexUnknown = iota
// Here are all the created lexemes
lexSys = iota + 1 // a system lexeme is different bracket, =, comma and so on.
lexOper // Operator is +, -, *, /
lexNumber // Number
lexIdent // Identifier
lexNewLine // Line translation
lexString // String
lexComment // Comment
lexKeyword // Key word
lexType // Name of the type
lexExtend // Referring to an external variable or function - $myname
lexError = 0xff
// flags of lexical states
lexfNext = 1
lexfPush = 2
lexfPop = 4
lexfSkip = 8
// Constants for system lexemes
isLPar = 0x2801 // (
isRPar = 0x2901 // )
isComma = 0x2c01 // ,
isDot = 0x2e01 // .
isEq = 0x3d01 // =
isLCurly = 0x7b01 // {
isRCurly = 0x7d01 // }
isLBrack = 0x5b01 // [
isRBrack = 0x5d01 // ]
// Constants for operations
isNot = 0x0021 // !
isAsterisk = 0x002a // *
isPlus = 0x002b // +
isMinus = 0x002d // -
isSign = 0x012d // - unary
isSolidus = 0x002f // /
isLess = 0x003c // <
isGreat = 0x003e // >
isNotEq = 0x213d // !=
isAnd = 0x2626 // &&
isLessEq = 0x3c3d // <=
isEqEq = 0x3d3d // ==
isGrEq = 0x3e3d // >=
isOr = 0x7c7c // ||
)
const (
// The list of keyword identifiers
// Constants for keywords
// keyUnknown = iota
keyContract = iota + 1
keyFunc
keyReturn
keyIf
keyElse
keyWhile
keyTrue
keyFalse
keyVar
keyTX
keySettings
keyBreak
keyContinue
keyWarning
keyInfo
keyNil
keyAction
keyCond
keyTail
keyError
)
const (
msgWarning = `warning`
msgError = `error`
msgInfo = `info`
)
var (
// The list of key words
keywords = map[string]uint32{`contract`: keyContract, `func`: keyFunc, `return`: keyReturn,
`if`: keyIf, `else`: keyElse, msgError: keyError, msgWarning: keyWarning, msgInfo: keyInfo,
`while`: keyWhile, `data`: keyTX, `settings`: keySettings, `nil`: keyNil, `action`: keyAction, `conditions`: keyCond,
`true`: keyTrue, `false`: keyFalse, `break`: keyBreak, `continue`: keyContinue,
`var`: keyVar, `...`: keyTail}
// list of available types
// The list of types which save the corresponding 'reflect' type
types = map[string]reflect.Type{`bool`: reflect.TypeOf(true), `bytes`: reflect.TypeOf([]byte{}),
`int`: reflect.TypeOf(int64(0)), `address`: reflect.TypeOf(uint64(0)),
`array`: reflect.TypeOf([]interface{}{}),
`map`: reflect.TypeOf(map[string]interface{}{}), `money`: reflect.TypeOf(decimal.New(0, 0)),
`float`: reflect.TypeOf(float64(0.0)), `string`: reflect.TypeOf(``)}
)
// Lexem contains information about language item
type Lexem struct {
Type uint32 // Type of the lexem
Value interface{} // Value of lexem
Line uint32 // Line of the lexem
Column uint32 // Position inside the line
}
// GetLogger returns logger
func (l Lexem) GetLogger() *log.Entry {
return log.WithFields(log.Fields{"lex_type": l.Type, "lex_line": l.Line, "lex_column": l.Column})
}
// Lexems is a slice of lexems
type Lexems []*Lexem
// The lexical analysis is based on the finite machine which is described in the file
// tools/lextable/lextable.go. lextable.go generates a representation of a finite machine as an array
// and records it in the file lex_table.go. In fact, the lexTable array is a set of states and
// depending on the next sign, the machine goes into a new state.
// lexParser parsers the input language source code
func lexParser(input []rune) (Lexems, error) {
var (
curState uint8
length, line, off, offline, flags, start, lexID uint32
)
lexems := make(Lexems, 0, len(input)/4)
irune := len(alphabet) - 1
// This function according to the next symbol looks with help of lexTable what new state we will have,
// whether we got the lexeme and what flags are displayed
todo := func(r rune) {
var letter uint8
if r > 127 {
letter = alphabet[irune]
} else {
letter = alphabet[r]
}
val := lexTable[curState][letter]
curState = uint8(val >> 16)
lexID = (val >> 8) & 0xff
flags = val & 0xff
}
length = uint32(len(input)) + 1
line = 1
skip := false
for off < length {
// Here we go through the symbols one by one
if off == length-1 {
todo(rune(' '))
} else {
todo(input[off])
}
if curState == lexError {
return nil, fmt.Errorf(`unknown lexem %s [Ln:%d Col:%d]`,
string(input[off:off+1]), line, off-offline+1)
}
if (flags & lexfSkip) != 0 {
off++
skip = true
continue
}
// If machine determined the completed lexeme, we record it in the list of lexemes.
if lexID > 0 {
// We do not start a stack for symbols but memorize the displacement when the parse of lexeme began.
// To get a string of a lexeme we take a substring from the initial displacement to the current one.
// We immediately write a string as values, a number or a binary representation of operations.
lexOff := off
if (flags & lexfPop) != 0 {
lexOff = start
}
right := off
if (flags & lexfNext) != 0 {
right++
}
var value interface{}
switch lexID {
case lexNewLine:
if input[lexOff] == rune(0x0a) {
line++
offline = off
}
case lexSys:
ch := uint32(input[lexOff])
lexID |= ch << 8
value = ch
case lexString, lexComment:
value = string(input[lexOff+1 : right-1])
if lexID == lexString && skip {
skip = false
value = strings.Replace(value.(string), `\"`, `"`, -1)
value = strings.Replace(strings.Replace(value.(string), `\r`, "\r", -1), `\n`, "\n", -1)
}
for i, ch := range value.(string) {
if ch == 0xa {
line++
offline = off + uint32(i) + 1
}
}
case lexOper:
oper := []byte(string(input[lexOff:right]))
value = binary.BigEndian.Uint32(append(make([]byte, 4-len(oper)), oper...))
case lexNumber:
name := string(input[lexOff:right])
if strings.ContainsAny(name, `.`) {
if val, err := strconv.ParseFloat(name, 64); err == nil {
value = val
} else {
log.WithFields(log.Fields{"error": err, "value": name, "lex_line": line, "lex_col": off - offline + 1, "type": consts.ConversionError}).Error("converting lex number to float")
return nil, fmt.Errorf(`%v %s [Ln:%d Col:%d]`, err, name, line, off-offline+1)
}
} else if val, err := strconv.ParseInt(name, 10, 64); err == nil {
value = val
} else {
log.WithFields(log.Fields{"error": err, "value": name, "lex_line": line, "lex_col": off - offline + 1, "type": consts.ConversionError}).Error("converting lex number to int")
return nil, fmt.Errorf(`%v %s [Ln:%d Col:%d]`, err, name, line, off-offline+1)
}
case lexIdent:
name := string(input[lexOff:right])
if name[0] == '$' {
lexID = lexExtend
value = name[1:]
} else if keyID, ok := keywords[name]; ok {
switch keyID {
case keyAction, keyCond:
if len(lexems) > 0 {
lexf := *lexems[len(lexems)-1]
if lexf.Type&0xff != lexKeyword || lexf.Value.(uint32) != keyFunc {
lexems = append(lexems, &Lexem{lexKeyword | (keyFunc << 8),
keyFunc, line, lexOff - offline + 1})
}
}
value = name
case keyTrue:
lexID = lexNumber
value = true
case keyFalse:
lexID = lexNumber
value = false
case keyNil:
lexID = lexNumber
value = nil
default:
lexID = lexKeyword | (keyID << 8)
value = keyID
}
} else if typeID, ok := types[name]; ok {
lexID = lexType
value = typeID
} else {
value = name
}
}
if lexID != lexComment {
lexems = append(lexems, &Lexem{lexID, value, line, lexOff - offline + 1})
}
}
if (flags & lexfPush) != 0 {
start = off
}
if (flags & lexfNext) != 0 {
off++
}
}
return lexems, nil
}