| 1 |
#ifndef PARSER_H |
| 2 |
#define PARSER_H |
| 3 |
|
| 4 |
#include "main.h" |
| 5 |
#include "token.h" |
| 6 |
#include "utils/hash.h" |
| 7 |
#include "utils/vector.h" |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdint.h> |
| 10 |
|
| 11 |
typedef struct |
| 12 |
{ |
| 13 |
hkey_t key; |
| 14 |
uint16_t code_nr; |
| 15 |
// when key.string != NULL, key stores the label string |
| 16 |
} label_t; |
| 17 |
|
| 18 |
typedef struct |
| 19 |
{ |
| 20 |
token_type type; |
| 21 |
uint32_t data; |
| 22 |
string_t literal; |
| 23 |
// store idx for MEM | ATTR_LOWARG | ATTR_HIGHARG |
| 24 |
// store value for NUMBER | TRAP | TRACE | ERRNO |
| 25 |
} obj_t; |
| 26 |
|
| 27 |
typedef struct |
| 28 |
{ |
| 29 |
obj_t left_var; |
| 30 |
token_type operator; |
| 31 |
obj_t right_var; |
| 32 |
// if operator is NEGATIVE, then it should be A EQUAL NEGATIVE A |
| 33 |
// but EQUAL is skipped |
| 34 |
} assign_line_t; |
| 35 |
|
| 36 |
typedef struct |
| 37 |
{ |
| 38 |
// token_t if; |
| 39 |
// jump_line_t must starts with if, so skip it |
| 40 |
bool if_bang; |
| 41 |
// if match '!' before jump_condition |
| 42 |
bool if_condition; |
| 43 |
// does condition exists |
| 44 |
// if true, jt and jf both uint16_t |
| 45 |
// else jt is uint32_t, jf is ignored |
| 46 |
|
| 47 |
// var A; |
| 48 |
// jump always compare A with something else, so skip it |
| 49 |
token_type comparator; |
| 50 |
obj_t cmpobj; |
| 51 |
|
| 52 |
label_t jt; |
| 53 |
label_t jf; |
| 54 |
// pc += (jump_condition ? jt : jf) + 1 |
| 55 |
// pc += jt + 1 |
| 56 |
} jump_line_t; |
| 57 |
|
| 58 |
typedef struct |
| 59 |
{ |
| 60 |
// token_type return |
| 61 |
// return_line_t must have return, so skip it |
| 62 |
obj_t ret_obj; |
| 63 |
obj_t real_obj; // only used in emu quiet |
| 64 |
} return_line_t; |
| 65 |
|
| 66 |
typedef void *empty_line_t; |
| 67 |
typedef void *eof_line_t; |
| 68 |
|
| 69 |
typedef struct |
| 70 |
{ |
| 71 |
char *error_start; |
| 72 |
const char *error_msg; |
| 73 |
} error_line_t; |
| 74 |
|
| 75 |
typedef enum |
| 76 |
{ |
| 77 |
ASSIGN_LINE, |
| 78 |
JUMP_LINE, |
| 79 |
RETURN_LINE, |
| 80 |
EMPTY_LINE, |
| 81 |
EOF_LINE, |
| 82 |
ERROR_LINE, |
| 83 |
} expr_type; |
| 84 |
|
| 85 |
typedef struct |
| 86 |
{ |
| 87 |
expr_type type; |
| 88 |
string_t label_decl; |
| 89 |
uint16_t text_nr; |
| 90 |
uint16_t code_nr; |
| 91 |
char *line_start; |
| 92 |
int16_t comment; |
| 93 |
uint16_t line_len; |
| 94 |
|
| 95 |
union |
| 96 |
{ |
| 97 |
assign_line_t assign_line; |
| 98 |
jump_line_t jump_line; |
| 99 |
return_line_t return_line; |
| 100 |
empty_line_t empty_line; |
| 101 |
eof_line_t eof_line; |
| 102 |
error_line_t error_line; |
| 103 |
}; |
| 104 |
} statement_t; |
| 105 |
|
| 106 |
extern void init_parser (uint32_t scmp_arch); |
| 107 |
|
| 108 |
// see vector.h for vector details |
| 109 |
extern void parser (vector_t *text_v, vector_t *code_ptr_v); |
| 110 |
|
| 111 |
#endif |