blob: 830d5275a5d299f5ede0b5eea0cd7a009eff2eed (
plain)
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
|
#!/usr/bin/env python3
import ply.lex as lex
# List of token names. This is always required
tokens = (
"OP",
"JUMP",
"COMMA",
"COLON",
"SYMBOL",
"NUMBER",
"HEXNUMBER",
"DOT",
"REG",
"NL",
)
# Regular expression rules for simple tokens
t_COMMA = r","
t_COLON = r":"
t_DOT = r"\."
def t_OP(t):
r"and|or|xor|not|mov|add|inc|sub|dec|cmp|neg|hlt|nop"
return t
def t_REG(t):
r"\b(AD?M?|DM?|M|_)\b"
return t
def t_JUMP(t):
r"jmp|jlt|jgt|jle|jge|jeq|jne"
return t
def t_NUMBER(t):
r"\#\d+"
t.value = int(t.value[1:])
return t
def t_HEXNUMBER(t):
r"\#0x[0-9a-fA-F]+"
t.value = int(t.value[1:], 16)
return t
def t_SYMBOL(t):
r"[A-Za-z][A-Za-z0-9_]+"
return t
# Define a rule so we can track line numbers
def t_NL(t):
r"\n+"
t.lexer.lineno += len(t.value)
return t
# A string containing ignored characters (spaces and tabs)
t_ignore = " \t"
# t_ignore_COMMENT = r';.*'
def t_COMMENT(t):
r";.*"
pass
# Error handling rule
def t_error(t):
print("!!! Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# EOF handling rule
def t_eof(t):
if not t.lexer.newline_added:
t.lexer.input("\n")
t.lexer.newline_added = True
return t.lexer.token()
return None
# Build the lexer
lexer = lex.lex()
lexer.newline_added = False
|