aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorLibravatar Comrade Martin Miroslavovich Michalec <martin@michalec.dev>2023-11-26 21:34:11 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-07 02:13:38 +0000
commit252df6c74c7be9b82aa89cdb4972f91c87b06184 (patch)
tree988497c67eb3d8d8789aa9bdea47bafd300e25b2 /src/main.c
downloadcoursework-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..54b7ac3
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,105 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <regex.h>
5#include "utils.h"
6#include "arith.h"
7#include "stack.h"
8#include "ops.h"
9
10void
11print_number (u32 num)
12{
13 char sign = num & MASK_NEG && num & MASK_FLAGS ? '-' : ' ';
14 num &= ~MASK_FLAGS;
15 u8 offset;
16 printf (" ");
17 for (offset = 7*BPD; offset; offset -= BPD) {
18 if (num >> offset & DIGIT_MASK)
19 break;
20 putchar (' ');
21 }
22 printf ("%c", sign);
23 for (; offset; offset -= BPD)
24 printf ("%c", I2C (num >> offset & DIGIT_MASK));
25 printf ("%c\n", I2C (num & DIGIT_MASK));
26}
27
28static void
29print_stack (void)
30{
31 const u32 *ptr = (u32 *) stack_base ();
32 const u32 *end = (u32 *) stack_end ();
33
34 while (ptr != end)
35 print_number (*ptr++);
36}
37
38int
39main (int argc, char *argv[])
40{
41 stack_init (sizeof (u32));
42
43 const char *regexp = "^\\s*([+-][a-h]{1,8}|nop|dup|del|clr|swp|neg|add|mul|sub|div|mod|inc|dec|pow|gcd|lcm)\\s*$";
44 regex_t compiled_regexp;
45 int errcode = regcomp (&compiled_regexp, regexp, REG_EXTENDED | REG_NOSUB);
46 if (errcode != 0) {
47 size_t length = regerror (errcode, &compiled_regexp, NULL, 0);
48 char *buffer = malloc (length);
49 (void) regerror (errcode, &compiled_regexp, buffer, length);
50 DIE (EXIT_FAILURE, "regcomp: %s", buffer);
51 }
52
53 char *line = NULL;
54 size_t line_size = 0;
55 ssize_t line_len;
56 enum action {
57 ACTION_NONE,
58 ACTION_QUOTE,
59 ACTION_OP,
60 };
61 enum action last_action = ACTION_NONE;
62 u32 last_num;
63 op_fn last_op = op_fns[OP_NOP];
64 for (;;) {
65 printf ("> ");
66 line_len = getline (&line, &line_size, stdin);
67 line[--line_len] = '\0';
68
69 if (line_len == 0) {
70 switch (last_action) {
71 case ACTION_NONE:
72 break;
73 case ACTION_QUOTE:
74 stack_push_u32 (last_num);
75 break;
76 case ACTION_OP:
77 last_op ();
78 break;
79 }
80 } else {
81 if (regexec (&compiled_regexp, line, 0, NULL, 0) != 0) {
82 ERROR ("input doesn't match regexp `%s'", regexp);
83 continue;
84 }
85
86 if (line[0] == '+' ||
87 line[0] == '-') {
88 last_action = ACTION_QUOTE;
89 last_num = str2num (line);
90 stack_push_u32 (last_num);
91 } else {
92 last_action = ACTION_OP;
93 last_op = op_fns[str2op (line)];
94 last_op ();
95 }
96 }
97
98 print_stack ();
99 NEWLINE ();
100 } free (line);
101
102 regfree (&compiled_regexp);
103 stack_free ();
104 return EXIT_SUCCESS;
105}