use of org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext in project incubator-systemml by apache.
the class PyDMLParserWrapper method createDMLProgram.
private DMLProgram createDMLProgram(ProgramrootContext ast, String sourceNamespace) {
DMLProgram dmlPgm = new DMLProgram();
String namespace = (sourceNamespace != null && sourceNamespace.length() > 0) ? sourceNamespace : DMLProgram.DEFAULT_NAMESPACE;
dmlPgm.getNamespaces().put(namespace, dmlPgm);
// First add all the functions
for (FunctionStatementContext fn : ast.functionBlocks) {
FunctionStatementBlock functionStmtBlk = new FunctionStatementBlock();
functionStmtBlk.addStatement(fn.info.stmt);
try {
dmlPgm.addFunctionStatementBlock(namespace, fn.info.functionName, functionStmtBlk);
} catch (LanguageException e) {
LOG.error("line: " + fn.start.getLine() + ":" + fn.start.getCharPositionInLine() + " cannot process the function " + fn.info.functionName);
return null;
}
}
// Then add all the statements
for (StatementContext stmtCtx : ast.blocks) {
Statement current = stmtCtx.info.stmt;
if (current == null) {
LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the statement");
return null;
}
// Ignore Newline logic
if (current.isEmptyNewLineStatement()) {
continue;
}
if (current instanceof ImportStatement) {
// Handle import statements separately
if (stmtCtx.info.namespaces != null) {
// Add the DMLProgram entries into current program
for (Map.Entry<String, DMLProgram> entry : stmtCtx.info.namespaces.entrySet()) {
// TODO handle namespace key already exists for different program value instead of overwriting
DMLProgram prog = entry.getValue();
if (prog != null && prog.getNamespaces().size() > 0) {
dmlPgm.getNamespaces().put(entry.getKey(), prog);
}
// Add dependent programs (handle imported script that also imports scripts)
for (Map.Entry<String, DMLProgram> dependency : entry.getValue().getNamespaces().entrySet()) {
String depNamespace = dependency.getKey();
DMLProgram depProgram = dependency.getValue();
if (dmlPgm.getNamespaces().get(depNamespace) == null) {
dmlPgm.getNamespaces().put(depNamespace, depProgram);
}
}
}
} else {
LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the import statement");
return null;
}
}
// Now wrap statement into individual statement block
// merge statement will take care of merging these blocks
dmlPgm.addStatementBlock(getStatementBlock(current));
}
dmlPgm.mergeStatementBlocks();
return dmlPgm;
}
Aggregations