2 * parser.c -- part of ZilUtils/ZilAsm
4 * Copyright (C) 2016 Jason Self <j@jxself.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
20 #include <stdio.h> /* fopen, fgets */
21 #include <string.h> /* strlen */
24 #include "directives.h"
28 #define iscomment(c) ((c) == '#')
29 #define isbindigit(c) ((c) == '0' || (c) == '1')
32 #define fatal_error(errmsg)
35 void checksep(const char *p)
37 if (!*p || iscomment(*p) || isspace(*p)) return;
38 fatal_error("wrong chars");
41 const char *pass_spaces(const char *p)
43 while(p && isspace(*p)) p++;
44 return (p && *p) ? p : NULL;
47 const char *pass_alnums(const char *p)
49 while(p && isalnum(*p)) p++;
50 return (p && *p) ? p : NULL;
53 int tryparse_directive(const char *p)
58 const char *b = pass_alnums(a);
60 Directive_handler f = directive_lookup(a, b - a);
65 int tryparse_assignment(const char *a, const char *b, const char *c)
70 int tryparse_label(const char *a, const char *b, const char *c)
73 symtable_add2(Local_labels, a, b - a, PC);
74 } else if (*(c+2) != ':') {
75 symtable_add2(Global_labels, a, b - a, PC);
77 fatal_error("wrong label type");
81 if (*c && ((c = pass_spaces(c)) != NULL) && *c)
82 return tryparse_instruction(c);
86 int tryparse_name(const char *a)
88 const char *b = pass_alnums(a);
89 const char *c = pass_spaces(b);
92 if (*c == '=') return tryparse_assignment(a, b, c + 1);
93 if (*c == ':') return tryparse_label(a, b, c);
97 int tryparse_instruction(const char *a)
99 const char *b = pass_alnums(a);
100 ZOpcode *op = symtable_lookup2(Opcodes, a, b - a);
102 ZOpcode_flags flags = op->flags;
108 * Line can be one from: Comment, Global label, Local label, Directive, Name=Value, Instruction
110 int parse_line(const char *p)
115 if (isspace(c)) continue;
116 if (iscomment(c)) return 0;
117 if (n = tryparse_directive(p)) return n;
118 if (n = tryparse_name(p)) return n; // ..label or assignment
119 if (n = tryparse_instruction(p)) return n;
120 fatal_error("wrong line");
125 int parse_file(const char *filename)
127 FILE *fp = fopen(filename, "r");
128 if (!fp) fatal_error("wrong file");
130 const int MAX_LINESIZE = 1024;
131 char line[MAX_LINESIZE];
132 int newline_missing = 0;
134 while (fgets(line, MAX_LINESIZE, fp)) {
135 if (newline_missing) fatal_error("line too long");
137 int n = strlen(line);
142 newline_missing = (line[n-1] != '\n');
152 return (current_token == LINE_END || current_token == LINE_COMMENT);
155 if (line_passed()) continue;
156 if (current_token == DIRECTIVE) {
157 if (!try_next_token(NAME))
158 fatal_error("directive contains incorrect chars")
159 handler = get_directive_handler(current_token);
161 fatal error("unknown directive");
162 (*handler)(remaining_line);
163 if (line_passed()) continue;
164 fatal_error("unexpected line tail");
165 } else if (current_token == NAME) {
167 if (current_token == ASSIGNMENT)