diff --git a/parser/Parser.py b/parser/Parser.py index 792a851..cd7a5d2 100644 --- a/parser/Parser.py +++ b/parser/Parser.py @@ -31,6 +31,10 @@ class Parser(object): 'unit_item : c_expr SEMICOLON' p[0] = p[1] + def p_unit_item_function(self, p): + 'unit_item : function' + p[0] = p[1] + def p_statement(self, p): 'statement : expr SEMICOLON' p[0] = StatementNode([p[1]]) @@ -43,6 +47,29 @@ class Parser(object): 'c_expr : C LPAREN STRING RPAREN' p[0] = CExprNode(p[3]) + def p_function(self, p): + 'function : type ID LPAREN RPAREN LCURLY function_items RCURLY' + p[0] = FunctionNode(p[2], p[1], None, p[6]) + + def p_function_items(self, p): + 'function_items : function_item function_items' + p[0] = Node([p[1] + p[2].children]) + + def p_function_items_empty(self, p): + 'function_items : empty' + p[0] = p[1] + + def p_function_item_stmt(self, p): + 'function_item : statement' + p[0] = p[1] + + def p_type(self, p): + '''type : CHAR + | SHORT + | INT + | LONG''' + p[0] = p[1] + def p_empty(self, p): 'empty :' p[0] = Node() diff --git a/parser/nodes.py b/parser/nodes.py index c4ab03b..da109ef 100644 --- a/parser/nodes.py +++ b/parser/nodes.py @@ -25,3 +25,9 @@ class CExprNode(ExprNode): class UnitNode(Node): def __init__(self, children=None): Node.__init__(self, children) + +class FunctionNode(Node): + def __init__(self, name, ret_type, children=None): + Node.__init__(self, children) + self.name = name + self.ret_type = ret_type