diff --git a/nodes.py b/nodes.py index 1dffa19..edeed2f 100644 --- a/nodes.py +++ b/nodes.py @@ -49,3 +49,7 @@ class ParamNode(Node): def __init__(self, name, typ): self.name = name self.typ = typ + +class IntegerNode(Node): + def __init__(self, value): + self.value = value diff --git a/parserdef.py b/parserdef.py index 1e60d10..98711ee 100644 --- a/parserdef.py +++ b/parserdef.py @@ -29,7 +29,9 @@ tokens = [ 'ID', # literals - 'NUMBER', + 'NUMBERLIT', + 'HEXNUMBERLIT', + 'STRINGLIT', # assignment 'ASSIGN', @@ -57,9 +59,11 @@ tokens = [ 'COMMA', ] + list(reserved.values()) -t_ASSIGN = r':=' +states = ( + ('string', 'exclusive'), + ) -t_NUMBER = r'[0-9]+' +t_ASSIGN = r':=' t_EQUALS = r'=' t_DEQUALS = r'==' @@ -86,6 +90,43 @@ def t_ID(t): t.type = reserved.get(t.value, 'ID') # check for reserved words 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): r'\n' t.lexer.lineno += 1 @@ -203,13 +244,18 @@ def p_expression_id(p): p[0] = p[1] def p_expression_literal(p): - '''expression : NUMBER''' + '''expression : integer''' p[0] = p[1] def p_expression_binop(p): '''expression : expression binop expression''' 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): '''binop : TIMES | DIVIDE