website

Website contents
git clone git://git.reagancfischer.dev/website.git
Log | Files | Refs

token.h (3370B)


      1 /* token.h */
      2 #ifndef TOKEN_H
      3 #define TOKEN_H
      4 #include <stdint.h> // We use this for int64_t
      5 #include "hash_table.h" // We need this for the string table
      6 /* Token Types */
      7 typedef enum {
      8   // Control Keywords
      9   TOK_IF,
     10   TOK_ELSE,
     11   TOK_SWITCH,
     12   TOK_CASE,
     13   TOK_DEFAULT,
     14   TOK_WHILE,
     15   TOK_DO,
     16   TOK_FOR,
     17   TOK_CONTINUE,
     18   TOK_BREAK,
     19   TOK_RETURN,
     20   TOK_GOTO,
     21 
     22   // Type Keywords
     23   TOK_VOID,
     24   TOK_CHAR,
     25   TOK_SHORT,
     26   TOK_INT,
     27   TOK_LONG,
     28   TOK_FLOAT,
     29   TOK_DOUBLE,
     30   TOK_SIGNED,
     31   TOK_UNSIGNED,
     32   TOK_STRUCT,
     33   TOK_UNION,
     34   TOK_ENUM,
     35   TOK_TYPEDEF,
     36 
     37   // Storage Class/Specifier Keywords
     38   TOK_AUTO,
     39   TOK_REGISTER,
     40   TOK_STATIC,
     41   TOK_EXTERN,
     42   TOK_CONST,
     43   TOK_VOLATILE,
     44 
     45   // Misc Keywords
     46   TOK_SIZEOF,
     47 
     48   // Operators
     49   TOK_ADD,            // +
     50   TOK_SUB,            // -
     51   TOK_MUL,            // *
     52   TOK_DIV,            // /
     53   TOK_MOD,            // %
     54   TOK_BIT_AND,        // &
     55   TOK_BIT_OR,         // |
     56   TOK_BIT_XOR,        // ^
     57   TOK_BIT_NOT,        // ~
     58   TOK_LSHIFT,         // <<
     59   TOK_RSHIFT,         // >>
     60   TOK_NOT,            // !
     61   TOK_ASSIGN,         // =
     62   TOK_LT,             // <
     63   TOK_GT,             // >
     64   TOK_INC,            // ++
     65   TOK_DEC,            // --
     66   TOK_EQ,             // ==
     67   TOK_NE,             // !=
     68   TOK_LE,             // <=
     69   TOK_GE,             // >=
     70   TOK_AND,            // &&
     71   TOK_OR,             // ||
     72   TOK_MEMBER_POINTER, // ->
     73   TOK_MEMBER,         // .
     74   TOK_COND_DECISION,  // :
     75   TOK_COND,           // ?
     76   TOK_ASSIGN_ADD,     // +=
     77   TOK_ASSIGN_SUB,     // -=
     78   TOK_ASSIGN_MUL,     // *=
     79   TOK_ASSIGN_DIV,     // /=
     80   TOK_ASSIGN_MOD,     // %=
     81   TOK_ASSIGN_BITAND,  // &=
     82   TOK_ASSIGN_BITOR,   // |=
     83   TOK_ASSIGN_BITXOR,  // ^=
     84   TOK_ASSIGN_LSHIFT,  // <<=
     85   TOK_ASSIGN_RSHIFT,  // >>=
     86 
     87   // Separators
     88   TOK_LEFT_PAREN,    // (
     89   TOK_RIGHT_PAREN,   // )
     90   TOK_LEFT_BRACKET,  // [
     91   TOK_RIGHT_BRACKET, // ]
     92   TOK_LEFT_BRACE,    // {
     93   TOK_RIGHT_BRACE,   // }
     94   TOK_COMMA,         // ,
     95   TOK_SEMICOLON,     // ;
     96   TOK_DOT,           // .
     97   TOK_ELLIPSIS,      // ...
     98   TOK_HASH,          // #
     99 
    100   // Identifiers
    101   TOK_ID,
    102   TOK_TYPEDEF_NAME,
    103 
    104   // Constants
    105   TOK_INTEGER_U32,  // u
    106   TOK_INTEGER_U64,  // ul
    107   TOK_INTEGER_S32,  // (no suffix)
    108   TOK_INTEGER_S64,  // l
    109   TOK_FLOAT_32,     // f
    110   TOK_FLOAT_64,     // (no suffix)
    111   TOK_CHAR_CONST,         // 'c'
    112   TOK_STRING_ASCII, // "string" (width of 8 bits)
    113 
    114   // Special
    115   TOK_EOF,
    116   TOK_ERROR,
    117 } c_token_types;
    118 
    119 /* Opaque Token Type */
    120 typedef struct token token_t;
    121 
    122 /* Token Creation and Destruction Interface */
    123 token_t *token_create(c_token_types kind, int lin, int col, int len);
    124 token_t *token_create_int(c_token_types kind, int lin, int col, int64_t i, int len);
    125 token_t *token_create_float(c_token_types kind, int lin, int col, double f, int len);
    126 token_t *token_create_char(c_token_types kind, int lin, int col, char c, int len);
    127 token_t *token_create_string(c_token_types kind, int lin, int col, const char *s, int len);
    128 void token_destroy(token_t *token);
    129 
    130 /* Token Interface */
    131 c_token_types token_type(token_t *token);
    132 int64_t token_int(token_t *token);
    133 double token_float(token_t *token);
    134 const char *token_string(token_t *token);
    135 char token_char(token_t *token);
    136 int token_line(token_t *token);
    137 int token_column(token_t *token);
    138 void print_token(token_t *tok);
    139 
    140 extern hash_table_t *string_table;
    141 extern int column;
    142 extern int line;
    143 #endif
    144