improved integer literals, added string literals
git-svn-id: svn://anubis/jackalc/trunk@3 2d0ce1a6-f80c-425e-916c-c881d0336438
This commit is contained in:
parent
1661d11c8f
commit
b4dd8064a3
4
nodes.py
4
nodes.py
@ -49,3 +49,7 @@ class ParamNode(Node):
|
|||||||
def __init__(self, name, typ):
|
def __init__(self, name, typ):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.typ = typ
|
self.typ = typ
|
||||||
|
|
||||||
|
class IntegerNode(Node):
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
54
parserdef.py
54
parserdef.py
@ -29,7 +29,9 @@ tokens = [
|
|||||||
'ID',
|
'ID',
|
||||||
|
|
||||||
# literals
|
# literals
|
||||||
'NUMBER',
|
'NUMBERLIT',
|
||||||
|
'HEXNUMBERLIT',
|
||||||
|
'STRINGLIT',
|
||||||
|
|
||||||
# assignment
|
# assignment
|
||||||
'ASSIGN',
|
'ASSIGN',
|
||||||
@ -57,9 +59,11 @@ tokens = [
|
|||||||
'COMMA',
|
'COMMA',
|
||||||
] + list(reserved.values())
|
] + list(reserved.values())
|
||||||
|
|
||||||
t_ASSIGN = r':='
|
states = (
|
||||||
|
('string', 'exclusive'),
|
||||||
|
)
|
||||||
|
|
||||||
t_NUMBER = r'[0-9]+'
|
t_ASSIGN = r':='
|
||||||
|
|
||||||
t_EQUALS = r'='
|
t_EQUALS = r'='
|
||||||
t_DEQUALS = r'=='
|
t_DEQUALS = r'=='
|
||||||
@ -86,6 +90,43 @@ def t_ID(t):
|
|||||||
t.type = reserved.get(t.value, 'ID') # check for reserved words
|
t.type = reserved.get(t.value, 'ID') # check for reserved words
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
def t_NUMBERLIT(t):
|
||||||
|
r'[0-9]+'
|
||||||
|
t.value = int(t.value)
|
||||||
|
return t
|
||||||
|
|
||||||
|
def t_HEXNUMBERLIT(t):
|
||||||
|
r'0x[0-9a-fA-F]+'
|
||||||
|
t.value = int(t.value)
|
||||||
|
return t
|
||||||
|
|
||||||
|
def t_begin_stringlit(t):
|
||||||
|
r'"'
|
||||||
|
global gather_string
|
||||||
|
gather_string = ''
|
||||||
|
t.lexer.push_state('string')
|
||||||
|
|
||||||
|
def t_string_elem(t):
|
||||||
|
r'[^\\\n"]'
|
||||||
|
global gather_string
|
||||||
|
gather_string += t.value
|
||||||
|
|
||||||
|
def t_string_escape(t):
|
||||||
|
r'\\[^\n]'
|
||||||
|
global gather_string
|
||||||
|
e = t.value[1]
|
||||||
|
gather_string += {'n':"\n", 't':"\t", 'b':"\b", 'v':"\v", 'r':"\r"}.get(e,e)
|
||||||
|
|
||||||
|
def t_string_end(t):
|
||||||
|
r'"'
|
||||||
|
global gather_string
|
||||||
|
t.type = 'STRING'
|
||||||
|
t.value = gather_string
|
||||||
|
t.lexer.pop_state()
|
||||||
|
return t
|
||||||
|
|
||||||
|
t_string_ignore = ''
|
||||||
|
|
||||||
def t_newline(t):
|
def t_newline(t):
|
||||||
r'\n'
|
r'\n'
|
||||||
t.lexer.lineno += 1
|
t.lexer.lineno += 1
|
||||||
@ -203,13 +244,18 @@ def p_expression_id(p):
|
|||||||
p[0] = p[1]
|
p[0] = p[1]
|
||||||
|
|
||||||
def p_expression_literal(p):
|
def p_expression_literal(p):
|
||||||
'''expression : NUMBER'''
|
'''expression : integer'''
|
||||||
p[0] = p[1]
|
p[0] = p[1]
|
||||||
|
|
||||||
def p_expression_binop(p):
|
def p_expression_binop(p):
|
||||||
'''expression : expression binop expression'''
|
'''expression : expression binop expression'''
|
||||||
p[0] = BinOpNode(p[2], p[1], p[3])
|
p[0] = BinOpNode(p[2], p[1], p[3])
|
||||||
|
|
||||||
|
def p_integer(p):
|
||||||
|
'''integer : NUMBERLIT
|
||||||
|
| HEXNUMBERLIT'''
|
||||||
|
p[0] = IntegerNode(p[1])
|
||||||
|
|
||||||
def p_binop(p):
|
def p_binop(p):
|
||||||
'''binop : TIMES
|
'''binop : TIMES
|
||||||
| DIVIDE
|
| DIVIDE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user