Skip to content

Commit 62c9ce2

Browse files
committed
checkpoint on adding new AST elements
Signed-off-by: Matthew Ballance <[email protected]>
1 parent d405056 commit 62c9ce2

17 files changed

+472
-278
lines changed

ast/datatypes.json

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"classes": [
3+
{
4+
"name": "Scope",
5+
"super": "ScopeChild",
6+
"data": {
7+
"children": "list<UP<ScopeChild>>",
8+
"symtab": {
9+
"type": "map<string,P<NamedScopeChild>>",
10+
"is_ctor": false
11+
}
12+
}
13+
},
14+
{
15+
"name": "ScopeChild",
16+
"data": {
17+
"parent": {
18+
"type": "P<Scope>",
19+
"is_ctor": false,
20+
"init": 0
21+
}
22+
}
23+
},
24+
{
25+
"name": "GlobalScope",
26+
"super": "Scope"
27+
},
28+
{
29+
"name": "NamedScope",
30+
"super": "Scope",
31+
"data": {
32+
"name" : "UP<ExprId>"
33+
}
34+
},
35+
{
36+
"name": "NamedScopeChild",
37+
"super": "ScopeChild",
38+
"data": {
39+
"name": "UP<ExprId>"
40+
}
41+
},
42+
{
43+
"name": "PackageScope",
44+
"super": "NamedScope",
45+
"data": {
46+
"sibling": {
47+
"type": "P<PackageScope>",
48+
"is_ctor": false,
49+
"init": 0
50+
}
51+
}
52+
},
53+
{
54+
"name": "TypeScope",
55+
"super": "NamedScope",
56+
"data": {
57+
"super_t" : "UP<TypeIdentifier>"
58+
}
59+
},
60+
{
61+
"name": "Action",
62+
"super": "TypeScope"
63+
},
64+
{
65+
"name": "Component",
66+
"super": "TypeScope"
67+
},
68+
{
69+
"name": "Struct",
70+
"super": "TypeScope"
71+
},
72+
{
73+
"name": "Buffer",
74+
"super": "Struct"
75+
},
76+
{
77+
"name": "Resource",
78+
"super": "Struct"
79+
},
80+
{
81+
"name": "State",
82+
"super": "Struct"
83+
},
84+
{
85+
"name": "Stream",
86+
"super": "Struct"
87+
}
88+
],
89+
"flags": [
90+
{
91+
"name": "FieldAttr",
92+
"values": [
93+
"FieldAttr_Builtin"
94+
]
95+
}
96+
]
97+
}

src/parser/AstBuilder.cpp src/AstBuilder.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include "AstBuilder.h"
9+
910
#include "AstBuilderInt.h"
1011

1112
namespace pssp {
@@ -21,8 +22,9 @@ AstBuilder::~AstBuilder() {
2122
}
2223

2324
void AstBuilder::build(
24-
std::istream *in) {
25-
m_builder_int->build(in);
25+
GlobalScope *global,
26+
std::istream *in) {
27+
m_builder_int->build(global, in);
2628
}
2729

2830
} /* namespace pssp */

src/parser/AstBuilder.h src/AstBuilder.h

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma once
99
#include <memory>
1010
#include <iostream>
11+
#include "GlobalScope.h"
1112
#include "IMarkerListener.h"
1213

1314
namespace pssp {
@@ -22,6 +23,7 @@ class AstBuilder {
2223
virtual ~AstBuilder();
2324

2425
void build(
26+
GlobalScope *global,
2527
std::istream *in);
2628

2729
private:

src/AstBuilderInt.cpp

+288-263
Large diffs are not rendered by default.

src/AstBuilderInt.h

+13-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "IMarkerListener.h"
1212
#include "PSSBaseVisitor.h"
1313
#include "BaseErrorListener.h"
14+
#include "GlobalScope.h"
15+
#include "Scope.h"
1416

1517
using namespace antlr4;
1618

@@ -24,16 +26,12 @@ class AstBuilderInt :
2426

2527
virtual ~AstBuilderInt();
2628

27-
void build(std::istream *in);
28-
29-
virtual antlrcpp::Any visitCompilation_unit(PSSParser::Compilation_unitContext *context) override;
30-
31-
virtual antlrcpp::Any visitPortable_stimulus_description(PSSParser::Portable_stimulus_descriptionContext *context) override;
29+
void build(
30+
GlobalScope *global,
31+
std::istream *in);
3232

3333
virtual antlrcpp::Any visitPackage_declaration(PSSParser::Package_declarationContext *context) override;
3434

35-
virtual antlrcpp::Any visitPackage_body_item(PSSParser::Package_body_itemContext *context) override;
36-
3735
virtual antlrcpp::Any visitImport_stmt(PSSParser::Import_stmtContext *context) override;
3836

3937
virtual antlrcpp::Any visitPackage_import_pattern(PSSParser::Package_import_patternContext *context) override;
@@ -556,8 +554,16 @@ class AstBuilderInt :
556554
const std::string &msg,
557555
std::exception_ptr e) override;
558556

557+
private:
558+
Scope *scope() const { return m_scopes.back(); }
559+
560+
void push_scope(Scope *s) { m_scopes.push_back(s); }
561+
562+
void pop_scope() { m_scopes.pop_back(); }
563+
559564
private:
560565
IMarkerListener *m_marker_l;
566+
std::vector<Scope *> m_scopes;
561567

562568
};
563569

src/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ file(GLOB pssparser_SRC
3232
add_library(pssparser
3333
${pssparser_SRC}
3434
${ANTLR_PSS_OUTPUTS})
35+
36+
add_dependencies(pssparser pss_ast libantlr4)
3537

3638
add_library(formatter
3739
${parser_SRC}
@@ -46,7 +48,7 @@ target_link_libraries(pssformat
4648

4749

4850
add_dependencies(pssformat
49-
LIBANTLR4)
51+
libantlr4)
5052

5153
#add_dependencies(parser gen_pss_parser)
5254

src/Formatter.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
* Author: ballance
66
*/
77

8+
#include <stdio.h>
89
#include "Formatter.h"
910
#include "PSSLexer.h"
1011
#include "PSSParser.h"
1112

1213
using namespace antlr4;
1314

15+
namespace pss {
16+
1417
Formatter::Formatter() {
1518
// TODO Auto-generated constructor stub
1619

@@ -34,12 +37,35 @@ void Formatter::format(
3437
parser.compilation_unit();
3538
}
3639

40+
void Formatter::enterAction_declaration(PSSParser::Action_declarationContext *ctx) {
41+
fprintf(stdout, "enterAction_declaration %p %s\n",
42+
ctx->start,
43+
ctx->start->getText().c_str());
44+
}
45+
46+
void Formatter::exitAction_declaration(PSSParser::Action_declarationContext *ctx) {
47+
fprintf(stdout, "exitAction_declaration %s\n", ctx->start->getText().c_str());
48+
}
49+
50+
void Formatter::enterComponent_declaration(PSSParser::Component_declarationContext *ctx) {
51+
fprintf(stdout, "enterComponent_declaration %p %s\n",
52+
ctx->start,
53+
ctx->start->getText().c_str());
54+
}
55+
56+
void Formatter::exitComponent_declaration(PSSParser::Component_declarationContext *ctx) {
57+
fprintf(stdout, "exitComponent_declaration %s\n", ctx->start->getText().c_str());
58+
}
59+
3760
void Formatter::visitTerminal(antlr4::tree::TerminalNode *node) {
38-
fprintf(stdout, "visitTerminal: %s\n", node->getText().c_str());
61+
fprintf(stdout, "visitTerminal: %p %s\n",
62+
node->getSymbol(),
63+
node->getText().c_str());
3964

4065
}
4166

4267
void Formatter::visitErrorNode(antlr4::tree::ErrorNode *node) {
4368
fprintf(stdout, "visitErrorNode: %s\n", node->getText().c_str());
4469
}
4570

71+
}

src/Formatter.h

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "PSSBaseListener.h"
1010
#include <iostream>
1111

12+
namespace pss {
13+
1214
class Formatter : public PSSBaseListener {
1315
public:
1416
Formatter();
@@ -19,9 +21,16 @@ class Formatter : public PSSBaseListener {
1921
std::istream *in,
2022
std::ostream *out);
2123

24+
virtual void enterAction_declaration(PSSParser::Action_declarationContext *ctx) override;
25+
virtual void exitAction_declaration(PSSParser::Action_declarationContext *ctx) override;
26+
27+
virtual void enterComponent_declaration(PSSParser::Component_declarationContext *ctx) override;
28+
virtual void exitComponent_declaration(PSSParser::Component_declarationContext *ctx) override;
29+
2230
virtual void visitTerminal(antlr4::tree::TerminalNode *node) override;
2331

2432
virtual void visitErrorNode(antlr4::tree::ErrorNode *node) override;
2533

2634
};
2735

36+
}

src/IMarkerListener.h

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99
#include "Marker.h"
10+
#include "MarkerSeverityE.h"
1011

1112
namespace pssp {
1213

@@ -16,6 +17,8 @@ class IMarkerListener {
1617

1718
virtual void marker(const Marker &m) = 0;
1819

20+
virtual bool hasSeverity(MarkerSeverityE s) = 0;
21+
1922
};
2023

2124
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/parser/Marker.h src/Marker.h

File renamed without changes.

src/parser/MarkerCollector.cpp src/MarkerCollector.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@
1010
namespace pssp {
1111

1212
MarkerCollector::MarkerCollector() {
13-
// TODO Auto-generated constructor stub
14-
13+
for (uint32_t i=0; i<Severity_NumLevels; i++) {
14+
m_count[i] = 0;
15+
}
1516
}
1617

1718
MarkerCollector::~MarkerCollector() {
1819
// TODO Auto-generated destructor stub
1920
}
2021

22+
void MarkerCollector::marker(const Marker &m) {
23+
m_markers.push_back(m);
24+
m_count[m.severity()]++;
25+
}
26+
27+
bool MarkerCollector::hasSeverity(MarkerSeverityE s) {
28+
return m_count[s];
29+
}
30+
2131
} /* namespace pssp */

src/parser/MarkerCollector.h src/MarkerCollector.h

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#pragma once
9+
#include <vector>
910
#include "IMarkerListener.h"
1011

1112
namespace pssp {
@@ -16,6 +17,14 @@ class MarkerCollector : public virtual IMarkerListener {
1617

1718
virtual ~MarkerCollector();
1819

20+
virtual void marker(const Marker &m) override;
21+
22+
virtual bool hasSeverity(MarkerSeverityE s) override;
23+
24+
private:
25+
uint32_t m_count[Severity_NumLevels];
26+
std::vector<Marker> m_markers;
27+
1928

2029
};
2130

src/parser/MarkerSeverityE.h src/MarkerSeverityE.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
* Author: ballance
66
*/
77

8+
#pragma once
9+
810
namespace pssp {
911

1012
enum MarkerSeverityE {
1113
Severity_Error,
1214
Severity_Warn,
1315
Severity_Info,
14-
Severity_Hint
16+
Severity_Hint,
17+
Severity_NumLevels
1518
};
1619

1720
}

src/apps/pssformat_main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int main(int argc, char **argv) {
2020
exit(1);
2121
}
2222

23-
Formatter f;
23+
pss::Formatter f;
2424

2525

2626

0 commit comments

Comments
 (0)