Merged in pa2 and updated test scripts
This commit is contained in:
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Literals True or False. */
|
||||
public final class BooleanLiteral extends Literal {
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/** A function call. */
|
||||
public class CallExpr extends Expr {
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/** A class definition. */
|
||||
public class ClassDef extends Declaration {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** A simple class type name. */
|
||||
public final class ClassType extends TypeAnnotation {
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
/** Represents a single error. Does not correspond to any Python source construct. */
|
||||
public class CompilerError extends Node {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Base of all AST nodes representing definitions or declarations. */
|
||||
public abstract class Declaration extends Node {
|
||||
|
||||
@@ -11,6 +12,7 @@ public abstract class Declaration extends Node {
|
||||
super(left, right);
|
||||
}
|
||||
|
||||
|
||||
/** Return the identifier defined by this Declaration. */
|
||||
@JsonIgnore
|
||||
public abstract Identifier getIdentifier();
|
||||
|
||||
@@ -7,6 +7,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/** Collects the error messages in a Program. There is exactly one per Program node. */
|
||||
public class Errors extends Node {
|
||||
|
||||
@@ -26,11 +27,13 @@ public class Errors extends Node {
|
||||
allowMultipleErrors = true;
|
||||
}
|
||||
|
||||
|
||||
/** Return true iff there are any errors. */
|
||||
public boolean hasErrors() {
|
||||
return !this.errors.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
/** Prevent multiple semantic errors on the same node. */
|
||||
public void suppressMultipleErrors() {
|
||||
allowMultipleErrors = false;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package chocopy.common.astnodes;
|
||||
|
||||
|
||||
import chocopy.common.analysis.types.Type;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Statements consisting of expressions. */
|
||||
public final class ExprStmt extends Stmt {
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/** For statements. */
|
||||
public class ForStmt extends Stmt {
|
||||
/** Control variable. */
|
||||
|
||||
@@ -5,6 +5,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/** Def statements. */
|
||||
public class FuncDef extends Declaration {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Declaration of global variable. */
|
||||
public class GlobalDecl extends Declaration {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** A simple identifier. */
|
||||
public class Identifier extends Expr {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Conditional expressions. */
|
||||
public class IfExpr extends Expr {
|
||||
/** Boolean condition. */
|
||||
|
||||
@@ -5,6 +5,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/** Conditional statement. */
|
||||
public class IfStmt extends Stmt {
|
||||
/** Test condition. */
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** List-indexing expression. */
|
||||
public class IndexExpr extends Expr {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Integer numerals. */
|
||||
public final class IntegerLiteral extends Literal {
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/** List displays. */
|
||||
public final class ListExpr extends Expr {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Type denotation for a list type. */
|
||||
public final class ListType extends TypeAnnotation {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Attribute accessor. */
|
||||
public class MemberExpr extends Expr {
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/** Method calls. */
|
||||
public class MethodCallExpr extends Expr {
|
||||
|
||||
|
||||
@@ -78,6 +78,7 @@ public abstract class Node {
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
private String errorMsg;
|
||||
|
||||
|
||||
/** A Node corresponding to source text between LEFT and RIGHT. */
|
||||
public Node(Location left, Location right) {
|
||||
if (left != null) {
|
||||
@@ -100,6 +101,7 @@ public abstract class Node {
|
||||
return location;
|
||||
}
|
||||
|
||||
|
||||
/** Copy LOCATION as getLocation(). */
|
||||
public void setLocation(final int[] location) {
|
||||
System.arraycopy(location, 0, this.location, 0, 4);
|
||||
@@ -113,6 +115,7 @@ public abstract class Node {
|
||||
this.errorMsg = msg;
|
||||
}
|
||||
|
||||
|
||||
/** Return true iff I have been marked with an error message. */
|
||||
@JsonIgnore
|
||||
public boolean hasError() {
|
||||
@@ -135,11 +138,13 @@ public abstract class Node {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Return a serialization of this node in JSON format. */
|
||||
public String toJSON() throws JsonProcessingException {
|
||||
return mapper.writeValueAsString(this);
|
||||
}
|
||||
|
||||
|
||||
/** Mapper to-and-from serialized JSON. */
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@@ -148,6 +153,7 @@ public abstract class Node {
|
||||
mapper.registerModule(new ParameterNamesModule());
|
||||
}
|
||||
|
||||
|
||||
/** Returns a T from JSON, a JSON-serialized T value with class CLAS. */
|
||||
public static <T> T fromJSON(String json, Class<T> clas) throws IOException {
|
||||
return mapper.readValue(json, clas);
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Nonlocal declaration. */
|
||||
public class NonLocalDecl extends Declaration {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** The expression 'None'. */
|
||||
public final class NoneLiteral extends Literal {
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** An entire ChocoPy program. */
|
||||
|
||||
public class Program extends Node {
|
||||
|
||||
/** Initial variable, class, and function declarations. */
|
||||
@@ -42,12 +42,14 @@ public class Program extends Node {
|
||||
return analyzer.analyze(this);
|
||||
}
|
||||
|
||||
|
||||
/** Returns true iff there is at least one error in the program. */
|
||||
@JsonIgnore
|
||||
public boolean hasErrors() {
|
||||
return errors.hasErrors();
|
||||
}
|
||||
|
||||
|
||||
/** A convenience method returning the list of all CompilerErrors for this program. */
|
||||
@JsonIgnore
|
||||
public List<CompilerError> getErrorList() {
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Return from function. */
|
||||
public class ReturnStmt extends Stmt {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** String constants. */
|
||||
public final class StringLiteral extends Literal {
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package chocopy.common.astnodes;
|
||||
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** Base of all AST nodes representing type annotations (list or class types. */
|
||||
public abstract class TypeAnnotation extends Node {
|
||||
/** An annotation spanning source locations [LEFT..RIGHT]. */
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** An identifier with attached type annotation. */
|
||||
public class TypedVar extends Node {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** An expression applying a unary operator. */
|
||||
public class UnaryExpr extends Expr {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package chocopy.common.astnodes;
|
||||
import chocopy.common.analysis.NodeAnalyzer;
|
||||
import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
|
||||
/** A declaration of a variable (i.e., with type annotation). */
|
||||
public class VarDef extends Declaration {
|
||||
/** The variable and its assigned type. */
|
||||
|
||||
@@ -5,6 +5,7 @@ import java_cup.runtime.ComplexSymbolFactory.Location;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/** Indefinite repetition construct. */
|
||||
public class WhileStmt extends Stmt {
|
||||
/** Test for whether to continue. */
|
||||
@@ -19,6 +20,7 @@ public class WhileStmt extends Stmt {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
|
||||
public <T> T dispatch(NodeAnalyzer<T> analyzer) {
|
||||
return analyzer.analyze(this);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user