-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdirectives_if.cpp
147 lines (123 loc) · 2.71 KB
/
directives_if.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* naken_asm assembler.
* Author: Michael Kohn
* Email: [email protected]
* Web: https://www.mikekohn.net/
* License: GPLv3
*
* Copyright 2010-2025 by Michael Kohn
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/assembler.h"
#include "common/directives_if.h"
#include "common/ifdef_expression.h"
#include "common/tokens.h"
#include "common/Macros.h"
#include "common/print_error.h"
int ifdef_ignore(AsmContext *asm_context)
{
char token[TOKENLEN];
int token_type;
int nested_if = 0;
while (1)
{
token_type = tokens_get(asm_context, token, TOKENLEN);
if (token_type == TOKEN_EOF)
{
print_error(asm_context, "Missing endif");
return -1;
}
if (token_type == TOKEN_EOL)
{
asm_context->tokens.line++;
}
else
if (token_type == TOKEN_POUND || IS_TOKEN(token,'.'))
{
token_type = tokens_get(asm_context, token, TOKENLEN);
if (strcasecmp(token, "endif") == 0)
{
if (nested_if == 0) { return 0; }
nested_if--;
}
else
if (strcasecmp(token, "else") == 0)
{
if (nested_if == 0) { return 2; }
}
else
if (strcasecmp(token, "if") == 0 || strcasecmp(token, "ifdef") == 0)
{
nested_if++;
}
}
}
}
int parse_ifdef_ignore(AsmContext *asm_context, int ignore_section)
{
if (ignore_section == 1)
{
if (ifdef_ignore(asm_context) == 2)
{
assemble(asm_context);
}
}
else
{
if (assemble(asm_context) == 2)
{
ifdef_ignore(asm_context);
}
}
return 0;
}
int parse_ifdef(AsmContext *asm_context, int ifndef)
{
char token[TOKENLEN];
int token_type;
int ignore_section = 0;
int param_count; // throw away
asm_context->ifdef_count++;
asm_context->parsing_ifdef = 1;
token_type = tokens_get(asm_context, token, TOKENLEN);
asm_context->parsing_ifdef = 0;
if (token_type != TOKEN_STRING)
{
print_error(asm_context, "#ifdef has no label");
return -1;
}
if (macros_lookup(&asm_context->macros, token, ¶m_count) != NULL ||
asm_context->symbols.find(token) != NULL)
{
if (ifndef == 1) { ignore_section = 1; }
}
else
{
if (ifndef == 0) { ignore_section = 1; }
}
parse_ifdef_ignore(asm_context, ignore_section);
asm_context->ifdef_count--;
return 0;
}
int parse_if(AsmContext *asm_context)
{
int num;
asm_context->ifdef_count++;
asm_context->parsing_ifdef = 1;
num = eval_ifdef_expression(asm_context);
asm_context->parsing_ifdef = 0;
if (num == -1) { return -1; }
if (num != 0)
{
parse_ifdef_ignore(asm_context, 0);
}
else
{
parse_ifdef_ignore(asm_context, 1);
}
asm_context->ifdef_count--;
return 0;
}