summaryrefslogtreecommitdiff
path: root/src/macro_processor/macro_processor.rs
blob: 0d931f90ff954b1e1298d36ee0c7a8188a28f98f (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std::collections::HashMap;
use std::fs;
use std::process::Command;

// print only with debug_assertions
macro_rules! dprint {
    ($($x:tt)*) => {
        #[cfg(debug_assertions)]
        print!($($x)*)
    }
}

// // println only with debug_assertions
// macro_rules! dprintln {
//     ($($x:tt)*) => {
//         #[cfg(debug_assertions)]
//         println!($($x)*)
//     }
// }

// Point to one or more ranges of a string, useful for highlighting parts of string
macro_rules! highlight_debug {
    ($hi_col:expr, $str:expr $(, ($pos:tt  -> $endpos:tt))*) => {
        for (i, _c) in $str.char_indices() {
            if false $(|| (i >= $pos) && (i < $endpos))* {
                dprint!("{}{}\x1b[0m", $hi_col, _c);
            } else {
                dprint!("{}", _c);
            }
        }
        dprint!("\n");
    };
    ($str:expr, $pos:expr, $endpos:expr) => {
        highlight_debug!("\x1b[7m", $str, ($pos -> $endpos))
    };
    ($str:expr, $pos:expr) => {
        highlight_debug!($str, $pos, $pos+1)
    };
}

pub enum MacroType {
    Function(fn(smp: &mut MacroProcessor, macro_name: &str, args: &mut [String]) -> String),
    String(String)
}

pub struct MacroProcessor {
    macros: HashMap<String, MacroType>,
}

fn smp_builtin_define(smp: &mut MacroProcessor, macro_name: &str, args: &mut [String]) -> String {
    if args.len() < 1 {
        return macro_name.to_string();
    }
    let arg0 = smp.process_input(&args[0]);
    if args.len() > 1 {
        let arg1 = smp.process_input(&args[1]);
        smp.define_macro(arg0, arg1);
    } else {
        smp.define_macro(arg0, String::new());
    }
    String::new()
}

fn smp_builtin_ifdef(smp: &mut MacroProcessor, macro_name: &str, args: &mut [String]) -> String {
    if args.len() < 2 {
        return macro_name.to_string();
    }
    // We need to expand the first argument here as well, but we need to make the parser
    // support literal, and phrase strings
    if smp.macros.contains_key(&args[0]) {
        return smp.process_input(&args[1]);
    }
    if args.len() > 2 {
        return smp.process_input(&args[2]);
    }
    String::new()
}

fn smp_builtin_ifndef(smp: &mut MacroProcessor, macro_name: &str, args: &mut [String]) -> String {
    if args.len() < 2 {
        return macro_name.to_string();
    }
    // We need to expand the first argument here as well, but we need to make the parser
    // support literal, and phrase strings
    if !smp.macros.contains_key(&args[0]) {
        return smp.process_input(&args[1]);
    }
    if args.len() > 2 {
        return smp.process_input(&args[2]);
    }
    String::new()
}

fn smp_builtin_ifeq(smp: &mut MacroProcessor, macro_name: &str, args: &mut [String]) -> String {
    if args.len() < 3 {
        return macro_name.to_string();
    }
    let arg0 = smp.process_input(&args[0]);
    let arg1 = smp.process_input(&args[1]);
    if arg0 == arg1 {
        return smp.process_input(&args[2]);
    }
    if args.len() > 3 {
        return smp.process_input(&args[3]);
    }
    String::new()
}

fn smp_builtin_ifneq(smp: &mut MacroProcessor, macro_name: &str, args: &mut [String]) -> String {
    if args.len() < 3 {
        return macro_name.to_string();
    }
    let arg0 = smp.process_input(&args[0]);
    let arg1 = smp.process_input(&args[1]);
    if arg0 != arg1 {
        return smp.process_input(&args[2]);
    }
    if args.len() > 3 {
        return smp.process_input(&args[3]);
    }
    return String::new();
}

fn smp_builtin_include(smp: &mut MacroProcessor, macro_name: &str, args: &mut [String]) -> String {
    if args.len() < 1 {
        return macro_name.to_string();
    }
    let arg0 = smp.process_input(&args[0]);
    let input_file = fs::read_to_string(&arg0).expect("Failed to read input file");
    return smp.process_input(&input_file);
}

fn smp_builtin_shell(smp: &mut MacroProcessor, macro_name: &str, args: &mut [String]) -> String {
    if args.len() < 1 {
        return macro_name.to_string();
    }
    let arg0 = smp.process_input(&args[0]);
    let res = Command::new("sh")
        .arg("-c")
        .arg(arg0)
        .output();
    match res {
        Ok(output) => String::from_utf8(output.stdout).expect("SMP1"),
        Err(_) => String::new()
    }
}

/// Would like one that is better than this tbh
fn smp_builtin_expr(smp: &mut MacroProcessor, macro_name: &str, args: &mut [String]) -> String {
    if args.len() < 1 {
        return macro_name.to_string();
    }

    for arg in args.iter_mut() {
        *arg = smp.process_input(&arg);
    }

    let res = Command::new("expr")
        .args(args)
        .output();
    match res {
        Ok(output) => String::from_utf8(output.stdout).expect("SMP1"),
        Err(_) => String::new()
    }
}

impl MacroProcessor {
    pub fn new() -> Self {
        let mut smp = Self {
            macros: HashMap::new(),
        };
        smp.define_builtins();
        smp
    }

    fn define_builtins(&mut self) {
        self.define_macro_fn(String::from("define"), MacroType::Function(smp_builtin_define));
        self.define_macro_fn(String::from("ifdef"), MacroType::Function(smp_builtin_ifdef));
        self.define_macro_fn(String::from("ifndef"), MacroType::Function(smp_builtin_ifndef));
        self.define_macro_fn(String::from("ifeq"), MacroType::Function(smp_builtin_ifeq));
        self.define_macro_fn(String::from("ifneq"), MacroType::Function(smp_builtin_ifneq));
        self.define_macro_fn(String::from("include"), MacroType::Function(smp_builtin_include));
        self.define_macro_fn(String::from("shell"), MacroType::Function(smp_builtin_shell));
        self.define_macro_fn(String::from("expr"), MacroType::Function(smp_builtin_expr));
        // TODO
        // format('Result id %d', 3282)
    }

    pub fn define_macro(&mut self, name: String, body: String) {
        self.macros.insert(name, MacroType::String(body));
    }

    pub fn define_macro_fn(&mut self, name: String, macro_expansion: MacroType) {
        self.macros.insert(name, macro_expansion);
    }
    
    /// This expands a macro definition, and it executes builtin functions, like define
    fn expand_macro(&mut self, macro_name: &str, args: &mut [String]) -> String {
        let Some(macro_body) = self.macros.get(macro_name) else {
            return format!("{}", macro_name);
        };

        match macro_body {
            MacroType::String(body) => {
                let mut expanded = body.clone();
                for (i, arg) in args.iter().enumerate() {
                    let placeholder = format!("${}", i + 1);
                    expanded = expanded.replace(&placeholder, arg);
                }
                return expanded;
            },
            MacroType::Function(func) => {
                return func(self, macro_name, args);
            },
        }
    }

    pub fn process_input(&mut self, input: &str) -> String {
        let mut output = String::new();
        let mut state = ParserState::Normal;
        let mut macro_name = String::new();
        let mut macro_args = Vec::new();
        let mut argument = String::new();
        let mut macro_name_start = 0;

        let mut skip_next_line_ending = false;

        let mut in_quote_single = false;
        let mut in_quote_double = false;

        for (i, c) in input.char_indices() {
            highlight_debug!(input, macro_name_start, i);

            match state {
                ParserState::Normal => {
                    macro_name_start = i;

                    if skip_next_line_ending && (c == '\n') {
                        skip_next_line_ending = false;
                        continue;
                    }

                    if c.is_alphanumeric() {
                        state = ParserState::InMacro;
                        macro_name.push(c);
                    } else {
                        output.push(c);
                    }
                }
                ParserState::InMacro => {
                    if c.is_alphanumeric() || c == '_' {
                        macro_name.push(c);
                    } else if c == '(' {
                        state = ParserState::InMacroArgs;
                    } else {
                        if self.macros.contains_key(&macro_name) {
                            highlight_debug!("\x1b[32m\x1b[7m", input, (macro_name_start -> i));
                        }
                        if macro_name == "SNNL" {
                            skip_next_line_ending = c != '\n';
                        } else {
                            let expanded = self.expand_macro(&macro_name, &mut []);
                            output.push_str(&expanded);
                            output.push(c);
                        }
                        macro_name.clear();
                        state = ParserState::Normal;
                    }
                }
                ParserState::InMacroArgs => {
                    if c == ')' {
                        highlight_debug!("\x1b[32m\x1b[7m", input, (macro_name_start -> i));

                        macro_args.push(argument.trim().to_string());
                        let expanded = self.expand_macro(&macro_name, &mut macro_args);
                        output.push_str(&expanded);
                        state = ParserState::Normal;
                        macro_name.clear();
                        macro_args.clear();
                        argument.clear();
                    } else if c == ',' {
                        macro_args.push(argument.trim().to_string());
                        argument.clear();
                    } else {
                        argument.push(c);
                    }
                }
            }
        }

        // Handle cases where the text ends with a macro without arguments
        if !macro_name.is_empty() {
            output.push_str(&self.expand_macro(&macro_name, &mut []));
        }

        output
    }
}

#[derive(Debug, PartialEq)]
enum ParserState {
    Normal,
    InMacro,
    InMacroArgs,
}