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

adding more expr

parent 1b4d9b43
No related branches found
No related tags found
No related merge requests found
Pipeline #33 passed with warnings with stages
in 6 minutes and 48 seconds
......@@ -45,6 +45,7 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> {
continue;
}
// TODO: DO NOT throw on duplicate id. generate a compiler error.
sym.put(name, type);
}
......
......@@ -7,12 +7,10 @@ import chocopy.common.analysis.types.ListValueType;
import chocopy.common.analysis.types.SymbolType;
import chocopy.common.analysis.types.ValueType;
import chocopy.common.astnodes.*;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import static chocopy.common.analysis.types.SymbolType.INT_TYPE;
import static chocopy.common.analysis.types.SymbolType.OBJECT_TYPE;
import static chocopy.common.analysis.types.SymbolType.*;
/** Analyzer that performs ChocoPy type checks on all nodes. Applied after
* collecting declarations. */
......@@ -66,7 +64,7 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
}
@Override
public SymbolType analyze(NoneLiteral node) {
return node.setInferredType(SymbolType.NONE_TYPE);
return node.setInferredType(NONE_TYPE);
}
@Override
public SymbolType analyze(BooleanLiteral node) {
......@@ -77,7 +75,7 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
public SymbolType analyze(AssignStmt node) {
SymbolType right_type = node.value.dispatch(this);
if(right_type == null)
right_type = ValueType.NONE_TYPE;
right_type = NONE_TYPE;
if(node.targets.size() == 1) {
String target = node.targets.get(0).toString();
System.out.println("debug: assign to target " + target);
......@@ -108,6 +106,10 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
String op = node.operator;
SymbolType left_type = node.left.dispatch(this);
SymbolType right_type = node.right.dispatch(this);
if(left_type == null)
left_type = NONE_TYPE;
if(right_type == null)
right_type = NONE_TYPE;
switch(op) {
case "-":
......@@ -146,7 +148,7 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
}
// Now set target type.
SymbolType result_type = ValueType.NONE_TYPE;
SymbolType result_type = NONE_TYPE;
switch(op) {
case "-":
case "*":
......@@ -195,6 +197,56 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
return node.setInferredType(fType.returnType);
}
@Override
public SymbolType analyze(UnaryExpr node) {
String op = node.operator;
switch(op) {
case "not":
if(!node.operand.dispatch(this).equals(ValueType.BOOL_TYPE)) {
err(node, "operand of not should be bool.");
}
return node.setInferredType(ValueType.BOOL_TYPE);
case "-":
if(!node.operand.dispatch(this).equals(INT_TYPE)) {
err(node, "operand of - should be int");
}
return node.setInferredType(INT_TYPE);
default:
err(node, "operator " + op + " unknown.");
return node.setInferredType(NONE_TYPE);
}
}
@Override
public SymbolType analyze(IfExpr node) {
SymbolType then_type = node.thenExpr.dispatch(this);
SymbolType else_type = node.elseExpr.dispatch(this);
if(!then_type.equals(else_type)) {
err(node, "then_type not equals to else_type. ");
}
return node.setInferredType(then_type);
}
@Override
public SymbolType analyze(IndexExpr node) {
SymbolType list_type = node.list.dispatch(this);
SymbolType index_type = node.index.dispatch(this);
if(index_type != INT_TYPE) {
err(node, "index_type should be int.");
}
if(list_type == STR_TYPE) {
return node.setInferredType(STR_TYPE);
}
else if(list_type.isListType()) {
ListValueType t = (ListValueType) list_type;
return node.setInferredType(t.elementType);
}
else {
err(node, "type " + list_type.toString() + " is not index-able.");
return node.setInferredType(NONE_TYPE);
}
}
@Override
public SymbolType analyze(Identifier id) {
String varName = id.name;
......
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