ruby: slicc: avoid duplicate code for function argument check
Both FuncCallExprAST and MethodCallExprAST had code for checking the arguments with which a function is being called. The patch does away with this duplication. Now the code for checking function call arguments resides in the Func class.
This commit is contained in:
parent
4727fc26f8
commit
426e38af8b
4 changed files with 32 additions and 36 deletions
|
@ -93,22 +93,7 @@ class FuncCallExprAST(ExprAST):
|
||||||
if func is None:
|
if func is None:
|
||||||
self.error("Unrecognized function name: '%s'", func_name_args)
|
self.error("Unrecognized function name: '%s'", func_name_args)
|
||||||
|
|
||||||
if len(self.exprs) != len(func.param_types):
|
cvec, type_vec = func.checkArguments(self.exprs)
|
||||||
self.error("Wrong number of arguments passed to function : '%s'" +\
|
|
||||||
" Expected %d, got %d", self.proc_name,
|
|
||||||
len(func.param_types), len(self.exprs))
|
|
||||||
|
|
||||||
cvec = []
|
|
||||||
type_vec = []
|
|
||||||
for expr,expected_type in zip(self.exprs, func.param_types):
|
|
||||||
# Check the types of the parameter
|
|
||||||
actual_type,param_code = expr.inline(True)
|
|
||||||
if str(actual_type) != 'OOD' and \
|
|
||||||
str(actual_type) != str(expected_type):
|
|
||||||
expr.error("Type mismatch: expected: %s actual: %s" % \
|
|
||||||
(expected_type, actual_type))
|
|
||||||
cvec.append(param_code)
|
|
||||||
type_vec.append(expected_type)
|
|
||||||
|
|
||||||
# OK, the semantics of "trigger" here is that, ports in the
|
# OK, the semantics of "trigger" here is that, ports in the
|
||||||
# machine have different priorities. We always check the first
|
# machine have different priorities. We always check the first
|
||||||
|
|
|
@ -56,20 +56,8 @@ class MethodCallExprAST(ExprAST):
|
||||||
self.error("Invalid method call: Type '%s' does not have a method '%s'",
|
self.error("Invalid method call: Type '%s' does not have a method '%s'",
|
||||||
obj_type, methodId)
|
obj_type, methodId)
|
||||||
|
|
||||||
if len(self.expr_ast_vec) != \
|
func = obj_type.methods[methodId]
|
||||||
len(obj_type.methods[methodId].param_types):
|
func.checkArguments(self.expr_ast_vec)
|
||||||
# Right number of parameters
|
|
||||||
self.error("Wrong number of parameters for function name: '%s', " + \
|
|
||||||
"expected: , actual: ", proc_name,
|
|
||||||
len(obj_type.methods[methodId].param_types),
|
|
||||||
len(self.expr_ast_vec))
|
|
||||||
|
|
||||||
for actual_type, expected_type in \
|
|
||||||
zip(paramTypes, obj_type.methods[methodId].param_types):
|
|
||||||
if actual_type != expected_type and \
|
|
||||||
str(actual_type["interface"]) != str(expected_type):
|
|
||||||
self.error("Type mismatch: expected: %s actual: %s",
|
|
||||||
expected_type, actual_type)
|
|
||||||
|
|
||||||
# Return the return type of the method
|
# Return the return type of the method
|
||||||
return obj_type.methods[methodId].return_type
|
return obj_type.methods[methodId].return_type
|
||||||
|
@ -78,10 +66,9 @@ class MethodCallExprAST(ExprAST):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class MemberMethodCallExprAST(MethodCallExprAST):
|
class MemberMethodCallExprAST(MethodCallExprAST):
|
||||||
def __init__(self, slicc, obj_expr_ast, proc_name, expr_ast_vec):
|
def __init__(self, slicc, obj_expr_ast, func_call):
|
||||||
s = super(MemberMethodCallExprAST, self)
|
s = super(MemberMethodCallExprAST, self)
|
||||||
s.__init__(slicc, proc_name, expr_ast_vec)
|
s.__init__(slicc, func_call.proc_name, func_call.exprs)
|
||||||
|
|
||||||
self.obj_expr_ast = obj_expr_ast
|
self.obj_expr_ast = obj_expr_ast
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|
|
@ -669,15 +669,18 @@ class SLICC(Grammar):
|
||||||
|
|
||||||
def p_expr__member_method_call(self, p):
|
def p_expr__member_method_call(self, p):
|
||||||
"aexpr : aexpr DOT ident '(' exprs ')'"
|
"aexpr : aexpr DOT ident '(' exprs ')'"
|
||||||
p[0] = ast.MemberMethodCallExprAST(self, p[1], p[3], p[5])
|
p[0] = ast.MemberMethodCallExprAST(self, p[1],
|
||||||
|
ast.FuncCallExprAST(self, p[3], p[5]))
|
||||||
|
|
||||||
def p_expr__member_method_call_lookup(self, p):
|
def p_expr__member_method_call_lookup(self, p):
|
||||||
"aexpr : aexpr '[' exprs ']'"
|
"aexpr : aexpr '[' exprs ']'"
|
||||||
p[0] = ast.MemberMethodCallExprAST(self, p[1], "lookup", p[3])
|
p[0] = ast.MemberMethodCallExprAST(self, p[1],
|
||||||
|
ast.FuncCallExprAST(self, "lookup", p[3]))
|
||||||
|
|
||||||
def p_expr__class_method_call(self, p):
|
def p_expr__class_method_call(self, p):
|
||||||
"aexpr : type DOUBLE_COLON ident '(' exprs ')'"
|
"aexpr : type DOUBLE_COLON ident '(' exprs ')'"
|
||||||
p[0] = ast.ClassMethodCallExprAST(self, p[1], p[3], p[5])
|
p[0] = ast.ClassMethodCallExprAST(self, p[1],
|
||||||
|
ast.FuncCallExprAST(self, p[3], p[5]))
|
||||||
|
|
||||||
def p_expr__aexpr(self, p):
|
def p_expr__aexpr(self, p):
|
||||||
"expr : aexpr"
|
"expr : aexpr"
|
||||||
|
|
|
@ -62,6 +62,27 @@ class Func(Symbol):
|
||||||
def writeCodeFiles(self, path, includes):
|
def writeCodeFiles(self, path, includes):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def checkArguments(self, args):
|
||||||
|
if len(args) != len(self.param_types):
|
||||||
|
self.error("Wrong number of arguments passed to function : '%s'" +\
|
||||||
|
" Expected %d, got %d", self.c_ident,
|
||||||
|
len(self.param_types), len(args))
|
||||||
|
|
||||||
|
cvec = []
|
||||||
|
type_vec = []
|
||||||
|
for expr,expected_type in zip(args, self.param_types):
|
||||||
|
# Check the types of the parameter
|
||||||
|
actual_type,param_code = expr.inline(True)
|
||||||
|
if str(actual_type) != 'OOD' and \
|
||||||
|
str(actual_type) != str(expected_type) and \
|
||||||
|
str(actual_type["interface"]) != str(expected_type):
|
||||||
|
expr.error("Type mismatch: expected: %s actual: %s" % \
|
||||||
|
(expected_type, actual_type))
|
||||||
|
cvec.append(param_code)
|
||||||
|
type_vec.append(expected_type)
|
||||||
|
|
||||||
|
return cvec, type_vec
|
||||||
|
|
||||||
def generateCode(self):
|
def generateCode(self):
|
||||||
'''This write a function of object Chip'''
|
'''This write a function of object Chip'''
|
||||||
if "external" in self:
|
if "external" in self:
|
||||||
|
|
Loading…
Reference in a new issue