remove std namespace from header files

This commit is contained in:
Shautvast 2024-10-27 15:45:15 +01:00
parent ea734dd234
commit e3c729550e
4 changed files with 33 additions and 41 deletions

View file

@ -3,13 +3,11 @@
#include <string>
#include <variant>
using namespace std;
class Error {
public:
string message;
Error(string _message) : message(_message){};
std::string message;
Error(std::string _message) : message(_message){};
};
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); }
/// enables rewrapping errors in a new Result type
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;
}

View file

@ -8,51 +8,49 @@
enum class ExprType { Binary, Grouping, Unary, Literal, None };
using namespace std;
/// Base class for expressions
class Expression {
public:
virtual string as_string() = 0; // get string rep for debugging
virtual std::string as_string() = 0; // get string rep for debugging
virtual ~Expression();
};
/// An expression with two operands
class Binary : public Expression {
unique_ptr<Expression> left;
unique_ptr<Token> op;
unique_ptr<Expression> right;
std::unique_ptr<Expression> left;
std::unique_ptr<Token> op;
std::unique_ptr<Expression> right;
public:
string as_string() override;
std::string as_string() override;
Binary(Expression *_left, Token *_operator, Expression *_right);
~Binary();
};
/// An expression between parentheses
class Grouping : public Expression {
unique_ptr<Expression> expr;
std::unique_ptr<Expression> expr;
public:
string as_string() override;
std::string as_string() override;
Grouping(Expression *_expr);
~Grouping();
};
/// An expression with one operand (operator is `-` or `!`)
class Unary : public Expression {
unique_ptr<Token> op;
unique_ptr<Expression> right;
std::unique_ptr<Token> op;
std::unique_ptr<Expression> right;
public:
string as_string() override;
std::string as_string() override;
Unary(Token *_operator, Expression *_right);
~Unary();
};
/// empty class that is the type of the Nil value
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
class Literal : public Expression {
@ -63,15 +61,15 @@ public:
Literal(NilType v) : value(v){};
Literal(double_t _numeric) : value(_numeric){};
Literal(string _str) : value(_str){};
Literal(std::string _str) : value(_str){};
Literal(bool _boolean) : value(_boolean){};
string as_string() override;
std::string as_string() override;
};
class Parser {
vector<Expression> expressions;
vector<Token> tokens;
std::vector<Expression> expressions;
std::vector<Token> tokens;
int current_token;
/// 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
/// moves the token forward if so, otherwise throws an exception with
/// 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
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)
Result<Expression *> primary();
/// tries to parse the tokens as a unary expression
@ -113,5 +111,5 @@ class Parser {
public:
/// public method for parsing expressions
Result<Expression *> parse(vector<Token> tokenlist);
Result<Expression *> parse(std::vector<Token> tokenlist);
};

View file

@ -6,11 +6,9 @@
#include <string>
#include <vector>
using namespace std;
typedef struct {
bool had_error;
vector<Token> token_list;
std::vector<Token> token_list;
} ScanResult;
class Scanner {
@ -19,15 +17,15 @@ private:
size_t current_pos;
int start;
int current_line;
string source;
vector<Token> token_list;
std::string source;
std::vector<Token> token_list;
public:
Scanner(string s);
Result<vector<Token>> scan_tokens();
Scanner(std::string s);
Result<std::vector<Token>> scan_tokens();
Result<Void> scan_token();
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();
void identifier();
void number();
@ -39,6 +37,6 @@ public:
bool is_alpha(char c);
bool is_alphanumeric(char c);
bool is_at_end(void);
void report(string message);
void report(string where, string message);
void report(std::string message);
void report(std::string where, std::string message);
};

View file

@ -2,12 +2,10 @@
#include <string>
using namespace std;
class Token {
public:
string lexeme;
string literal;
std::string lexeme;
std::string literal;
int line;
enum Type {
END_OF_FILE = 0,
@ -51,10 +49,10 @@ public:
WHILE = 38,
} tokentype;
string as_string();
std::string as_string();
Token(Token::Type _tokentype, std::string _lexeme, std::string _literal,
int line);
};
string token_name(Token::Type tokentype);
std::string token_name(Token::Type tokentype);