| 1 | // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code. |
| 2 | // Copyright (C) 1998-2024 Marco Baye |
| 3 | // Have a look at "acme.c" for further info |
| 4 | // |
| 5 | // Input stuff |
| 6 | #ifndef input_H |
| 7 | #define input_H |
| 8 | |
| 9 | |
| 10 | #include <stdio.h> // for FILE |
| 11 | #include "config.h" // for bits and scope_t |
| 12 | |
| 13 | |
| 14 | // type definitions |
| 15 | |
| 16 | enum inputsrc { |
| 17 | INPUTSRC_NONE, // dummy before input is set up |
| 18 | INPUTSRC_FILE, |
| 19 | INPUTSRC_RAM |
| 20 | }; |
| 21 | struct input { |
| 22 | // the filename below (in "location") refers to the source file where |
| 23 | // the current code initially came from, i.e. it may change during macro execution. |
| 24 | struct location location; // file + line (during RAM reads as well) |
| 25 | enum inputsrc srctype; |
| 26 | int state; // state of input (type is really "enum inputstate") |
| 27 | union { |
| 28 | FILE *fd; // file descriptor |
| 29 | const char *ram_ptr; // RAM read ptr (loop or macro block) |
| 30 | } u; |
| 31 | boolean report; // copy to report file? |
| 32 | }; |
| 33 | struct filespecflags { |
| 34 | boolean uses_lib; // file name was given in <...> instead of "..." |
| 35 | boolean absolute; // file name started with '/' |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | // constants |
| 40 | |
| 41 | extern const char FILE_READBINARY[]; |
| 42 | // Special characters |
| 43 | // The program *heavily* relies on CHAR_EOS (end of statement) being 0x00! |
| 44 | #define CHAR_EOS (0) // end of statement (in high-level format) |
| 45 | #define CHAR_SOB '{' // start of block |
| 46 | #define CHAR_EOB '}' // end of block |
| 47 | #define CHAR_SOL (10) // start of line (in high-level format) |
| 48 | #define CHAR_EOF (13) // end of file (in high-level format) |
| 49 | // if the characters above are changed, don't forget to adjust global_byte_flags[]! |
| 50 | |
| 51 | |
| 52 | // variables |
| 53 | |
| 54 | extern char GotByte; // last byte read (processed) |
| 55 | // name of source file used for resolving relative paths |
| 56 | // (i.e. not changed during macro execution): |
| 57 | extern const char *input_plat_pathref_filename; // file name in platform format |
| 58 | |
| 59 | |
| 60 | |
| 61 | // prototypes |
| 62 | |
| 63 | // get next byte from currently active byte source in shortened high-level |
| 64 | // format. When inside quotes, use input_quoted_to_dynabuf() instead! |
| 65 | extern char GetByte(void); |
| 66 | |
| 67 | // skip remainder of statement, for example on error |
| 68 | extern void parser_skip_remainder(void); |
| 69 | |
| 70 | // ensure that the remainder of the current statement is empty, for example |
| 71 | // after mnemonics using implied addressing. |
| 72 | extern void parser_ensure_EOS(void); |
| 73 | |
| 74 | // process backslash escapes in GlobalDynaBuf (so size might shrink) |
| 75 | // returns 1 on escaping errors |
| 76 | extern int unescape_dynabuf(void); |
| 77 | |
| 78 | // clear dynabuf, read string to it until closing quote is found, then process |
| 79 | // backslash escapes (so size might shrink) |
| 80 | // returns 1 on error (unterminated or escaping errors) |
| 81 | extern int input_read_string_literal(char closing_quote); |
| 82 | |
| 83 | // clear dynabuf, read remainder of statement into it, making sure to keep quoted stuff intact |
| 84 | extern void input_read_statement(char terminator); |
| 85 | |
| 86 | // Skip block (starting with next byte, so call directly after reading opening brace). |
| 87 | // After calling this function, GotByte holds '}'. Unless EOF was found first, |
| 88 | // but then a serious error would have been thrown. |
| 89 | extern void input_block_skip(void); |
| 90 | // Read block into GlobalDynabuf, make a copy and store pointer in struct. |
| 91 | // (reading starts with next byte, so call directly after reading opening brace). |
| 92 | // After calling this function, GotByte holds '}'. Unless EOF was found first, |
| 93 | // but then a serious error would have been thrown. |
| 94 | extern void input_block_getcopy(struct block *block); |
| 95 | |
| 96 | // append optional '.'/'@' prefix to GlobalDynaBuf, then keep |
| 97 | // appending while characters are legal for keywords. |
| 98 | // throw "Expected symbol name" error if none. |
| 99 | // return whether there was an error. |
| 100 | extern int input_append_symbol_name_to_global_dynabuf(void); |
| 101 | |
| 102 | // FIXME - move these to "symbol.h" and remove dependency on "scope": |
| 103 | // read symbol name into GlobalDynaBuf, set scope, |
| 104 | // return whether there was an error (namely, "Expected symbol name.") |
| 105 | extern int input_readscopeandsymbolname(scope_t *scope, boolean dotkluge); |
| 106 | #define input_read_scope_and_symbol_name(scope) input_readscopeandsymbolname(scope, FALSE) |
| 107 | #define input_read_scope_and_symbol_name_KLUGED(scope) input_readscopeandsymbolname(scope, TRUE) |
| 108 | |
| 109 | // Clear dynamic buffer, then append to it until an illegal (for a keyword) |
| 110 | // character is read. Zero-terminate the string. Return its length (without |
| 111 | // terminator). |
| 112 | // Zero lengths will produce an error. |
| 113 | extern int parser_read_keyword(void); |
| 114 | |
| 115 | // Clear dynamic buffer, then append to it until an illegal (for a keyword) |
| 116 | // character is read. Zero-terminate the string, then convert to lower case. |
| 117 | // Return its length (without terminator). |
| 118 | // Zero lengths will produce an "Expected keyword" error. |
| 119 | extern int parser_read_and_lower_keyword(void); |
| 120 | |
| 121 | // try to read a file name for an input file. |
| 122 | // library access by using <...> quoting is allowed. |
| 123 | // flags for "library access" and "absolute path" will be set accordingly. |
| 124 | // The file name given in the assembler source code is converted from |
| 125 | // UNIX style to platform style. |
| 126 | // on success, returns zero. filename is in GlobalDynaBuf, caller should then |
| 127 | // call includepaths_open_ro(). |
| 128 | // on error, returns nonzero. errors are handled and reported, but caller should |
| 129 | // then call parser_skip_remainder(). |
| 130 | extern int input_read_input_filename(struct filespecflags *flags); |
| 131 | |
| 132 | // try to read a file name for an output file ("!to" and "!sl" only). |
| 133 | // library access by using <...> quoting is forbidden. |
| 134 | // The file name given in the assembler source code is converted from |
| 135 | // UNIX style to platform style. |
| 136 | // on success, returns zero. filename is in GlobalDynaBuf, caller should then |
| 137 | // make a copy to pass it to fopen() later on. |
| 138 | // on error, returns nonzero. errors are handled and reported, but caller should |
| 139 | // then call parser_skip_remainder(). |
| 140 | // FIXME - the name suggests this fn reads "the" output filename, but it only |
| 141 | // reads "an" output filename: either symbollist or the real output file. |
| 142 | extern int input_read_output_filename(void); |
| 143 | |
| 144 | // Try to read a comma, skipping spaces before and after. Return TRUE if comma |
| 145 | // found, otherwise FALSE. |
| 146 | extern int parser_accept_comma(void); |
| 147 | |
| 148 | // Try to read given character. |
| 149 | // If found, eat character and return TRUE. |
| 150 | // If not found, throw syntax error and return FALSE. |
| 151 | extern int parser_expect(int chr); |
| 152 | |
| 153 | // force input system to return "end of file" on next read |
| 154 | // (back end function for "!eof" pseudo opcode) |
| 155 | extern void input_force_eof(void); |
| 156 | |
| 157 | // enable/disable writing to report file |
| 158 | // (only enable if report file has been opened!) |
| 159 | extern void input_set_report_enabled(boolean new_state); |
| 160 | |
| 161 | // write current "location" (file name and line number) to given target |
| 162 | extern void input_get_location(struct location *target); |
| 163 | |
| 164 | |
| 165 | // "input change" stuff: |
| 166 | |
| 167 | // treat this struct as opaque, its components should only be referenced by inputchange_* functions! |
| 168 | struct inputchange_buf { |
| 169 | struct input input; |
| 170 | char gb; // buffer for GotByte |
| 171 | }; |
| 172 | // save current input struct in buffer, then switch input to new source code file |
| 173 | extern void inputchange_new_file(struct inputchange_buf *icb, FILE *fd, const char *eternal_plat_filename); |
| 174 | // save current input struct in buffer, then switch to RAM |
| 175 | extern void inputchange_new_ram(struct inputchange_buf *icb); |
| 176 | // setup for reading from RAM (for parsing loop conditions etc.) |
| 177 | extern void inputchange_set_ram(int line_num, const char *body); |
| 178 | // switch input to macro parameters |
| 179 | extern void inputchange_macro1_params(const struct location *def, const char *params); |
| 180 | // switch from macro parameters to macro body |
| 181 | extern void inputchange_macro2_body(const char *macro_body); |
| 182 | // restore input struct from buffer |
| 183 | extern void inputchange_back(const struct inputchange_buf *icb); |
| 184 | |
| 185 | |
| 186 | // "include path" stuff: |
| 187 | |
| 188 | // add entry |
| 189 | extern void includepaths_add(const char *path); |
| 190 | |
| 191 | // open file for reading |
| 192 | // "flags" decide whether library access, search paths or absolute path is wanted. |
| 193 | // file name is expected in GlobalDynaBuf, in platform style and terminated. |
| 194 | // returns NULL or open stream |
| 195 | // on success, GlobalDynaBuf contains full file name in platform style |
| 196 | extern FILE *includepaths_open_ro(struct filespecflags *flags); |
| 197 | |
| 198 | |
| 199 | #endif |