Skip to content

Commit 1878123

Browse files
committed
Fixed symbol table.
1 parent 6ba3bb3 commit 1878123

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

pls.py

+30-14
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def p_start(t):
180180

181181
def p_statement_conc(t):
182182
'statement : statement statement'
183-
t[0] = "%s %s"%(t[1],t[2])
183+
t[0] = "%s\n%s"%(t[1],t[2])
184184

185185
def p_statement_sc(t):
186186
'statement : statement SC'
@@ -462,17 +462,19 @@ def p_func_call(t):
462462
else: t[0] = "%s ( %s )"%(t[1],t[3])
463463

464464
def p_params_rev(t):
465-
'''rev_params : rev_params rev_params
465+
'''rev_params : rev_params COMMA rev_params
466466
| type dim NAME
467467
| typename
468468
| '''
469469
global symbols, loadedFuncs
470470
if len(t) == 1: t[0] = ""
471471
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]
476478

477479

478480
def p_params_snd(t):
@@ -523,8 +525,11 @@ def p_type_graph(t):
523525

524526
def p_statement_print(t):
525527
'statement : PRINT value'
528+
global header
526529
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]
528533
else: t[0] = "System.out.println(%s);"%t[2]
529534

530535

@@ -568,7 +573,7 @@ def p_statement_for4(t):
568573
t[0] = "%s for(; (%s) != 0;) { %s}"%(t[3],t[5],t[9])
569574

570575
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]))
572577

573578

574579
def p_break(t):
@@ -597,7 +602,7 @@ def p_multidim(t):
597602

598603
def p_dim(t):
599604
'''dim : LSBRACKET value RSBRACKET'''
600-
t[0] = "[(int)%s]"%t[2]
605+
t[0] = "[(int)(%s)]"%t[2]
601606

602607
def p_dim_empty(t):
603608
'''dim : LSBRACKET RSBRACKET'''
@@ -643,7 +648,7 @@ def p_entry2(t):
643648

644649
def p_shell_args(t):
645650
'value : DOLLAR value'
646-
t[0] = "args[(int)%s]"%t[2]
651+
t[0] = "args[(int)(%s)]"%t[2]
647652

648653
######################### FILE_IO
649654

@@ -677,6 +682,8 @@ def get_type(x):
677682
return 'double'
678683
elif is_text_literal(x):
679684
return 'String'
685+
elif is_non_indexed_array(x):
686+
return get_non_indexed_array_type(x)
680687
elif is_indexed_array(x):
681688
return get_indexed_array_type(x)
682689
else:
@@ -688,19 +695,28 @@ def is_num_literal(x):
688695
def is_text_literal(x):
689696
return str(x).startswith('"') and x.endswith('"')
690697

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+
691707
def is_indexed_array(x):
692-
m = re.match('^[a-zA-Z0-9_]+\[\d+\]$',x)
708+
m = re.match('[a-zA-Z0-9_]+ \[[^[].*$',x)
693709
m = (m != None)
694710
return m
695711

696712
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])
699715

700716

701717
# perform translation
702718

703-
yacc.yacc()
719+
yacc.yacc(debug=False)
704720

705721
input_code = open(sys.argv[1],"r")
706722

0 commit comments

Comments
 (0)