@@ -180,7 +180,7 @@ def p_start(t):
180
180
181
181
def p_statement_conc (t ):
182
182
'statement : statement statement'
183
- t [0 ] = "%s %s" % (t [1 ],t [2 ])
183
+ t [0 ] = "%s\n %s" % (t [1 ],t [2 ])
184
184
185
185
def p_statement_sc (t ):
186
186
'statement : statement SC'
@@ -462,17 +462,19 @@ def p_func_call(t):
462
462
else : t [0 ] = "%s ( %s )" % (t [1 ],t [3 ])
463
463
464
464
def p_params_rev (t ):
465
- '''rev_params : rev_params rev_params
465
+ '''rev_params : rev_params COMMA rev_params
466
466
| type dim NAME
467
467
| typename
468
468
| '''
469
469
global symbols , loadedFuncs
470
470
if len (t ) == 1 : t [0 ] = ""
471
471
elif len (t ) == 4 :
472
- symbols [t [3 ]] = "%s[]" % t [1 ]
473
- t [0 ] = "%s %s %s" % (t [1 ],t [2 ],t [3 ])
474
- elif len (t ) == 2 : t [0 ] = t [1 ]
475
- else : t [0 ] = "%s, %s" % (t [1 ],t [2 ])
472
+ if t [2 ] == "," :
473
+ t [0 ] = "%s, %s" % (t [1 ],t [3 ])
474
+ else :
475
+ symbols [t [3 ]] = "%s[]" % t [1 ]
476
+ t [0 ] = "%s %s %s" % (t [1 ],t [2 ],t [3 ])
477
+ else : t [0 ] = t [1 ]
476
478
477
479
478
480
def p_params_snd (t ):
@@ -523,8 +525,11 @@ def p_type_graph(t):
523
525
524
526
def p_statement_print (t ):
525
527
'statement : PRINT value'
528
+ global header
526
529
type = get_type (t [2 ])
527
- if type == "double[]" or type == "String[]" : t [0 ] = "System.out.println( Arrays.toString(%s) );" % t [2 ]
530
+ if type == "double[]" or type == "String[]" :
531
+ header += "import java.util.Arrays;;\n "
532
+ t [0 ] = "System.out.println( Arrays.toString(%s) );" % t [2 ]
528
533
else : t [0 ] = "System.out.println(%s);" % t [2 ]
529
534
530
535
@@ -568,7 +573,7 @@ def p_statement_for4(t):
568
573
t [0 ] = "%s for(; (%s) != 0;) { %s}" % (t [3 ],t [5 ],t [9 ])
569
574
570
575
def p_error (t ):
571
- print ( " Syntax error at '%s'" % [t .value ])
576
+ raise BaseException ( "Line %s: Syntax error at '%s" % ( t . lexer . lineno - 1 , [t .value ]) )
572
577
573
578
574
579
def p_break (t ):
@@ -597,7 +602,7 @@ def p_multidim(t):
597
602
598
603
def p_dim (t ):
599
604
'''dim : LSBRACKET value RSBRACKET'''
600
- t [0 ] = "[(int)%s ]" % t [2 ]
605
+ t [0 ] = "[(int)(%s) ]" % t [2 ]
601
606
602
607
def p_dim_empty (t ):
603
608
'''dim : LSBRACKET RSBRACKET'''
@@ -643,7 +648,7 @@ def p_entry2(t):
643
648
644
649
def p_shell_args (t ):
645
650
'value : DOLLAR value'
646
- t [0 ] = "args[(int)%s ]" % t [2 ]
651
+ t [0 ] = "args[(int)(%s) ]" % t [2 ]
647
652
648
653
######################### FILE_IO
649
654
@@ -677,6 +682,8 @@ def get_type(x):
677
682
return 'double'
678
683
elif is_text_literal (x ):
679
684
return 'String'
685
+ elif is_non_indexed_array (x ):
686
+ return get_non_indexed_array_type (x )
680
687
elif is_indexed_array (x ):
681
688
return get_indexed_array_type (x )
682
689
else :
@@ -688,19 +695,28 @@ def is_num_literal(x):
688
695
def is_text_literal (x ):
689
696
return str (x ).startswith ('"' ) and x .endswith ('"' )
690
697
698
+ def is_non_indexed_array (x ):
699
+ m = re .match ('[a-zA-Z0-9_]+ \[\]' ,x )
700
+ m = (m != None )
701
+ return m
702
+
703
+ def get_non_indexed_array_type (x ):
704
+ array = x .split ('[' )[0 ][:- 1 ]
705
+ return "%s[]" % get_type (array )
706
+
691
707
def is_indexed_array (x ):
692
- m = re .match ('^ [a-zA-Z0-9_]+\[\d+\] $' ,x )
708
+ m = re .match ('[a-zA-Z0-9_]+ \[[^[].* $' ,x )
693
709
m = (m != None )
694
710
return m
695
711
696
712
def get_indexed_array_type (x ):
697
- array = x .split ('[' )[0 ]
698
- return "%s[]" % get_type (array )
713
+ array = x .split ('[' )[0 ][: - 1 ]
714
+ return "%s" % ( get_type (array ). split ( '[' )[ 0 ] )
699
715
700
716
701
717
# perform translation
702
718
703
- yacc .yacc ()
719
+ yacc .yacc (debug = False )
704
720
705
721
input_code = open (sys .argv [1 ],"r" )
706
722
0 commit comments