Bind and validate function calls

This commit is contained in:
joeduffy 2017-02-02 17:11:58 -08:00
parent a5b7a35629
commit f6c96c2c86
2 changed files with 26 additions and 4 deletions

View file

@ -316,6 +316,7 @@ func (a *astBinder) checkLoadLocationExpression(node *ast.LoadLocationExpression
}
func (a *astBinder) checkNewExpression(node *ast.NewExpression) {
// TODO: look up the ctor.
// TODO: check the arguments.
var ty symbols.Type
@ -335,10 +336,29 @@ func (a *astBinder) checkNewExpression(node *ast.NewExpression) {
}
func (a *astBinder) checkInvokeFunctionExpression(node *ast.InvokeFunctionExpression) {
// TODO: ensure the target is a function type.
// TODO: check the arguments.
// TODO: the result of this invocation is the return type.
contract.Failf("Binding of %v nodes not yet implemented", node.GetKind())
ty := a.b.ctx.RequireType(node.Function)
if funty, isfun := ty.(*symbols.FunctionType); isfun {
// Typecheck the arguments.
parc := len(funty.Parameters)
argc := 0
if node.Arguments != nil {
argc = len(*node.Arguments)
}
if parc != argc {
a.b.Diag().Errorf(errors.ErrorArgumentCountMismatch.At(node), parc, argc)
}
if argc > 0 {
for i := 0; i < parc && i < argc; i++ {
a.checkExprType((*node.Arguments)[i], funty.Parameters[i])
}
}
// The resulting type of this expression is the same as the function's return type.
a.b.ctx.RegisterType(node, funty.Return)
} else {
a.b.Diag().Errorf(errors.ErrorCannotInvokeNonFunction.At(node), ty)
a.b.ctx.RegisterType(node, types.Any)
}
}
func (a *astBinder) checkLambdaExpression(node *ast.LambdaExpression) {

View file

@ -27,4 +27,6 @@ var (
ErrorIllegalAssignmentLValue = newError(519, "Cannot assign to the target LHS expression")
ErrorIllegalNumericAssignmentLValue = newError(520, "Cannot perform numeric assignment %v on a non-numeric LHS")
ErrorIllegalAssignmentTypes = newError(521, "Cannot assign a value of type %v to target of type %v")
ErrorCannotInvokeNonFunction = newError(522, "Cannot invoke a non-function; type '%v' is not a function")
ErrorArgumentCountMismatch = newError(523, "Function expects %v arguments; got %v instead")
)