Updating to reflect the latest work.
[zilutils.git] / zilasm / parser.c
1 /*
2  * parser.c -- part of ZilUtils/ZilAsm
3  *
4  * Copyright (C) 2016 Jason Self <j@jxself.org>
5  *
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.
10  *
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.
15  *
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/>
18  */
19
20 #include <stdio.h>   /* fopen, fgets */
21 #include <string.h>  /* strlen */
22
23 #include "parser.h"
24 #include "directives.h"
25 #include "opcodes.h"
26 #include "labels.h"
27
28 #define iscomment(c)   ((c) == '#')
29 #define isbindigit(c)  ((c) == '0' || (c) == '1')
30
31 /* !!! TODO !!! */
32 #define fatal_error(errmsg)
33 #define PC NULL
34
35 void checksep(const char *p)
36 {
37         if (!*p || iscomment(*p) || isspace(*p)) return;
38         fatal_error("wrong chars");
39 }
40
41 const char *pass_spaces(const char *p)
42 {
43         while(p && isspace(*p)) p++;
44         return (p && *p) ? p : NULL;
45 }
46
47 const char *pass_alnums(const char *p)
48 {
49         while(p && isalnum(*p)) p++;
50         return (p && *p) ? p : NULL;
51 }
52
53 int tryparse_directive(const char *p)
54 {
55         if (*p != '.')
56                 return 0;
57         const char *a = p+1;
58         const char *b = pass_alnums(a);
59         checksep(b);
60         Directive_handler f = directive_lookup(a, b - a);
61         if (!f) return 0;
62         return (*f)(b);
63 }
64
65 int tryparse_assignment(const char *a, const char *b, const char *c)
66 {
67         return 0;
68 }
69
70 int tryparse_label(const char *a, const char *b, const char *c)
71 {
72         if (*(c+1) != ':') {
73                 symtable_add2(Local_labels, a, b - a, PC);
74         } else if (*(c+2) != ':') {
75                 symtable_add2(Global_labels, a, b - a, PC);
76         } else {
77                 fatal_error("wrong label type");
78         }
79
80         while (*c++ == ':');
81         if (*c && ((c = pass_spaces(c)) != NULL) && *c)
82                 return tryparse_instruction(c);
83         return 1;
84 }
85
86 int tryparse_name(const char *a)
87 {
88         const char *b = pass_alnums(a);
89         const char *c = pass_spaces(b);
90
91         if (!c)        return 0;
92         if (*c == '=') return tryparse_assignment(a, b, c + 1);
93         if (*c == ':') return tryparse_label(a, b, c);
94         return 0;
95 }
96
97 int tryparse_instruction(const char *a)
98 {
99         const char *b = pass_alnums(a);
100         ZOpcode *op = symtable_lookup2(Opcodes, a, b - a);
101         if (!op) return 0;
102         ZOpcode_flags flags = op->flags;
103         /* !!! TODO !!! */
104         return 0;
105 }
106
107 /*
108  *  Line can be one from: Comment, Global label, Local label, Directive, Name=Value, Instruction
109  */
110 int parse_line(const char *p)
111 {
112         for (; *p; p++) {
113                 char c = *p;
114                 int n;
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");
121         }
122         return 0;
123 }
124
125 int parse_file(const char *filename)
126 {
127         FILE *fp = fopen(filename, "r");
128         if (!fp) fatal_error("wrong file");
129
130         const int MAX_LINESIZE = 1024;
131         char line[MAX_LINESIZE];
132         int newline_missing = 0;
133
134         while (fgets(line, MAX_LINESIZE, fp)) {
135                 if (newline_missing) fatal_error("line too long");
136
137                 int n = strlen(line);
138                 if (!n) continue;
139
140                 parse_line(line);
141
142                 newline_missing = (line[n-1] != '\n');
143         }
144
145         close(fp);
146 }
147
148 /*
149
150 line_passed() {
151     skip_spaces();
152     return (current_token == LINE_END || current_token == LINE_COMMENT);
153 }
154
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);
160     if (!handler)
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) {
166     skip_spaces();
167     if (current_token == ASSIGNMENT)
168 }
169     
170
171 */