Changeset 3829


Ignore:
Timestamp:
Apr 18, 2014, 2:22:12 AM (4 years ago)
Author:
ksherdy
Message:

Fixed bug. Added support for foreach to while translation.

Location:
proto/s2k/trunk/framework/src/toolchain/s2k/ast
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • proto/s2k/trunk/framework/src/toolchain/s2k/ast/Accessors.java

    r3811 r3829  
    1616
    1717        ////////////////////////////////////////////////////////////////////////////
     18        // IdisaFuncCallNode
     19        ////////////////////////////////////////////////////////////////////////////
     20               
     21        public static IdisaFuncCallArgListNode argListNode(IdisaFuncCallNode node) {
     22                ASTNode idisaFuncCallArgListNode = node.child(2);
     23                assert idisaFuncCallArgListNode instanceof IdisaFuncCallArgListNode;
     24                return (IdisaFuncCallArgListNode) idisaFuncCallArgListNode;     
     25        }
     26       
     27    public static int argCount(IdisaFuncCallNode node) {
     28        ASTNode argList = Accessors.argListNode(node);
     29        return argList.nChildren();
     30    }
     31               
     32       
     33        ////////////////////////////////////////////////////////////////////////////
    1834        // Program
    1935        ////////////////////////////////////////////////////////////////////////////
    2036        public static List<StructDefNode> structDeclNodes(ProgramNode node) {
    2137               
     38                assert node instanceof ProgramNode;
    2239                List<StructDefNode> structTypeDecls = new ArrayList<StructDefNode>();
    2340               
     
    4764
    4865            assert node instanceof ProgramNode;
    49            
    5066            List<String> names = new ArrayList<String>();
    5167           
     
    199215        }       
    200216        public static BlockStmtNode elseClause(IfStmtNode node) {
    201                 return (BlockStmtNode) node.child(2);
    202         }
    203 
     217                ASTNode blockStmtNode = node.child(2);
     218                assert blockStmtNode instanceof BlockStmtNode;
     219                return (BlockStmtNode) blockStmtNode;
     220        }
     221
     222        ////////////////////////////////////////////////////////////////////////////
     223        // ForeachStmtNode
     224        ////////////////////////////////////////////////////////////////////////////
     225       
     226        public static List<IdentifierNode> indexStreamDeclIdentifierList(ForeachStmtNode node) {
     227                return Accessors.identifierList(Accessors.indexStreamDeclList(node));
     228        }
     229       
     230        public static List<IdentifierNode> targetStreamDeclIdentifierList(ForeachStmtNode node) {
     231                return Accessors.identifierList(Accessors.targetStreamDeclList(node));
     232        }
     233       
     234        public static List<IdentifierNode> identifierList(StreamDeclListNode node) {
     235               
     236                assert node instanceof StreamDeclListNode;
     237                List<IdentifierNode> structTypeDecls = new ArrayList<IdentifierNode>();
     238               
     239                for(ASTNode child: node.getChildren()) {
     240                        assert child instanceof StreamDeclNode;
     241                        structTypeDecls.add((IdentifierNode)Accessors.identifier((StreamDeclNode)child));
     242                }
     243               
     244                return structTypeDecls;
     245        }       
     246       
     247        public static List<ASTNode> indexStreamDeclListAsList(ForeachStmtNode node) {
     248                return Accessors.indexStreamDeclList(node).getChildren();
     249        }
     250       
     251        public static StreamDeclListNode indexStreamDeclList(ForeachStmtNode node) {
     252                ASTNode streamDeclListNode = node.child(0);
     253                assert streamDeclListNode instanceof StreamDeclListNode;
     254                return (StreamDeclListNode) streamDeclListNode;
     255        }
     256       
     257        public static StreamDeclListNode targetStreamDeclList(ForeachStmtNode node) {
     258                ASTNode streamDeclListNode = node.child(1);
     259                assert streamDeclListNode instanceof StreamDeclListNode;
     260                return (StreamDeclListNode) streamDeclListNode;
     261        }       
     262
     263        public static BlockStmtNode body(ForeachStmtNode node) {
     264                ASTNode blockStmtNode = node.child(2);
     265                assert blockStmtNode instanceof BlockStmtNode;
     266                return (BlockStmtNode) blockStmtNode;
     267        }
     268       
     269        public static StreamTypeNode streamDeclType(StreamDeclNode node) {
     270                ASTNode streamType = node.child(0);
     271                assert streamType instanceof StreamTypeNode;
     272                return (StreamTypeNode) streamType;
     273        }       
     274               
     275        public static IdentifierNode identifier(StreamDeclNode node) {
     276                ASTNode identifier = node.child(1);
     277                assert identifier instanceof IdentifierNode;
     278                return (IdentifierNode) identifier;
     279        }
     280       
     281        public static ASTNode nameNode(StreamDeclNode node) {
     282            return node.child(1);
     283        }
     284       
     285       
    204286        ////////////////////////////////////////////////////////////////////////////
    205287        // WhileStmtNode
     
    210292
    211293        public static BlockStmtNode body(WhileStmtNode node) {
    212                 return (BlockStmtNode) node.child(1);
     294                ASTNode blockStmtNode = node.child(1);
     295                assert blockStmtNode instanceof BlockStmtNode;
     296                return (BlockStmtNode) blockStmtNode;
    213297        }
    214298       
     
    450534            } else if(node instanceof StructTypeNode) {
    451535                return name(nameNode((StructTypeNode)node));
    452             }  else if(node instanceof VarDeclNode) {
     536            } else if(node instanceof VarDeclNode) {
    453537                return name(nameNode((VarDeclNode)node));
    454             }  else if(node instanceof FilterParameterNode) {
     538            } else if(node instanceof FilterParameterNode) {
    455539                return name(nameNode((FilterParameterNode)node));
     540            } else if(node instanceof StreamDeclNode) {
     541                return name(nameNode((StreamDeclNode)node));
    456542            }
    457543            assert false : "Accessors.name(ASTNode node) method not implemented on node class : " + node.getClass();
  • proto/s2k/trunk/framework/src/toolchain/s2k/ast/Generators.java

    r3822 r3829  
    1010package toolchain.s2k.ast;
    1111
     12
    1213import s2k.ast.*;
    13 
    1414import s2k.inputHandler.Locator;
    1515import s2k.lexicalAnalyzer.*;
     
    1818
    1919public class Generators {
    20 
    21         // TS: should change grammar and conventions so that every FuncDefNode has a ParameterListNode child
    22         //     but the PLN child has no children if there are no args.  This would eliminate the conditional
    23         //     in this method, and probably make things easier in other methods, too.
     20       
    2421        public static void appendParameter(FilterDefNode funcDefNode, FilterParameterNode parameterNode) {
    2522                FilterParameterListNode parameterListNode = null;
     
    3633        }
    3734       
    38         public static VarDeclNode makeVarDeclNode(Locator locator, ASTNode type, ASTNode lhs, ASTNode rhs) {           
    39             VarDeclNode localVarDecl = makeVarDeclNode(locator,type,lhs);
     35        public static VarDeclNode makeVarDeclNode(Locator locator, ASTNode type, ASTNode name, ASTNode rhs) {           
     36            VarDeclNode localVarDecl = makeVarDeclNode(locator,type, name);
    4037                localVarDecl.appendChild(rhs);
    4138                return localVarDecl;
     
    190187       
    191188        public static BlockStmtNode makeBlockStmtNode(Locator locator, ASTNode ...children) {
    192                 LextantToken token = Generators.makeLextantToken(locator, Lextant.LCURLY);
     189                LextantToken token = Generators.makeLextantToken(locator, Lextant.LROUND);
    193190               
    194191                BlockStmtNode result = new BlockStmtNode(token);
     
    199196        }
    200197
    201         public static IfStmtNode makeIfStmtNode(Locator locator, ASTNode expression, BlockStmtNode ifBlockStmt, BlockStmtNode elseBlockStmt) {
    202                 IfStmtNode ifStmtNode = makeIfStmtNode(locator, expression, ifBlockStmt);
     198        public static IfStmtNode makeIfStmtNode(Locator locator, ASTNode condition, BlockStmtNode ifBlockStmt, BlockStmtNode elseBlockStmt) {
     199                IfStmtNode ifStmtNode = makeIfStmtNode(locator, condition, ifBlockStmt);
    203200                ifStmtNode.appendChild(elseBlockStmt);
    204201                return ifStmtNode;
    205202        }       
    206203       
    207         public static IfStmtNode makeIfStmtNode(Locator locator, ASTNode expression, BlockStmtNode blockStmt) {
    208                 LextantToken token = Generators.makeLextantToken(locator, Lextant.LCURLY);
     204        public static IfStmtNode makeIfStmtNode(Locator locator, ASTNode condition, BlockStmtNode blockStmt) {
     205                LextantToken token = Generators.makeLextantToken(locator, Lextant.LROUND);
    209206                IfStmtNode ifStmtNode = new IfStmtNode(token);
    210                 ifStmtNode.appendChild(expression);
     207                ifStmtNode.appendChild(condition);
    211208                ifStmtNode.appendChild((ASTNode)blockStmt);
    212209                return ifStmtNode;
    213210        }
    214211
     212        public static WhileStmtNode makeWhileStmtNode(Locator locator, ASTNode condition, BlockStmtNode body) {
     213                LextantToken token = Generators.makeLextantToken(locator, Lextant.LROUND);
     214                WhileStmtNode whileStmtNode = new WhileStmtNode(token);
     215                whileStmtNode.appendChild(condition);
     216                whileStmtNode.appendChild((ASTNode)body);
     217                return whileStmtNode;
     218        }
     219       
    215220    ///////////////////////////////////////////////////////////////////
    216221    // Mask calls
     
    239244        }
    240245
     246// KH: Unused (for now)
     247//     
     248//  public static IdisaFuncCallNode makeIdisaFuncCallNode(Locator locator, String packageName, String baseName, FieldWidthNode fieldWidth, ASTNode... args ) {
     249//  CompoundIdentifierNode identifier = Generators.makeCompoundIdentifierNode(locator, packageName, baseName);   
     250//  IdisaFuncCallNode idisaFuncCall = Generators.makeIdisaFuncCallNode(locator, identifier, fieldWidth, args);
     251//  return idisaFuncCall;
     252//}   
     253//
     254//public static IdisaFuncCallNode makeIdisaFuncCallNode(Locator locator, CompoundIdentifierNode identifier, FieldWidthNode fieldWidth, ASTNode... args) {
     255//  IdisaFuncCallNode idisaFuncCall = new IdisaFuncCallNode(makeLextantToken(locator, Lextant.LANGLE));
     256//  idisaFuncCall.appendChild(identifier);
     257//  idisaFuncCall.appendChild(fieldWidth);
     258// 
     259// 
     260//  idisaFuncCall.appendChild(new IdisaFuncCallArgListNode( Generators.makeLextantToken(locator, Lextant.LROUND)));   
     261//  Generators.addIdisaFuncCallArgs(idisaFuncCall, args);
     262//  return idisaFuncCall;
     263//}
     264//
     265//private static void addIdisaFuncCallArgs(IdisaFuncCallNode node, ASTNode ...args) {
     266//  if(args.length > 0) {
     267//      for(ASTNode child: args) {
     268//          Accessors.argListNode(node).appendChild(child.deepCopy());
     269//      }
     270//      Accessors.argListNode(node).setToken(args[0].getToken());
     271//  }   
     272//}     
     273//
     274//public static FieldWidthNode makeIdisaFieldWidthNode(Locator locator, int fw) {
     275//  IntConstantToken fieldWidthConstant = Generators.makeIntegerConstantToken(locator, fw);
     276//  FieldWidthNode fieldWidthNode = new FieldWidthNode(fieldWidthConstant);
     277// 
     278//  IntegerConstantNode integerConstant = Generators.makeIntegerConstantNode(locator, fw);
     279//  fieldWidthNode.appendChild(integerConstant);
     280// 
     281//  return fieldWidthNode;
     282//}     
     283       
    241284}
Note: See TracChangeset for help on using the changeset viewer.