xod 1.0.1

A tiny REPL for bitwise arithmetic and expression evaluation.
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use color_print::cprintln;
use prettytable::{
    Attr, Cell, Row, Table, color,
    format::{Alignment, FormatBuilder, LinePosition, LineSeparator, TableFormat},
};

// Using cardinal directions
// E.g. Character ╯:
//   N
// W ╯ E
//   S
// ╯ = North-West = NW
pub(crate) const SE: char = '';
pub(crate) const NE: char = '';
const SW: char = '';
pub(crate) const NS: char = '';
const NW: char = '';
const SEW: char = '';
const NSE: char = '';
const NSW: char = '';
const NEW: char = '';
const NSEW: char = '';
pub(crate) const EW: char = '';

pub fn print_help() {
    // Set up the table format
    let table_format = FormatBuilder::new()
        .padding(2, 2)
        .column_separator(NS)
        .borders(NS)
        .separator(LinePosition::Top, LineSeparator::new(EW, SEW, SE, SW))
        .separator(LinePosition::Bottom, LineSeparator::new(EW, NEW, NE, NW))
        .separators(
            &[LinePosition::Title, LinePosition::Intern],
            LineSeparator::new(EW, NSEW, NSE, NSW),
        )
        .indent(4)
        .build();

    let commands = commands_table(table_format);
    let bit_operators = bitwise_operators_table(table_format);
    let cmp_operators = comparison_operators_table(table_format);
    let list_methods = list_methods_table(table_format);
    let loops = loops_table(table_format);

    cprintln!(
        r#"
<s><m>Welcome to the Xod REPL!</></>

    This REPL allows you to evaluate bitwise expressions interactively. You can enter any valid Xod expression, and it will be evaluated immediately. Below is a breakdown of the domain specific language (DSL) and commands available in this REPL: 

<s><y!>Commands:</></>
"#
    );
    commands.printstd();
    cprintln!(
        r#"
<s><y!>Basic operators:</></>
"#
    );
    bit_operators.printstd();
    cprintln!(
        r#"
<s><y!>Boolean operators:</></>
"#
    );
    cmp_operators.printstd();
    cprintln!(
        r#"
<s><y!>List methods:</></>
"#
    );
    list_methods.printstd();
    cprintln!(
        r#"
<s><y!>Block statements:</></>
"#
    );
    loops.printstd();
    cprintln!(
        r#"
<s><r!>Note:</></>

    There is not an agreed upon standard for the order of operations in bitwise expressions. To avoid ambiguity, it is required to use parentheses to group chained expressions. For example, instead of writing `a & b | c`, you should write `(a & b) | c` or `a & (b | c)` to clarify the order of operations.

    Because this was designed for bitwise expressions, floating point and negative numbers are not supported. The REPL will return an error if you try to use them.

    This REPL is designed to be a simple and interactive way to evaluate bitwise expressions. It is not a full programming language, but rather a tool for evaluating expressions in a specific domain. If you have any questions or suggestions, please feel free to reach out.

"#
    );
}

fn loops_table(table_format: TableFormat) -> Table {
    let mut loops = Table::new();
    loops.set_format(table_format);
    loops.set_titles(Row::new(vec![
        Cell::new_align("Block", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
        Cell::new_align("Arguments", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
        Cell::new_align("Description", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
    ]));
    loops.extend(vec![
        Row::new(vec![
            Cell::new_align(
                "for(<var> in <iterable>) { ... }",
                Alignment::CENTER,
            ).with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A variable and an iterable. The iterable can be a range or a list.\nThe value of the iterator is assigned to the variable.", Alignment::LEFT),
            Cell::new_align("Iterate over a range or iterable.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align(
                "while(<condition>) { ... }",
                Alignment::CENTER,
            ).with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A conditional statement. (See 'Boolean operators')", Alignment::LEFT),
            Cell::new_align("Repeat while the condition is true.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align(
                "if(<condition>) { ... }",
                Alignment::CENTER,
            ).with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A conditional statement. (See 'Boolean operators')", Alignment::LEFT),
            Cell::new_align(
                "Execute the block if the condition is true.",
                Alignment::LEFT,
            ),
        ]),
    ]);
    loops
}

fn list_methods_table(table_format: TableFormat) -> Table {
    let mut methods = Table::new();
    methods.set_format(table_format);
    methods.set_titles(Row::new(vec![
        Cell::new_align("Method", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
        Cell::new_align("Arguments", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
        Cell::new_align("Description", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
    ]));
    methods.extend(vec![
        Row::new(vec![
            Cell::new_align("append(<value>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A variable or number", Alignment::LEFT),
            Cell::new_align("Append a value to the back of the list.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("prepend(<value>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A variable or number", Alignment::LEFT),
            Cell::new_align("Prepend a value to the front of the list.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("back()", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("", Alignment::LEFT),
            Cell::new_align(
                "Remove and return the last value from the list.",
                Alignment::LEFT,
            ),
        ]),
        Row::new(vec![
            Cell::new_align("front()", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("", Alignment::LEFT),
            Cell::new_align(
                "Remove and return the first value from the list.",
                Alignment::LEFT,
            ),
        ]),
        Row::new(vec![
            Cell::new_align("index(<index>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A variable or number", Alignment::LEFT),
            Cell::new_align(
                "Get the element at the specified index (0 indexed).",
                Alignment::LEFT,
            ),
        ]),
    ]);
    methods
}

fn comparison_operators_table(table_format: TableFormat) -> Table {
    let mut operators = Table::new();
    operators.set_format(table_format);
    operators.set_titles(Row::new(vec![
        Cell::new_align("Operator", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
        Cell::new_align("Description", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
    ]));
    operators.extend(vec![
        Row::new(vec![
            Cell::new_align("==", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Equality operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("!=", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Inequality operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("<", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Less than operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align(">", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Greater than operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("<=", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Less than or equal to operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align(">=", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Greater than or equal to operator.", Alignment::LEFT),
        ]),
    ]);
    operators
}

fn bitwise_operators_table(table_format: TableFormat) -> Table {
    let mut operators = Table::new();
    operators.set_format(table_format);
    operators.set_titles(Row::new(vec![
        Cell::new_align("Operator", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
        Cell::new_align("Description", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
    ]));
    operators.extend(vec![
        Row::new(vec![
            Cell::new_align("&", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Bitwise AND operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("|", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Bitwise OR operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("^", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Bitwise XOR operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("! or ~", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Bitwise NOT operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("<<", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Bitwise left shift operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align(">>", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Bitwise right shift operator.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("+", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Addition.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("-", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Subtraction.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("*", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Multiplication.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("/", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Division.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("%", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Modulo (remainder).", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("**", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("Exponent/Power.", Alignment::LEFT),
        ]),
    ]);
    operators
}

fn commands_table(table_format: TableFormat) -> Table {
    let mut commands = Table::new();
    commands.set_format(table_format);
    commands.set_titles(Row::new(vec![
        Cell::new_align("Command", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
        Cell::new_align("Args", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
        Cell::new_align("Description", Alignment::LEFT)
            .with_style(Attr::Bold)
            .with_style(Attr::ForegroundColor(color::BRIGHT_CYAN)),
    ]));
    commands.extend(vec![
        Row::new(vec![
            Cell::new_align("help()", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("", Alignment::LEFT),
            Cell::new_align("Show this help message.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("quit()", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("", Alignment::LEFT),
            Cell::new_align("Exit the REPL.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("clear()", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("", Alignment::LEFT),
            Cell::new_align("Clear the screen.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("history()", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("", Alignment::LEFT),
            Cell::new_align("Show command history.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("bool(<stmt>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A boolean comparison or a variable", Alignment::LEFT),
            Cell::new_align(
                "Returns the input as a boolean value (true = 1, false = 0).",
                Alignment::LEFT,
            ),
        ]),
        Row::new(vec![
            Cell::new_align("range(<value>, <value>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align(
                "Two values of either a variable or a number",
                Alignment::LEFT,
            ),
            Cell::new_align(
                "Returns a non-inclusive iterator over the range.",
                Alignment::LEFT,
            ),
        ]),
        Row::new(vec![
            Cell::new_align("log(<value>, <value>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align(
                "Two values of either a variable or a number",
                Alignment::LEFT,
            ),
            Cell::new_align(
                "Returns a floored integer representation of the log.\nThe left value is the base, and the right value is the number to log.",
                Alignment::LEFT,
            ),
        ]),
        Row::new(vec![
            Cell::new_align("hex(<value>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A variable or a number", Alignment::LEFT),
            Cell::new_align("Prints the input as a hexadecimal number.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("oct(<value>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A variable or a number", Alignment::LEFT),
            Cell::new_align("Prints the input as a octal number.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("bin(<value>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A variable or a number", Alignment::LEFT),
            Cell::new_align("Prints the input as a binary number.", Alignment::LEFT),
        ]),
        Row::new(vec![
            Cell::new_align("dec(<value>)", Alignment::CENTER)
                .with_style(Attr::Bold)
                .with_style(Attr::ForegroundColor(color::BRIGHT_GREEN)),
            Cell::new_align("A variable or a number", Alignment::LEFT),
            Cell::new_align("Prints the input as a decimal number.", Alignment::LEFT),
        ]),
    ]);
    commands
}