Generate bytecode while parsing
This commit is contained in:
+104
-112
@@ -18,121 +18,120 @@
|
||||
|
||||
#include "globals.h"
|
||||
|
||||
typedef uint8_t string_id;
|
||||
|
||||
/* Keywords. */
|
||||
typedef enum
|
||||
{
|
||||
/* Not a keyword. */
|
||||
KW_NONE = 0,
|
||||
/* Future reserved keyword. */
|
||||
KW_RESERVED,
|
||||
typedef uint8_t keyword;
|
||||
|
||||
KW_BREAK,
|
||||
KW_CASE,
|
||||
KW_CATCH,
|
||||
KW_CONTINUE,
|
||||
KW_DEBUGGER,
|
||||
KW_DEFAULT,
|
||||
KW_DELETE,
|
||||
/* Not a keyword. */
|
||||
#define KW_NONE 0
|
||||
/* Future reserved keyword. */
|
||||
#define KW_RESERVED 1
|
||||
#define KW_BREAK 2
|
||||
#define KW_CASE 3
|
||||
#define KW_CATCH 4
|
||||
|
||||
KW_DO,
|
||||
KW_ELSE,
|
||||
KW_FINALLY,
|
||||
KW_FOR,
|
||||
KW_FUNCTION,
|
||||
KW_IF,
|
||||
KW_IN,
|
||||
#define KW_CONTINUE 5
|
||||
#define KW_DEBUGGER 6
|
||||
#define KW_DEFAULT 7
|
||||
#define KW_DELETE 8
|
||||
#define KW_DO 9
|
||||
|
||||
KW_INSTANCEOF,
|
||||
KW_NEW,
|
||||
KW_RETURN,
|
||||
KW_SWITCH,
|
||||
KW_THIS,
|
||||
KW_THROW,
|
||||
KW_TRY,
|
||||
#define KW_ELSE 10
|
||||
#define KW_FINALLY 11
|
||||
#define KW_FOR 12
|
||||
#define KW_FUNCTION 13
|
||||
#define KW_IF 14
|
||||
|
||||
#define KW_IN 15
|
||||
#define KW_INSTANCEOF 16
|
||||
#define KW_NEW 17
|
||||
#define KW_RETURN 18
|
||||
#define KW_SWITCH 19
|
||||
|
||||
#define KW_THIS 20
|
||||
#define KW_THROW 21
|
||||
#define KW_TRY 22
|
||||
#define KW_TYPEOF 23
|
||||
#define KW_VAR 24
|
||||
|
||||
#define KW_VOID 25
|
||||
#define KW_WHILE 26
|
||||
#define KW_WITH 27
|
||||
|
||||
KW_TYPEOF,
|
||||
KW_VAR,
|
||||
KW_VOID,
|
||||
KW_WHILE,
|
||||
KW_WITH
|
||||
}
|
||||
keyword;
|
||||
|
||||
/* Type of tokens. */
|
||||
typedef enum
|
||||
{
|
||||
TOK_EOF = 0x0, // End of file
|
||||
TOK_NAME = 0x1, // Identifier
|
||||
TOK_KEYWORD = 0x2, // Keyword
|
||||
TOK_INT = 0x3,
|
||||
TOK_FLOAT = 0x4,
|
||||
TOK_NULL = 0x5,
|
||||
TOK_BOOL = 0x6,
|
||||
TOK_NEWLINE = 0x7,
|
||||
TOK_STRING = 0x8,
|
||||
typedef uint8_t token_type;
|
||||
|
||||
/* Punctuators. */
|
||||
TOK_OPEN_BRACE = 0x9, // {
|
||||
TOK_CLOSE_BRACE = 0xa, // }
|
||||
TOK_OPEN_PAREN = 0xb, // (
|
||||
TOK_CLOSE_PAREN = 0xc, // )
|
||||
TOK_OPEN_SQUARE, // [
|
||||
TOK_CLOSE_SQUARE, // [
|
||||
#define TOK_EOF 0 // End of file
|
||||
#define TOK_NAME 1 // Identifier
|
||||
#define TOK_KEYWORD 2 // Keyword
|
||||
#define TOK_INT 3
|
||||
#define TOK_FLOAT 4
|
||||
|
||||
TOK_DOT, // .
|
||||
TOK_SEMICOLON, // ;
|
||||
TOK_COMMA, // ,
|
||||
TOK_LESS, // <
|
||||
TOK_GREATER, // >
|
||||
TOK_LESS_EQ, // <=
|
||||
#define TOK_NULL 5
|
||||
#define TOK_BOOL 6
|
||||
#define TOK_NEWLINE 7
|
||||
#define TOK_STRING 8
|
||||
#define TOK_OPEN_BRACE 9 // {
|
||||
|
||||
TOK_GREATER_EQ, // <=
|
||||
TOK_DOUBLE_EQ, // ==
|
||||
TOK_NOT_EQ, // !=
|
||||
TOK_TRIPLE_EQ, // ===
|
||||
TOK_NOT_DOUBLE_EQ, // !==
|
||||
#define TOK_CLOSE_BRACE 10 // }
|
||||
#define TOK_OPEN_PAREN 11 // (
|
||||
#define TOK_CLOSE_PAREN 12 // )
|
||||
#define TOK_OPEN_SQUARE 13 // [
|
||||
#define TOK_CLOSE_SQUARE 14 // [
|
||||
|
||||
TOK_PLUS, // +
|
||||
TOK_MINUS, // -
|
||||
TOK_MULT, // *
|
||||
TOK_MOD, // %
|
||||
TOK_DOUBLE_PLUS, // ++
|
||||
TOK_DOUBLE_MINUS, // --
|
||||
#define TOK_DOT 15 // .
|
||||
#define TOK_SEMICOLON 16 // ;
|
||||
#define TOK_COMMA 17 // ,
|
||||
#define TOK_LESS 18 // <
|
||||
#define TOK_GREATER 19 // >
|
||||
|
||||
TOK_LSHIFT, // <<
|
||||
TOK_RSHIFT, // >>
|
||||
TOK_RSHIFT_EX, // >>>
|
||||
TOK_AND, // &
|
||||
TOK_OR, // |
|
||||
TOK_XOR, // ^
|
||||
#define TOK_LESS_EQ 20 // <=
|
||||
#define TOK_GREATER_EQ 21 // <=
|
||||
#define TOK_DOUBLE_EQ 22 // ==
|
||||
#define TOK_NOT_EQ 23 // !=
|
||||
#define TOK_TRIPLE_EQ 24 // ===
|
||||
|
||||
TOK_NOT, // !
|
||||
TOK_COMPL, // ~
|
||||
TOK_DOUBLE_AND, // &&
|
||||
TOK_DOUBLE_OR, // ||
|
||||
TOK_QUERY, // ?
|
||||
TOK_COLON, // :
|
||||
#define TOK_NOT_DOUBLE_EQ 25 // !==
|
||||
#define TOK_PLUS 26 // +
|
||||
#define TOK_MINUS 27 // -
|
||||
#define TOK_MULT 28 // *
|
||||
#define TOK_MOD 29 // %
|
||||
|
||||
TOK_EQ, // =
|
||||
TOK_PLUS_EQ, // +=
|
||||
TOK_MINUS_EQ, // -=
|
||||
TOK_MULT_EQ, // *=
|
||||
TOK_MOD_EQ, // %=
|
||||
TOK_LSHIFT_EQ, // <<=
|
||||
#define TOK_DOUBLE_PLUS 30 // ++
|
||||
#define TOK_DOUBLE_MINUS 31 // --
|
||||
#define TOK_LSHIFT 32 // <<
|
||||
#define TOK_RSHIFT 33 // >>
|
||||
#define TOK_RSHIFT_EX 34 // >>>
|
||||
|
||||
TOK_RSHIFT_EQ, // >>=
|
||||
TOK_RSHIFT_EX_EQ, // >>>=
|
||||
TOK_AND_EQ, // &=
|
||||
TOK_OR_EQ, // |=
|
||||
TOK_XOR_EQ, // ^=
|
||||
#define TOK_AND 35 // &
|
||||
#define TOK_OR 36 // |
|
||||
#define TOK_XOR 37 // ^
|
||||
#define TOK_NOT 38 // !
|
||||
#define TOK_COMPL 39 // ~
|
||||
|
||||
#define TOK_DOUBLE_AND 40 // &&
|
||||
#define TOK_DOUBLE_OR 41 // ||
|
||||
#define TOK_QUERY 42 // ?
|
||||
#define TOK_COLON 43 // :
|
||||
#define TOK_EQ 44 // =
|
||||
|
||||
#define TOK_PLUS_EQ 45 // +=
|
||||
#define TOK_MINUS_EQ 46 // -=
|
||||
#define TOK_MULT_EQ 47 // *=
|
||||
#define TOK_MOD_EQ 48 // %=
|
||||
#define TOK_LSHIFT_EQ 49 // <<=
|
||||
|
||||
#define TOK_RSHIFT_EQ 50 // >>=
|
||||
#define TOK_RSHIFT_EX_EQ 51 // >>>=
|
||||
#define TOK_AND_EQ 52 // &=
|
||||
#define TOK_OR_EQ 53 // |=
|
||||
#define TOK_XOR_EQ 54 // ^=
|
||||
|
||||
#define TOK_DIV 55 // /
|
||||
#define TOK_DIV_EQ 56 // /=
|
||||
#define TOK_EMPTY 57
|
||||
|
||||
TOK_DIV, // /
|
||||
TOK_DIV_EQ, // /=
|
||||
TOK_EMPTY
|
||||
}
|
||||
token_type;
|
||||
|
||||
/* Represents the contents of a token. */
|
||||
typedef struct
|
||||
@@ -140,17 +139,12 @@ typedef struct
|
||||
token_type type;
|
||||
|
||||
union
|
||||
{
|
||||
void *none;
|
||||
keyword kw;
|
||||
string_id name;
|
||||
bool is_true;
|
||||
int num;
|
||||
float fp_num;
|
||||
string_id str;
|
||||
}
|
||||
{
|
||||
keyword kw;
|
||||
uint8_t uid;
|
||||
}
|
||||
data;
|
||||
}
|
||||
} __packed
|
||||
token;
|
||||
|
||||
#ifdef __HOST
|
||||
@@ -160,11 +154,9 @@ void lexer_set_source (const char *);
|
||||
#endif
|
||||
token lexer_next_token (void);
|
||||
void lexer_save_token (token);
|
||||
|
||||
void lexer_dump_buffer_state (void);
|
||||
|
||||
uint8_t lexer_get_strings (const char **);
|
||||
|
||||
const char *lexer_get_string_by_id (string_id id);
|
||||
const char *lexer_get_string_by_id (uint8_t);
|
||||
uint8_t lexer_get_nums (int *);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user