Skip to content

Commit

Permalink
fix a several bugs when testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeeef committed Jun 4, 2019
1 parent e391522 commit 6f273c2
Show file tree
Hide file tree
Showing 11 changed files with 1,080 additions and 3,483 deletions.
8 changes: 5 additions & 3 deletions yapc/AST.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ def parse_const_node(ast_node, symb_tab_node):
for child in const_expr_node_list:
id_, const_val_node = child.children
const_type = const_val_node.type
if const_type == 'sys_con':
const_type = 'boolean'
const_val, *_ = const_val_node.children
const_val = CONST_TYPE_TO_FUNC[const_type](const_val)
symb_tab_item = SymbolTableItem('const', {'const_val': const_val, 'const_type': const_type})
Expand Down Expand Up @@ -832,7 +834,7 @@ def parse_proc_stmt_node(ast_node, symb_tab_node):
# raise Exception("procedure `{}` expect {} args, got {} args"
# .format(proc_id, len(param_list), len(args_type_list)))
# 检查变量类型是否合适
for idx in range(len(param_list)):
for idx in range(min(len(param_list), len(args_type_list))):
_, arg_name, expect_type = param_list[idx]
given_type = args_type_list[idx]
if given_type != expect_type:
Expand Down Expand Up @@ -1000,7 +1002,7 @@ def parse_assign_stmt_node(ast_node, symb_tab_node):

else:
SemanticLogger.error(ast_node.lineno, '`{}` is not a valid field for record variable `{}`'
.format(record_field, ret_val))
.format(record_field, record_var))


def parse_expression_list(ast_node, symb_table):
Expand Down Expand Up @@ -1241,7 +1243,7 @@ def parse_factor_node(ast_node, symb_table):
return None, var_type
elif ast_node.type in CONST_VALUE_TYPE: # const value / true / false / maxint
const_val, = ast_node.children
const_type = ast_node.type
const_type = 'boolean' if ast_node.type == 'sys_con' else ast_node.type
return const_val, const_type
# return ConstantFoldItem(const_val, ast_node.type)
elif ast_node.type.startswith('factor'):
Expand Down
5 changes: 5 additions & 0 deletions yapc/SymbolTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def _to_dict(self):
def to_dict(self):
return self.dict_form

def __eq__(self, other):
if isinstance(other, RecordType):
other = other.dict_form
return self.dict_form == other

def __str__(self):
return str(self.dict_form)

Expand Down
19 changes: 19 additions & 0 deletions yapc/demo_test_cases/assign_demo.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
program Arithmetic;
const a = 2; b = 3.4; c = 'l'; flag=true;
type
int=integer;
people=record
score: integer;
sex: char;
end;
people_arr=array [1..3] of people;
var x, y, z: integer; q:boolean; newton: people; peoples: people_arr;
begin
q := true and true and true and not flag;
x := (a + 13) div 5 mod 1;
eistein.sex := 'm';
newton.sex := 'm';
newton.not_score := 100;
peoples[1] := newton;
peoples[10] := eistein;
end.
8 changes: 8 additions & 0 deletions yapc/demo_test_cases/const_fold.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
program ConstFold;
const aa = 1; bb = 1.5; flag = true;
var x, y: integer; z: real;
begin
x := -3 + aa * 10 div 2 / bb;
y := x + (1 + 3 mod (2 * 50 / 7));
z := not flag and aa = 1;
end.
17 changes: 17 additions & 0 deletions yapc/demo_test_cases/procedure.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
program Simple;
const a = 1; b = 1.5; flag=true;
type
real_arr=array[2..10] of real;
var x, y, s: integer; arr1: real_arr;

procedure print(var s: real; x, y: integer; xx,yy,zz: boolean);
var aa,bb,cc: real;
begin
s := x * x + y * y + a;
writeln(s);
end;
begin
print(a, true, false);
print(a, true, false, b, b * b, a div 2);
{print(a, b, b * b, true, flag, not flag);}
end.
4,206 changes: 872 additions & 3,334 deletions yapc/parselog.txt

Large diffs are not rendered by default.

270 changes: 135 additions & 135 deletions yapc/parsetab.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions yapc/test/comparisons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
writeln(3);


writeln("----");
{writeln("----");
for a := 1 to 10 do
writeln(a);
writeln("----");
Expand All @@ -25,6 +25,6 @@
begin
writeln(a);
a := a * 2;
end until a > 100;
end until a > 100;}

end.
6 changes: 3 additions & 3 deletions yapc/test_yacc/simple_arithmetic.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
const a = 2; b = 3.4; c = 'l'; flag=true;
var x, y, z: integer;
begin
x := 2;;
{x := true and true and true and not 1;
x := 2;
x := true and true and true and not 1;
x := true and false;
x := not flag and not (not flag or false);}
x := not flag and not (not flag or false);
x := 2 / 1;
x := (a + 13) div 5 mod 2;
z := -3 * 2 + -5 * -3;
Expand Down
17 changes: 12 additions & 5 deletions yapc/yacc_pas.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ def p_else_clause(p):


def p_repeat_stmt(p):
'repeat_stmt : kREPEAT stmt_list kUNTIL expression'
"""
repeat_stmt : kREPEAT stmt_list kUNTIL expression
"""
p[0] = Node("repeat_stmt", p.lexer.lineno, p[2], p[4])


Expand All @@ -444,18 +446,23 @@ def p_while_stmt(p):


def p_for_stmt(p):
'for_stmt : kFOR ID ASSIGN expression direction expression kDO stmt'
"""
for_stmt : kFOR ID ASSIGN expression direction expression kDO stmt
"""
p[0] = Node("for_stmt", p.lexer.lineno, p[2], p[4], p[5], p[6], p[8])


def p_direction(p):
'''direction : kTO
| kDOWNTO'''
"""direction : kTO
| kDOWNTO
"""
p[0] = p[1]


def p_case_stmt(p):
'''case_stmt : kCASE expression kOF case_expr_list kEND'''
"""
case_stmt : kCASE expression kOF case_expr_list kEND
"""
p[0] = Node("case_stmt", p.lexer.lineno, p[2], p[4])


Expand Down
3 changes: 2 additions & 1 deletion yapc/yapc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from CodeGenerator import CodeGenerator
from ErrorHandler import SemanticLogger

test_file = 'test_yacc/simple.pas'
# test_file = 'test_yacc/simple.pas'
test_file = 'demo_test_cases/assign_demo.pas'
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--input', help='input pascal file', default=test_file)
arg_parser.add_argument('--output', help='output intermediate code path', default='./yapc.out')
Expand Down

0 comments on commit 6f273c2

Please sign in to comment.