remove std namespace from header files
This commit is contained in:
parent
ea734dd234
commit
e3c729550e
4 changed files with 33 additions and 41 deletions
|
|
@ -3,13 +3,11 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class Error {
|
class Error {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
string message;
|
std::string message;
|
||||||
Error(string _message) : message(_message){};
|
Error(std::string _message) : message(_message){};
|
||||||
};
|
};
|
||||||
|
|
||||||
class Void {};
|
class Void {};
|
||||||
|
|
@ -26,6 +24,6 @@ template <typename R> bool is_ok(Result<R> r) {
|
||||||
template <typename R> R Ok(Result<R> r) { return std::get<R>(r); }
|
template <typename R> R Ok(Result<R> r) { return std::get<R>(r); }
|
||||||
/// enables rewrapping errors in a new Result type
|
/// enables rewrapping errors in a new Result type
|
||||||
template <typename R> Error Err(Result<R> r) { return std::get<Error>(r); }
|
template <typename R> Error Err(Result<R> r) { return std::get<Error>(r); }
|
||||||
template <typename R> string err_msg(Result<R> r) {
|
template <typename R> std::string err_msg(Result<R> r) {
|
||||||
return std::get<Error>(r).message;
|
return std::get<Error>(r).message;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,51 +8,49 @@
|
||||||
|
|
||||||
enum class ExprType { Binary, Grouping, Unary, Literal, None };
|
enum class ExprType { Binary, Grouping, Unary, Literal, None };
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
/// Base class for expressions
|
/// Base class for expressions
|
||||||
class Expression {
|
class Expression {
|
||||||
public:
|
public:
|
||||||
virtual string as_string() = 0; // get string rep for debugging
|
virtual std::string as_string() = 0; // get string rep for debugging
|
||||||
virtual ~Expression();
|
virtual ~Expression();
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An expression with two operands
|
/// An expression with two operands
|
||||||
class Binary : public Expression {
|
class Binary : public Expression {
|
||||||
unique_ptr<Expression> left;
|
std::unique_ptr<Expression> left;
|
||||||
unique_ptr<Token> op;
|
std::unique_ptr<Token> op;
|
||||||
unique_ptr<Expression> right;
|
std::unique_ptr<Expression> right;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
string as_string() override;
|
std::string as_string() override;
|
||||||
Binary(Expression *_left, Token *_operator, Expression *_right);
|
Binary(Expression *_left, Token *_operator, Expression *_right);
|
||||||
~Binary();
|
~Binary();
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An expression between parentheses
|
/// An expression between parentheses
|
||||||
class Grouping : public Expression {
|
class Grouping : public Expression {
|
||||||
unique_ptr<Expression> expr;
|
std::unique_ptr<Expression> expr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
string as_string() override;
|
std::string as_string() override;
|
||||||
Grouping(Expression *_expr);
|
Grouping(Expression *_expr);
|
||||||
~Grouping();
|
~Grouping();
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An expression with one operand (operator is `-` or `!`)
|
/// An expression with one operand (operator is `-` or `!`)
|
||||||
class Unary : public Expression {
|
class Unary : public Expression {
|
||||||
unique_ptr<Token> op;
|
std::unique_ptr<Token> op;
|
||||||
unique_ptr<Expression> right;
|
std::unique_ptr<Expression> right;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
string as_string() override;
|
std::string as_string() override;
|
||||||
Unary(Token *_operator, Expression *_right);
|
Unary(Token *_operator, Expression *_right);
|
||||||
~Unary();
|
~Unary();
|
||||||
};
|
};
|
||||||
|
|
||||||
/// empty class that is the type of the Nil value
|
/// empty class that is the type of the Nil value
|
||||||
class NilType {};
|
class NilType {};
|
||||||
typedef std::variant<double_t, bool, string, NilType> Value;
|
typedef std::variant<double_t, bool, std::string, NilType> Value;
|
||||||
|
|
||||||
/// encapsulates a value: numeric, string etc
|
/// encapsulates a value: numeric, string etc
|
||||||
class Literal : public Expression {
|
class Literal : public Expression {
|
||||||
|
|
@ -63,15 +61,15 @@ public:
|
||||||
|
|
||||||
Literal(NilType v) : value(v){};
|
Literal(NilType v) : value(v){};
|
||||||
Literal(double_t _numeric) : value(_numeric){};
|
Literal(double_t _numeric) : value(_numeric){};
|
||||||
Literal(string _str) : value(_str){};
|
Literal(std::string _str) : value(_str){};
|
||||||
Literal(bool _boolean) : value(_boolean){};
|
Literal(bool _boolean) : value(_boolean){};
|
||||||
|
|
||||||
string as_string() override;
|
std::string as_string() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Parser {
|
class Parser {
|
||||||
vector<Expression> expressions;
|
std::vector<Expression> expressions;
|
||||||
vector<Token> tokens;
|
std::vector<Token> tokens;
|
||||||
int current_token;
|
int current_token;
|
||||||
|
|
||||||
/// returns the current token without moving the pointer;
|
/// returns the current token without moving the pointer;
|
||||||
|
|
@ -92,9 +90,9 @@ class Parser {
|
||||||
/// checks if the current token is of the specified type and
|
/// checks if the current token is of the specified type and
|
||||||
/// moves the token forward if so, otherwise throws an exception with
|
/// moves the token forward if so, otherwise throws an exception with
|
||||||
/// the specified message
|
/// the specified message
|
||||||
Result<Token *> consume(Token::Type typ, string message);
|
Result<Token *> consume(Token::Type typ, std::string message);
|
||||||
/// throws an exception for the specified token with the specified message
|
/// throws an exception for the specified token with the specified message
|
||||||
Error error(Token token, string message);
|
Error error(Token token, std::string message);
|
||||||
/// tries to parse the token as a primary value (string, number etc)
|
/// tries to parse the token as a primary value (string, number etc)
|
||||||
Result<Expression *> primary();
|
Result<Expression *> primary();
|
||||||
/// tries to parse the tokens as a unary expression
|
/// tries to parse the tokens as a unary expression
|
||||||
|
|
@ -113,5 +111,5 @@ class Parser {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// public method for parsing expressions
|
/// public method for parsing expressions
|
||||||
Result<Expression *> parse(vector<Token> tokenlist);
|
Result<Expression *> parse(std::vector<Token> tokenlist);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,9 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool had_error;
|
bool had_error;
|
||||||
vector<Token> token_list;
|
std::vector<Token> token_list;
|
||||||
} ScanResult;
|
} ScanResult;
|
||||||
|
|
||||||
class Scanner {
|
class Scanner {
|
||||||
|
|
@ -19,15 +17,15 @@ private:
|
||||||
size_t current_pos;
|
size_t current_pos;
|
||||||
int start;
|
int start;
|
||||||
int current_line;
|
int current_line;
|
||||||
string source;
|
std::string source;
|
||||||
vector<Token> token_list;
|
std::vector<Token> token_list;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Scanner(string s);
|
Scanner(std::string s);
|
||||||
Result<vector<Token>> scan_tokens();
|
Result<std::vector<Token>> scan_tokens();
|
||||||
Result<Void> scan_token();
|
Result<Void> scan_token();
|
||||||
void add_token(Token::Type type);
|
void add_token(Token::Type type);
|
||||||
void add_token(Token::Type type, string literal);
|
void add_token(Token::Type type, std::string literal);
|
||||||
char advance();
|
char advance();
|
||||||
void identifier();
|
void identifier();
|
||||||
void number();
|
void number();
|
||||||
|
|
@ -39,6 +37,6 @@ public:
|
||||||
bool is_alpha(char c);
|
bool is_alpha(char c);
|
||||||
bool is_alphanumeric(char c);
|
bool is_alphanumeric(char c);
|
||||||
bool is_at_end(void);
|
bool is_at_end(void);
|
||||||
void report(string message);
|
void report(std::string message);
|
||||||
void report(string where, string message);
|
void report(std::string where, std::string message);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,10 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class Token {
|
class Token {
|
||||||
public:
|
public:
|
||||||
string lexeme;
|
std::string lexeme;
|
||||||
string literal;
|
std::string literal;
|
||||||
int line;
|
int line;
|
||||||
enum Type {
|
enum Type {
|
||||||
END_OF_FILE = 0,
|
END_OF_FILE = 0,
|
||||||
|
|
@ -51,10 +49,10 @@ public:
|
||||||
WHILE = 38,
|
WHILE = 38,
|
||||||
} tokentype;
|
} tokentype;
|
||||||
|
|
||||||
string as_string();
|
std::string as_string();
|
||||||
|
|
||||||
Token(Token::Type _tokentype, std::string _lexeme, std::string _literal,
|
Token(Token::Type _tokentype, std::string _lexeme, std::string _literal,
|
||||||
int line);
|
int line);
|
||||||
};
|
};
|
||||||
|
|
||||||
string token_name(Token::Type tokentype);
|
std::string token_name(Token::Type tokentype);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue