-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredcodelex.py
More file actions
53 lines (42 loc) · 869 Bytes
/
redcodelex.py
File metadata and controls
53 lines (42 loc) · 869 Bytes
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
import ply.lex as lex
tokens = [
'ID',
'IMMEDIATE',
'SEMICOLON',
'COLON',
'COMMA',
]
reserved = {
'DAT': 'DAT', # DAT B
'MOV': 'MOV', # MOV A, B
'ADD': 'ADD', # ADD A, B
'SUB': 'SUB', # SUB A, B
'JMP': 'JMP', # JMP B
'JMZ': 'JMZ', # JMZ A, B
'CMP': 'CMP', # CMP A, B
'NOP': 'NOP', # NOP
}
tokens += list(reserved.values())
t_SEMICOLON = r'\;'
t_COLON = r'\:'
t_COMMA = r'\,'
#t_NEWLINE = r'\n+'
#t_WHITESPACE = r'[\t ]+'
# Ignore comments
t_ignore_COMMENT = r'\;.*'
# Ignore whitespace
t_ignore = r' \t'
def t_NEWLINE(t):
r'\n+'
pass
# Generic identifier token
def t_ID(t):
r'[a-zA-Z0-9]+'
t.type = reserved.get(t.value.upper(), 'IMMEDIATE')
return t
# Support only for integer decimal immediates
def t_IMMEDIATE(t):
r'\d+'
t.value = int(t.value)
return t
lexer = lex.lex()