From fc712f94a7ed8554d8d44f4965be367354a7e670 Mon Sep 17 00:00:00 2001 From: L0P0P Date: Wed, 26 Jun 2024 10:06:12 +0200 Subject: Using child --- src/ast/Python3VisitorImpl.java | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'src/ast/Python3VisitorImpl.java') diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 604c8d2..f5b369d 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -21,17 +21,19 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * ``` */ public Node visitRoot(RootContext ctx) { - ArrayList stmts = new ArrayList(); - ArrayList compStmts = new ArrayList(); - - for (Simple_stmtsContext stm : ctx.simple_stmts()) { - stmts.add(visit(stm)); - } - for (Compound_stmtContext stm : ctx.compound_stmt()) { - compStmts.add(visit(stm)); + ArrayList childs = new ArrayList(); + + for (int i = 0; i < ctx.getChildCount(); i++){ + var child = ctx.getChild(i); + + if (child instanceof Simple_stmtsContext) { + childs.add(visit((Simple_stmtsContext) child)); + } else if (child instanceof Compound_stmtContext) { + childs.add(visit((Compound_stmtContext) child)); + } } - return new RootNode(stmts, compStmts); + return new RootNode(childs); } /** @@ -331,17 +333,19 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * ``` */ public Node visitBlock(BlockContext ctx) { - ArrayList stmts = new ArrayList(); - ArrayList compStmts = new ArrayList(); - - for (Simple_stmtsContext s : ctx.simple_stmts()) { - stmts.add(visit(s)); - } - for (Compound_stmtContext s : ctx.compound_stmt()) { - compStmts.add(visit(s)); + ArrayList childs = new ArrayList(); + + for (int i = 0; i < ctx.getChildCount(); i++){ + var child = ctx.getChild(i); + + if (child instanceof Simple_stmtsContext) { + childs.add(visit((Simple_stmtsContext) child)); + } else if (child instanceof Compound_stmtContext) { + childs.add(visit((Compound_stmtContext) child)); + } } - return new BlockNode(stmts, compStmts); + return new BlockNode(childs); } /** -- cgit v1.2.3-18-g5258 From 3c4229fc9e0ec6da9a7f60b57b9e93c49d1b6b6c Mon Sep 17 00:00:00 2001 From: L0P0P Date: Thu, 27 Jun 2024 12:02:35 +0200 Subject: Fixed a lot of problems from all the progs we need to parse --- src/ast/Python3VisitorImpl.java | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'src/ast/Python3VisitorImpl.java') diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index f5b369d..d4b4fbf 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -264,10 +264,28 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { x = new AugassignNode(ctx.SUB_ASSIGN()); } else if (ctx.MULT_ASSIGN() != null) { x = new AugassignNode(ctx.MULT_ASSIGN()); + } else if (ctx.AT_ASSIGN() != null) { + x = new AugassignNode(ctx.AT_ASSIGN()); } else if (ctx.DIV_ASSIGN() != null) { x = new AugassignNode(ctx.DIV_ASSIGN()); + } else if (ctx.MOD_ASSIGN() != null) { + x = new AugassignNode(ctx.MOD_ASSIGN()); + } else if (ctx.AND_ASSIGN() != null) { + x = new AugassignNode(ctx.AND_ASSIGN()); + } else if (ctx.OR_ASSIGN() != null) { + x = new AugassignNode(ctx.OR_ASSIGN()); + } else if (ctx.XOR_ASSIGN() != null) { + x = new AugassignNode(ctx.XOR_ASSIGN()); + } else if (ctx.LEFT_SHIFT_ASSIGN() != null) { + x = new AugassignNode(ctx.LEFT_SHIFT_ASSIGN()); + } else if (ctx.RIGHT_SHIFT_ASSIGN() != null) { + x = new AugassignNode(ctx.RIGHT_SHIFT_ASSIGN()); + } else if (ctx.POWER_ASSIGN() != null) { + x = new AugassignNode(ctx.POWER_ASSIGN()); + } else if (ctx.IDIV_ASSIGN() != null) { + x = new AugassignNode(ctx.IDIV_ASSIGN()); } - + return x; } @@ -495,7 +513,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { methodCall = ctx.NAME(); } - return new TrailerNode(arglist, exprs, methodCall); + return new TrailerNode(arglist, exprs, methodCall, ctx.OPEN_PAREN() != null); } /** @@ -507,7 +525,13 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * ``` */ public Node visitExprlist(ExprlistContext ctx) { - Node exp = visit(ctx.expr(0)); + Node exp; + + if (ctx.expr(0).expr(0) != null){ + exp = visit(ctx.expr(0).expr(0)); + } else { + exp = visit(ctx.expr(0)); + } return exp; } -- cgit v1.2.3-18-g5258 From eb7fb04d2dfadd004fa86f28da42681e0038df06 Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 18:20:34 +0200 Subject: left, right = 1, 2 assignment covered sfaccimm ho fatto refactoring dell exprlist --- src/ast/Python3VisitorImpl.java | 192 ++++++++++++++++++---------------------- 1 file changed, 88 insertions(+), 104 deletions(-) (limited to 'src/ast/Python3VisitorImpl.java') diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index d4b4fbf..5b812ea 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -12,20 +12,19 @@ import org.antlr.v4.runtime.tree.TerminalNode; * Overrides each `visitNODE` method from the base class. */ public class Python3VisitorImpl extends Python3ParserBaseVisitor { + /** * Since a root can be a simple_stmts or a compound_stmt, this method * returns a new `RootNode` with a list of them. * - * ``` - * root : NEWLINE* (simple_stmts | compound_stmt)* EOF; - * ``` + * ``` root : NEWLINE* (simple_stmts | compound_stmt)* EOF; ``` */ public Node visitRoot(RootContext ctx) { ArrayList childs = new ArrayList(); - - for (int i = 0; i < ctx.getChildCount(); i++){ + + for (int i = 0; i < ctx.getChildCount(); i++) { var child = ctx.getChild(i); - + if (child instanceof Simple_stmtsContext) { childs.add(visit((Simple_stmtsContext) child)); } else if (child instanceof Compound_stmtContext) { @@ -39,9 +38,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { /** * Returns a `SimpleStmtsNode`, made by an array of SimpleStmtNode * - * ``` - * simple_stmts : simple_stmt (';' simple_stmt)* ';'? NEWLINE ; - * ``` + * ``` simple_stmts : simple_stmt (';' simple_stmt)* ';'? NEWLINE ; ``` */ public Node visitSimple_stmts(Simple_stmtsContext ctx) { ArrayList stmts = new ArrayList(); @@ -57,9 +54,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * Returns a `CompoundNode`. It can be built by a different kind of * statements, so only one of theme won't be null. * - * ``` - * compound_stmt : if_stmt | while_stmt | for_stmt | funcdef ; - * ``` + * ``` compound_stmt : if_stmt | while_stmt | for_stmt | funcdef ; ``` */ public Node visitCompound_stmt(Compound_stmtContext ctx) { Node ifStmt = null; @@ -90,9 +85,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * Returns a `SimpleStmtNode`. It can be built by a different kind of * statements, so only one of theme won't be null. * - * ``` - * simple_stmt : assignment | expr | return_stmt | import_stm ; - * ``` + * ``` simple_stmt : assignment | expr | return_stmt | import_stm ; ``` */ public Node visitSimple_stmt(Simple_stmtContext ctx) { Node assignment = null; @@ -122,10 +115,8 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { /** * Returns an `AssignmentNode`. It's made by left side, an assignment and a * right side. - * - * ``` - * assignment : exprlist augassign exprlist ; - * ``` + * + * ``` assignment : exprlist augassign exprlist ; ``` */ public Node visitAssignment(AssignmentContext ctx) { Node lhr = visit(ctx.exprlist(0)); @@ -138,9 +129,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { /** * Returns a `ReturnStmtNode`. The returned exprlist can be null. * - * ``` - * return_stmt : 'return' exprlist? ; - * ``` + * ``` return_stmt : 'return' exprlist? ; ``` */ public Node visitReturn_stmt(Return_stmtContext ctx) { Node exprList = null; @@ -155,10 +144,8 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * Returns a `ImportNode`. An import can be made in different ways so we * check the way in a module is imported (by from, by alias or by star). * - * ``` - * import_stm : 'import' dotted_name ('as' NAME)? - * | 'from' dotted_name 'import' (NAME (',' NAME)* | '*') ; - * ``` + * ``` import_stm : 'import' dotted_name ('as' NAME)? | 'from' dotted_name + * 'import' (NAME (',' NAME)* | '*') ; ``` */ public Node visitImport_stm(Import_stmContext ctx) { boolean isFrom = ctx.FROM() != null; @@ -179,9 +166,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { /** * Returns a `DottedNameNode` used in `import_stm`. * - * ``` - * dotted_name : NAME ('.' NAME)* ; - * ``` + * ``` dotted_name : NAME ('.' NAME)* ; ``` */ public Node visitDotted_name(Dotted_nameContext ctx) { ArrayList names = new ArrayList(); @@ -196,9 +181,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { /** * Returns a `FuncdefNode`. A paramlist can be null. * - * ``` - * funcdef : 'def' NAME '(' paramlist? ')' ':' block ; - * ``` + * ``` funcdef : 'def' NAME '(' paramlist? ')' ':' block ; ``` */ public Node visitFuncdef(FuncdefContext ctx) { Node paramlist = null; @@ -216,9 +199,8 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * Returns a `ParamlistNode`. We ignore the paramdef with default values * (eg: is_used=False) because there is no test which uses this feature. * - * ``` - * paramlist : paramdef ('=' expr)? (',' paramdef ('=' expr)?)* ; - * + * ``` paramlist : paramdef ('=' expr)? (',' paramdef ('=' expr)?)* ; + * * ``` */ public Node visitParamlist(ParamlistContext ctx) { @@ -235,9 +217,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * Returns a `ParamdefNode`. We ignore the paramdef with type annotation * (eg: x : int) because there is no test which uses this feature. * - * ``` - * paramdef : NAME (':' expr)? ; - * ``` + * ``` paramdef : NAME (':' expr)? ; ``` */ public Node visitParamdef(ParamdefContext ctx) { return new ParamdefNode(ctx.NAME().toString()); @@ -247,11 +227,8 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * Returns an `AugassignNode`. We don't provide all kinds of assignment * below. * - * ``` - * augassign : '=' | '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | - * '^=' | '<<=' | '>>=' | '**=' | '//=' - * ; - * ``` + * ``` augassign : '=' | '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | + * '|=' | '^=' | '<<=' | '>>=' | '**=' | '//=' ; ``` */ public Node visitAugassign(AugassignContext ctx) { Node x = null; @@ -285,17 +262,15 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { } else if (ctx.IDIV_ASSIGN() != null) { x = new AugassignNode(ctx.IDIV_ASSIGN()); } - + return x; } /** - * Returns a `IfNode`. - * FIXME: add support for elif statement. + * Returns a `IfNode`. FIXME: add support for elif statement. * - * ``` - * if_stmt : 'if' expr ':' block ('elif' expr ':' block)* ('else' ':' block)? ; - * ``` + * ``` if_stmt : 'if' expr ':' block ('elif' expr ':' block)* ('else' ':' + * block)? ; ``` */ public Node visitIf_stmt(If_stmtContext ctx) { var blocks = ctx.block(); @@ -312,9 +287,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { /** * Returns a `WhileStmtNode`. We do not provide 'else' branch. * - * ``` - * while_stmt : 'while' expr ':' block ('else' ':' block)? ; - * ``` + * ``` while_stmt : 'while' expr ':' block ('else' ':' block)? ; ``` */ public Node visitWhile_stmt(While_stmtContext ctx) { Node expr = visit(ctx.expr()); @@ -328,9 +301,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { /** * Returns a `ForSmtNode`. We do not provide 'else' branch. * - * ``` - * for_stmt : 'for' exprlist ':' block ('else' ':' block)? ; - * ``` + * ``` for_stmt : 'for' exprlist ':' block ('else' ':' block)? ; ``` */ public Node visitFor_stmt(For_stmtContext ctx) { Node exprList = visit(ctx.exprlist()); @@ -345,17 +316,15 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * Returns a `BlockNode`. A block can be be a simple_stmts or a list of * simple_stms and/or compound_stmt, so we just use a list for each kind. * - * ``` - * block : simple_stmts - * | NEWLINE INDENT (simple_stmts | compound_stmt)+ DEDENT ; - * ``` + * ``` block : simple_stmts | NEWLINE INDENT (simple_stmts | compound_stmt)+ + * DEDENT ; ``` */ public Node visitBlock(BlockContext ctx) { ArrayList childs = new ArrayList(); - for (int i = 0; i < ctx.getChildCount(); i++){ + for (int i = 0; i < ctx.getChildCount(); i++) { var child = ctx.getChild(i); - + if (child instanceof Simple_stmtsContext) { childs.add(visit((Simple_stmtsContext) child)); } else if (child instanceof Compound_stmtContext) { @@ -369,10 +338,8 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { /** * Returns a `CompNode`. It should never be null. * - * ``` - * comp_op : '<' | '>' | '==' | '>=' | '<=' | '<>' | '!=' | 'in' | 'not' 'in' | - * 'is' | 'is' 'not' ; - * ``` + * ``` comp_op : '<' | '>' | '==' | '>=' | '<=' | '<>' | '!=' | 'in' | 'not' + * 'in' | 'is' | 'is' 'not' ; ``` */ public Node visitComp_op(Comp_opContext ctx) { Node comp = null; @@ -404,13 +371,11 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * Returns an `ExprNode`. An expession can be a different kind of * sub-expression. We do not provide all kinds of expr(s). * - * ``` - * expr : atom trailer* | expr '**' expr | ('+' | '-' | '~')+ expr | expr ('*' | - * '@' | '/' | '%' | '//') expr | expr ('+' | '-') expr | expr ('<<' | '>>') - * expr | expr '&' expr | expr '^' expr | expr '|' expr | 'not' expr | expr - * comp_op expr | expr 'and' expr | expr 'or' expr | expr 'if' expr 'else' expr - * ; - * ``` + * ``` expr : atom trailer* | expr '**' expr | ('+' | '-' | '~')+ expr | + * expr ('*' | '@' | '/' | '%' | '//') expr | expr ('+' | '-') expr | expr + * ('<<' | '>>') expr | expr '&' expr | expr '^' expr | expr '|' expr | + * 'not' expr | expr comp_op expr | expr 'and' expr | expr 'or' expr | expr + * 'if' expr 'else' expr ; ``` */ public Node visitExpr(ExprContext ctx) { Node atom = null; @@ -459,42 +424,52 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { } /** - * Returns an `AtomNode`. - * FIXME: add support for testlist_comp + * Returns an `AtomNode`. FIXME: add support for testlist_comp * - * ``` - * atom : '(' testlist_comp? ')' | '[' testlist_comp? ']' | '{' testlist_comp? - * '}' | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False' ; - * ``` + * ``` atom : '(' testlist_comp? ')' | '[' testlist_comp? ']' | '{' + * testlist_comp? '}' | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | + * 'False' ; ``` */ public Node visitAtom(AtomContext ctx) { + Testlist_compContext tlc = ctx.testlist_comp(); if (ctx.NUMBER() != null) { - return new AtomNode(ctx.NUMBER().toString()); + return new AtomNode(ctx.NUMBER().toString(), null); } else if (ctx.TRUE() != null) { - return new AtomNode(ctx.TRUE().toString()); + return new AtomNode(ctx.TRUE().toString(), null); } else if (ctx.FALSE() != null) { - return new AtomNode(ctx.FALSE().toString()); + return new AtomNode(ctx.FALSE().toString(), null); } else if (ctx.NAME() != null) { - return new AtomNode(ctx.NAME().toString()); - } else if (ctx.STRING() != null) { - + return new AtomNode(ctx.NAME().toString(), null); + } else if (!ctx.STRING().isEmpty()) { var varName = ""; for (var x : ctx.STRING()) { varName += x; } + return new AtomNode(varName, null); + } else if (ctx.OPEN_BRACE() != null && ctx.CLOSE_BRACE() != null) { + return manageTlc(tlc); + } else if (ctx.OPEN_BRACK() != null && ctx.CLOSE_BRACK() != null) { + return manageTlc(tlc); + } else if (ctx.OPEN_PAREN() != null && ctx.CLOSE_PAREN() != null) { + return manageTlc(tlc); + } + return new AtomNode(ctx.NONE().toString(), null); + } - return new AtomNode(varName); + public AtomNode manageTlc(Testlist_compContext tlc) { + if (tlc != null) { + Node testlist_comp = visit(tlc); + return new AtomNode(null, testlist_comp); + } else { + return new AtomNode(null, null); } - return new AtomNode(ctx.NONE().toString()); } /** * Returns an `TrailerNode`. * - * ``` - * trailer : '(' arglist? ')' | '[' expr (',' expr)* ','? ']' | '.' NAME | '[' - * expr? ':' expr? (':' expr? )? ']' ; - * ``` + * ``` trailer : '(' arglist? ')' | '[' expr (',' expr)* ','? ']' | '.' NAME + * | '[' expr? ':' expr? (':' expr? )? ']' ; ``` */ public Node visitTrailer(TrailerContext ctx) { Node arglist = null; @@ -517,31 +492,24 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { } /** - * Returns a `Node`. - * FIXME: what to do in case of list?? + * Returns a `Node`. FIXME: what to do in case of list?? * - * ``` - * exprlist : expr (',' expr )* ','? ; - * ``` + * ``` exprlist : expr (',' expr )* ','? ; ``` */ public Node visitExprlist(ExprlistContext ctx) { - Node exp; + ArrayList exprlist = new ArrayList(); - if (ctx.expr(0).expr(0) != null){ - exp = visit(ctx.expr(0).expr(0)); - } else { - exp = visit(ctx.expr(0)); + for (ExprContext c : ctx.expr()) { + exprlist.add(visit(c)); } - return exp; + return new ExprListNode(exprlist); } /** * Returns a `ArglistNode`. * - * ``` - * arglist : argument (',' argument)* ','? ; - * ``` + * ``` arglist : argument (',' argument)* ','? ; ``` */ public Node visitArglist(ArglistContext ctx) { ArrayList arguments = new ArrayList(); @@ -552,4 +520,20 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { return new ArglistNode(arguments); } + + /** + * Returns a `TestlistCompNode`. + * + * ``` testlist_comp : expr (comp_for | (',' expr)* ','?) ; ``` + */ + public Node visitTestlist_comp(Testlist_compContext ctx) { + // TODO: implement comp_for + ArrayList exprlist = new ArrayList(); + + for (ExprContext c : ctx.expr()) { + exprlist.add(visit(c)); + } + + return new TestlistCompNode(exprlist); + } } -- cgit v1.2.3-18-g5258 From fd85e9980c0305c6dfb91aaeb199430a89163c3e Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 20:49:56 +0200 Subject: fixed comp_for and added reseved word type --- src/ast/Python3VisitorImpl.java | 44 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'src/ast/Python3VisitorImpl.java') diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 5b812ea..c5f1bfa 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -434,6 +434,8 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { Testlist_compContext tlc = ctx.testlist_comp(); if (ctx.NUMBER() != null) { return new AtomNode(ctx.NUMBER().toString(), null); + } else if (ctx.NONE() != null) { + return new AtomNode(ctx.NONE().toString(), null); } else if (ctx.TRUE() != null) { return new AtomNode(ctx.TRUE().toString(), null); } else if (ctx.FALSE() != null) { @@ -453,7 +455,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { } else if (ctx.OPEN_PAREN() != null && ctx.CLOSE_PAREN() != null) { return manageTlc(tlc); } - return new AtomNode(ctx.NONE().toString(), null); + return new AtomNode(null, null); } public AtomNode manageTlc(Testlist_compContext tlc) { @@ -533,7 +535,45 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { for (ExprContext c : ctx.expr()) { exprlist.add(visit(c)); } + Comp_forContext cfc = ctx.comp_for(); + if (cfc != null) { + Node comp = visit(ctx.comp_for()); + return new TestlistCompNode(exprlist, comp); + } + return new TestlistCompNode(exprlist, null); + } + + /** + * Returns a `CompForNode`. + * + * ``` comp_for : 'for' exprlist 'in' expr comp_iter? ;``` + */ + public Node visitComp_for(Comp_forContext ctx) { + Node exprlist = visit(ctx.exprlist()); + Node expr = visit(ctx.expr()); + Comp_iterContext cic = ctx.comp_iter(); - return new TestlistCompNode(exprlist); + if (cic != null) { + Node comp = visit(ctx.comp_iter()); + return new CompForNode(exprlist, expr, comp); + } + return new CompForNode(exprlist, expr, null); + } + + /** + * Returns a `CompIterNode`. + * + * ``` comp_iter : comp_for | comp_if ; ;``` + */ + public Node visitComp_iter(Comp_iterContext ctx) { + // TODO: Implement comp_if + // Node iter = visit(ctx.comp_if()); + Comp_forContext cfc = ctx.comp_for(); + if (cfc != null) { + Node forNode = visit(ctx.comp_for()); + return new CompIterNode(forNode); + + } + return new CompIterNode(null); } } -- cgit v1.2.3-18-g5258 From 089787db950056a35eb1a5930b5b919c45523863 Mon Sep 17 00:00:00 2001 From: Geno <48206120+gabrielegenovese@users.noreply.github.com> Date: Fri, 28 Jun 2024 11:20:17 +0200 Subject: Update src/ast/Python3VisitorImpl.java suggested by santo Co-authored-by: Santo Cariotti --- src/ast/Python3VisitorImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/ast/Python3VisitorImpl.java') diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index c5f1bfa..bef75cf 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -536,11 +536,11 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { exprlist.add(visit(c)); } Comp_forContext cfc = ctx.comp_for(); + Node comp = null; if (cfc != null) { - Node comp = visit(ctx.comp_for()); - return new TestlistCompNode(exprlist, comp); + comp = visit(ctx.comp_for()); } - return new TestlistCompNode(exprlist, null); + return new TestlistCompNode(exprlist, comp); } /** -- cgit v1.2.3-18-g5258 From 1d5c4862e136419ab1ed3fcf8d8edaa0ee5fda10 Mon Sep 17 00:00:00 2001 From: Geno <48206120+gabrielegenovese@users.noreply.github.com> Date: Fri, 28 Jun 2024 11:21:12 +0200 Subject: Update src/ast/Python3VisitorImpl.java suggested by santo Co-authored-by: Santo Cariotti --- src/ast/Python3VisitorImpl.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/ast/Python3VisitorImpl.java') diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index bef75cf..42e2537 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -569,11 +569,10 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { // TODO: Implement comp_if // Node iter = visit(ctx.comp_if()); Comp_forContext cfc = ctx.comp_for(); + Node forNode = null; if (cfc != null) { - Node forNode = visit(ctx.comp_for()); - return new CompIterNode(forNode); - + forNode = visit(ctx.comp_for()); } - return new CompIterNode(null); + return new CompIterNode(forNode); } } -- cgit v1.2.3-18-g5258 From 37665fb6d0bc1eb29396ae949354cf7d6f9d54ca Mon Sep 17 00:00:00 2001 From: geno Date: Fri, 28 Jun 2024 12:23:28 +0200 Subject: resolving all the comments santo made --- src/ast/Python3VisitorImpl.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/ast/Python3VisitorImpl.java') diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 42e2537..1cf15b7 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -424,7 +424,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { } /** - * Returns an `AtomNode`. FIXME: add support for testlist_comp + * Returns an `AtomNode`. * * ``` atom : '(' testlist_comp? ')' | '[' testlist_comp? ']' | '{' * testlist_comp? '}' | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | @@ -449,22 +449,26 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { } return new AtomNode(varName, null); } else if (ctx.OPEN_BRACE() != null && ctx.CLOSE_BRACE() != null) { - return manageTlc(tlc); + return manageCompListContext(tlc); } else if (ctx.OPEN_BRACK() != null && ctx.CLOSE_BRACK() != null) { - return manageTlc(tlc); + return manageCompListContext(tlc); } else if (ctx.OPEN_PAREN() != null && ctx.CLOSE_PAREN() != null) { - return manageTlc(tlc); + return manageCompListContext(tlc); } return new AtomNode(null, null); } - public AtomNode manageTlc(Testlist_compContext tlc) { + /** + * Supporting function for `visitAtom`. Returns an `AtomNode` with + * `testlist_comp` set if the context is not null. Otherwise, returns an + * `AtomNode` with nulls. + */ + public AtomNode manageCompListContext(Testlist_compContext tlc) { if (tlc != null) { Node testlist_comp = visit(tlc); return new AtomNode(null, testlist_comp); - } else { - return new AtomNode(null, null); } + return new AtomNode(null, null); } /** @@ -529,7 +533,6 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * ``` testlist_comp : expr (comp_for | (',' expr)* ','?) ; ``` */ public Node visitTestlist_comp(Testlist_compContext ctx) { - // TODO: implement comp_for ArrayList exprlist = new ArrayList(); for (ExprContext c : ctx.expr()) { -- cgit v1.2.3-18-g5258