-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunciones.py
153 lines (137 loc) · 3.36 KB
/
funciones.py
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
import ply.lex as lex
class OperandosInvalidosException(Exception):
def __init__(self):
self.mensaje = "Operadores incompatibles para realizar operación artimética"
class CondicionInvalidaException(Exception):
def __init__(self):
self.mensaje = "Operadores incompatibles para realizar operación lógica"
class TipoDatoException(Exception):
def __init__(self):
self.mensaje = "Valor de dato incompatible con el tipo de dato asignado"
# List of token names. This is always required
reserved = {
'bool': 'BOOL',
'byte': 'BYTE',
'char': 'CHAR',
'decimal': 'DECIMAL',
'double': 'DOUBLE',
'else': 'ELSE',
'float': 'FLOAT',
'for': 'FOR',
'if': 'IF',
'int': 'INT',
'long': 'LONG',
'new': 'NEW',
'sbyte': 'SBYTE',
'short': 'SHORT',
'uint': 'UINT',
'ulong': 'ULONG',
'ushort': 'USHORT',
'while': 'WHILE',
'false': 'FALSE',
'true': 'TRUE',
'List': 'LIST',
'Tuple': 'TUPLE',
'add': 'ADD',
'remove': 'REMOVE',
'removeAt': 'REMOVEAT',
'Equals': 'EQUALS',
'CompareTo': 'COMPARETO',
'Item': 'ITEM',
'string' : 'STRING'
}
tokens = (
'SEMICOLON',
'COMMA',
'POINT',
'ASSIGNMENT',
'NUMBER',
'PLUS',
'MINUS',
'TIMES',
'DIVIDE',
'LPAREN',
'RPAREN',
'MOD',
'VARIABLE',
'EQUAL',
'NOTEQUAL',
'GREATERTHAN',
'GREATERTHANEQUAL',
'LESSERTHAN',
'LESSERTHANEQUAL',
'INCREMENT',
'DECREMENT',
'COMPASSIGPLUS',
'COMPASSIGMINUS',
'COMPASSIGTIMES',
'COMPASSIGDIVIDE',
'LBRACKET',
'RBRACKET',
'AND',
'OR',
'TEXT',
'NUMDEC',
) + tuple(reserved.values())
# Regular expression rules for simple tokens
t_SEMICOLON = r'\;'
t_COMMA = r'\,'
t_POINT = r'\.'
t_ASSIGNMENT = r'\='
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_MOD = r'\%'
t_EQUAL = r'\=\='
t_NOTEQUAL = r'\!\='
t_GREATERTHAN = r'\>'
t_GREATERTHANEQUAL = r'\>\='
t_LESSERTHAN = r'\<'
t_LESSERTHANEQUAL = r'\<\='
t_INCREMENT = r'\+\+'
t_DECREMENT = r'\-\-'
t_COMPASSIGPLUS = r'\+\='
t_COMPASSIGMINUS = r'\-\='
t_COMPASSIGTIMES = r'\*\='
t_COMPASSIGDIVIDE= r'\/\='
t_LBRACKET= r'\{'
t_RBRACKET= r'\}'
t_AND=r'\&\&'
t_OR=r'\|\|'
t_TEXT=r'\".*\"'
def t_VARIABLE(t):
r'([a-zA-Z]|\_)([a-zA-Z0-9]|\_)*'
t.type = reserved.get(t.value, 'VARIABLE') # Check for reserved words
return t
def t_NUMDEC(t):
r'-{0,1}[0-9]+\.[0-9]+'
t.value = float(t.value)
return t
def t_NUMBER(t):
r'\-{0,1}[0-9]+'
t.value = int(t.value)
return t
# Define a rule so we can track line numbers
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_comment(t):
r'/\*.*\*/'
pass
# Error handling rule
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
def getTokens(lexer):
list_token = []
while True:
tok = lexer.token()
if not tok:
break # No more input
list_token.append([tok.type, tok.value, tok.lineno, tok.lexpos])
return list_token
# Build the lexer
lexer = lex.lex()