Skip to content

Commit

Permalink
Simplification to path-ref scheme, and progress on python integration
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Ballance <matt.ballance@gmail.com>
  • Loading branch information
mballance committed Nov 20, 2023
1 parent 9de5e7c commit 75f57dc
Show file tree
Hide file tree
Showing 16 changed files with 1,110 additions and 791 deletions.
19 changes: 19 additions & 0 deletions ast/coretypes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ classes:
- type: UP<TypeIdentifier>
- is_ctor: false

- PyImportStmt:
super: ScopeChild
data:
- path:
type: list<UP<ExprId>>
is_ctor: false
- alias:
type: UP<ExprId>
is_ctor: false
- PyImportFromStmt:
super: ScopeChild
data:
- path:
type: list<UP<ExprId>>
is_ctor: false
- targets:
type: list<UP<ExprId>>
is_ctor: false

- Action:
super: TypeScope
data:
Expand Down
2 changes: 1 addition & 1 deletion ast/expr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ classes:
- ExprRefPathStaticRooted:
super: ExprRefPath
data:
- root: "UP<ExprStaticRefPath>"
- root: "UP<ExprRefPathStatic>"
- leaf: "UP<ExprHierarchicalId>"
- slice:
type: UP<ExprBitSlice>
Expand Down
4 changes: 4 additions & 0 deletions ast/linking.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ classes:
- type: list<SymbolRefPathElem>
- is_ctor: false
- visit: false
- pyref_idx:
- type: int32_t
- is_ctor: false
- init: -1

#- AggregateTypeScope:
# super: ScopeChild
Expand Down
2 changes: 1 addition & 1 deletion src/.antlr/PSSLexer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from /project/fun/zuspec/zuspec-parser/src/PSSLexer.g4 by ANTLR 4.13.1
// Generated from /project/fun/zuspec/zuspec-py/packages/zuspec-parser/src/PSSLexer.g4 by ANTLR 4.13.1
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Token;
Expand Down
2 changes: 1 addition & 1 deletion src/.antlr/PSSParser.interp

Large diffs are not rendered by default.

1,555 changes: 786 additions & 769 deletions src/.antlr/PSSParser.java

Large diffs are not rendered by default.

107 changes: 91 additions & 16 deletions src/AstBuilderInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,34 @@ antlrcpp::Any AstBuilderInt::visitImport_stmt(PSSParser::Import_stmtContext *ctx
return 0;
}

antlrcpp::Any AstBuilderInt::visitPyimport_single_module(PSSParser::Pyimport_single_moduleContext *ctx) {
DEBUG_ENTER("visitPyimport_single_module");
ast::IPyImportStmt *imp = m_factory->mkPyImportStmt();

std::vector<PSSParser::IdentifierContext *> path = ctx->pyimport_mod_path()->identifier();
for (std::vector<PSSParser::IdentifierContext *>::const_iterator
it=path.begin();
it!=path.end(); it++) {
imp->getPath().push_back(ast::IExprIdUP(mkId(*it)));
}
if (ctx->identifier()) {
// Have an alias
imp->setAlias(mkId(ctx->identifier()));
}

setLoc(imp, ctx->start);
addChild(imp, ctx->start);
DEBUG_LEAVE("visitPyimport_single_module");
return 0;
}

antlrcpp::Any AstBuilderInt::visitPyimport_from_module(PSSParser::Pyimport_from_moduleContext *ctx) {
DEBUG_ENTER("visitPyimport_from_module");
ast::IPyImportFromStmt *imp = m_factory->mkPyImportFromStmt();
DEBUG_LEAVE("visitPyimport_from_module");
return 0;
}

static std::map<std::string,ast::ExtendTargetE> ExtendKind_m = {
{"action", ast::ExtendTargetE::Action},
{"buffer", ast::ExtendTargetE::Buffer},
Expand Down Expand Up @@ -652,14 +680,12 @@ antlrcpp::Any AstBuilderInt::visitProcedural_assignment_stmt(PSSParser::Procedur

antlrcpp::Any AstBuilderInt::visitProcedural_void_function_call_stmt(PSSParser::Procedural_void_function_call_stmtContext *ctx) {
DEBUG_ENTER("visitProcedural_void_function_call_stmt");
IExprStaticRefPath *prefix = 0;
IExprRefPathStatic *prefix = 0;

if (ctx->function_call()->is_global ||
ctx->function_call()->type_identifier_elem().size() > 0) {
// Have a static component
prefix = m_factory->mkExprStaticRefPath(
ctx->function_call()->is_global,
0);
prefix = m_factory->mkExprRefPathStatic(ctx->function_call()->is_global);

std::vector<PSSParser::Type_identifier_elemContext *> items =
ctx->function_call()->type_identifier_elem();
Expand Down Expand Up @@ -2333,6 +2359,36 @@ ast::IExprHierarchicalId *AstBuilderInt::mkHierarchicalId(PSSParser::Hierarchica
return ret;
}

ast::IExprHierarchicalId *AstBuilderInt::mkHierarchicalId(PSSParser::Member_path_elemContext *ctx) {
DEBUG_ENTER("mkHierarchicalId(member_path_elem)");
ast::IExprHierarchicalId *ret = m_factory->mkExprHierarchicalId();
ret->getElems().push_back(ast::IExprMemberPathElemUP(mkMemberPathElem(ctx)));

DEBUG_LEAVE("mkHierarchicalId(member_path_elem)");
return ret;
}

ast::IExprHierarchicalId *AstBuilderInt::mkHierarchicalId(
PSSParser::Static_ref_pathContext *root_ctx,
PSSParser::Hierarchical_idContext *leaf_ctx) {
DEBUG_ENTER("mkHierarchicalId(base_ctx, leaf_ctx)");
ast::IExprHierarchicalId *ret = m_factory->mkExprHierarchicalId();
ret->getElems().push_back(ast::IExprMemberPathElemUP(mkMemberPathElem(
root_ctx->member_path_elem())));

std::vector<PSSParser::Member_path_elemContext *> items = leaf_ctx->member_path_elem();

for (std::vector<PSSParser::Member_path_elemContext *>::const_iterator
it=items.begin();
it!=items.end(); it++) {
ret->getElems().push_back(ast::IExprMemberPathElemUP(mkMemberPathElem(*it)));
}

DEBUG_LEAVE("mkHierarchicalId(base_ctx, leaf_ctx)");
return ret;
}


ast::IExprMemberPathElem *AstBuilderInt::mkMemberPathElem(
PSSParser::Member_path_elemContext *ctx) {
ast::IExprId *id = 0;
Expand Down Expand Up @@ -2451,8 +2507,10 @@ ast::IExprRefPath *AstBuilderInt::mkExprRefPath(
if (ctx->hierarchical_id()) {
DEBUG("hierarchical_id: ");
// Has a context portion
ast::IExprStaticRefPath *static_ref = mkExprStaticRefPath(ctx->static_ref_path());
ast::IExprHierarchicalId *context_ref = mkHierarchicalId(ctx->hierarchical_id());
ast::IExprRefPathStatic *static_ref = mkExprRefPathStatic(ctx->static_ref_path());
ast::IExprHierarchicalId *context_ref = mkHierarchicalId(
ctx->static_ref_path(),
ctx->hierarchical_id());

fprintf(stdout, "mkExprRefPath: static_ref=%p context_ref=%p\n", static_ref, context_ref);
ast::IExprRefPathStaticRooted *ref = m_factory->mkExprRefPathStaticRooted(
Expand All @@ -2464,7 +2522,7 @@ ast::IExprRefPath *AstBuilderInt::mkExprRefPath(
}

ret = ref;
} else {
} else { // Does not have a hierarchical_id component
/*
* ref_path:
* static_ref_path ( TOK_DOT hierarchical_id )? bit_slice? // <-- We're here
Expand Down Expand Up @@ -2503,28 +2561,47 @@ ast::IExprRefPath *AstBuilderInt::mkExprRefPath(

ret = ref;
} else {
DEBUG("case2 (multi-element path)");
DEBUG("case2 (multi-element path) size=%d", items.size());
// static_ref_path_prefix type_identifier_elem+ member_path_elem

ast::IExprRefPathStatic *ref = m_factory->mkExprRefPathStatic(
ctx->static_ref_path()->static_ref_path_prefix()->is_global
);

if (!ctx->static_ref_path()->static_ref_path_prefix()->is_global) {
DEBUG("Add root elem");
ref->getBase().push_back(ast::ITypeIdentifierElemUP(
mkTypeIdElem(ctx->static_ref_path()->static_ref_path_prefix()->type_identifier_elem())));
}

for (std::vector<PSSParser::Type_identifier_elemContext *>::const_iterator
it=items.begin();
it!=items.end(); it++) {
ref->getBase().push_back(ast::ITypeIdentifierElemUP(mkTypeIdElem(*it)));
}

if (ctx->static_ref_path()->member_path_elem()->function_parameter_list()) {
// Last element is a function call. Use ExprStaticRooted to express
ast::IExprRefPathStaticRooted *expr = m_factory->mkExprRefPathStaticRooted(
ref,
mkHierarchicalId(ctx->static_ref_path()->member_path_elem())
);
ret = expr;
} else {
// Last element is a field/constant reference
ref->getBase().push_back(ast::ITypeIdentifierElemUP(
mkTypeIdElem(ctx->static_ref_path()->member_path_elem()->identifier())));
ret = ref;
}

if (ctx->bit_slice()) {
ERROR("Revisit handling of bit_slice");
ref->setSlice(mkExprBitSlice(ctx->bit_slice()));
}

ret = ref;
}
}

} else {
} else { // Does not have a static_ref_path prefix
// Context ref
DEBUG("!static_ref_path: ExprRefPathContext");
ast::IExprRefPathContext *cref = m_factory->mkExprRefPathContext(
Expand All @@ -2542,13 +2619,11 @@ ast::IExprRefPath *AstBuilderInt::mkExprRefPath(
return ret;
}

ast::IExprStaticRefPath *AstBuilderInt::mkExprStaticRefPath(
ast::IExprRefPathStatic *AstBuilderInt::mkExprRefPathStatic(
PSSParser::Static_ref_pathContext *ctx) {
IExprStaticRefPath *ret = 0;
IExprRefPathStatic *ret = 0;

ret = m_factory->mkExprStaticRefPath(
ctx->static_ref_path_prefix()->is_global,
mkMemberPathElem(ctx->member_path_elem()));
ret = m_factory->mkExprRefPathStatic(ctx->static_ref_path_prefix()->is_global);

std::vector<PSSParser::Type_identifier_elemContext *> items =
ctx->type_identifier_elem();
Expand Down
12 changes: 11 additions & 1 deletion src/AstBuilderInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class AstBuilderInt :

virtual antlrcpp::Any visitImport_stmt(PSSParser::Import_stmtContext *ctx) override;

virtual antlrcpp::Any visitPyimport_single_module(PSSParser::Pyimport_single_moduleContext *ctx) override;

virtual antlrcpp::Any visitPyimport_from_module(PSSParser::Pyimport_from_moduleContext *ctx) override;

virtual antlrcpp::Any visitExtend_stmt(PSSParser::Extend_stmtContext *ctx) override;

virtual antlrcpp::Any visitConst_field_declaration(PSSParser::Const_field_declarationContext *ctx) override;
Expand Down Expand Up @@ -292,6 +296,12 @@ class AstBuilderInt :

ast::IExprHierarchicalId *mkHierarchicalId(PSSParser::Hierarchical_idContext *ctx);

ast::IExprHierarchicalId *mkHierarchicalId(
PSSParser::Static_ref_pathContext *root_ctx,
PSSParser::Hierarchical_idContext *leaf_ctx);

ast::IExprHierarchicalId *mkHierarchicalId(PSSParser::Member_path_elemContext *ctx);

ast::IExprMemberPathElem *mkMemberPathElem(PSSParser::Member_path_elemContext *ctx);

void mkTypeId(
Expand All @@ -316,7 +326,7 @@ class AstBuilderInt :
ast::IExprRefPath *mkExprRefPath(
PSSParser::Ref_pathContext *ctx);

ast::IExprStaticRefPath *mkExprStaticRefPath(
ast::IExprRefPathStatic *mkExprRefPathStatic(
PSSParser::Static_ref_pathContext *ctx);

ast::ITemplateParamDeclList *mkTypeParamDecl(
Expand Down
1 change: 1 addition & 0 deletions src/PSSParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ expression:
lhs=expression binary_or_op rhs=expression |
lhs=expression logical_and_op rhs=expression |
lhs=expression logical_or_op rhs=expression |
// lhs=expression TOK_SINGLE_EQ rhs=expression { notify
lhs=expression conditional_expr
;

Expand Down
41 changes: 41 additions & 0 deletions src/TaskBuildSymbolTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,47 @@ void TaskBuildSymbolTree::visitPackageImportStmt(ast::IPackageImportStmt *i) {
DEBUG_LEAVE("visitPackageImportStmt");
}

void TaskBuildSymbolTree::visitPyImportStmt(ast::IPyImportStmt *i) {
DEBUG_ENTER("visitPyImportStmt");
ast::ISymbolScope *scope = m_scope_s.back();
std::map<std::string, int32_t>::const_iterator it;

if (i->getAlias()) {
// Register the alias name
if ((it=scope->getSymtab().find(i->getAlias()->getId())) != scope->getSymtab().end()) {
// Error:
ERROR("TODO: symbol collision with pyimport %s", i->getAlias()->getId().c_str());
} else {
int32_t id = scope->getChildren().size();
scope->getChildren().push_back(i);
scope->getSymtab().insert({
i->getAlias()->getId(),
id
});
}
} else {
// Register the basename
if ((it=scope->getSymtab().find(i->getPath().front()->getId())) != scope->getSymtab().end()) {
// Error:
ERROR("TODO: symbol collision with pyimport %s", i->getPath().front()->getId().c_str());
} else {
int32_t id = scope->getChildren().size();
scope->getChildren().push_back(i);
scope->getSymtab().insert({
i->getPath().front()->getId(),
id
});
}
}
DEBUG_LEAVE("visitPyImportStmt");
}

void TaskBuildSymbolTree::visitPyImportFromStmt(ast::IPyImportFromStmt *i) {
DEBUG_ENTER("visitPyImportFromStmt");
DEBUG("TODO: visitPyImportFromStmt");
DEBUG_LEAVE("visitPyImportFromStmt");
}

void TaskBuildSymbolTree::visitProceduralStmtDataDeclaration(ast::IProceduralStmtDataDeclaration *i) {
DEBUG_ENTER("visitProceduralStmtDataDeclaration");
ast::ISymbolScope *scope = m_scope_s.back();
Expand Down
4 changes: 4 additions & 0 deletions src/TaskBuildSymbolTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class TaskBuildSymbolTree : public virtual ast::VisitorBase {

virtual void visitPackageImportStmt(ast::IPackageImportStmt *i) override;

virtual void visitPyImportStmt(ast::IPyImportStmt *i) override;

virtual void visitPyImportFromStmt(ast::IPyImportFromStmt *i) override;

virtual void visitProceduralStmtDataDeclaration(ast::IProceduralStmtDataDeclaration *i) override;

virtual void visitScope(ast::IScope *i) override;
Expand Down
Loading

0 comments on commit 75f57dc

Please sign in to comment.