diff --git a/src/error.hpp b/src/error.hpp index c045ddc..8beae48 100644 --- a/src/error.hpp +++ b/src/error.hpp @@ -3,13 +3,11 @@ #include #include -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 bool is_ok(Result r) { template R Ok(Result r) { return std::get(r); } /// enables rewrapping errors in a new Result type template Error Err(Result r) { return std::get(r); } -template string err_msg(Result r) { +template std::string err_msg(Result r) { return std::get(r).message; } diff --git a/src/parser.hpp b/src/parser.hpp index eaea00a..d8835ed 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -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 left; - unique_ptr op; - unique_ptr right; + std::unique_ptr left; + std::unique_ptr op; + std::unique_ptr 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 expr; + std::unique_ptr 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 op; - unique_ptr right; + std::unique_ptr op; + std::unique_ptr 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 Value; +typedef std::variant 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 expressions; - vector tokens; + std::vector expressions; + std::vector 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 consume(Token::Type typ, string message); + Result 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 primary(); /// tries to parse the tokens as a unary expression @@ -113,5 +111,5 @@ class Parser { public: /// public method for parsing expressions - Result parse(vector tokenlist); + Result parse(std::vector tokenlist); }; diff --git a/src/scanner.hpp b/src/scanner.hpp index 296548b..c9fb590 100644 --- a/src/scanner.hpp +++ b/src/scanner.hpp @@ -6,11 +6,9 @@ #include #include -using namespace std; - typedef struct { bool had_error; - vector token_list; + std::vector token_list; } ScanResult; class Scanner { @@ -19,15 +17,15 @@ private: size_t current_pos; int start; int current_line; - string source; - vector token_list; + std::string source; + std::vector token_list; public: - Scanner(string s); - Result> scan_tokens(); + Scanner(std::string s); + Result> scan_tokens(); Result 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); }; diff --git a/src/tokens.hpp b/src/tokens.hpp index 6e3312d..1cd9b69 100644 --- a/src/tokens.hpp +++ b/src/tokens.hpp @@ -2,12 +2,10 @@ #include -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);