From a0304a77cad37737c163115c29989e7bba4fb188 Mon Sep 17 00:00:00 2001 From: Tobias Platen Date: Fri, 24 Jan 2020 13:51:53 +0100 Subject: [PATCH] convert binary and unary operators --- examples/assignment2.sv | 27 +++++++++++ parse_sv.py | 103 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 123 insertions(+), 7 deletions(-) diff --git a/examples/assignment2.sv b/examples/assignment2.sv index 22e0ab4..3d58fda 100644 --- a/examples/assignment2.sv +++ b/examples/assignment2.sv @@ -6,4 +6,31 @@ wire x,y; wire [15:0] z; assign x = i; assign o = x; + +assign plusexp = x + y; +assign minusexp = x - y; +assign multexp = x * y; +assign divexp = x / y; +assign modexp = x % y; + +assign gtexp = x > y; +assign ltexp = x < y; +assign geexp = x >= y; +assign leexp = x <= y; + +assign eq_short = x == y; +assign neq_short = x != y; +assign eq_long = x === y; +assign neq_long = x !== y; + + +assign and2 = x && y; +assign or2 = x || y; + +assign andexp = x & y; +assign orexp = x | y; +assign nandexp = x ~& y; +assign norexp = x ~| y; + +assign xorexp = x ^ y; endmodule diff --git a/parse_sv.py b/parse_sv.py index c1c0a9c..f178237 100644 --- a/parse_sv.py +++ b/parse_sv.py @@ -5294,11 +5294,30 @@ def p_expression_4(p): () +def PEUnary(op, o1): + #Leaf(token.STRING, ' ') + try: + return Node(syms.atom, [op, o1]) + except: + return "error in PEUnary: "+str(op)+","+str(o1) + + +def PEBinary(op, o1, o2): + try: + return Node(syms.atom, [o1, Leaf(token.STRING, ' '), op, Leaf(token.STRING, ' '), o2]) + except: + return "error in PEBinary: "+str(op)+","+str(o1)+","+str(o2) + +# unary minus + + def p_expression_5(p): '''expression : '-' attribute_list_opt expr_primary %prec UNARY_PREC ''' if(parse_debug): print('expression_5', list(p)) + p[0] = PEUnary(Leaf(token.MINUS, '-'), p[3]) + # { PEUnary*tmp = new PEUnary('-', p[3]); # FILE_NAME(tmp, @3); # p[0] = tmp; @@ -5311,6 +5330,8 @@ def p_expression_6(p): if(parse_debug): print('expression_6', list(p)) + p[0] = PEUnary(Leaf(token.TILDE, '~'), p[3]) + # { PEUnary*tmp = new PEUnary('~', p[3]); # FILE_NAME(tmp, @3); # p[0] = tmp; @@ -5323,6 +5344,8 @@ def p_expression_7(p): if(parse_debug): print('expression_7', list(p)) + p[0] = PEUnary(Leaf(token.AMPER, '&'), p[3]) + # { PEUnary*tmp = new PEUnary('&', p[3]); # FILE_NAME(tmp, @3); # p[0] = tmp; @@ -5335,6 +5358,8 @@ def p_expression_8(p): if(parse_debug): print('expression_8', list(p)) + p[0] = PEUnary(Leaf(token.STRING, '!'), p[3]) + # { PEUnary*tmp = new PEUnary('!', p[3]); # FILE_NAME(tmp, @3); # p[0] = tmp; @@ -5347,6 +5372,8 @@ def p_expression_9(p): if(parse_debug): print('expression_9', list(p)) + p[0] = PEUnary(Leaf(token.STRING, '|'), p[3]) + # { PEUnary*tmp = new PEUnary('|', p[3]); # FILE_NAME(tmp, @3); # p[0] = tmp; @@ -5359,6 +5386,8 @@ def p_expression_10(p): if(parse_debug): print('expression_10', list(p)) + p[0] = PEUnary(Leaf(token.STRING, '^'), p[3]) + # { PEUnary*tmp = new PEUnary('^', p[3]); # FILE_NAME(tmp, @3); # p[0] = tmp; @@ -5407,6 +5436,8 @@ def p_expression_14(p): if(parse_debug): print('expression_14', list(p)) + p[0] = PEUnary(Leaf(token.STRING, 'K_NAND'), p[3]) + # { PEUnary*tmp = new PEUnary('A', p[3]); # FILE_NAME(tmp, @3); # p[0] = tmp; @@ -5419,6 +5450,8 @@ def p_expression_15(p): if(parse_debug): print('expression_15', list(p)) + p[0] = PEUnary(Leaf(token.STRING, 'K_NOR'), p[3]) + # { PEUnary*tmp = new PEUnary('N', p[3]); # FILE_NAME(tmp, @3); # p[0] = tmp; @@ -5431,6 +5464,8 @@ def p_expression_16(p): if(parse_debug): print('expression_16', list(p)) + p[0] = PEUnary(Leaf(token.STRING, 'K_NXOR'), p[3]) + # { PEUnary*tmp = new PEUnary('X', p[3]); # FILE_NAME(tmp, @3); # p[0] = tmp; @@ -5467,6 +5502,8 @@ def p_expression_19(p): if(parse_debug): print('expression_19', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '^'), p[1], p[4]) + # { PEBinary*tmp = new PEBinary('^', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5479,6 +5516,8 @@ def p_expression_20(p): if(parse_debug): print('expression_20', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '**'), p[1], p[4]) + # { PEBinary*tmp = new PEBPower('p', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5491,6 +5530,8 @@ def p_expression_21(p): if(parse_debug): print('expression_21', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '*'), p[1], p[4]) + # { PEBinary*tmp = new PEBinary('*', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5503,6 +5544,8 @@ def p_expression_22(p): if(parse_debug): print('expression_22', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '/'), p[1], p[4]) + # { PEBinary*tmp = new PEBinary('/', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5515,6 +5558,8 @@ def p_expression_23(p): if(parse_debug): print('expression_23', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '%'), p[1], p[4]) + # { PEBinary*tmp = new PEBinary('%', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5531,21 +5576,21 @@ def p_expression_24(p): # FILE_NAME(tmp, @2); # p[0] = tmp; # } + p[0] = PEBinary(Leaf(token.PLUS, '+'), p[1], p[4]) + + () def p_expression_25(p): '''expression : expression '-' attribute_list_opt expression ''' - if(parse_debug): + if(parse_debug > 2): print('expression_25', list(p)) # { PEBinary*tmp = new PEBinary('-', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; # } - try: - p[0] = Node(syms.atom, [p[1], Leaf(token.MINUS, '-'), p[4]]) - except: - p[0] = "bad input in p_expression_25" + p[0] = PEBinary(Leaf(token.MINUS, '-'), p[1], p[4]) () @@ -5553,9 +5598,11 @@ def p_expression_25(p): def p_expression_26(p): '''expression : expression '&' attribute_list_opt expression ''' - if(parse_debug): + if(parse_debug > 2): print('expression_26', list(p)) + p[0] = PEBinary(Leaf(token.AMPER, '&'), p[1], p[4]) + # { PEBinary*tmp = new PEBinary('&', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5565,9 +5612,12 @@ def p_expression_26(p): def p_expression_27(p): '''expression : expression '|' attribute_list_opt expression ''' - if(parse_debug): + if(parse_debug > 2): print('expression_27', list(p)) + p[0] = PEBinary(Leaf(token.VBAR, '|'), p[1], p[4]) + + # { PEBinary*tmp = new PEBinary('|', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5580,6 +5630,8 @@ def p_expression_28(p): if(parse_debug): print('expression_28', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '~&'), p[1], p[4]) + # { PEBinary*tmp = new PEBinary('A', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5592,6 +5644,8 @@ def p_expression_29(p): if(parse_debug): print('expression_29', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '~|'), p[1], p[4]) + # { PEBinary*tmp = new PEBinary('O', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5604,6 +5658,8 @@ def p_expression_30(p): if(parse_debug): print('expression_30', list(p)) + p[0] = PEBinary(Leaf(token.STRING, 'K_XNOR'), p[1], p[4]) + # { PEBinary*tmp = new PEBinary('X', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5616,6 +5672,8 @@ def p_expression_31(p): if(parse_debug): print('expression_31', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '<'), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('<', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5628,6 +5686,8 @@ def p_expression_32(p): if(parse_debug): print('expression_32', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '>'), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('>', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5640,6 +5700,8 @@ def p_expression_33(p): if(parse_debug): print('expression_33', list(p)) + p[0] = PEBinary(Leaf(token.STRING, 'K_LS'), p[1], p[4]) + # { PEBinary*tmp = new PEBShift('l', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5652,6 +5714,8 @@ def p_expression_34(p): if(parse_debug): print('expression_34', list(p)) + p[0] = PEBinary(Leaf(token.STRING, 'K_RS'), p[1], p[4]) + # { PEBinary*tmp = new PEBShift('r', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5664,6 +5728,8 @@ def p_expression_35(p): if(parse_debug): print('expression_35', list(p)) + p[0] = PEBinary(Leaf(token.STRING, 'K_RSS'), p[1], p[4]) + # { PEBinary*tmp = new PEBShift('R', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5676,6 +5742,8 @@ def p_expression_36(p): if(parse_debug): print('expression_36', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '=='), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('e', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5688,6 +5756,8 @@ def p_expression_37(p): if(parse_debug): print('expression_37', list(p)) + p[0] = PEBinary(Leaf(token.STRING, 'K_CEQ'), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('E', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5700,6 +5770,8 @@ def p_expression_38(p): if(parse_debug): print('expression_38', list(p)) + p[0] = PEBinary(Leaf(token.STRING, 'K_WEQ'), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('w', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5712,6 +5784,8 @@ def p_expression_39(p): if(parse_debug): print('expression_39', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '<='), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('L', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5724,6 +5798,8 @@ def p_expression_40(p): if(parse_debug): print('expression_40', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '>='), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('G', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5736,6 +5812,8 @@ def p_expression_41(p): if(parse_debug): print('expression_41', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '!='), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('n', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5748,6 +5826,8 @@ def p_expression_42(p): if(parse_debug): print('expression_42', list(p)) + p[0] = PEBinary(Leaf(token.STRING, 'K_CNE'), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('N', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5760,6 +5840,8 @@ def p_expression_43(p): if(parse_debug): print('expression_43', list(p)) + p[0] = PEBinary(Leaf(token.STRING, 'K_WNE'), p[1], p[4]) + # { PEBinary*tmp = new PEBComp('W', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5772,6 +5854,8 @@ def p_expression_44(p): if(parse_debug): print('expression_44', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '||'), p[1], p[4]) + # { PEBinary*tmp = new PEBLogic('o', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5784,6 +5868,8 @@ def p_expression_45(p): if(parse_debug): print('expression_45', list(p)) + p[0] = PEBinary(Leaf(token.STRING, '&&'), p[1], p[4]) + # { PEBinary*tmp = new PEBLogic('a', p[1], p[4]); # FILE_NAME(tmp, @2); # p[0] = tmp; @@ -5796,6 +5882,9 @@ def p_expression_46(p): if(parse_debug): print('expression_46', list(p)) + p[0] = Node(syms.atom, [p[1], Leaf(token.STRING, ' ? '), + p[4], Leaf(token.STRING, ' : '), p[6]]) + # { PETernary*tmp = new PETernary(p[1], p[4], p[6]); # FILE_NAME(tmp, @2); # p[0] = tmp; -- 2.30.2