Added ability to define multiple functions (without parameters) and
[zilutils.git] / zilasm / parser.h
1 /*
2  * parser.h -- part of ZilUtils/ZilAsm
3  *
4  * Copyright (C) 2016, 2019, 2020 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  * SPDX-License-Identifier: AGPL-3.0-or-later
20  */
21
22 #ifndef ZILASM_PARSER
23 #define ZILASM_PARSER 1
24
25
26 struct Instruction
27 {
28   Byte *pCodes;
29   int size;
30 };
31
32 #define MAX_NUMBER_OF_INSTRUCTIONS 65536
33
34 struct Function
35 {
36         Function() : index(0), address(0), number_of_local_variables(0)
37         {
38         }
39
40         unsigned index;
41         unsigned address;
42         unsigned number_of_local_variables;
43 };
44
45
46 class CParser
47 {
48 public:
49         CParser();
50         ~CParser();
51         void calculate_function_addresses();
52         unsigned get_number_of_instructions();
53         ZMemblock **get_codes();
54         bool have_errors();
55         unsigned output_codes(ZMemblock *zmem_code);
56         int     parse_file();
57         void add_function(const char *s);
58         unsigned m_current_line_number;
59         unsigned m_current_address;
60
61
62         map <string, Function> m_functions;
63         vector<unsigned> m_function_addresses;
64         string m_start_function_name;
65
66 private:
67         unsigned g_number_of_instructions;// = 0;
68
69
70         ZMemblock(*g_codes[MAX_NUMBER_OF_INSTRUCTIONS]);
71         bool g_haveErrors;
72         CDirectives *m_pdirectives;
73         vector<string> m_calls; // contains functions names' if current instruction is call/icall or empty string otherwise
74
75         string build_error_message(const char *message);
76         void checksep(const char *p);
77         void fatal_error(const char *errmsg);
78         int parse_line(const char *p);
79         const char *pass_spaces(const char *p);
80         int     read_instructions_parameter(char *a, string& str);
81         int     read_instructions_parameter2(char *a, string& str);
82         int     tryparse_directive(const char *p);
83         int     tryparse_startup_directive(const char *p);
84
85         int     tryparse_name(const char *a);
86         int tryparse_label(const char *a, const char *b, const char *c);
87         int tryparse_instruction(const char *a);
88 };
89
90 //void init_parser ();
91
92 //int parse_file();// const char *filename);
93
94 //extern unsigned g_number_of_instructions;
95
96
97 //extern ZMemblock (*g_codes[MAX_NUMBER_OF_INSTRUCTIONS]);
98
99 struct Parsing_Context
100 {
101         string current_directory;
102         string current_file_name;
103 };
104
105 extern stack<Parsing_Context> g_parsing_contexts;
106 //extern unsigned g_currentLineNumber;
107 //extern bool g_haveErrors;
108
109 #endif /* ifndef ZILASM_PARSER */