Merge branch 'sanjar/improvements' of https://github.com/nyu-compiler-construction/pa3-chocopy-code-generation-mjolnir into sanjar/improvements
This commit is contained in:
@@ -0,0 +1,516 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import java_cup.runtime.*;
|
||||
import chocopy.common.astnodes.*;
|
||||
|
||||
/* The following code section is copied verbatim to the generated
|
||||
* parser class. */
|
||||
parser code {:
|
||||
|
||||
/* The following fields and methods deal with error reporting
|
||||
* Avoid changing these unless you know what you are doing. */
|
||||
|
||||
/** Node that accumulates error messages to be added to the Program
|
||||
* node produced as a result. */
|
||||
public final Errors errors = new Errors(new ArrayList<>());
|
||||
|
||||
/** Return the Program node that results from parsing the stream of
|
||||
* tokens produced by lexical analysis. In the case of syntax errors,
|
||||
* the program may be empty, but will have error messages. */
|
||||
public Program parseProgram(boolean debug) {
|
||||
try {
|
||||
Symbol result = debug ? debug_parse() : parse();
|
||||
if (result == null || !(result.value instanceof Program)) {
|
||||
return new Program(new Location(0, 0), new Location(0, 0),
|
||||
new ArrayList<Declaration>(),
|
||||
new ArrayList<Stmt>(),
|
||||
errors);
|
||||
} else {
|
||||
return (Program) result.value;
|
||||
}
|
||||
} catch (RuntimeException excp) {
|
||||
throw excp;
|
||||
} catch (Exception excp) {
|
||||
String msg =
|
||||
String.format("Internal parser error detected: %s%n", excp);
|
||||
throw new AssertionError(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SymbolFactory getSymbolFactory() {
|
||||
return ((ChocoPyLexer) getScanner()).symbolFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void syntax_error(Symbol cur_token) {
|
||||
String token = symbl_name_from_id(cur_token.sym);
|
||||
String text = ((ChocoPyLexer) getScanner()).yytext();
|
||||
errors.syntaxError(
|
||||
((ComplexSymbolFactory.ComplexSymbol) cur_token).xleft,
|
||||
((ComplexSymbolFactory.ComplexSymbol) cur_token).xright,
|
||||
"Parse error near token %s: %s", token, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unrecovered_syntax_error(Symbol cur_token) {
|
||||
/* Do not die */
|
||||
}
|
||||
:}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
* FEEL FREE TO MODIFY ANYTHING BELOW THIS LINE
|
||||
*
|
||||
* The rules provided below parse expressions of the form <INT> + <INT> + ...
|
||||
* You can re-use these rules or edit them as you wish. The start rule
|
||||
* should return a node of type Program.
|
||||
*
|
||||
* Tips: Production rules are usually followed by action code that will be
|
||||
* copied to the generated parser to be executed immediately after a reduce
|
||||
* operation; that is, when a production rule has been matched. You can name
|
||||
* a nonterminal or terminal symbol in a production rule using the colon
|
||||
* notation, e.g. expr_stmt ::= expr:e, to get the AST node for the matched
|
||||
* expression. In the action code, `e` will be a variable of whatever type
|
||||
* has been declared for the corresponding nonterminal, such as `Expr`.
|
||||
* Therefore, you can construct an AST Node of type `ExprStmt` with `e` in the
|
||||
* constructor: `new ExprStmt(exleft, exright, e)`
|
||||
*
|
||||
* The variables `exleft` and `exright` are automatically generated by CUP
|
||||
* and contain Location objects for the start and end of the expression `e`.
|
||||
* You can collect start and line number info for AST nodes by taking the
|
||||
* location of the left end of the leftmost symbol in a rule and the
|
||||
* location of the right end of the rightmost symbol. The auto-generated
|
||||
* variables have names `<sym>xleft` and `<sym>xright`, where <sym> is the
|
||||
* name given to the symbol using the colon notation.
|
||||
*
|
||||
* When you have nonterminals that are lists of things, e.g. List<Stmt> or
|
||||
* List<Declaration>, it is helpful to get the leftmost and rightmost
|
||||
* source location from within this list; we have provided some utility
|
||||
* functions below to do just that.
|
||||
**************************************************************************/
|
||||
|
||||
|
||||
/* The following code section is copied verbatim to the class that performs
|
||||
* production-rule actions. */
|
||||
action code {:
|
||||
|
||||
/** Return a mutable list initially containing the single value ITEM. */
|
||||
<T> List<T> single(T item) {
|
||||
List<T> list = new ArrayList<>();
|
||||
if (item != null) {
|
||||
list.add(item);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/** If ITEM is non-null, appends it to the end of LIST. Then returns
|
||||
* LIST. */
|
||||
<T> List<T> combine(List<T> list, T item) {
|
||||
if (item != null) {
|
||||
list.add(item);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
<T> List<T> combine(List<T> list, List<T> item) {
|
||||
if (item != null) {
|
||||
Iterator<T> it = item.iterator();
|
||||
while(it.hasNext())
|
||||
list.add(it.next());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
/** Return a mutable empty list. */
|
||||
<T> List<T> empty() {
|
||||
return new ArrayList<T>();
|
||||
}
|
||||
class FuncBody {
|
||||
public List<Declaration> fbd;
|
||||
public List<Stmt> sl;
|
||||
public FuncBody(List<Declaration> fbd, List<Stmt> sl){
|
||||
this.fbd = fbd;
|
||||
this.sl = sl;
|
||||
}
|
||||
}
|
||||
/** Return the leftmost non-whitespace location in NODES, or null if NODES
|
||||
* is empty. Assumes that the nodes of NODES are ordered in increasing
|
||||
* order of location, from left to right. */
|
||||
ComplexSymbolFactory.Location getLeft(List<? extends Node> nodes) {
|
||||
if (nodes.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Node first = nodes.get(0);
|
||||
return new ComplexSymbolFactory.Location(first.getLocation()[0],
|
||||
first.getLocation()[1]);
|
||||
}
|
||||
|
||||
/** Return the rightmost non-whitespace location in NODES, or null if NODES
|
||||
* is empty. Assumes that the nodes of NODES are ordered in increasing
|
||||
* order of location, from left to right. */
|
||||
ComplexSymbolFactory.Location getRight(List<? extends Node> nodes) {
|
||||
if (nodes.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Node last = nodes.get(nodes.size()-1);
|
||||
return new ComplexSymbolFactory.Location(last.getLocation()[2],
|
||||
last.getLocation()[3]);
|
||||
}
|
||||
|
||||
:}
|
||||
|
||||
/* Terminal symbols (tokens returned by the lexer). The declaration
|
||||
* terminal <identifier1>, <identifier2>, ...;
|
||||
* declares each <identifieri> as the denotation of a distinct type terminal
|
||||
* symbol for use in the grammar. The declaration
|
||||
* terminal <type> <identifier1>, ...;
|
||||
* does the same, and in addition indicates that the lexer supplies a
|
||||
* semantic value of type <type> for these symbols that may be referenced
|
||||
* in actions ( {: ... :} ).
|
||||
*/
|
||||
terminal INDENT;
|
||||
terminal DEDENT;
|
||||
terminal String ID;
|
||||
terminal String STRING;
|
||||
|
||||
|
||||
|
||||
/* Terminal Delimiters */
|
||||
terminal NEWLINE;
|
||||
terminal String COLON;
|
||||
terminal String COMMA;
|
||||
|
||||
/* Terminal Literals */
|
||||
terminal Integer NUMBER;
|
||||
terminal Boolean BOOL;
|
||||
terminal String NONE;
|
||||
|
||||
/* Terminal Keywords */
|
||||
terminal String IF;
|
||||
terminal String ELSE;
|
||||
terminal String ELIF;
|
||||
terminal String WHILE;
|
||||
terminal String CLASS;
|
||||
terminal String DEF;
|
||||
terminal String LAMBDA;
|
||||
terminal String AS;
|
||||
terminal String FOR;
|
||||
terminal String GLOBAL;
|
||||
terminal String IN;
|
||||
terminal String NONLOCAL;
|
||||
terminal String PASS;
|
||||
terminal String RETURN;
|
||||
terminal String ASSERT;
|
||||
terminal String AWAIT;
|
||||
terminal String BREAK;
|
||||
terminal String CONTINUE;
|
||||
terminal String DEL;
|
||||
terminal String EXCEPT;
|
||||
terminal String FINALLY;
|
||||
terminal String FROM;
|
||||
terminal String IMPORT;
|
||||
terminal String RAISE;
|
||||
terminal String TRY;
|
||||
terminal String WITH;
|
||||
terminal String YIELD;
|
||||
|
||||
|
||||
/* Terminal Operators */
|
||||
terminal String PLUS;
|
||||
terminal String MINUS;
|
||||
terminal String MUL;
|
||||
terminal String DIV;
|
||||
terminal String MOD;
|
||||
terminal String GT;
|
||||
terminal String LT;
|
||||
terminal String EQUAL;
|
||||
terminal String NEQ;
|
||||
terminal String GEQ;
|
||||
terminal String LEQ;
|
||||
terminal String ASSIGN;
|
||||
terminal String AND;
|
||||
terminal String OR;
|
||||
terminal String NOT;
|
||||
terminal String DOT;
|
||||
terminal String LPAR;
|
||||
terminal String RPAR;
|
||||
terminal String LBR;
|
||||
terminal String RBR;
|
||||
terminal String ARROW;
|
||||
terminal String IS;
|
||||
terminal String UMINUS;
|
||||
|
||||
|
||||
/* Returned by the lexer for erroneous tokens. Since it does not appear in
|
||||
* the grammar, it indicates a syntax error. */
|
||||
terminal String UNRECOGNIZED;
|
||||
|
||||
/* Nonterminal symbols (defined in production rules below).
|
||||
* As for terminal symbols,
|
||||
* non terminal <type> <identifier1>, ..., <identifiern>;
|
||||
* defines the listed nonterminal identifier symbols to have semantic values
|
||||
* of type <type>. */
|
||||
non terminal Program program;
|
||||
non terminal List<Declaration> defs, program_head, opt_program_head, class_body, class_body_defs, fun_body_decs;
|
||||
non terminal List<Stmt> stmt_list, opt_stmt_list, block, else_body;
|
||||
non terminal Stmt stmt, simple_stmt;
|
||||
non terminal Expr expr, pexpr, cexpr;
|
||||
non terminal VarDef var_def;
|
||||
non terminal ClassDef class_def;
|
||||
non terminal FuncDef fun_def;
|
||||
non terminal Literal literal;
|
||||
non terminal StringLiteral bin_op, comp_op;
|
||||
non terminal TypedVar typed_var;
|
||||
non terminal TypeAnnotation type, ret_type;
|
||||
non terminal Identifier identifier;
|
||||
non terminal List<TypedVar> typed_vars;
|
||||
non terminal GlobalDecl global_decl;
|
||||
non terminal NonLocalDecl nonlocal_decl;
|
||||
non terminal List<Expr> opt_target, expr_list;
|
||||
non terminal Expr target;
|
||||
non terminal MemberExpr member_expr;
|
||||
non terminal IndexExpr index_expr;
|
||||
non terminal FuncBody fun_body;
|
||||
|
||||
|
||||
|
||||
/* Precedences (lowest to highest) for resolving what would otherwise be
|
||||
* ambiguities in the form of shift/reduce conflicts.. */
|
||||
precedence left OR;
|
||||
precedence left AND;
|
||||
precedence left NOT;
|
||||
precedence nonassoc EQUAL, NEQ, LT, GT, LEQ, GEQ, IS;
|
||||
precedence left PLUS, MINUS;
|
||||
precedence left MUL, DIV, MOD;
|
||||
precedence left UMINUS;
|
||||
precedence left DOT, COMMA, LBR, RBR;
|
||||
precedence right IF, ELSE;
|
||||
|
||||
/* The start symbol. */
|
||||
start with program;
|
||||
|
||||
|
||||
/***** GRAMMAR RULES *****/
|
||||
|
||||
/* Rules are defined in the order given by the language reference */
|
||||
|
||||
/* program */
|
||||
program ::= program_head:d opt_stmt_list:s
|
||||
{:
|
||||
ComplexSymbolFactory.Location left = d.isEmpty() ? getLeft(s) : getLeft(d);
|
||||
if(left == null)
|
||||
left = new ComplexSymbolFactory.Location(1,1);
|
||||
RESULT = new Program(left, sxright, d, s, errors);
|
||||
:}
|
||||
;
|
||||
|
||||
program_head ::= program_head:d var_def:vd {: RESULT = combine(d, vd); :}
|
||||
| program_head:d class_def:cd {: RESULT = combine(d, cd); :}
|
||||
| program_head:d fun_def:fd {: RESULT = combine(d, fd); :}
|
||||
| program_head:d error:e {: RESULT = d; :}
|
||||
| {: RESULT = empty(); :}
|
||||
;
|
||||
|
||||
opt_stmt_list ::= {: RESULT = empty(); :}
|
||||
| stmt_list:s {: RESULT = s; :}
|
||||
;
|
||||
|
||||
|
||||
/* class_def */
|
||||
class_def ::= CLASS:c identifier:id LPAR identifier:parentId RPAR COLON NEWLINE INDENT class_body:cb DEDENT {: RESULT = new ClassDef(cxleft, getRight(cb), id, parentId, cb); :};
|
||||
|
||||
|
||||
/* class_body */
|
||||
class_body ::= PASS NEWLINE {: RESULT = empty(); :}
|
||||
| class_body_defs:defs {: RESULT = defs; :}
|
||||
;
|
||||
|
||||
class_body_defs ::= class_body_defs:defs var_def:vd {: RESULT = combine(defs, vd); :}
|
||||
| class_body_defs:defs fun_def:fd {: RESULT = combine(defs, fd); :}
|
||||
| class_body_defs:defs error {: RESULT = defs; :}
|
||||
| var_def:vd {: RESULT = single(vd); :}
|
||||
| fun_def:fd {: RESULT = single(fd); :}
|
||||
;
|
||||
|
||||
|
||||
/* fun_def */
|
||||
fun_def ::= DEF:def identifier:id LPAR typed_vars:params RPAR ret_type:rt COLON:col NEWLINE INDENT fun_body_decs:fbd stmt_list:sl DEDENT
|
||||
{: TypeAnnotation _rt = rt;if((rt instanceof ClassType) && ((ClassType)rt).className == "<None>") _rt = new ClassType( colxright, colxright, "<None>");RESULT = new FuncDef(defxleft, getRight(sl), id, params, _rt, fbd, sl); :}
|
||||
;
|
||||
|
||||
ret_type ::= ARROW type:t {: RESULT= t; :}
|
||||
| {: RESULT= new ClassType(null, null,"<None>"); :}
|
||||
;
|
||||
|
||||
typed_vars ::= typed_var:tv {: RESULT= single(tv); :}
|
||||
| typed_vars:tvs COMMA typed_var:tv {: RESULT= combine(tvs, tv); :}
|
||||
| typed_vars:tvs COMMA error {: RESULT= tvs; :}
|
||||
| {: RESULT= empty(); :}
|
||||
;
|
||||
|
||||
|
||||
/* fun_body */
|
||||
fun_body ::= fun_body_decs:fbd stmt_list:sl {: RESULT = new FuncBody(fbd, sl);:}
|
||||
| fun_body_decs:fbd {: RESULT = new FuncBody(fbd, new ArrayList<Stmt>());:}
|
||||
;
|
||||
|
||||
fun_body_decs ::= fun_body_decs:fbd global_decl:gd {: RESULT= combine(fbd, gd); :}
|
||||
| fun_body_decs:fbd nonlocal_decl:nd {: RESULT= combine(fbd, nd); :}
|
||||
| fun_body_decs:fbd var_def:vd {: RESULT= combine(fbd, vd); :}
|
||||
| fun_body_decs:fbd fun_def:fd {: RESULT= combine(fbd, fd); :}
|
||||
| fun_body_decs:fbd error {: RESULT= fbd; :}
|
||||
| {: RESULT= empty(); :}
|
||||
;
|
||||
|
||||
|
||||
/* typed_var */
|
||||
typed_var ::= identifier:id COLON type:t {: RESULT = new TypedVar(idxleft, txright, id, t); :};
|
||||
|
||||
|
||||
/* type */
|
||||
type ::= identifier:id {: RESULT = new ClassType(idxleft, idxright, id.name); :}
|
||||
| STRING:str {: RESULT = new ClassType(strxleft, strxright, str); :}
|
||||
| LBR:lbr type:t RBR:rbr {: RESULT = new ListType(lbrxleft, rbrxright, t); :}
|
||||
;
|
||||
|
||||
|
||||
/* global_decl */
|
||||
global_decl ::= GLOBAL:g identifier:id NEWLINE {: RESULT = new GlobalDecl(gxleft, idxright, id); :};
|
||||
|
||||
|
||||
/* nonlocal_decl */
|
||||
nonlocal_decl ::= NONLOCAL:n identifier:id NEWLINE {: RESULT = new NonLocalDecl(nxleft, idxright, id); :};
|
||||
|
||||
|
||||
/* var_def */
|
||||
var_def ::= typed_var:t ASSIGN literal:l NEWLINE {: RESULT = new VarDef(txleft, lxright, t, l); :};
|
||||
|
||||
|
||||
/* stmt */
|
||||
stmt ::= simple_stmt:s NEWLINE {: RESULT = s; :}
|
||||
| IF:i expr:cond COLON block:b else_body:elb {: RESULT = new IfStmt(ixleft, getRight(elb), cond, b, elb); :}
|
||||
| WHILE:wh expr:cond COLON block:b {: RESULT = new WhileStmt(whxleft, getRight(b), cond, b); :}
|
||||
| FOR:f identifier:id IN expr:e COLON block:b {: RESULT = new ForStmt(fxleft, getRight(b), id, e, b); :}
|
||||
;
|
||||
|
||||
|
||||
else_body ::= ELSE:el COLON block:b {: RESULT = b; :}
|
||||
| ELIF:el expr:cond COLON block:b else_body:elb {: RESULT = single(new IfStmt(elxleft, getRight(elb), cond, b, elb)); :}
|
||||
| {: RESULT = empty(); :}
|
||||
;
|
||||
|
||||
|
||||
/* simple_stmt */
|
||||
simple_stmt ::= PASS:p {: RESULT = null; :}
|
||||
| expr:e {: RESULT = new ExprStmt(exleft, exright, e); :}
|
||||
| RETURN:r expr:e {: RESULT = new ReturnStmt(rxleft, exright, e); :}
|
||||
| RETURN:r {: RESULT = new ReturnStmt(rxleft, rxright, null); :}
|
||||
| opt_target:ot expr:e {: RESULT = new AssignStmt(getLeft(ot), exright, ot, e); :}
|
||||
;
|
||||
|
||||
|
||||
opt_target ::= opt_target:ot target:t ASSIGN {: RESULT = combine(ot, t); :}
|
||||
| target:t ASSIGN {: RESULT = single(t); :}
|
||||
;
|
||||
|
||||
|
||||
/* block */
|
||||
block ::= NEWLINE INDENT stmt_list:sl DEDENT {: RESULT = sl; :};
|
||||
|
||||
|
||||
/* literal */
|
||||
literal ::= NONE:n {: RESULT = new NoneLiteral(nxleft, nxright); :}
|
||||
| BOOL:b {: RESULT = new BooleanLiteral(bxleft, bxright, b); :}
|
||||
| NUMBER:n {: RESULT = new IntegerLiteral(nxleft, nxright, n); :}
|
||||
| STRING:s {: RESULT = new StringLiteral(sxleft, sxright, s); :}
|
||||
;
|
||||
|
||||
|
||||
/* expr */
|
||||
expr ::= cexpr:ce {: RESULT = ce; :}
|
||||
| NOT:n expr:exp {: RESULT = new UnaryExpr(nxleft, expxright, n, exp); :}
|
||||
| expr:e1 AND:a expr:e2 {: RESULT = new BinaryExpr(e1xleft, e2xright, e1, a, e2); :}
|
||||
| expr:e1 OR:o expr:e2 {: RESULT = new BinaryExpr(e1xleft, e2xright, e1, o, e2); :}
|
||||
| expr:e1 IF expr:e2 ELSE expr:e3 {: RESULT = new IfExpr(e1xleft, e3xright, e2, e1, e3); :}
|
||||
;
|
||||
|
||||
|
||||
/* cexpr */
|
||||
cexpr ::= pexpr:pe {: RESULT = pe; :}
|
||||
| pexpr:p1 comp_op:co cexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, co.value, p2); :}
|
||||
;
|
||||
|
||||
|
||||
/* pexpr */
|
||||
pexpr ::= identifier:id {: RESULT = id; :}
|
||||
| literal:l {: RESULT = l; :}
|
||||
| LBR:lbr expr_list:l RBR:rbr {: RESULT = new ListExpr(lbrxleft, rbrxright, l); :}
|
||||
| LPAR:lpar expr:e RPAR:rpar {: RESULT = e; :}
|
||||
| member_expr:m {: RESULT = m; :}
|
||||
| index_expr:i {: RESULT = i; :}
|
||||
| member_expr:m LPAR expr_list:l RPAR:rpar {: RESULT = new MethodCallExpr(mxleft, rparxright, m, l); :}
|
||||
| identifier:id LPAR expr_list:l RPAR:rpar {: RESULT = new CallExpr(idxleft, rparxright, id, l); :}
|
||||
| pexpr:p1 PLUS:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :}
|
||||
| pexpr:p1 MINUS:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :}
|
||||
| pexpr:p1 MUL:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :}
|
||||
| pexpr:p1 DIV:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :}
|
||||
| pexpr:p1 MOD:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :}
|
||||
| MINUS:m pexpr:p {: RESULT = new UnaryExpr(mxleft, pxright, m, p); :} %prec UMINUS
|
||||
;
|
||||
|
||||
expr_list ::= expr:e {: RESULT = single(e); :}
|
||||
| expr_list:el COMMA expr:e {: RESULT = combine(el, e); :}
|
||||
| {: RESULT = new ArrayList<Expr>(); :}
|
||||
;
|
||||
|
||||
/* bin_op */ //We may still be able to use bin_op, so I left it here.
|
||||
bin_op ::= PLUS:a {: RESULT = new StringLiteral(axleft, axright, "+"); :}
|
||||
| MINUS:a {: RESULT = new StringLiteral(axleft, axright, "-"); :}
|
||||
| MUL:a {: RESULT = new StringLiteral(axleft, axright, "*"); :}
|
||||
| DIV:a {: RESULT = new StringLiteral(axleft, axright, "//"); :} //Section 2.6.3 in chocopy language reference
|
||||
| MOD:a {: RESULT = new StringLiteral(axleft, axright, "%"); :}
|
||||
;
|
||||
|
||||
|
||||
/* comp_op */ //this might also need some change in order not to break left associativity
|
||||
comp_op ::= EQUAL:a {: RESULT = new StringLiteral(axleft, axright, "=="); :}
|
||||
| NEQ:a {: RESULT = new StringLiteral(axleft, axright, "!="); :}
|
||||
| LEQ:a {: RESULT = new StringLiteral(axleft, axright, "<="); :}
|
||||
| GEQ:a {: RESULT = new StringLiteral(axleft, axright, ">="); :}
|
||||
| LT:a {: RESULT = new StringLiteral(axleft, axright, "<"); :}
|
||||
| GT:a {: RESULT = new StringLiteral(axleft, axright, ">"); :}
|
||||
| IS:a {: RESULT = new StringLiteral(axleft, axright, "is"); :}
|
||||
;
|
||||
|
||||
|
||||
/* member_expr */
|
||||
member_expr ::= pexpr:p DOT identifier:id {: RESULT = new MemberExpr(pxleft, idxright, p, id); :}
|
||||
;
|
||||
|
||||
|
||||
/* index_expr */
|
||||
index_expr ::= pexpr:p LBR expr:e RBR:rbr {: RESULT = new IndexExpr(pxleft, rbrxright, p, e); :}
|
||||
;
|
||||
|
||||
|
||||
/* target */
|
||||
target ::= identifier:id {: RESULT = id; :}
|
||||
| member_expr:m {: RESULT = m; :}
|
||||
| index_expr:i {: RESULT = i; :}
|
||||
;
|
||||
|
||||
|
||||
/* Extras - rules below have not been given in language reference, we have them to ease implementation */
|
||||
identifier ::= ID:idStr {: RESULT = new Identifier(idStrxleft, idStrxright, idStr); :};
|
||||
|
||||
|
||||
stmt_list ::= stmt:s {: RESULT = single(s); :}
|
||||
| stmt_list:l stmt:s {: RESULT = combine(l, s); :}
|
||||
| stmt_list:l error {: RESULT = l; :}
|
||||
/* If there is a syntax error in the source, this says to discard
|
||||
* symbols from the parsing stack and perform reductions until
|
||||
* there is a stmt_list on top of the stack, and then to discard
|
||||
* input symbols until it is possible to shift again, reporting
|
||||
* a syntax error. */
|
||||
;
|
||||
@@ -5,7 +5,6 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** Utility functions for general use. */
|
||||
public class Utils {
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package chocopy.common.analysis.types;
|
||||
|
||||
import chocopy.common.astnodes.ClassType;
|
||||
import chocopy.common.astnodes.Identifier;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import chocopy.common.analysis.SymbolTable;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Represents the semantic value of a simple class reference. */
|
||||
public class ClassVType extends ValueType {
|
||||
|
||||
/** The name of the class. */
|
||||
public final String className;
|
||||
public SymbolTable<Type> scope;
|
||||
public ClassVType super_class;
|
||||
/** A class type for the class named CLASSNAME. */
|
||||
@JsonCreator
|
||||
public ClassVType(@JsonProperty String className) {
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
/** A class type for the class referenced by CLASSTYPEANNOTATION. */
|
||||
public ClassVType(ClassType classTypeAnnotation) {
|
||||
this.className = classTypeAnnotation.className;
|
||||
}
|
||||
|
||||
@Override
|
||||
@JsonProperty
|
||||
public String className() {
|
||||
return className;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
ClassVType classType = (ClassVType) o;
|
||||
return Objects.equals(className, classType.className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return className;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package chocopy.common.analysis.types;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Semantic information for a function or method. */
|
||||
public class FuncValueType extends Type {
|
||||
|
||||
/** Function's name. */
|
||||
//public final String name;
|
||||
|
||||
/** Types of parameters. */
|
||||
public List<ValueType> parameters;
|
||||
/** Function's return type. */
|
||||
public final ValueType returnType;
|
||||
|
||||
|
||||
/** Create a FuncType returning RETURNTYPE0, initially parameterless. */
|
||||
public FuncValueType(ValueType returnType0) {
|
||||
this(new ArrayList<>(), returnType0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a FuncType for NAME0 with formal parameter types PARAMETERS0, returning type
|
||||
* RETURNTYPE0.
|
||||
*/
|
||||
@JsonCreator
|
||||
public FuncValueType(List<ValueType> parameters0, ValueType returnType0) {
|
||||
this.parameters = parameters0;
|
||||
this.returnType = returnType0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFuncType() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Return the type of the K-th parameter. */
|
||||
public ValueType getParamType(int k) {
|
||||
return parameters.get(k);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<function>";
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package chocopy.pa1;
|
||||
|
||||
import chocopy.common.astnodes.Program;
|
||||
import java_cup.runtime.ComplexSymbolFactory;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* Interface between driver and parser.
|
||||
*/
|
||||
public class StudentParser {
|
||||
|
||||
/**
|
||||
* Return the Program AST resulting from parsing INPUT. Turn on
|
||||
* parser debugging iff DEBUG.
|
||||
*/
|
||||
public static Program process(String input, boolean debug) {
|
||||
ChocoPyLexer lexer = new ChocoPyLexer(new StringReader(input));
|
||||
ChocoPyParser parser =
|
||||
new ChocoPyParser(lexer, new ComplexSymbolFactory());
|
||||
return parser.parseProgram(debug);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,377 @@
|
||||
package chocopy.pa2;
|
||||
|
||||
import chocopy.common.analysis.AbstractNodeAnalyzer;
|
||||
import chocopy.common.analysis.SymbolTable;
|
||||
import chocopy.common.analysis.types.*;
|
||||
import chocopy.common.astnodes.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/** Analyzes declarations to create a top-level symbol table. */
|
||||
public class DeclarationAnalyzer extends AbstractNodeAnalyzer<Type>
|
||||
{
|
||||
|
||||
/** Current symbol table. Changes with new declarative region. */
|
||||
private SymbolTable<Type> sym = new SymbolTable<>();
|
||||
/** Global symbol table. */
|
||||
private final SymbolTable<Type> globals;
|
||||
/** Receiver for semantic error messages. */
|
||||
|
||||
private final TypeChecker typeChecker;
|
||||
private final Errors errors;
|
||||
private final boolean firstPass;
|
||||
// In the first pass declanalyzer will create the global symtable
|
||||
// In the second pass, typeAnalyzer will call declanalyzer to
|
||||
// analyze local vars/func/class defs and create sub-scope symtable.
|
||||
private ClassVType current_class=null;
|
||||
private boolean postCheck = false;
|
||||
private String classDefError = null;
|
||||
/** A new declaration analyzer sending errors to ERRORS0. */
|
||||
public void initScope(SymbolTable<Type> s){
|
||||
// Symbol table entry for object class
|
||||
ClassVType cvt = new ClassVType("object"), obj = cvt;
|
||||
s.put("object", cvt);
|
||||
//Symbol table entry for int class
|
||||
cvt = new ClassVType("int");
|
||||
cvt.super_class = obj;
|
||||
s.put("int", cvt);
|
||||
//Symbol table entry for str class
|
||||
cvt = new ClassVType("str");
|
||||
cvt.super_class = obj;
|
||||
s.put("str", cvt);
|
||||
//Symbol table entry for bool class
|
||||
cvt = new ClassVType("bool");
|
||||
cvt.super_class = obj;
|
||||
s.put("bool", cvt);
|
||||
//Symbol table entry for None return type
|
||||
cvt = new ClassVType("<None>");
|
||||
cvt.super_class = obj;
|
||||
s.put("<None>", cvt);
|
||||
//Symbol table entry for inbuilt print function
|
||||
ArrayList<ValueType> param = new ArrayList<ValueType>();
|
||||
param.add(Type.OBJECT_TYPE);
|
||||
s.put("print", new FuncType(param, Type.NONE_TYPE));
|
||||
//Symbol table entry for inbuilt len function
|
||||
param = new ArrayList<ValueType>();
|
||||
param.add(Type.OBJECT_TYPE);
|
||||
s.put("len", new FuncType(param, Type.INT_TYPE));
|
||||
//Symbol table entry for inbuilt input function
|
||||
s.put("input", new FuncType(new ArrayList<>(), Type.STR_TYPE));
|
||||
}
|
||||
public SymbolTable<Type> createScope(SymbolTable<Type> s){
|
||||
SymbolTable<Type> newScope = new SymbolTable<>(s);
|
||||
initScope(newScope);
|
||||
return newScope;
|
||||
}
|
||||
//Initializer for the first pass.
|
||||
public DeclarationAnalyzer(Errors errors0)
|
||||
{
|
||||
firstPass = true;
|
||||
errors = errors0;
|
||||
globals = sym;
|
||||
initScope(sym);
|
||||
typeChecker = new TypeChecker(globals, errors);
|
||||
}
|
||||
//Initializer for the second pass.
|
||||
public DeclarationAnalyzer(Errors errors0, TypeChecker typeChecker, SymbolTable<Type> globals)
|
||||
{
|
||||
firstPass = false;
|
||||
this.typeChecker = typeChecker;
|
||||
errors = errors0;
|
||||
this.globals = globals;
|
||||
}
|
||||
public SymbolTable<Type> getGlobals()
|
||||
{
|
||||
return globals;
|
||||
}
|
||||
private boolean putSymChecked(Node node, String name, Type ty)
|
||||
{
|
||||
if (ty == null)
|
||||
return false;
|
||||
|
||||
if (globals.get(name)!= null && !(ty instanceof ClassVType) && globals.get(name) instanceof ClassVType) //class names are only in global scope
|
||||
errors.semError(node, "Cannot shadow class name: %s", name);
|
||||
else if (sym.declares(name))
|
||||
errors.semError(
|
||||
node, "Duplicate declaration of identifier in same scope: %s", name);
|
||||
else
|
||||
{
|
||||
sym.put(name, ty);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public Type analyze(Program program)
|
||||
{
|
||||
for (Declaration decl : program.declarations)
|
||||
{
|
||||
Identifier id = decl.getIdentifier();
|
||||
String name = id.name;
|
||||
Type type = decl.dispatch(this);
|
||||
}
|
||||
// Check for return statements at top
|
||||
for (Stmt stmt : program.statements)
|
||||
{
|
||||
if (stmt instanceof ReturnStmt)
|
||||
errors.semError(
|
||||
stmt, "Return statement cannot appear at the top level");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(FuncDef node)
|
||||
{
|
||||
if(!postCheck){
|
||||
Type fTy = null;
|
||||
if(sym.declares(node.name.name))
|
||||
fTy = sym.get(node.name.name);
|
||||
|
||||
FuncType current_func=null;
|
||||
|
||||
if(!(fTy instanceof FuncType))
|
||||
{
|
||||
if(fTy == null)
|
||||
{
|
||||
current_func = new FuncType(new ArrayList<ValueType>(),
|
||||
ValueType.annotationToValueType(node.returnType));
|
||||
|
||||
for (TypedVar param : node.params)
|
||||
{
|
||||
ValueType p = ValueType.annotationToValueType(param.type);
|
||||
current_func.parameters.add(p);
|
||||
if(classDefError != null && p.className().equals(classDefError))
|
||||
errors.semError(param.type, "Invalid type annotation; there is no class named: %s", classDefError);
|
||||
}
|
||||
sym.put(node.name.name, current_func);
|
||||
if(!firstPass)
|
||||
{
|
||||
SymbolTable<Type> parent = sym.getParent();
|
||||
if(parent!=null && parent != globals){
|
||||
parent.put(node.name.name, current_func);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(fTy instanceof ClassVType)
|
||||
errors.semError(node.name, "Cannot shadow class name: %s", node.name.name);
|
||||
else
|
||||
errors.semError(
|
||||
node.name, "Duplicate declaration of identifier in same scope: %s", node.name.name);
|
||||
|
||||
}
|
||||
else if(firstPass || sym.declares(node.name.name))
|
||||
errors.semError(
|
||||
node.name, "Duplicate declaration of identifier in same scope: %s", node.name.name);
|
||||
if(!firstPass){
|
||||
|
||||
}
|
||||
return current_func;
|
||||
} else {
|
||||
postCheck = false;
|
||||
|
||||
ValueType returnType = ValueType.annotationToValueType(node.returnType);
|
||||
if(returnType!=null && !returnType.isSpecialType() && !returnType.isListType() && !(globals.get(returnType.className()) instanceof ClassVType))
|
||||
errors.semError(
|
||||
node.returnType, "Invalid type annotation; there is no class named: %s", returnType.className());
|
||||
|
||||
for(TypedVar param : node.params)
|
||||
{
|
||||
ValueType pTy = ValueType.annotationToValueType(param.type);
|
||||
|
||||
if(!(pTy.isListType() && !pTy.elementType().equals(Type.EMPTY_TYPE))&&!pTy.isSpecialType() && !(globals.get(pTy.className()) instanceof ClassVType))
|
||||
errors.semError(param.type, "Invalid type annotation; there is no class named: %s", pTy.className());
|
||||
|
||||
putSymChecked(param.identifier, param.identifier.name, pTy);
|
||||
}
|
||||
ArrayList<Declaration> varDefs = new ArrayList<>(), otherDefs = new ArrayList<>();
|
||||
for (Declaration decl : node.declarations)
|
||||
if(decl instanceof VarDef || decl instanceof GlobalDecl || decl instanceof NonLocalDecl)
|
||||
varDefs.add(decl);
|
||||
else
|
||||
otherDefs.add(decl);
|
||||
for (Declaration decl : varDefs)
|
||||
if(decl instanceof VarDef||decl instanceof NonLocalDecl)
|
||||
decl.dispatch(this);
|
||||
else
|
||||
decl.dispatch(typeChecker);
|
||||
for(Declaration decl : otherDefs)
|
||||
decl.dispatch(this);
|
||||
for(Declaration decl : otherDefs)
|
||||
if(decl instanceof FuncDef)
|
||||
decl.dispatch(typeChecker);
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public boolean compare_functions(FuncType fun1, FuncType fun2)
|
||||
{
|
||||
if (fun1.returnType.equals(fun2.returnType)==false)
|
||||
return false;
|
||||
if (fun1.parameters.size() != fun2.parameters.size())
|
||||
return false;
|
||||
for (int i=1; i<fun1.parameters.size(); i++)
|
||||
if (fun1.parameters.get(i).equals(fun2.parameters.get(i))==false)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(ClassDef node)
|
||||
{
|
||||
ClassVType cvt=new ClassVType(node.name.name);
|
||||
if(!putSymChecked(node.name, node.name.name, cvt))
|
||||
classDefError = node.name.name;
|
||||
|
||||
SymbolTable<Type> current_scope=createScope(sym);
|
||||
sym=current_scope;
|
||||
current_class=cvt;
|
||||
Type super_class = sym.get(node.superClass.name);
|
||||
if(super_class instanceof ClassVType)
|
||||
cvt.super_class = (ClassVType)super_class;//new ClassVType(super_class.className());
|
||||
|
||||
SymbolTable<Type> super_scope=null;
|
||||
Set<String> super_syms=null;
|
||||
if (super_class == null)
|
||||
{
|
||||
errors.semError(
|
||||
node.superClass, "Super-class not defined: %s", node.superClass.name);
|
||||
}
|
||||
else if ((super_class instanceof ClassVType)==false)
|
||||
{
|
||||
errors.semError(
|
||||
node.superClass, "Super-class must be a class: %s", node.superClass.name);
|
||||
}
|
||||
else if (node.superClass.name.equals("int") || node.superClass.name.equals("bool") || node.superClass.name.equals("str"))
|
||||
{
|
||||
errors.semError(
|
||||
node.superClass, "Cannot extend special class: %s", node.superClass.name);
|
||||
}
|
||||
else
|
||||
{
|
||||
super_scope = cvt.super_class.scope;
|
||||
if(cvt.super_class.scope != null)
|
||||
super_syms = super_scope.getDeclaredSymbols();
|
||||
else
|
||||
super_syms = new HashSet<String>();
|
||||
HashSet<String> curr_syms = new HashSet<>();
|
||||
for (Declaration decl : node.declarations)
|
||||
{
|
||||
Identifier id = decl.getIdentifier();
|
||||
String name = id.name;
|
||||
|
||||
Type type = null;//decl.dispatch(this);
|
||||
type = decl.dispatch(this);
|
||||
if(type instanceof FuncType)
|
||||
{//For function declarations
|
||||
FuncType current_func = (FuncType) type;
|
||||
List<ValueType> params = current_func.parameters;
|
||||
if(name.equals("__init__") )
|
||||
if( params.size() != 1 ||
|
||||
!(params.get(0) instanceof ClassValueType)||
|
||||
!((ClassValueType)params.get(0)).className().equals(current_class.className))
|
||||
errors.semError(id, "Method overridden with different type signature: __init__");
|
||||
else
|
||||
sym.put(name, current_func);
|
||||
if(params.size() < 1 || !(params.get(0) instanceof ClassValueType) || !((ClassValueType)params.get(0)).className().equals(current_class.className))
|
||||
errors.semError(
|
||||
id, "First parameter of the following method must be of the enclosing class: %s", name);
|
||||
|
||||
if(curr_syms.contains(name)){
|
||||
errors.semError(id, "Duplicate declaration of identifier in same scope: %s", name);
|
||||
}
|
||||
else if (super_syms.contains(name))
|
||||
{
|
||||
if ((super_scope.get(id.name) instanceof FuncType)==false)
|
||||
errors.semError(id, "Cannot re-define attribute: %s", name);
|
||||
else
|
||||
{
|
||||
FuncType super_func = (FuncType) super_scope.get(id.name);
|
||||
if (compare_functions(super_func, current_func))
|
||||
sym.put(name, current_func);
|
||||
else
|
||||
errors.semError(
|
||||
id, "Method overridden with different type signature: %s", name);
|
||||
}
|
||||
}
|
||||
else
|
||||
sym.put(name, current_func);
|
||||
}
|
||||
else if (name.equals("__init__") && !(type instanceof FuncType))
|
||||
errors.semError(id, "Cannot re-define attribute: %s", name);
|
||||
else if (super_syms.contains(name))
|
||||
errors.semError(id, "Cannot re-define attribute: %s", name);
|
||||
else
|
||||
sym.put(name, type);
|
||||
curr_syms.add(name);
|
||||
}
|
||||
}
|
||||
if(super_syms != null)
|
||||
for (String super_sym : super_syms)
|
||||
{
|
||||
if (sym.getDeclaredSymbols().contains(super_sym)==false)
|
||||
sym.put(super_sym, super_scope.get(super_sym));
|
||||
}
|
||||
sym = sym.getParent();
|
||||
current_class.scope = current_scope;
|
||||
current_class=null;
|
||||
classDefError = null;
|
||||
return cvt;
|
||||
}
|
||||
boolean isVariableType(Type ty)
|
||||
{
|
||||
return ty.isSpecialType() || ty.equals(Type.OBJECT_TYPE)|| ty.isListType();
|
||||
}
|
||||
@Override
|
||||
public Type analyze(NonLocalDecl node)
|
||||
{
|
||||
SymbolTable<Type> parent=sym.getParent();
|
||||
if (parent.getParent()!=null && parent.declares(node.variable.name) && isVariableType(sym.get(node.variable.name)))
|
||||
{
|
||||
sym.put(node.variable.name, sym.get(node.variable.name));
|
||||
return sym.get(node.variable.name);
|
||||
}
|
||||
errors.semError(
|
||||
node.variable, "Not a nonlocal variable: %s", node.variable.name);
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Type analyze(VarDef node)
|
||||
{
|
||||
Type var_type = sym.get(node.var.identifier.name);
|
||||
if(firstPass || (sym != globals && (current_class==null || !sym.equals(current_class.scope)))){
|
||||
if (sym != globals && globals.get(node.var.identifier.name)!= null && globals.get(node.var.identifier.name) instanceof ClassVType) //class names are only in global scope
|
||||
errors.semError(node.var.identifier, "Cannot shadow class name: %s", node.var.identifier.name);
|
||||
else if(sym.getDeclaredSymbols().contains(node.var.identifier.name))
|
||||
errors.semError(
|
||||
node.var.identifier, "Duplicate declaration of identifier in same scope: %s", node.var.identifier.name);
|
||||
var_type = ValueType.annotationToValueType(node.var.type);
|
||||
sym.put(node.var.identifier.name, var_type);
|
||||
}
|
||||
Type val_type = node.value.dispatch(typeChecker);
|
||||
if( !firstPass && var_type instanceof ClassValueType)
|
||||
{
|
||||
String className = ((ClassValueType)var_type).className();
|
||||
Type varVType = sym.get(className);
|
||||
if(!(className != null && varVType instanceof ClassVType))
|
||||
errors.semError(node.var.type, "Invalid type annotation; there is no class named: %s", (className!=null?className:""));
|
||||
else if((!val_type.equals(Type.NONE_TYPE) && !StudentAnalysis.subClassOf(varVType,val_type, sym))||
|
||||
val_type.equals(Type.NONE_TYPE) && var_type.isSpecialType())
|
||||
errors.semError(node, "Expected type `%s`; got type `%s`", varVType, val_type);
|
||||
}
|
||||
return var_type;
|
||||
}
|
||||
|
||||
public void setScope(SymbolTable<Type> currentScope)
|
||||
{
|
||||
sym = currentScope;
|
||||
}
|
||||
public void setCurrClass(ClassVType current_class)
|
||||
{
|
||||
this.current_class = current_class;
|
||||
}
|
||||
public void setPostCheck(){
|
||||
postCheck = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package chocopy.pa2;
|
||||
|
||||
import chocopy.common.analysis.SymbolTable;
|
||||
import chocopy.common.analysis.types.ClassVType;
|
||||
import chocopy.common.analysis.types.ClassValueType;
|
||||
import chocopy.common.analysis.types.Type;
|
||||
import chocopy.common.analysis.types.ValueType;
|
||||
import chocopy.common.astnodes.ClassType;
|
||||
import chocopy.common.astnodes.Program;
|
||||
import java.util.ArrayList;
|
||||
/** Top-level class for performing semantic analysis. */
|
||||
public class StudentAnalysis {
|
||||
|
||||
/**
|
||||
* Perform semantic analysis on PROGRAM, adding error messages and type annotations. Provide
|
||||
* debugging output iff DEBUG. Returns modified tree.
|
||||
*/
|
||||
|
||||
public static
|
||||
boolean subClassOf(Type p, Type c, SymbolTable<Type> sym){
|
||||
String pName = p.className();
|
||||
if(pName!=null && pName.equals("object"))
|
||||
return true;
|
||||
if(c instanceof ClassValueType)
|
||||
c = sym.get(c.className());
|
||||
if(c instanceof ClassVType){
|
||||
ClassVType child = (ClassVType) c;
|
||||
|
||||
String typename = child.className;
|
||||
while(typename!=null){
|
||||
if(typename.equals(pName))
|
||||
return true;
|
||||
child = child.super_class;
|
||||
if(child!=null)
|
||||
typename = child.className;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
} else return p.equals(c);
|
||||
}
|
||||
private static void extractInhPath(Type ty, ArrayList<Type> res){
|
||||
if(ty == null)
|
||||
{
|
||||
res.add(Type.OBJECT_TYPE);
|
||||
return;
|
||||
}
|
||||
if(ty instanceof ClassVType){
|
||||
ClassVType t1 = (ClassVType) ty;
|
||||
String typename = t1.className;
|
||||
while(typename!=null){
|
||||
res.add(new ClassValueType(typename));
|
||||
t1 = t1.super_class;
|
||||
if(t1 != null)
|
||||
typename = t1.className();
|
||||
else break;
|
||||
}
|
||||
} else res.add(ty);
|
||||
if(!res.get(res.size() - 1).equals(Type.OBJECT_TYPE))
|
||||
res.add(Type.OBJECT_TYPE);
|
||||
}
|
||||
public static Type lowestCommonType(Type p, Type c, SymbolTable<Type> sym){
|
||||
if(p instanceof ClassValueType)
|
||||
p = sym.get(p.className());
|
||||
if(c instanceof ClassValueType)
|
||||
c = sym.get(c.className());
|
||||
ArrayList<Type> inhPath1 = new ArrayList<Type>(),
|
||||
inhPath2 = new ArrayList<Type>();
|
||||
extractInhPath(p, inhPath1);
|
||||
extractInhPath(c, inhPath2);
|
||||
int l1 = inhPath1.size(), l2 = inhPath2.size(),
|
||||
len = l1 < l2 ? l1 : l2;
|
||||
int i = 1;
|
||||
for(; i <= len; ++ i){
|
||||
if(!inhPath1.get(l1 - i).equals(inhPath2.get(l2 - i)))
|
||||
break;
|
||||
}
|
||||
return inhPath1.get(l1 - i + 1);
|
||||
}
|
||||
public static Program process(Program program, boolean debug) {
|
||||
if (program.hasErrors()) {
|
||||
return program;
|
||||
}
|
||||
|
||||
DeclarationAnalyzer declarationAnalyzer = new DeclarationAnalyzer(program.errors);
|
||||
program.dispatch(declarationAnalyzer);
|
||||
SymbolTable<Type> globalSym = declarationAnalyzer.getGlobals();
|
||||
|
||||
if (!program.hasErrors()) {
|
||||
TypeChecker typeChecker = new TypeChecker(globalSym, program.errors);
|
||||
program.dispatch(typeChecker);
|
||||
}
|
||||
// System.out.println(program);
|
||||
return program;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,549 @@
|
||||
package chocopy.pa2;
|
||||
|
||||
import chocopy.common.analysis.AbstractNodeAnalyzer;
|
||||
import chocopy.common.analysis.SymbolTable;
|
||||
import chocopy.common.analysis.types.ClassVType;
|
||||
import chocopy.common.analysis.types.ClassValueType;
|
||||
import chocopy.common.analysis.types.FuncType;
|
||||
import chocopy.common.analysis.types.FuncValueType;
|
||||
import chocopy.common.analysis.types.ListValueType;
|
||||
import chocopy.common.analysis.types.Type;
|
||||
import chocopy.common.analysis.types.ValueType;
|
||||
import chocopy.common.astnodes.*;
|
||||
|
||||
import static chocopy.common.analysis.types.Type.INT_TYPE;
|
||||
import static chocopy.common.analysis.types.Type.OBJECT_TYPE;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.swing.text.StyledEditorKit.BoldAction;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonInject.Value;
|
||||
|
||||
/**
|
||||
* Analyzer that performs ChocoPy type checks on all nodes. Applied after collecting declarations.
|
||||
*/
|
||||
public class TypeChecker extends AbstractNodeAnalyzer<Type> {
|
||||
// global scope
|
||||
private final SymbolTable<Type> sym;
|
||||
private final DeclarationAnalyzer declAnalyzer;
|
||||
/** The current symbol table (changes depending on the function being analyzed). */
|
||||
private SymbolTable<Type> currentScope;
|
||||
private Type currReturnType;
|
||||
private boolean returned = false, member = false;
|
||||
/** Collector for errors. */
|
||||
private final Errors errors;
|
||||
private boolean assign = false;
|
||||
private boolean declAnalyzed = false;
|
||||
private final HashMap<FuncDef, SymbolTable<Type>> funcScopes;
|
||||
/**
|
||||
* Creates a type checker using GLOBALSYMBOLS for the initial global symbol table and ERRORS0 to
|
||||
* receive semantic errors.
|
||||
*/
|
||||
public TypeChecker(SymbolTable<Type> globalSymbols, Errors errors0) {
|
||||
sym = globalSymbols;
|
||||
currentScope = sym;
|
||||
errors = errors0;
|
||||
currReturnType = null;
|
||||
declAnalyzer = new DeclarationAnalyzer(errors0, this, globalSymbols);
|
||||
funcScopes = new HashMap<>();
|
||||
}
|
||||
/**
|
||||
* Inserts an error message in NODE if there isn't one already. The message is constructed with
|
||||
* MESSAGE and ARGS as for String.format.
|
||||
*/
|
||||
private boolean isVariableType(Type ty){
|
||||
return ty.isSpecialType() || ty.equals(Type.OBJECT_TYPE);
|
||||
}
|
||||
public boolean pushDeclAnalyzed() {
|
||||
boolean orig = declAnalyzed;
|
||||
declAnalyzed = true;
|
||||
return orig;
|
||||
}
|
||||
public void popDeclAnalyzed(boolean orig) {
|
||||
declAnalyzed = orig;
|
||||
}
|
||||
private Type declAnalyze(Node node){
|
||||
//if(currentScope != sym)
|
||||
declAnalyzer.setScope(currentScope);
|
||||
return node.dispatch(declAnalyzer);
|
||||
}
|
||||
private void err(Node node, String message, Object... args) {
|
||||
errors.semError(node, message, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(Program program) {
|
||||
for (Declaration decl : program.declarations) {
|
||||
decl.dispatch(this);
|
||||
}
|
||||
for (Stmt stmt : program.statements) {
|
||||
stmt.dispatch(this);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Type analyze(ClassDef node){
|
||||
ClassVType t = (ClassVType) sym.get(node.name.name);
|
||||
SymbolTable<Type> backScope = currentScope;
|
||||
currentScope = t.scope;
|
||||
declAnalyzer.setCurrClass(t);
|
||||
for(Declaration decl : node.declarations){
|
||||
decl.dispatch(this);
|
||||
}
|
||||
declAnalyzer.setCurrClass(null);
|
||||
currentScope = backScope;
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Type analyze(AssignStmt node) {
|
||||
Type tr = node.value.dispatch(this);
|
||||
Type tl;
|
||||
boolean error = false;
|
||||
assign=true;
|
||||
for (Expr ex : node.targets)
|
||||
{
|
||||
tl = ex.dispatch(this);
|
||||
if(error) continue;
|
||||
else if(tl == null)
|
||||
{
|
||||
err(node, "Expression `%s` type inference error.", ex);
|
||||
error = true;
|
||||
}
|
||||
else if (ex instanceof IndexExpr &&
|
||||
((IndexExpr)ex).list.getInferredType().equals(Type.STR_TYPE))
|
||||
{
|
||||
err(ex, "`str` is not a list type");
|
||||
error = true;
|
||||
}
|
||||
else if(tr!=null && tl.isListType() && tr.isListType())
|
||||
{
|
||||
if(!((!tl.elementType().isSpecialType()&&tr.elementType().equals(Type.NONE_TYPE))||
|
||||
tl.equals(tr)||tr.elementType().equals(Type.EMPTY_TYPE)))
|
||||
{
|
||||
err(node, "Expected type `%s`; got type `%s`", tl, tr);
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
else if(tl.isListType() && Type.EMPTY_TYPE.equals(tr)) ; //continue;
|
||||
else if(tr != null && !(StudentAnalysis.subClassOf(tl, tr, currentScope) || !tl.isSpecialType() && tr.equals(Type.NONE_TYPE)))
|
||||
{
|
||||
err(node, "Expected type `%s`; got type `%s`", tl, tr);
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
assign=false;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(BooleanLiteral node) {
|
||||
return node.setInferredType(Type.BOOL_TYPE);
|
||||
}
|
||||
public void dispatchFuncDef(FuncDef node, SymbolTable<Type> scope, boolean declAnalyzed){
|
||||
boolean prevDeclAnalyzed = this.declAnalyzed;
|
||||
this.declAnalyzed = declAnalyzed;
|
||||
SymbolTable<Type> origScope = currentScope;
|
||||
currentScope = scope;
|
||||
node.dispatch(this);
|
||||
currentScope = origScope;
|
||||
this.declAnalyzed = prevDeclAnalyzed;
|
||||
}
|
||||
@Override
|
||||
public Type analyze(FuncDef node) {
|
||||
SymbolTable<Type> origScope = currentScope;
|
||||
if(funcScopes.get(node) != null)
|
||||
System.out.println("error");
|
||||
{
|
||||
currentScope = declAnalyzer.createScope(currentScope);
|
||||
funcScopes.put(node, currentScope);
|
||||
declAnalyzer.setPostCheck();
|
||||
declAnalyze(node);
|
||||
//currentScope = funcScopes.get(node);
|
||||
returned = false;
|
||||
Type prevReturnType = this.currReturnType;
|
||||
this.currReturnType = ValueType.annotationToValueType(node.returnType);
|
||||
for(Stmt st : node.statements)
|
||||
st.dispatch(this);
|
||||
|
||||
if(currReturnType != null && currReturnType.isSpecialType() && !returned)
|
||||
err(node.name, "All paths in this function/method must have a return statement: %s", node.name.name);
|
||||
this.currReturnType = prevReturnType;
|
||||
}
|
||||
currentScope = origScope;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(CallExpr node) {
|
||||
Type f = currentScope.get(node.function.name);
|
||||
ArrayList<Type> types = new ArrayList<>();
|
||||
for(Expr ex: node.args)
|
||||
types.add(ex.dispatch(this));
|
||||
if(f != null && f.isFuncType())
|
||||
{
|
||||
FuncType fty = (FuncType) f;
|
||||
int lArgs = node.args.size(), lPars = fty.parameters.size();
|
||||
if(lArgs != lPars)
|
||||
err(node, "Expected %d arguments; got %d", lPars, lArgs);
|
||||
else{
|
||||
for(int i = 0; i < lArgs; ++i){
|
||||
Type p = fty.parameters.get(i);
|
||||
Type c = types.get(i);
|
||||
if(((p.isSpecialType()&&!p.equals(c)) ||
|
||||
(!p.isSpecialType()&&!StudentAnalysis.subClassOf(p, c, currentScope)))
|
||||
&&!(p.isListType()&&c.equals(Type.EMPTY_TYPE))
|
||||
)
|
||||
err(node,"Expected type `%s`; got type `%s` in parameter %d", p, c, i);
|
||||
}
|
||||
}
|
||||
node.function.setInferredType(new FuncType(fty.parameters, fty.returnType));
|
||||
return node.setInferredType(fty.returnType);
|
||||
}
|
||||
else if (f != null && f instanceof ClassVType){
|
||||
ClassVType cty = (ClassVType) f;
|
||||
return node.setInferredType(new ClassValueType(f.className()));
|
||||
}
|
||||
else{
|
||||
err(node, "Not a function or class: %s", node.function.name);
|
||||
return node.setInferredType(Type.NONE_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(ClassType node) {
|
||||
return sym.get(node.className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(ForStmt node) {
|
||||
Type iterableType = node.iterable.setInferredType(
|
||||
node.iterable.dispatch(this));
|
||||
if(iterableType == null)
|
||||
err(node, "Iterable `%s` type inference error.", node.iterable);
|
||||
else if (iterableType.equals(Type.STR_TYPE))
|
||||
node.identifier.setInferredType(Type.STR_TYPE);
|
||||
else if(iterableType.elementType() == null){
|
||||
err(node, "`%s` isn't iterable", iterableType);
|
||||
}
|
||||
else
|
||||
node.identifier.setInferredType(
|
||||
iterableType.elementType()
|
||||
);
|
||||
for(Stmt st : node.body)
|
||||
st.dispatch(this);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(IfExpr node) {
|
||||
Type condTy = node.condition.dispatch(this);
|
||||
if(!condTy.equals(Type.BOOL_TYPE)){
|
||||
err(node, "Condition expression cannot be of type `%s`", condTy.className());
|
||||
}
|
||||
|
||||
Type ifTy = node.thenExpr.dispatch(this),
|
||||
elseTy = node.elseExpr.dispatch(this);
|
||||
|
||||
|
||||
return node.setInferredType(StudentAnalysis.lowestCommonType(ifTy, elseTy, currentScope));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(IfStmt node) {
|
||||
Type condTy = node.condition.dispatch(this);
|
||||
if(!condTy.equals(Type.BOOL_TYPE)){
|
||||
err(node, "Condition expression cannot be of type `%s`", condTy.className());
|
||||
}
|
||||
boolean prevReturned = returned, thenReturned;
|
||||
for(Stmt st : node.thenBody)
|
||||
st.dispatch(this);
|
||||
thenReturned = prevReturned || returned;
|
||||
returned = prevReturned;
|
||||
for(Stmt st : node.elseBody)
|
||||
st.dispatch(this);
|
||||
returned = returned && thenReturned;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(IndexExpr node) {
|
||||
Type listTy = node.list.dispatch(this);
|
||||
if(!(listTy.isListType() || listTy.equals(Type.STR_TYPE)))
|
||||
err(node, "Cannot index into type `%s`", listTy);
|
||||
if(!node.index.dispatch(this).equals(Type.INT_TYPE))
|
||||
err(node, "Index is of non-integer type `%s`", node.index.getInferredType());
|
||||
if(listTy.equals(Type.STR_TYPE))
|
||||
return node.setInferredType(Type.STR_TYPE);
|
||||
else if(listTy.elementType() != null)
|
||||
return node.setInferredType(listTy.elementType());
|
||||
else return node.setInferredType(Type.OBJECT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(ListExpr node) {
|
||||
Type t = null;
|
||||
for(Expr ex : node.elements)
|
||||
{
|
||||
Type thisType = ex.dispatch(this);
|
||||
if(t == null)
|
||||
t = thisType;
|
||||
t = StudentAnalysis.lowestCommonType(t, thisType, currentScope);
|
||||
}
|
||||
if(t == null)
|
||||
return node.setInferredType(Type.EMPTY_TYPE);
|
||||
return node.setInferredType(new ListValueType(t));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(MemberExpr node) {
|
||||
boolean prevIsMember = member;
|
||||
member = false;
|
||||
Type ty = node.object.dispatch(this);
|
||||
if(ty instanceof ClassValueType){
|
||||
ty = currentScope.get(((ClassValueType) ty).className());
|
||||
if(ty instanceof ClassVType){
|
||||
ClassVType classTy = (ClassVType) ty;
|
||||
Type type = classTy.scope == null? null:classTy.scope.get(node.member.name);
|
||||
if(type != null)
|
||||
return node.setInferredType(type);
|
||||
else
|
||||
err(node, "There is no %s named `%s` in class `%s`",
|
||||
prevIsMember?"method":"attribute", node.member.name, classTy);
|
||||
} else
|
||||
err(node, "Class `%s` undefined", ty.className());
|
||||
}
|
||||
else
|
||||
err(node, "`%s` isn't a class.", ty);
|
||||
return node.setInferredType(ValueType.OBJECT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(MethodCallExpr node) {
|
||||
boolean prevIsMember = member;
|
||||
member = true;
|
||||
Type ty = node.method.dispatch(this);
|
||||
member = prevIsMember;
|
||||
Type thisTy = Type.OBJECT_TYPE;
|
||||
if(ty instanceof FuncType){
|
||||
FuncType funcTy = (FuncType) ty;
|
||||
int len = funcTy.parameters.size() - 1, largs = node.args.size();
|
||||
if(largs != len)
|
||||
err(node, "Expected %d arguments; got %d", len, largs);
|
||||
len = len<=largs?len:largs;
|
||||
for(int i = 0; i < len; ++i){
|
||||
Expr thisArg = node.args.get(i);
|
||||
Type thisArgTy = thisArg.setInferredType(thisArg.dispatch(this)),
|
||||
thisParamTy = funcTy.parameters.get(i + 1);
|
||||
|
||||
if(!thisParamTy.equals(thisArgTy) && !StudentAnalysis.subClassOf(thisParamTy, thisArgTy, currentScope)
|
||||
&&!(thisParamTy.isListType()&&thisArgTy.equals(Type.EMPTY_TYPE)))
|
||||
err(node, "Expected type `%s`; got type `%s` in parameter %d",
|
||||
thisParamTy, thisArgTy, i + 1);
|
||||
}
|
||||
thisTy = funcTy.returnType;
|
||||
}
|
||||
// else
|
||||
// err(node.method.member, "`%s` isn't a MemberFunction.", node.method.member.name);
|
||||
|
||||
for(Expr args : node.args)
|
||||
args.dispatch(this);
|
||||
|
||||
return node.setInferredType(thisTy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(NoneLiteral node) {
|
||||
return node.setInferredType(Type.NONE_TYPE);
|
||||
}
|
||||
/*
|
||||
@Override
|
||||
public Type analyze(NonLocalDecl node) {
|
||||
SymbolTable<Type> parent = currentScope.getParent();
|
||||
if(parent == null || parent == sym ||
|
||||
!parent.getDeclaredSymbols().contains(node.variable.name)||
|
||||
!isVariableType(parent.get(node.variable.name))
|
||||
){
|
||||
err(node.variable, "Not a nonlocal variable: %s", node.variable.name);
|
||||
} else if(currentScope.getDeclaredSymbols().contains(node.variable.name)){
|
||||
errors.semError(
|
||||
node.variable, "Duplicate declaration of identifier in same scope: %s", node.variable.name);
|
||||
}
|
||||
else
|
||||
{
|
||||
Type nonlocalVar = parent.get(node.variable.name);
|
||||
currentScope.put(node.variable.name, nonlocalVar);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
@Override
|
||||
public Type analyze(ReturnStmt node) {
|
||||
if(node.value != null)
|
||||
node.value.dispatch(this);
|
||||
Type p = this.currReturnType;
|
||||
Type c = (node.value == null ? Type.NONE_TYPE: node.value.getInferredType());
|
||||
if(node.value == null && p.isSpecialType())
|
||||
err(node, "Expected type `%s`; got `None`", p);
|
||||
else if(p.isSpecialType()&&(c.equals(Type.NONE_TYPE)) || (!c.equals(Type.NONE_TYPE) && !StudentAnalysis.subClassOf(p, c, currentScope)))
|
||||
err(node, "Expected type `%s`; got type `%s`", p, c);
|
||||
returned = true;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(StringLiteral node) {
|
||||
return node.setInferredType(Type.STR_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(TypedVar node) {
|
||||
return ValueType.annotationToValueType(node.type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(VarDef node) {
|
||||
return declAnalyze(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(UnaryExpr node) {
|
||||
Type t = node.operand.dispatch(this);
|
||||
switch (node.operator) {
|
||||
case "-":
|
||||
case "+":
|
||||
if (INT_TYPE.equals(t)) {
|
||||
return node.setInferredType(INT_TYPE);
|
||||
} else {
|
||||
err(node, "Cannot apply operator `%s` on type `%s`", node.operator, t);
|
||||
return node.setInferredType(INT_TYPE);
|
||||
}
|
||||
case "not":
|
||||
if (!(Type.BOOL_TYPE.equals(t)))
|
||||
err(node, "Cannot apply operator `not` on type `%s`", t);
|
||||
return node.setInferredType(Type.BOOL_TYPE);
|
||||
default:
|
||||
return node.setInferredType(OBJECT_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Type analyze(WhileStmt node) {
|
||||
if(!node.condition.dispatch(this).equals(Type.BOOL_TYPE))
|
||||
err(node, "`%s` isn't a boolean expression.", node.condition);
|
||||
for(Stmt st : node.body)
|
||||
st.dispatch(this);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Type analyze(ExprStmt s) {
|
||||
s.expr.dispatch(this);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(IntegerLiteral i) {
|
||||
return i.setInferredType(Type.INT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(BinaryExpr e) {
|
||||
Type t1 = e.left.dispatch(this);
|
||||
Type 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);
|
||||
}
|
||||
case "+":
|
||||
if (INT_TYPE.equals(t1) && INT_TYPE.equals(t2)) {
|
||||
return e.setInferredType(INT_TYPE);
|
||||
} else if(Type.STR_TYPE.equals(t1) && Type.STR_TYPE.equals(t2)){
|
||||
return e.setInferredType(Type.STR_TYPE);
|
||||
} else if (t1.isListType() && t2.isListType()){
|
||||
return e.setInferredType(new ListValueType(StudentAnalysis.
|
||||
lowestCommonType(t1.elementType(), t2.elementType(), currentScope)));
|
||||
}else if (t1.isListType() && t2.equals(Type.EMPTY_TYPE))
|
||||
return e.setInferredType(t1);
|
||||
else {
|
||||
err(e, "Cannot apply operator `+` on types `%s` and `%s`", t1, t2);
|
||||
return e.setInferredType(INT_TYPE);
|
||||
}
|
||||
case "and":
|
||||
case "or":
|
||||
if (!(Type.BOOL_TYPE.equals(t1) && Type.BOOL_TYPE.equals(t2)))
|
||||
err(e, "Cannot apply operator `%s` on types `%s` and `%s`", e.operator, t1, t2);
|
||||
return e.setInferredType(Type.BOOL_TYPE);
|
||||
case ">":
|
||||
case "<":
|
||||
case ">=":
|
||||
case "<=":
|
||||
if (!(INT_TYPE.equals(t1) && INT_TYPE.equals(t2)))
|
||||
err(e, "Cannot apply operator `%s` on types `%s` and `%s`", e.operator, t1, t2);
|
||||
return e.setInferredType(Type.BOOL_TYPE);
|
||||
case "!=":
|
||||
case "==":
|
||||
if (!(INT_TYPE.equals(t1) && INT_TYPE.equals(t2)
|
||||
|| Type.BOOL_TYPE.equals(t1) && Type.BOOL_TYPE.equals(t2)
|
||||
|| Type.STR_TYPE.equals(t1) && Type.STR_TYPE.equals(t2)))
|
||||
err(e, "Cannot apply operator `%s` on types `%s` and `%s`", e.operator, t1, t2);
|
||||
return e.setInferredType(Type.BOOL_TYPE);
|
||||
case "is":
|
||||
if(t1.isSpecialType()||t2.isSpecialType())
|
||||
err(e, "Cannot apply operator `%s` on types `%s` and `%s`", e.operator, t1, t2);
|
||||
return e.setInferredType(Type.BOOL_TYPE);
|
||||
default:
|
||||
return e.setInferredType(OBJECT_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(Identifier id) {
|
||||
String varName = id.name;
|
||||
Type varType = currentScope.get(varName);
|
||||
if(varType!=null)
|
||||
{
|
||||
if(assign==true && !currentScope.getDeclaredSymbols().contains(varName))
|
||||
err(id, "Cannot assign to variable that is not explicitly declared in this scope: %s", varName);
|
||||
else if(assign==false && currentScope.get(varName)==null)
|
||||
err(id, "Variable not declared in scope: %s", varName);
|
||||
}
|
||||
if (varType != null && varType.isValueType()) {
|
||||
return id.setInferredType(varType);
|
||||
}
|
||||
|
||||
err(id, "Not a variable: %s", varName);
|
||||
return id.setInferredType(ValueType.OBJECT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type analyze(GlobalDecl node)
|
||||
{
|
||||
Type ty = sym.get(node.variable.name);
|
||||
if (sym.declares(node.variable.name)==false || !isVariableType(ty))
|
||||
{
|
||||
err(
|
||||
node.variable, "Not a global variable: %s", node.variable.name);
|
||||
return null;
|
||||
}
|
||||
else if(currentScope.getDeclaredSymbols().contains(node.variable.name)){
|
||||
err(
|
||||
node.variable, "Duplicate declaration of identifier in same scope: %s", node.variable.name);
|
||||
}
|
||||
else
|
||||
currentScope.put(node.variable.name, ty);
|
||||
return ty;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
package chocopy.pa1;
|
||||
import java_cup.runtime.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
%%
|
||||
|
||||
/*** Do not change the flags below unless you know what you are doing. ***/
|
||||
|
||||
%unicode
|
||||
%line
|
||||
%column
|
||||
%states AFTER, STR
|
||||
%class ChocoPyLexer
|
||||
%public
|
||||
|
||||
%cupsym ChocoPyTokens
|
||||
%cup
|
||||
%cupdebug
|
||||
|
||||
%eofclose false
|
||||
|
||||
/*** Do not change the flags above unless you know what you are doing. ***/
|
||||
|
||||
/* The following code section is copied verbatim to the
|
||||
* generated lexer class. */
|
||||
%{
|
||||
/* The code below includes some convenience methods to create tokens
|
||||
* of a given type and optionally a value that the CUP parser can
|
||||
* understand. Specifically, a lot of the logic below deals with
|
||||
* embedded information about where in the source code a given token
|
||||
* was recognized, so that the parser can report errors accurately.
|
||||
* (It need not be modified for this project.) */
|
||||
|
||||
/** Producer of token-related values for the parser. */
|
||||
final ComplexSymbolFactory symbolFactory = new ComplexSymbolFactory();
|
||||
private int currIndent = 0; //Current Indentation Level
|
||||
private String currString = "";
|
||||
private int str_l = 0, str_c = 0; //Start location of a string.
|
||||
/*A stack that keeps track of the spaces in each Indentation Level*/
|
||||
private ArrayList<Integer> stack = new ArrayList<Integer>(20);
|
||||
private boolean indentErrorUnchecked = true;
|
||||
/** Return a terminal symbol of syntactic category TYPE and no
|
||||
* semantic value at the current source location. */
|
||||
private Symbol symbol(int type) {
|
||||
return symbol(type, yytext());
|
||||
}
|
||||
|
||||
/** Return a terminal symbol of syntactic category TYPE and semantic
|
||||
* value VALUE at the current source location. */
|
||||
private Symbol symbol(int type, Object value) {
|
||||
return symbolFactory.newSymbol(ChocoPyTokens.terminalNames[type], type,
|
||||
new ComplexSymbolFactory.Location(yyline + 1, yycolumn + 1),
|
||||
new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()),
|
||||
value);
|
||||
}
|
||||
|
||||
private void push(int indent){
|
||||
stack.add(indent);
|
||||
}
|
||||
private int pop(){
|
||||
if(stack.isEmpty()) return 0;
|
||||
return stack.remove(stack.size() - 1);
|
||||
}
|
||||
private int top(){
|
||||
if(stack.isEmpty()) return 0;
|
||||
return stack.get(stack.size() - 1);
|
||||
}
|
||||
private boolean find(int indent){
|
||||
if(indent == 0) return true;
|
||||
Iterator<Integer> it = stack.iterator();
|
||||
while(it.hasNext()){
|
||||
if(it.next() == indent)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
%}
|
||||
|
||||
/* Macros (regexes used in rules below) */
|
||||
|
||||
WhiteSpace = [ \t]
|
||||
LineBreak = \r|\n|\r\n
|
||||
|
||||
|
||||
IntegerLiteral = 0|[1-9][0-9]* // Accroding to the manual, 00+ is illeagal
|
||||
StringLiteral = ([^\"\\]|(\\\")|(\\t)|(\\r)|(\\n)|(\\\\))+ // \n, \r, \t, \\, \" and Anything except \ and "
|
||||
Identifiers = (_|[a-z]|[A-Z])(_|[a-z]|[A-Z]|[0-9])*
|
||||
Comments = #[^\r\n]*
|
||||
%%
|
||||
//YYINITIAL state is where we're dealing with indentations.
|
||||
//We will set the state to YYINITIAL when starting a
|
||||
//new line unless this line is within a string, e.g.:
|
||||
/*
|
||||
"this is \
|
||||
a string across \
|
||||
multiple lines\
|
||||
"
|
||||
*/
|
||||
<YYINITIAL>{
|
||||
{WhiteSpace}
|
||||
{
|
||||
/*Add indentation */
|
||||
if(yytext() == "\t")
|
||||
currIndent += 8; //'\t' = 8 spaces
|
||||
else
|
||||
currIndent ++;
|
||||
}
|
||||
/*
|
||||
# This python code will test if '\t' is 8 spaces
|
||||
# It will run and print '1\n2'
|
||||
# Please tell me if your Python reports an error
|
||||
# Or you find documentations that says otherwise
|
||||
|
||||
if True:
|
||||
print(1) # \t
|
||||
print(2) # 8 spaces
|
||||
*/
|
||||
|
||||
{LineBreak}
|
||||
{
|
||||
/*
|
||||
If this is a blank line, start over on the next line.
|
||||
An empty line should just be ignored, therefore we don't
|
||||
pass a NEWLINE to Cup.
|
||||
*/
|
||||
currIndent = 0;
|
||||
}
|
||||
{Comments} { /* ignored */ } //Ignore blank lines
|
||||
|
||||
/*If it's not a blank line (Current character isn't a
|
||||
Whitespace/linebreak/comment), deal with indentation here and
|
||||
start accepting whatever is on this line in `AFTER' state*/
|
||||
[^ \t\r\n#]
|
||||
{
|
||||
//rewind the current character.
|
||||
yypushback(1);
|
||||
if(top() > currIndent)
|
||||
{
|
||||
/*
|
||||
If the indentation of the line is less than number of
|
||||
indents current level should have,
|
||||
keep dedenting until it reaches the level with the same
|
||||
number of indents.
|
||||
It's like a loop, because we're not changing the state
|
||||
and we rewinded the current character. So it will keep
|
||||
going until top()<= currIndent and it will switch to
|
||||
AFTER state.
|
||||
*/
|
||||
pop();
|
||||
if(top() < currIndent)
|
||||
{
|
||||
currIndent = top();
|
||||
return symbolFactory.newSymbol("<bad indentation>", ChocoPyTokens.UNRECOGNIZED,
|
||||
new ComplexSymbolFactory.Location(yyline + 1, yycolumn - 1),
|
||||
new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()),
|
||||
currIndent);
|
||||
}
|
||||
return symbolFactory.newSymbol(ChocoPyTokens.terminalNames[ChocoPyTokens.DEDENT], ChocoPyTokens.DEDENT,
|
||||
new ComplexSymbolFactory.Location(yyline + 1, yycolumn - 1),
|
||||
new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()),
|
||||
currIndent);
|
||||
}
|
||||
/*Otherwise, we will start dealing with the rest
|
||||
of the line after indentation in AFTER state. */
|
||||
yybegin(AFTER);
|
||||
if(top()< currIndent)
|
||||
{
|
||||
/*
|
||||
If current indentation is more than the number of indents
|
||||
current level should have, start a new level which will have
|
||||
`currIndent' indents.
|
||||
*/
|
||||
|
||||
push(currIndent);
|
||||
return symbolFactory.newSymbol(ChocoPyTokens.terminalNames[ChocoPyTokens.INDENT], ChocoPyTokens.INDENT,
|
||||
new ComplexSymbolFactory.Location(yyline + 1, yycolumn - 1),
|
||||
new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()),
|
||||
currIndent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
<AFTER> {
|
||||
|
||||
/* Delimiters. */
|
||||
{LineBreak} { yybegin(YYINITIAL); currIndent = 0;indentErrorUnchecked = true; return symbol(ChocoPyTokens.NEWLINE);}
|
||||
":" { return symbol(ChocoPyTokens.COLON); }
|
||||
"," { return symbol(ChocoPyTokens.COMMA); }
|
||||
|
||||
/* Literals. */
|
||||
{IntegerLiteral} { return symbol(ChocoPyTokens.NUMBER,
|
||||
Integer.parseInt(yytext())); }
|
||||
|
||||
"\"" { yybegin(STR); str_l = yyline + 1; str_c = yycolumn + 1; currString = ""; } //Start taking a string when see a "
|
||||
"False" { return symbol(ChocoPyTokens.BOOL, false); }
|
||||
"True" { return symbol(ChocoPyTokens.BOOL, true); }
|
||||
"None" { return symbol(ChocoPyTokens.NONE); }
|
||||
|
||||
/*Keywords*/
|
||||
"if" { return symbol(ChocoPyTokens.IF); }
|
||||
"else" { return symbol(ChocoPyTokens.ELSE); }
|
||||
"elif" { return symbol(ChocoPyTokens.ELIF); }
|
||||
"while" { return symbol(ChocoPyTokens.WHILE); }
|
||||
"class" { return symbol(ChocoPyTokens.CLASS); }
|
||||
"def" { return symbol(ChocoPyTokens.DEF); }
|
||||
"lambda" { return symbol(ChocoPyTokens.LAMBDA); }
|
||||
"as" { return symbol(ChocoPyTokens.AS); }
|
||||
"for" { return symbol(ChocoPyTokens.FOR); }
|
||||
"global" { return symbol(ChocoPyTokens.GLOBAL); }
|
||||
"in" { return symbol(ChocoPyTokens.IN); }
|
||||
"nonlocal" { return symbol(ChocoPyTokens.NONLOCAL); }
|
||||
"pass" { return symbol(ChocoPyTokens.PASS); }
|
||||
"return" { return symbol(ChocoPyTokens.RETURN); }
|
||||
"assert" { return symbol(ChocoPyTokens.ASSERT); }
|
||||
"await" { return symbol(ChocoPyTokens.AWAIT); }
|
||||
"break" { return symbol(ChocoPyTokens.BREAK); }
|
||||
"continue" { return symbol(ChocoPyTokens.CONTINUE); }
|
||||
"del" { return symbol(ChocoPyTokens.DEL); }
|
||||
"except" { return symbol(ChocoPyTokens.EXCEPT); }
|
||||
"finally" { return symbol(ChocoPyTokens.FINALLY); }
|
||||
"from" { return symbol(ChocoPyTokens.FROM); }
|
||||
"import" { return symbol(ChocoPyTokens.IMPORT); }
|
||||
"raise" { return symbol(ChocoPyTokens.RAISE); }
|
||||
"try" { return symbol(ChocoPyTokens.TRY); }
|
||||
"with" { return symbol(ChocoPyTokens.WITH); }
|
||||
"yield" { return symbol(ChocoPyTokens.YIELD); }
|
||||
|
||||
|
||||
/* Operators. */
|
||||
"+" { return symbol(ChocoPyTokens.PLUS); }
|
||||
"-" { return symbol(ChocoPyTokens.MINUS); }
|
||||
"*" { return symbol(ChocoPyTokens.MUL); }
|
||||
"//" { return symbol(ChocoPyTokens.DIV); }
|
||||
"/" { return symbol(ChocoPyTokens.DIV); } //Accroding to manual, chocopy don't have fp division, '/', '//' should be integr division
|
||||
"%" { return symbol(ChocoPyTokens.MOD); }
|
||||
">" { return symbol(ChocoPyTokens.GT); }
|
||||
"<" { return symbol(ChocoPyTokens.LT); }
|
||||
"==" { return symbol(ChocoPyTokens.EQUAL); }
|
||||
"!=" { return symbol(ChocoPyTokens.NEQ); }
|
||||
">=" { return symbol(ChocoPyTokens.GEQ); }
|
||||
"<=" { return symbol(ChocoPyTokens.LEQ); }
|
||||
"=" { return symbol(ChocoPyTokens.ASSIGN); }
|
||||
"and" { return symbol(ChocoPyTokens.AND); }
|
||||
"or" { return symbol(ChocoPyTokens.OR); }
|
||||
"not" { return symbol(ChocoPyTokens.NOT); }
|
||||
"." { return symbol(ChocoPyTokens.DOT); }
|
||||
"(" { return symbol(ChocoPyTokens.LPAR); }
|
||||
")" { return symbol(ChocoPyTokens.RPAR); }
|
||||
"[" { return symbol(ChocoPyTokens.LBR); }
|
||||
"]" { return symbol(ChocoPyTokens.RBR); }
|
||||
"->" { return symbol(ChocoPyTokens.ARROW); }
|
||||
"is" { return symbol(ChocoPyTokens.IS); }
|
||||
|
||||
|
||||
/*Identifiers*/
|
||||
{Identifiers} { return symbol(ChocoPyTokens.ID, yytext()); }
|
||||
|
||||
/* Whitespace. */
|
||||
{WhiteSpace} { /* ignore */ }
|
||||
|
||||
/* Comment. */
|
||||
{Comments} { /* ignore */ }
|
||||
}
|
||||
<STR>{
|
||||
{StringLiteral} { currString += yytext(); }
|
||||
|
||||
\\$ { /*'\' at the end of line, do nothing.*/ }
|
||||
|
||||
"\"" { yybegin(AFTER); return symbolFactory.newSymbol(ChocoPyTokens.terminalNames[ChocoPyTokens.STRING], ChocoPyTokens.STRING,
|
||||
new ComplexSymbolFactory.Location(str_l, str_c),
|
||||
new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()),
|
||||
currString); } // accepted a ", return to AFTER state
|
||||
}
|
||||
<<EOF>> { if(!stack.isEmpty()){ return symbol(ChocoPyTokens.DEDENT, pop());} return symbol(ChocoPyTokens.EOF); }
|
||||
|
||||
/* Error fallback. */
|
||||
[^] { return symbol(ChocoPyTokens.UNRECOGNIZED); }
|
||||
Reference in New Issue
Block a user