Advertisement
microwerx

kasl.hpp

Dec 30th, 2017
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. // SSPHH/Fluxions/Unicornfish/Viperfish/Hatchetfish/Sunfish/KASL/GLUT Extensions
  2. // Copyright (C) 2017 Jonathan Metzgar
  3. // All rights reserved.
  4. //
  5. // This program is free software : you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with this program.If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. // For any other type of licensing, please contact me at jmetzgar@outlook.com
  19. #ifndef KASL_HPP
  20. #define KASL_HPP
  21.  
  22. #include <map>
  23. #include <string>
  24. #include <vector>
  25.  
  26. //#ifndef KASL_STATIC
  27. //#ifdef KASLDLLEXPORTS
  28. //#define KASL_API __declspec(dllexport)
  29. //#else
  30. //#define KASL_API __declspec(dllimport)
  31. //#endif
  32. //#else
  33. //#define KASL_API
  34. //#endif
  35.  
  36. namespace KASL
  37. {
  38.     using namespace std;
  39.     using StringType = std::string;
  40.  
  41.     enum class TokenType {
  42.         TT0_NOTHING,
  43.         TT0_CHAR,
  44.         TT0_SPACE,
  45.         TT0_TAB,
  46.         TT0_NEWLINE,
  47.         TT0_DIGIT,
  48.         TT0_ALPHA,
  49.         TT0_CNTRL,
  50.         TT0_PUNCT,
  51.         TT1_EXCLAMATION_POINT,
  52.         TT1_DOUBLE_QUOTE,
  53.         TT1_NUMBER_SIGN,
  54.         TT1_DOLLAR_SIGN,
  55.         TT1_PERCENT_SIGN,
  56.         TT1_AMPERSAND,
  57.         TT1_SINGLE_QUOTE,
  58.         TT1_LPAREN,
  59.         TT1_RPAREN,
  60.         TT1_ASTERICK,
  61.         TT1_PLUS_SIGN,
  62.         TT1_COMMA,
  63.         TT1_DASH,
  64.         TT1_PERIOD,
  65.         TT1_FORWARD_SLASH,
  66.         TT1_COLON,
  67.         TT1_SEMICOLON,
  68.         TT1_LESS_THAN_SIGN,
  69.         TT1_EQUAL_SIGN,
  70.         TT1_GREATER_THAN_SIGN,
  71.         TT1_QUESTION_MARK,
  72.         TT1_AT_SYMBOL,
  73.         TT1_LBRACKET,
  74.         TT1_BACK_SLASH,
  75.         TT1_RBRACKET,
  76.         TT1_CARET,
  77.         TT1_UNDERSCORE,
  78.         TT1_GRAVE_ACCENT,
  79.         TT1_LBRACE,
  80.         TT1_VERTICAL_BAR,
  81.         TT1_RBRACE,
  82.         TT1_TILDE,
  83.         TT2_ID,
  84.         TT2_STRING,
  85.         TT2_DOUBLE,
  86.         TT2_INTEGER,
  87.         TT2_HEXINTEGER
  88.     };
  89.  
  90.     struct LexerToken
  91.     {
  92.         TokenType type;
  93.  
  94.         size_t start;
  95.         size_t length;
  96.         char *cptr;
  97.     };
  98.  
  99.     struct LexerState
  100.     {
  101.         StringType buffer;
  102.         vector<LexerToken> tokens;
  103.  
  104.         LexerToken tmptoken;
  105.         LexerToken *lasttoken;
  106.         bool eatChar;
  107.     };
  108.  
  109.     struct Token
  110.     {
  111.         TokenType type;
  112.         string sval;
  113.         int ival;
  114.         double dval;
  115.  
  116.         bool IsInteger() const { return type == TokenType::TT2_INTEGER; }
  117.         bool IsDouble() const { return type == TokenType::TT2_DOUBLE; }
  118.         bool IsString() const { return type == TokenType::TT2_STRING; }
  119.         bool IsIdentifier() const { return type == TokenType::TT2_ID; }
  120.         bool IsStringOrIdentifier() const { return type == TokenType::TT2_STRING || type == TokenType::TT2_ID; }
  121.         bool IsIntegerOrDouble() const { return type == TokenType::TT2_INTEGER || type == TokenType::TT2_DOUBLE; }
  122.         bool IsType(TokenType which) const { return type == which; }
  123.     };
  124.  
  125.     using LexReplacementType = tuple<TokenType, TokenType, StringType::value_type>;
  126.     using TokenVector = vector<Token>;
  127.  
  128.     ostream & operator << (ostream & ostr, const TokenType & type);
  129.     size_t lex_init(LexerState &ls);
  130.     size_t lex(LexerState &ls, const StringType &inputStr, int level = 0);
  131.     void lex_print(LexerState &ls);
  132.     void lex_replace(LexerState &ls, LexerState &output, const vector<LexReplacementType> &replacements);
  133.     size_t lex_scan_number(StringType::value_type *cptr, LexerToken &token);
  134.     size_t lex_scan_string(StringType::value_type *cptr, LexerToken &token);
  135.     size_t lex_tokens(LexerState &ls, vector<Token> & symbols);
  136.     void lex_tokens_print(vector<Token> &tokens);
  137.     size_t lex_quick_parse(const StringType &inputStr, int level, const vector<LexReplacementType> &replacements, TokenVector &tokens);
  138.     size_t lex_quick_parse(const StringType &inputStr, TokenVector &tokens);
  139.     size_t lex_quick_l2_parse(const StringType &inputStr, TokenVector &tokens);
  140.     size_t lex_quick_l3_parse(const StringType &inputStr, TokenVector &tokens);
  141.     string TokenVectorJoin(const TokenVector tokens, const string &separator);
  142.  
  143.     class VariableList
  144.     {
  145.     private:
  146.         string blankString;
  147.     public:
  148.         VariableList();
  149.         ~VariableList();
  150.  
  151.         map<string, KASL::Token> variables;
  152.         double get_var_double(const string &name) const;
  153.         int get_var_integer(const string &name) const;
  154.         const string &get_var_string(const string &name) const;
  155.         void set_var(const string &name, double dval);
  156.         void set_var(const string &name, int ival);
  157.         void set_var(const string &name, const string &sval);
  158.         bool is_var(const string &name) const;
  159.         map<string, KASL::Token>::const_iterator get_var(const string &name) const;
  160.         map<string, KASL::Token>::const_iterator get_var_end() const;
  161.     };
  162. }
  163.  
  164. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement