Skip to content

Commit 77d3fbe

Browse files
author
robertDurst
committed
some general c++ cleanup for finishing up of parser2.0
1 parent acd39b3 commit 77d3fbe

File tree

13 files changed

+331
-314
lines changed

13 files changed

+331
-314
lines changed

src/errorhandler/Error2.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <iostream>
88
#include <string>
99

10-
// data structure describing an error
1110
class Error2
1211
{
1312
private:
@@ -20,8 +19,8 @@ class Error2
2019
std::string errtype;
2120

2221
public:
23-
Error2(const int c, const int l, const std::string m, const std::string le,
24-
const std::string mid, const std::string ri)
22+
Error2(int c, int l, const std::string& m, const std::string& le,
23+
const std::string& mid, const std::string& ri)
2524
{
2625
col = c;
2726
line = l;
@@ -32,18 +31,19 @@ class Error2
3231
}
3332
// set method for receiving type to utilize internally
3433
void
35-
setErrorType(const std::string e)
34+
setErrorType(const std::string& e)
3635
{
3736
errtype = e;
3837
}
3938
// helper method for pretty printing error message
4039
void
4140
displayMessage()
4241
{
43-
Color::Modifier red(Color::FG_RED);
44-
Color::Modifier def(Color::FG_DEFAULT);
45-
Color::Modifier underline(Color::UNDERLINE);
46-
Color::Modifier normal(Color::RESET);
42+
Prettify::Formatter red(Prettify::FG_RED);
43+
Prettify::Formatter def(Prettify::FG_DEFAULT);
44+
Prettify::Formatter underline(Prettify::UNDERLINE);
45+
Prettify::Formatter normal(Prettify::RESET);
46+
4747
std::cout << red << "[" << errtype << " ERROR at [" << line << ":"
4848
<< col << "]: " << msg << def << "\n\n\t" << left << underline
4949
<< middle << normal << right << "\n";

src/errorhandler/Parser2ErrorHandler.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
* Sailfish Programming Language
44
*/
55
#include "Parser2ErrorHandler.h"
6-
#include <iostream>
76

87
void
9-
Parser2ErrorHandler::handle(Error2* err)
8+
Parser2ErrorHandler::handle(std::unique_ptr<Error2> err)
109
{
1110
// finish the initialization of Error
1211
err->setErrorType("PARSER");

src/errorhandler/Parser2ErrorHandler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
*/
55
#pragma once
66
#include "Error2.h"
7+
#include <memory>
78

89
class Parser2ErrorHandler
910
{
1011
public:
1112
Parser2ErrorHandler(){};
12-
void handle(Error2*);
13+
void handle(std::unique_ptr<Error2>);
1314
};

src/errorhandler/display.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
// https://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
1+
/*
2+
* Robert Durst 2019
3+
* Sailfish Programming Language
4+
*
5+
* Base structure and credit:
6+
* https://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
7+
*/
8+
29
#include <ostream>
3-
namespace Color
10+
namespace Prettify
411
{
512
enum Code
613
{
714
RESET = 0,
815
UNDERLINE = 4,
916
FG_RED = 31,
10-
FG_GREEN = 32,
11-
FG_BLUE = 34,
1217
FG_DEFAULT = 39,
13-
BG_RED = 41,
14-
BG_GREEN = 42,
15-
BG_BLUE = 44,
16-
BG_DEFAULT = 49
1718
};
18-
class Modifier
19+
class Formatter
1920
{
2021
Code code;
2122

2223
public:
23-
Modifier(Code pCode) : code(pCode)
24-
{
25-
}
24+
Formatter(Code c) : code(c){};
2625
friend std::ostream&
27-
operator<<(std::ostream& os, const Modifier& mod)
26+
operator<<(std::ostream& os, const Formatter& mod)
2827
{
2928
return os << "\033[" << mod.code << "m";
3029
}

src/lexar/Lexar2.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ std::unique_ptr<Token2>
1515
Lexar2::makeToken(const TokenKind& k, const std::string& v)
1616
{
1717
auto kd = k; // since kd is constant we copy here
18+
19+
// instead of a symbol table or a keyword table/data structure, we have this
20+
// nice if-else tree
1821
if (kd == TokenKind::IDENTIFIER)
1922
{
2023
if (v == "start")
@@ -61,6 +64,8 @@ Lexar2::makeTokenPutback(const TokenKind& k, std::string& v, char& c)
6164
// put the char back in the filebuffer
6265
file.putback(c);
6366

67+
// edge case where if the last char was a space, even though we want to
68+
// putback, the buffer itself won't be off by one
6469
if (!isspace(c))
6570
v.pop_back();
6671

@@ -71,9 +76,7 @@ char
7176
Lexar2::getNextChar()
7277
{
7378
if (file.peek() == EOF)
74-
{
7579
return -1;
76-
}
7780

7881
char c;
7982
file.get(c);

src/lexar/Lexar2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Lexar2
1919
std::unique_ptr<Token2> makeToken(const TokenKind&, const std::string&);
2020
std::unique_ptr<Token2> makeTokenPutback(const TokenKind&, std::string&,
2121
char&);
22-
22+
// represents dfa states in our pseudo dfa/state machine implementation
2323
enum State
2424
{
2525
START,

src/lexar/Token2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct Token2
8484
std::string value;
8585
int col;
8686
int line;
87-
Token2(TokenKind k, std::string v, int c, int l)
87+
Token2(TokenKind k, const std::string& v, int c, int l)
8888
: kind(k), value(v), col(c), line(l){};
8989
std::string prettifyFormatToken();
9090
};

src/main/CommandLine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ handleCommandLine(int argc, char* const* argv)
164164

165165
Parser2* p = new Parser2(filename);
166166
auto n = p->parse();
167-
p->postorder(
168-
n, [](std::string s) -> void { std::cout << s << '\n'; });
167+
p->postorder(std::move(n), [](std::string s) -> void {
168+
std::cout << s << '\n';
169+
});
169170
}
170171
catch (const std::string msg)
171172
{

src/parser/Lexeme.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
#include "Lexeme.h"
22

3-
std::shared_ptr<NodeLexeme>
4-
makeNode(OP o, Lexeme l, Lexeme r)
5-
{
6-
return std::make_shared<NodeLexeme>(o, l, r);
7-
}
8-
9-
std::shared_ptr<NodeLexeme>
10-
makeNode(OP o, Lexeme l)
11-
{
12-
return std::make_shared<NodeLexeme>(o, l, makeNullNode());
13-
}
14-
153
std::string
164
disp(OP o)
175
{
@@ -152,13 +140,25 @@ disp(LIT o)
152140
}
153141
}
154142

155-
std::shared_ptr<LeafLexeme>
143+
LeafPtr
156144
makeLeaf(LIT l, std::string v)
157145
{
158-
return std::make_shared<LeafLexeme>(l, v);
146+
return std::make_unique<LeafLexeme>(l, v);
147+
}
148+
149+
NodePtr
150+
makeNode(OP o, Lexeme l, Lexeme r)
151+
{
152+
return std::make_unique<NodeLexeme>(o, std::move(l), std::move(r));
153+
}
154+
155+
NodePtr
156+
makeNode(OP o, Lexeme l)
157+
{
158+
return std::make_unique<NodeLexeme>(o, std::move(l), makeNullNode());
159159
}
160160

161-
std::shared_ptr<NodeLexeme>
161+
NodePtr
162162
makeNullNode()
163163
{
164164
return makeNode(OP::NULL_VAL, makeLeaf(LIT::IDENTIFIER, ""),

src/parser/Lexeme.h

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,96 +11,111 @@
1111
#include <variant>
1212
#include <vector>
1313

14+
// OP represents the AST node type for a given nonterminal
1415
enum class OP
1516
{
16-
SOURCE,
17-
SOURCE_PART,
18-
IMPORT,
19-
UDT,
20-
UDA,
21-
START,
22-
SCRIPT,
23-
VARIABLE,
24-
FUNCTION,
25-
FUNCTION_INFO,
26-
FUNCTION_IN_OUT,
27-
TREE,
17+
ADDITION,
18+
ADDTO,
19+
AND,
20+
ASSIGNMENT,
21+
ATTRIBUTE_ACCESS,
22+
ATTRIBUTE,
23+
BLOCK,
2824
BRANCH,
25+
DECLARATION,
26+
DIVFROM,
27+
DIVISION,
2928
E0,
29+
END,
30+
EQUIVALENCE,
3031
EXPONENT,
31-
MULTIPLICATION,
32-
DIVISION,
33-
MODULO,
34-
ADDITION,
35-
SUBTRACTION,
36-
LESS_THAN,
37-
LESS_THAN_OR_EQUALS,
38-
GREATER_THAN,
32+
FUNCTION_CALL,
33+
FUNCTION_IN_OUT,
34+
FUNCTION_INFO,
35+
FUNCTION_INPUT,
36+
FUNCTION,
3937
GREATER_THAN_OR_EQUALS,
40-
EQUIVALENCE,
38+
GREATER_THAN,
39+
IMPORT,
40+
INPUT,
41+
LESS_THAN_OR_EQUALS,
42+
LESS_THAN,
43+
MEMBER,
44+
METHOD_ACCESS,
45+
METHOD,
46+
MODULO,
47+
MULTIPLICATION,
48+
MULTTO,
49+
NEGATION,
4150
NONEQUIVALENCE,
42-
AND,
51+
NULL_VAL,
4352
OR,
44-
ASSIGNMENT,
45-
NEGATION,
46-
END,
4753
PRIMARY,
48-
DECLARATION,
4954
RETURN,
50-
ATTRIBUTE,
51-
METHOD,
52-
FUNCTION_INPUT,
55+
SCRIPT,
56+
SOURCE_PART,
57+
SOURCE,
58+
START,
5359
STATEMENT,
54-
BLOCK,
55-
NULL_VAL,
56-
MEMBER,
57-
ATTRIBUTE_ACCESS,
58-
METHOD_ACCESS,
59-
INPUT,
60-
FUNCTION_CALL,
60+
SUBFROM,
61+
SUBTRACTION,
62+
TREE,
63+
UDA,
64+
UDT,
6165
UDTDEC,
6266
UDTDECITEM,
6367
UNARYADD,
6468
UNARYMINUS,
65-
ADDTO,
66-
SUBFROM,
67-
DIVFROM,
68-
MULTTO,
69+
VARIABLE,
6970
};
7071
std::string disp(OP o);
7172

73+
// LIT represents the AST node type for a given terminal
7274
enum class LIT
7375
{
74-
IDENTIFIER,
75-
STRING,
7676
BOOLEAN,
77-
INTEGER,
7877
FLOAT,
78+
IDENTIFIER,
79+
INTEGER,
7980
LIST,
8081
LISTTYPE,
82+
STRING,
8183
};
8284
std::string disp(LIT l);
8385

86+
// node = nonterminal and leaf = terminal
8487
struct LeafLexeme;
8588
struct NodeLexeme;
86-
typedef std::variant<std::shared_ptr<NodeLexeme>, std::shared_ptr<LeafLexeme>>
87-
Lexeme;
89+
90+
// type alias for more legible code
91+
using LeafPtr = std::unique_ptr<LeafLexeme>;
92+
using NodePtr = std::unique_ptr<NodeLexeme>;
93+
94+
// Lexeme is short for the variant that may either be a node or a leaf,
95+
// convenient for nonterminals that have one or two children
96+
// using Lexeme =
97+
// std::variant<std::shared_ptr<NodeLexeme>, std::shared_ptr<LeafLexeme>>;
98+
using Lexeme = std::variant<LeafPtr, NodePtr>;
8899

89100
struct LeafLexeme
90101
{
91102
LIT lit;
92103
std::string value;
93104
LeafLexeme(LIT l, std::string v) : lit(l), value(v){};
94105
};
95-
std::shared_ptr<LeafLexeme> makeLeaf(LIT l, std::string v);
106+
LeafPtr makeLeaf(LIT l, std::string v);
96107

97108
struct NodeLexeme
98109
{
99110
OP op;
100111
Lexeme left;
101112
Lexeme right;
102-
NodeLexeme(OP o, Lexeme l, Lexeme r) : op(o), left(l), right(r){};
113+
NodeLexeme(OP o, Lexeme l, Lexeme r)
114+
: op(o), left(std::move(l)), right(std::move(r)){};
103115
};
104-
std::shared_ptr<NodeLexeme> makeNode(OP o, Lexeme, Lexeme);
105-
std::shared_ptr<NodeLexeme> makeNode(OP o, Lexeme);
106-
std::shared_ptr<NodeLexeme> makeNullNode();
116+
117+
// overloaded since nonterminal may have only a single child, in that case we
118+
// utilize the null node constructor
119+
NodePtr makeNode(OP o, Lexeme, Lexeme);
120+
NodePtr makeNode(OP o, Lexeme);
121+
NodePtr makeNullNode();

0 commit comments

Comments
 (0)