Skip to content
Snippets Groups Projects
Verified Commit d080332c authored by Recolic Keghart's avatar Recolic Keghart
Browse files

classdef callexpr fit

parent bde10c64
No related branches found
No related tags found
No related merge requests found
Pipeline #47 passed with warnings with stages
in 4 minutes and 47 seconds
......@@ -5,6 +5,7 @@ import java.util.List;
import chocopy.common.analysis.NodeAnalyzer;
import chocopy.common.analysis.types.SymbolType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java_cup.runtime.ComplexSymbolFactory.Location;
/** A class definition. */
......@@ -18,6 +19,7 @@ public class ClassDef extends Declaration {
public final List<Declaration> declarations;
// Recolic:
@JsonIgnore
public Map<String, SymbolType> memberMap = null;
/** An AST for class
......
......@@ -49,6 +49,9 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
///////////////////////////// Program //////////////////////////////////////
@Override
public SymbolType analyze(Program program) {
if(isFirstRun)
sym.put("object", OBJECT_TYPE);
for (Declaration decl : program.declarations) {
String name = decl.getIdentifier().name;
......@@ -89,8 +92,8 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
assert classDef.memberMap != null;
// My-name already pushed to symbolMap
classDef.name.dispatch(this);
classDef.superClass.dispatch(this);
// TA doesn't like this: // classDef.name.dispatch(this);
// TA doesn't like this: // classDef.superClass.dispatch(this);
ClassValueType result_type = new ClassValueType(classDef.name.name);
SymbolTable<SymbolType> symLayer = new SymbolTable<>(sym);
......@@ -364,24 +367,33 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
@Override
public SymbolType analyze(CallExpr node) {
SymbolType funcType = node.function.dispatch(this);
if(funcType == null) {
SymbolType calledType = node.function.dispatch(this);
if(calledType == null) {
err(node, "calling undeclared function " + node.function.name);
return null;
}
if(!funcType.isFuncType()) {
err(node, "calling non-function variable " + node.function.name);
return null;
if(calledType.isFuncType()) {
FuncType fType = (FuncType) calledType;
List<SymbolType> args_type = new ArrayList<SymbolType>();
for(Expr arg : node.args) {
args_type.add(arg.dispatch(this));
}
if(!fType.parameters.equals(args_type)) {
err(node, "function parameter type list mismatch: " + node.function.name + ". Expected " + fType.parameters.toString() + ", got " + args_type.toString());
}
return node.setInferredType(fType.returnType);
}
FuncType fType = (FuncType) funcType;
List<SymbolType> args_type = new ArrayList<SymbolType>();
for(Expr arg : node.args) {
args_type.add(arg.dispatch(this));
else if(calledType.equals(NONE_TYPE)) {
err(node, "can not create instance of NONE_TYPE: " + node.function.name);
return NONE_TYPE;
}
if(!fType.parameters.equals(args_type)) {
err(node, "function parameter type list mismatch: " + node.function.name + ". Expected " + fType.parameters.toString() + ", got " + args_type.toString());
else {
// a = int(123)
// b = classA(fuck, you)
node.function.setInferredType(null); // TA doesn't like this too. clear it!
return node.setInferredType(calledType);
}
return node.setInferredType(fType.returnType);
}
@Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment