Type check for Assignment and functioncall

Assignment is “=” as far as i understand

according to our compiler material on page 6, we should do the following things for its type checking

image
but on page 16…we only need to check it the left and the right expressions are scalar and the left one should be assignable L-Value…
I think these two requirements on page 6 and 16 are not exactly the same…should I obey the rules on page 6 and not on page 16, because the ones on 6 are more specifical …

according to page 6 regarding function calls : the parameter of the function is considered the left side ad the passed value the right side…I know like if we have the following function

int foo (int x, int y){
  return  x%y + y;}

void main(){
    foo ("c", "0.003);  //should be wrong because foo supposed to have two integer parameters...
}

but where and how can I get the passed value for the type check???

The rules on page 16 specify that the left side must be an “assignable L-value”. What precisely “assignable” means is described on page 6.

So, both things need to be true.

Your code example does not parse, if we ignore both the spurious " and the fact that TinyC does not have floats, we get an example that can be parsed but then is invalid for the reasons you mentioned.

You are already printing the arguments in your toString(), so you get the passed expressions (not values(!)) from the same place – you stored them in your FunctionCallExpression.

1 Like

about the checkType Method for callexpression,

i think we should compare the expressionlist in our callexpression class with the externalDeclaration list in AST factory?

first check their size, if they have the same size then we only need to check their types on the same positions are the same?

so for this Type check function by call expression our return type should be void?

The return type is Type and the value you return represents of the represented expression. Note that you can not overwrite a method and change the return type to void.

You should not explicitly do anything with the list here.

You rather check the type of the function designator (i.e. the foo in foo(1,2,3)). If it has function type, you can check that all of the argument expressions are assignable to the correct arguments, that the lengths match, etc. (If it does not have function type or there are other errors, that’s a type error).

The type of a call foo(1,2,3) of course depends on the type of foo. If foo : (int,int,int) -> int, then the type of the call is int. In general, the type of the call expression is the return type of the called function.

1 Like