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

working on decl analyser

parent 98fcb13c
No related branches found
No related tags found
No related merge requests found
Pipeline #23 passed with warnings with stages
in 6 minutes and 9 seconds
......@@ -4,12 +4,15 @@ task="$1"
input="$2"
output="$3"
# recolic: I edited class FuncDef so place target/assignment.jar before chocopy-ref.jar!
classPath="target/assignment.jar:chocopy-ref.jar"
if [[ "$task" = parse ]]; then
java -cp "chocopy-ref.jar:target/assignment.jar" chocopy.ChocoPy --pass=r --out "$output" "$input"
java -cp "$classPath" chocopy.ChocoPy --pass=r --out "$output" "$input"
elif [[ "$task" = type ]]; then
java -cp "chocopy-ref.jar:target/assignment.jar" chocopy.ChocoPy --pass=.s --out "$output" "$input"
java -cp "$classPath" chocopy.ChocoPy --pass=.s --out "$output" "$input"
elif [[ "$task" = ass ]]; then
java -cp "chocopy-ref.jar:target/assignment.jar" chocopy.ChocoPy --pass=.s --dir src/test/data/pa2/sample --test
java -cp "$classPath" chocopy.ChocoPy --pass=.s --dir src/test/data/pa2/sample --test
fi
exit $?
......
......@@ -62,4 +62,8 @@ public class SymbolTable<T> {
return this.parent;
}
public boolean isEmpty() {
return tab.isEmpty();
}
}
......@@ -21,7 +21,7 @@ public class FuncDef extends Declaration {
/** Other statements. */
public final List<Stmt> statements;
// Recolic
// recolic
public SymbolTable<SymbolType> symTable = null;
/** The AST for
......
......@@ -66,9 +66,9 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> {
@Override
public SymbolType analyze(FuncDef funcDef) {
System.out.println("debug: start funcDef");
System.out.println("debug: start funcDef: " + funcDef.getClass().getName());
assert funcDef.symTable == null;
sym = funcDef.symTable = new SymbolTable<>(sym);
sym = funcDef.symTable = new SymbolTable<SymbolType>(sym);
// Func parameter list
for(TypedVar param : funcDef.params) {
......
......@@ -65,26 +65,85 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
}
@Override
public SymbolType analyze(BinaryExpr e) {
SymbolType t1 = e.left.dispatch(this);
SymbolType t2 = e.right.dispatch(this);
switch (e.operator) {
case "-":
case "*":
case "//":
case "%":
if (INT_TYPE.equals(t1) && INT_TYPE.equals(t2)) {
return e.setInferredType(INT_TYPE);
} else {
err(e, "Cannot apply operator `%s` on types `%s` and `%s`",
e.operator, t1, t2);
return e.setInferredType(INT_TYPE);
}
default:
return e.setInferredType(OBJECT_TYPE);
public SymbolType analyze(BinaryExpr node) {
String op = node.operator;
SymbolType left_type = node.left.dispatch(this);
SymbolType right_type = node.right.dispatch(this);
//switch (e.operator) {
//case "-":
//case "*":
//case "//":
//case "%":
// if (INT_TYPE.equals(t1) && INT_TYPE.equals(t2)) {
// return e.setInferredType(INT_TYPE);
// } else {
// err(e, "Cannot apply operator `%s` on types `%s` and `%s`",
// e.operator, t1, t2);
// return e.setInferredType(INT_TYPE);
// }
//default:
// return e.setInferredType(OBJECT_TYPE);
//}
switch(op) {
case "-":
case "*":
case "//":
case "%":
case ">":
case "<":
case ">=":
case "<=":
if(left_type != ValueType.INT_TYPE || right_type != ValueType.INT_TYPE)
err(node, "Syntax Error: operand should be INT");
break;
case "and":
case "or":
if(left_type != ValueType.BOOL_TYPE || right_type != ValueType.BOOL_TYPE)
err(node, "Syntax Error: operand should be BOOL");
break;
case "+":
if(left_type != ValueType.INT_TYPE && left_type != ValueType.STR_TYPE && ! left_type.isListType())
err(node, "Syntax Error: operand of + should be INT or STR or LIST");
// fallthrough
case "==":
case "!=":
case "is":
if(node.left.getInferredType() != node.right.getInferredType())
err(node, "Syntax Error: binary operator operand type mismatch");
break;
default:
err(node, "Syntax Error: binary operator operand type not supported.");
}
// Now set target type.
SymbolType result_type = ValueType.NONE_TYPE;
switch(op) {
case "-":
case "*":
case "//":
case "%":
case "and":
case "or":
case "+":
result_type = left_type;
break;
case ">":
case "<":
case ">=":
case "<=":
case "==":
case "!=":
case "is":
result_type = ValueType.BOOL_TYPE;
break;
default:
throw new RuntimeException("Logic error");
}
return node.setInferredType(result_type);
}
@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