author | Myhailo Danylenko <isbear@ukrpost.net> |
Thu, 25 Mar 2010 21:03:23 +0200 | |
changeset 10 | 14987412be3b |
parent 9 | 70bb1e988a4a |
child 11 | 5bbf342f450a |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* yaubil.c -- Yet Another Useless Built-In Language |
|
3 |
* |
|
4 |
* Copyrigth (C) 2009 Myhailo Danylenko <isbear@ukrpost.net> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
9 |
* your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but |
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 |
* USA |
|
20 |
*/ |
|
21 |
||
22 |
#include <glib.h> |
|
23 |
#include <string.h> |
|
24 |
||
2
6a47079725da
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
25 |
#include <mcabber/commands.h> |
6a47079725da
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
26 |
#include <mcabber/compl.h> |
6a47079725da
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
27 |
#include <mcabber/logprint.h> |
6a47079725da
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
28 |
#include <mcabber/settings.h> |
8
2033d1c5d226
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
29 |
#include <mcabber/modules.h> |
2033d1c5d226
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
30 |
|
9
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
31 |
#include "config.h" |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
32 |
|
8
2033d1c5d226
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
33 |
static void yaubil_init (void); |
2033d1c5d226
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
34 |
static void yaubil_uninit (void); |
2033d1c5d226
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
35 |
|
9
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
36 |
#define DESCRIPTION ( "Yet Another Useless Built-In Language\nProvides commands /multi, /if, /then, /else, /eval, /let" ) |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
37 |
|
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
38 |
static module_info_t info_yaubil_experimental = { |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
39 |
.branch = "experimental", |
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
40 |
.api = 10, |
9
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
41 |
.version = PROJECT_VERSION, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
42 |
.description = DESCRIPTION, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
43 |
.requires = NULL, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
44 |
.init = yaubil_init, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
45 |
.uninit = yaubil_uninit, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
46 |
.next = NULL, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
47 |
}; |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
48 |
|
8
2033d1c5d226
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
49 |
module_info_t info_yaubil = { |
9
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
50 |
.branch = "dev", |
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
51 |
.api = 11, |
9
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
52 |
.version = PROJECT_VERSION, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
53 |
.description = DESCRIPTION, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
54 |
.requires = NULL, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
55 |
.init = yaubil_init, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
56 |
.uninit = yaubil_uninit, |
70bb1e988a4a
Use new module info strut with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
57 |
.next = &info_yaubil_experimental, |
8
2033d1c5d226
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
58 |
}; |
0 | 59 |
|
60 |
static gboolean ifresult = TRUE; |
|
61 |
||
62 |
#define MSGPREFIX "yaubil: " |
|
63 |
||
64 |
#define TYPE_UNDEF ( 0 ) |
|
65 |
#define TYPE_STR ( 1 ) |
|
66 |
#define TYPE_INT ( 2 ) |
|
67 |
||
68 |
#define STATE_LVALUE ( 1 ) |
|
69 |
#define STATE_OP ( 2 ) |
|
70 |
#define STATE_RVALUE ( 3 ) |
|
71 |
||
72 |
typedef struct { |
|
73 |
int type; |
|
74 |
int int_value; |
|
75 |
char *str_value; |
|
76 |
} value_t; |
|
77 |
||
78 |
typedef struct { |
|
79 |
char op; |
|
80 |
gboolean (*handler) (value_t *l, value_t *r); |
|
81 |
} op_t; |
|
82 |
||
83 |
static int check_value_type (const char *value) |
|
84 |
{ |
|
85 |
if (value) { |
|
86 |
const char *e; |
|
4
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
87 |
gboolean integer = (*value == '-') ? TRUE : (g_ascii_isdigit (*value) ? TRUE : FALSE); |
0 | 88 |
|
89 |
if (integer) { |
|
90 |
for (e = value + 1; *e; ++e) { |
|
91 |
if (g_ascii_isdigit (*e)) |
|
92 |
integer = TRUE; |
|
93 |
else { |
|
94 |
integer = FALSE; |
|
95 |
break; |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
if (integer) |
|
101 |
return TYPE_INT; |
|
102 |
else |
|
103 |
return TYPE_STR; |
|
104 |
} else |
|
105 |
return TYPE_UNDEF; |
|
106 |
} |
|
107 |
||
108 |
static gboolean op_concat (value_t *l, value_t *r) |
|
109 |
{ |
|
4
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
110 |
GString *res = g_string_new (NULL); |
0 | 111 |
|
112 |
if (l->type == TYPE_INT) |
|
113 |
g_string_append_printf (res, "%d", l->int_value); |
|
114 |
else if (l->type == TYPE_STR) |
|
115 |
g_string_append (res, l->str_value); |
|
116 |
||
117 |
if (r->type == TYPE_INT) |
|
118 |
g_string_append_printf (res, "%d", r->int_value); |
|
119 |
else if (r->type == TYPE_STR) |
|
120 |
g_string_append (res, r->str_value); |
|
121 |
||
122 |
l->type = TYPE_STR; |
|
123 |
g_free (l->str_value); |
|
124 |
l->str_value = g_string_free (res, FALSE); |
|
125 |
||
126 |
return TRUE; |
|
127 |
} |
|
128 |
||
4
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
129 |
static gboolean op_head (value_t *l, value_t *r) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
130 |
{ |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
131 |
if (l->type == TYPE_UNDEF) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
132 |
l->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
133 |
l->int_value = 0; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
134 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
135 |
if (r->type == TYPE_UNDEF) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
136 |
r->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
137 |
r->int_value = 0; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
138 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
139 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
140 |
if (l -> type == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
141 |
l -> type = TYPE_STR; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
142 |
if (!l -> str_value) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
143 |
l -> str_value = g_strdup_printf ("%d", l -> int_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
144 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
145 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
146 |
r->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
147 |
r->int_value = atoi (r->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
148 |
g_free (r->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
149 |
r->str_value = NULL; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
150 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
151 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
152 |
if (l->type == TYPE_STR && r->type == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
153 |
gchar *res = NULL; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
154 |
int len = l -> str_value ? strlen (l -> str_value) : 0; // XXX can str_value be NULL? |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
155 |
int off = r -> int_value; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
156 |
if (off < 0 && len + off > 0) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
157 |
res = g_strndup (l -> str_value, len + off); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
158 |
else if (off > 0 && len > off) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
159 |
res = g_strndup (l -> str_value, off); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
160 |
else |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
161 |
res = g_strdup (""); // XXX leave NULL here? |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
162 |
g_free (l->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
163 |
l->str_value = res; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
164 |
} else |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
165 |
return FALSE; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
166 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
167 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
168 |
static gboolean op_tail (value_t *l, value_t *r) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
169 |
{ |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
170 |
if (l->type == TYPE_UNDEF) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
171 |
l->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
172 |
l->int_value = 0; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
173 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
174 |
if (r->type == TYPE_UNDEF) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
175 |
r->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
176 |
r->int_value = 0; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
177 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
178 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
179 |
if (l -> type == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
180 |
l -> type = TYPE_STR; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
181 |
if (!l -> str_value) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
182 |
l -> str_value = g_strdup_printf ("%d", l -> int_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
183 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
184 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
185 |
r->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
186 |
r->int_value = atoi (r->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
187 |
g_free (r->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
188 |
r->str_value = NULL; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
189 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
190 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
191 |
if (l->type == TYPE_STR && r->type == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
192 |
gchar *res = NULL; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
193 |
int len = l -> str_value ? strlen (l -> str_value) : 0; // XXX can str_value be NULL? |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
194 |
int off = r -> int_value; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
195 |
if (off < 0 && len + off > 0) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
196 |
res = g_strdup (l -> str_value + (-off)); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
197 |
else if (off > 0 && len > off) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
198 |
res = g_strdup (l -> str_value + (len - off)); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
199 |
else |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
200 |
res = g_strdup (""); // XXX leave NULL here? |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
201 |
g_free (l->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
202 |
l->str_value = res; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
203 |
} else |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
204 |
return FALSE; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
205 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
206 |
|
0 | 207 |
static gboolean op_plus (value_t *l, value_t *r) |
208 |
{ |
|
209 |
if (l->type == TYPE_UNDEF) { |
|
210 |
l->type = TYPE_INT; |
|
211 |
l->int_value = 0; |
|
212 |
} |
|
213 |
if (r->type == TYPE_UNDEF) { |
|
214 |
r->type = TYPE_INT; |
|
215 |
r->int_value = 0; |
|
216 |
} |
|
217 |
||
218 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
219 |
l->type = TYPE_INT; |
|
220 |
l->int_value = atoi (l->str_value); |
|
221 |
// not free value here |
|
222 |
} |
|
223 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
224 |
r->type = TYPE_INT; |
|
225 |
r->int_value = atoi (r->str_value); |
|
226 |
// not free value here |
|
227 |
} |
|
228 |
||
229 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
230 |
// integer |
|
231 |
l->int_value += r->int_value; |
|
232 |
g_free (l->str_value); |
|
233 |
l->str_value = NULL; |
|
234 |
g_free (r->str_value); |
|
235 |
r->str_value = NULL; |
|
236 |
} else { |
|
237 |
// convert both to strings |
|
238 |
if (l->type == TYPE_INT) { |
|
239 |
l->type = TYPE_STR; |
|
240 |
if (!l->str_value) |
|
241 |
l->str_value = g_strdup_printf ("%d", l->int_value); |
|
242 |
} |
|
243 |
if (r->type == TYPE_INT) { |
|
244 |
r->type = TYPE_STR; |
|
245 |
if (!r->str_value) |
|
246 |
r->str_value = g_strdup_printf ("%d", r->int_value); |
|
247 |
} |
|
248 |
||
249 |
{ |
|
250 |
char *tmp = l->str_value; |
|
251 |
l->str_value = g_strdup_printf ("%s%s", tmp, r->str_value); |
|
252 |
g_free (tmp); |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
return TRUE; |
|
257 |
} |
|
258 |
||
259 |
static gboolean op_minus (value_t *l, value_t *r) |
|
260 |
{ |
|
261 |
if (l->type == TYPE_UNDEF) { |
|
262 |
l->type = TYPE_INT; |
|
263 |
l->int_value = 0; |
|
264 |
} |
|
265 |
if (r->type == TYPE_UNDEF) { |
|
266 |
r->type = TYPE_INT; |
|
267 |
r->int_value = 0; |
|
268 |
} |
|
269 |
||
270 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
271 |
l->type = TYPE_INT; |
|
272 |
l->int_value = atoi (l->str_value); |
|
273 |
g_free (l->str_value); |
|
274 |
l->str_value = NULL; |
|
275 |
} |
|
276 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
277 |
r->type = TYPE_INT; |
|
278 |
r->int_value = atoi (r->str_value); |
|
279 |
g_free (r->str_value); |
|
280 |
r->str_value = NULL; |
|
281 |
} |
|
282 |
||
283 |
if (l->type == TYPE_INT && r->type == TYPE_INT) |
|
284 |
l->int_value -= r->int_value; |
|
285 |
else |
|
286 |
return FALSE; |
|
287 |
||
288 |
return TRUE; |
|
289 |
} |
|
290 |
||
291 |
static gboolean op_multiply (value_t *l, value_t *r) |
|
292 |
{ |
|
293 |
if (l->type == TYPE_UNDEF) { |
|
294 |
l->type = TYPE_INT; |
|
295 |
l->int_value = 0; |
|
296 |
} |
|
297 |
if (r->type == TYPE_UNDEF) { |
|
298 |
r->type = TYPE_INT; |
|
299 |
r->int_value = 0; |
|
300 |
} |
|
301 |
||
302 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
303 |
l->type = TYPE_INT; |
|
304 |
l->int_value = atoi (l->str_value); |
|
305 |
// not free value here |
|
306 |
} |
|
307 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
308 |
r->type = TYPE_INT; |
|
309 |
r->int_value = atoi (r->str_value); |
|
310 |
g_free (r->str_value); |
|
311 |
r->str_value = NULL; |
|
312 |
} |
|
313 |
||
314 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
315 |
l->int_value *= r->int_value; |
|
316 |
g_free (l->str_value); |
|
317 |
l->str_value = NULL; |
|
318 |
} else if (r->type == TYPE_INT) { |
|
319 |
GString *res = g_string_new (NULL); |
|
320 |
int i; |
|
321 |
for (i = r->int_value; i; --i) |
|
322 |
g_string_append (res, l->str_value); |
|
323 |
g_free (l->str_value); |
|
324 |
l->str_value = g_string_free (res, FALSE); |
|
325 |
} else |
|
326 |
return FALSE; |
|
327 |
||
328 |
return TRUE; |
|
329 |
} |
|
330 |
||
331 |
static gboolean op_divide (value_t *l, value_t *r) |
|
332 |
{ |
|
333 |
if (l->type == TYPE_UNDEF) { |
|
334 |
l->type = TYPE_INT; |
|
335 |
l->int_value = 0; |
|
336 |
} |
|
337 |
if (r->type == TYPE_UNDEF) { |
|
338 |
r->type = TYPE_INT; |
|
339 |
r->int_value = 0; |
|
340 |
} |
|
341 |
||
342 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
343 |
l->type = TYPE_INT; |
|
344 |
l->int_value = atoi (l->str_value); |
|
345 |
g_free (l->str_value); |
|
346 |
l->str_value = NULL; |
|
347 |
} |
|
348 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
349 |
r->type = TYPE_INT; |
|
350 |
r->int_value = atoi (r->str_value); |
|
351 |
g_free (r->str_value); |
|
352 |
r->str_value = NULL; |
|
353 |
} |
|
354 |
||
355 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
356 |
if (r->int_value == 0) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
357 |
scr_log_print (LPRINT_NORMAL, MSGPREFIX "/: Error: division by zero."); |
0 | 358 |
return FALSE; |
359 |
} |
|
360 |
l->int_value /= r->int_value; |
|
361 |
} else |
|
362 |
return FALSE; |
|
363 |
||
364 |
return TRUE; |
|
365 |
} |
|
366 |
||
367 |
static gboolean op_remain (value_t *l, value_t *r) |
|
368 |
{ |
|
369 |
if (l->type == TYPE_UNDEF) { |
|
370 |
l->type = TYPE_INT; |
|
371 |
l->int_value = 0; |
|
372 |
} |
|
373 |
if (r->type == TYPE_UNDEF) { |
|
374 |
r->type = TYPE_INT; |
|
375 |
r->int_value = 0; |
|
376 |
} |
|
377 |
||
378 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
379 |
l->type = TYPE_INT; |
|
380 |
l->int_value = atoi (l->str_value); |
|
381 |
g_free (l->str_value); |
|
382 |
l->str_value = NULL; |
|
383 |
} |
|
384 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
385 |
r->type = TYPE_INT; |
|
386 |
r->int_value = atoi (r->str_value); |
|
387 |
g_free (r->str_value); |
|
388 |
r->str_value = NULL; |
|
389 |
} |
|
390 |
||
391 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
392 |
if (r->int_value == 0) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
393 |
scr_log_print (LPRINT_NORMAL, MSGPREFIX "%%: Error: division by zero."); |
0 | 394 |
return FALSE; |
395 |
} |
|
396 |
l->int_value %= r->int_value; |
|
397 |
} else |
|
398 |
return FALSE; |
|
399 |
||
400 |
return TRUE; |
|
401 |
} |
|
402 |
||
403 |
static gboolean op_equal (value_t *l, value_t *r) |
|
404 |
{ |
|
405 |
if (l->type == TYPE_UNDEF) { |
|
406 |
l->type = TYPE_INT; |
|
407 |
l->int_value = 0; |
|
408 |
} |
|
409 |
if (r->type == TYPE_UNDEF) { |
|
410 |
r->type = TYPE_INT; |
|
411 |
r->int_value = 0; |
|
412 |
} |
|
413 |
||
414 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
415 |
l->type = TYPE_INT; |
|
416 |
l->int_value = atoi (l->str_value); |
|
417 |
// not free value here |
|
418 |
} |
|
419 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
420 |
r->type = TYPE_INT; |
|
421 |
r->int_value = atoi (r->str_value); |
|
422 |
// not free value here |
|
423 |
} |
|
424 |
||
425 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
426 |
// integer |
|
427 |
l->int_value = (l->int_value == r->int_value) ? 1 : 0; |
|
428 |
g_free (l->str_value); |
|
429 |
l->str_value = NULL; |
|
430 |
g_free (r->str_value); |
|
431 |
r->str_value = NULL; |
|
432 |
} else { |
|
433 |
// convert both to strings |
|
434 |
if (l->type == TYPE_INT) { |
|
435 |
l->type = TYPE_STR; |
|
436 |
if (!l->str_value) |
|
437 |
l->str_value = g_strdup_printf ("%d", l->int_value); |
|
438 |
} |
|
439 |
if (r->type == TYPE_INT) { |
|
440 |
r->type = TYPE_STR; |
|
441 |
if (!r->str_value) |
|
442 |
r->str_value = g_strdup_printf ("%d", r->int_value); |
|
443 |
} |
|
444 |
||
445 |
{ |
|
446 |
char *tmp = l->str_value; |
|
447 |
l->type = TYPE_INT; |
|
448 |
l->int_value = (g_strcmp0 (l->str_value, r->str_value) == 0) ? 1 : 0; |
|
449 |
g_free (tmp); |
|
450 |
} |
|
451 |
} |
|
452 |
||
453 |
return TRUE; |
|
454 |
} |
|
455 |
||
456 |
static gboolean op_lt (value_t *l, value_t *r) |
|
457 |
{ |
|
458 |
if (l->type == TYPE_UNDEF) { |
|
459 |
l->type = TYPE_INT; |
|
460 |
l->int_value = 0; |
|
461 |
} |
|
462 |
if (r->type == TYPE_UNDEF) { |
|
463 |
r->type = TYPE_INT; |
|
464 |
r->int_value = 0; |
|
465 |
} |
|
466 |
||
467 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
468 |
l->type = TYPE_INT; |
|
469 |
l->int_value = atoi (l->str_value); |
|
470 |
// not free value here |
|
471 |
} |
|
472 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
473 |
r->type = TYPE_INT; |
|
474 |
r->int_value = atoi (r->str_value); |
|
475 |
// not free value here |
|
476 |
} |
|
477 |
||
478 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
479 |
// integer |
|
480 |
l->int_value = (l->int_value < r->int_value) ? 1 : 0; |
|
481 |
g_free (l->str_value); |
|
482 |
l->str_value = NULL; |
|
483 |
g_free (r->str_value); |
|
484 |
r->str_value = NULL; |
|
485 |
} else { |
|
486 |
// convert both to strings |
|
487 |
if (l->type == TYPE_INT) { |
|
488 |
l->type = TYPE_STR; |
|
489 |
if (!l->str_value) |
|
490 |
l->str_value = g_strdup_printf ("%d", l->int_value); |
|
491 |
} |
|
492 |
if (r->type == TYPE_INT) { |
|
493 |
r->type = TYPE_STR; |
|
494 |
if (!r->str_value) |
|
495 |
r->str_value = g_strdup_printf ("%d", r->int_value); |
|
496 |
} |
|
497 |
||
498 |
{ |
|
499 |
char *tmp = l->str_value; |
|
500 |
l->type = TYPE_INT; |
|
501 |
l->int_value = (g_strcmp0 (l->str_value, r->str_value) < 0) ? 1 : 0; |
|
502 |
g_free (tmp); |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
return TRUE; |
|
507 |
} |
|
508 |
||
509 |
static gboolean op_gt (value_t *l, value_t *r) |
|
510 |
{ |
|
511 |
if (l->type == TYPE_UNDEF) { |
|
512 |
l->type = TYPE_INT; |
|
513 |
l->int_value = 0; |
|
514 |
} |
|
515 |
if (r->type == TYPE_UNDEF) { |
|
516 |
r->type = TYPE_INT; |
|
517 |
r->int_value = 0; |
|
518 |
} |
|
519 |
||
520 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
521 |
l->type = TYPE_INT; |
|
522 |
l->int_value = atoi (l->str_value); |
|
523 |
// not free value here |
|
524 |
} |
|
525 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
526 |
r->type = TYPE_INT; |
|
527 |
r->int_value = atoi (r->str_value); |
|
528 |
// not free value here |
|
529 |
} |
|
530 |
||
531 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
532 |
// integer |
|
533 |
l->int_value = (l->int_value > r->int_value) ? 1 : 0; |
|
534 |
g_free (l->str_value); |
|
535 |
l->str_value = NULL; |
|
536 |
g_free (r->str_value); |
|
537 |
r->str_value = NULL; |
|
538 |
} else { |
|
539 |
// convert both to strings |
|
540 |
if (l->type == TYPE_INT) { |
|
541 |
l->type = TYPE_STR; |
|
542 |
if (!l->str_value) |
|
543 |
l->str_value = g_strdup_printf ("%d", l->int_value); |
|
544 |
} |
|
545 |
if (r->type == TYPE_INT) { |
|
546 |
r->type = TYPE_STR; |
|
547 |
if (!r->str_value) |
|
548 |
r->str_value = g_strdup_printf ("%d", r->int_value); |
|
549 |
} |
|
550 |
||
551 |
{ |
|
552 |
char *tmp = l->str_value; |
|
553 |
l->type = TYPE_INT; |
|
554 |
l->int_value = (g_strcmp0 (l->str_value, r->str_value) > 0) ? 1 : 0; |
|
555 |
g_free (tmp); |
|
556 |
} |
|
557 |
} |
|
558 |
||
559 |
return TRUE; |
|
560 |
} |
|
561 |
||
562 |
static op_t operators[] = { |
|
563 |
{ '.', op_concat }, |
|
4
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
564 |
{ ':', op_head }, |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
565 |
{ '^', op_tail }, |
0 | 566 |
{ '+', op_plus }, |
567 |
{ '-', op_minus }, |
|
568 |
{ '*', op_multiply }, |
|
569 |
{ '/', op_divide }, |
|
570 |
{ '%', op_remain }, |
|
571 |
{ '=', op_equal }, |
|
572 |
{ '<', op_lt }, |
|
573 |
{ '>', op_gt }, |
|
574 |
{ 0, NULL }, |
|
575 |
}; |
|
576 |
||
577 |
static void destroy_value (value_t *value) |
|
578 |
{ |
|
579 |
if (value->str_value) |
|
580 |
g_free (value->str_value); |
|
581 |
g_free (value); |
|
582 |
} |
|
583 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
584 |
static value_t *process_expression (const char *str, gsize *len) |
0 | 585 |
{ |
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
586 |
const char *strend = str + *len; |
0 | 587 |
const char *p; |
588 |
op_t *op = NULL; |
|
589 |
int state = STATE_LVALUE; |
|
590 |
value_t val = { |
|
591 |
.type = TYPE_UNDEF, |
|
592 |
.int_value = 0, |
|
593 |
.str_value = NULL, |
|
594 |
}; |
|
595 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
596 |
for (p = str; *p && p < strend && *p != ')'; ++p) { |
0 | 597 |
switch (state) { |
598 |
case STATE_LVALUE: |
|
599 |
case STATE_RVALUE: |
|
600 |
||
601 |
if (*p == ' ') |
|
602 |
break; |
|
603 |
||
604 |
{ |
|
605 |
value_t rval = { |
|
606 |
.type = TYPE_UNDEF, |
|
607 |
.int_value = 0, |
|
608 |
.str_value = NULL, |
|
609 |
}; |
|
610 |
||
611 |
if (g_ascii_isdigit (*p) || *p == '-') { // integer // XXX: no unary operators for now... |
|
612 |
||
613 |
const char *e; |
|
614 |
||
615 |
rval.type = TYPE_INT; |
|
616 |
||
617 |
for (e = p + 1; g_ascii_isdigit (*e) && e < strend; ++e); // TODO: does atoi handle 0x etc? then also allow this? |
|
618 |
||
619 |
{ |
|
620 |
char *v = g_strndup (p, e - p); |
|
621 |
rval.int_value = atoi (v); |
|
622 |
g_free (v); |
|
623 |
} |
|
624 |
||
625 |
p = e - 1; |
|
626 |
||
627 |
} else if (*p == '"') { // string |
|
628 |
||
629 |
const char *e; |
|
630 |
||
631 |
rval.type = TYPE_STR; |
|
632 |
||
633 |
{ |
|
634 |
gboolean finished = FALSE; |
|
635 |
gboolean escape = FALSE; |
|
636 |
GString *v = g_string_new (NULL); |
|
637 |
||
638 |
for (e = p + 1; *e && e < strend; ++e) { |
|
639 |
switch (*e) { |
|
640 |
case '\\': |
|
641 |
if (!escape) |
|
642 |
escape = TRUE; |
|
643 |
else |
|
644 |
escape = FALSE; |
|
645 |
break; |
|
646 |
case '"': |
|
647 |
if (!escape) { |
|
648 |
finished = TRUE; |
|
649 |
break; |
|
650 |
} else |
|
651 |
escape = FALSE; |
|
652 |
break; |
|
653 |
default: |
|
654 |
escape = FALSE; |
|
655 |
break; |
|
656 |
} |
|
657 |
||
658 |
if (finished) |
|
659 |
break; |
|
660 |
||
661 |
if (!escape) |
|
662 |
g_string_append_c (v, *e); |
|
663 |
}; |
|
664 |
||
665 |
if (!finished) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
666 |
scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: Unmatched quote."); |
0 | 667 |
g_string_free (v, TRUE); |
668 |
g_free (val.str_value); |
|
669 |
return NULL; |
|
670 |
} |
|
671 |
||
672 |
rval.str_value = g_string_free (v, FALSE); |
|
673 |
} |
|
674 |
||
675 |
p = e; |
|
676 |
||
677 |
} else if (g_ascii_isalpha (*p)) { // variable (MUST start from alpha) |
|
678 |
||
679 |
const char *e; |
|
680 |
||
681 |
for (e = p + 1; (g_ascii_isalnum (*e) || *e == '-' || *e == '_') && e < strend; ++e); |
|
682 |
||
683 |
char *name = g_strndup (p, e - p); |
|
684 |
const char *value = settings_opt_get (name); |
|
685 |
g_free (name); |
|
686 |
||
687 |
rval.type = check_value_type (value); |
|
688 |
if (rval.type == TYPE_INT) |
|
689 |
rval.int_value = atoi (value); |
|
690 |
else if (rval.type == TYPE_STR) |
|
691 |
rval.str_value = g_strdup (value); |
|
692 |
||
693 |
p = e - 1; |
|
694 |
||
695 |
} else if (*p == '(') { |
|
696 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
697 |
gsize len = strend - p - 1; |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
698 |
value_t *n = process_expression (p + 1, &len); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
699 |
|
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
700 |
if (!n) { |
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
701 |
scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: Error in subexpression."); |
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
702 |
g_free (val.str_value); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
703 |
return NULL; |
0 | 704 |
} |
705 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
706 |
rval.type = n->type; |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
707 |
rval.int_value = n->int_value; |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
708 |
rval.str_value = g_strdup (n->str_value); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
709 |
destroy_value (n); |
0 | 710 |
|
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
711 |
p += len + 1; |
0 | 712 |
|
713 |
} else { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
714 |
scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: unrecognized symbols."); |
0 | 715 |
g_free (val.str_value); |
716 |
return NULL; |
|
717 |
} |
|
718 |
||
719 |
if (state == STATE_RVALUE) { |
|
720 |
if (op->handler) { |
|
721 |
if (!op->handler (&val, &rval)) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
722 |
scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: operand argument types mismatch."); |
0 | 723 |
g_free (val.str_value); |
724 |
g_free (rval.str_value); |
|
725 |
return NULL; |
|
726 |
} |
|
727 |
} |
|
728 |
g_free (rval.str_value); |
|
729 |
} else { |
|
730 |
val.type = rval.type; |
|
731 |
val.int_value = rval.int_value; |
|
732 |
val.str_value = rval.str_value; |
|
733 |
} |
|
734 |
||
735 |
state = STATE_OP; |
|
736 |
} |
|
737 |
||
738 |
break; |
|
739 |
||
740 |
case STATE_OP: |
|
741 |
||
742 |
if (*p == ' ') |
|
743 |
break; |
|
744 |
||
745 |
{ |
|
746 |
op_t *operator; |
|
747 |
gboolean found = FALSE; |
|
748 |
||
749 |
for (operator = operators; operator->op; ++operator) { |
|
750 |
if (operator->op == *p) { |
|
751 |
op = operator; |
|
752 |
found = TRUE; |
|
753 |
break; |
|
754 |
} |
|
755 |
} |
|
756 |
||
757 |
if (!found) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
758 |
scr_log_print (LPRINT_NORMAL, MSGPREFIX "Error: unknown operator."); |
0 | 759 |
g_free (val.str_value); |
760 |
} |
|
761 |
} |
|
762 |
||
763 |
state = STATE_RVALUE; |
|
764 |
||
765 |
break; |
|
766 |
default: |
|
767 |
break; |
|
768 |
} |
|
769 |
} |
|
770 |
||
771 |
{ // return value |
|
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
772 |
*len = p - str; |
0 | 773 |
value_t *rval = g_new (value_t, 1); |
774 |
||
775 |
rval->type = val.type; |
|
776 |
rval->int_value = val.int_value; |
|
777 |
rval->str_value = val.str_value; |
|
778 |
||
779 |
return rval; |
|
780 |
} |
|
781 |
} |
|
782 |
||
783 |
static void do_eval (char *arg) |
|
784 |
{ |
|
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
785 |
gsize len = strlen (arg); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
786 |
value_t *val = process_expression (arg, &len); |
0 | 787 |
|
788 |
if (!val) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
789 |
scr_log_print (LPRINT_NORMAL, "eval: Evaluation error."); |
0 | 790 |
return; |
791 |
} |
|
792 |
||
793 |
if (val->type == TYPE_STR) |
|
794 |
process_command (val->str_value, TRUE); |
|
795 |
else |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
796 |
scr_log_print (LPRINT_NORMAL, "eval: Expression does not result in string."); |
0 | 797 |
|
798 |
destroy_value (val); |
|
799 |
} |
|
800 |
||
801 |
static void do_let (char *arg) |
|
802 |
{ |
|
803 |
char *varname; |
|
804 |
value_t *value; |
|
805 |
int namelen; |
|
806 |
char *val = strchr (arg, '='); |
|
807 |
||
808 |
if (!val) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
809 |
scr_log_print (LPRINT_NORMAL, "let: Syntax error: no equal sign in line."); |
0 | 810 |
return; |
811 |
} |
|
812 |
||
813 |
{ |
|
814 |
char *p = val; |
|
815 |
||
816 |
for (p = val - 1; p >= arg && *p == ' '; --p); |
|
817 |
if (p < arg) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
818 |
scr_log_print (LPRINT_NORMAL, "let: Syntax error: no destination variable name specified."); |
0 | 819 |
return; |
820 |
} |
|
821 |
||
822 |
namelen = p + 1 - arg; |
|
823 |
} |
|
824 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
825 |
{ // evaluate expression |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
826 |
gsize len = strlen (val + 1); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
827 |
|
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
828 |
value = process_expression (val + 1, &len); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
829 |
} |
0 | 830 |
|
831 |
if (!value) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
832 |
scr_log_print (LPRINT_NORMAL, "let: Evaluation error."); |
0 | 833 |
return; |
834 |
} |
|
835 |
||
836 |
if (value->type == TYPE_INT) { |
|
837 |
value->type = TYPE_STR; |
|
838 |
value->str_value = g_strdup_printf ("%d", value->int_value); |
|
839 |
} |
|
840 |
||
841 |
{ // assign value |
|
842 |
char *varname = g_strndup (arg, namelen); |
|
843 |
||
844 |
if (value->str_value) |
|
845 |
settings_set (SETTINGS_TYPE_OPTION, varname, value->str_value); |
|
846 |
else |
|
847 |
settings_del (SETTINGS_TYPE_OPTION, varname); |
|
848 |
||
849 |
g_free (varname); |
|
850 |
} |
|
851 |
||
852 |
destroy_value (value); |
|
853 |
} |
|
854 |
||
855 |
static void do_if (char *arg) |
|
856 |
{ |
|
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
857 |
gsize len = strlen (arg); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
858 |
value_t *val = process_expression (arg, &len); |
0 | 859 |
|
860 |
if (!val) { |
|
10
14987412be3b
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
861 |
scr_log_print (LPRINT_NORMAL, "if: Evaluation error."); |
0 | 862 |
return; |
863 |
} |
|
864 |
||
865 |
if (val->type == TYPE_UNDEF) { |
|
866 |
val->type = TYPE_INT; |
|
867 |
val->int_value = 0; |
|
868 |
} |
|
869 |
if (val->type == TYPE_STR && check_value_type (val->str_value) == TYPE_INT) { |
|
870 |
val->type = TYPE_INT; |
|
871 |
val->int_value = atoi (val->str_value); |
|
872 |
g_free (val->str_value); |
|
873 |
val->str_value = NULL; |
|
874 |
} |
|
875 |
||
876 |
if (val->type == TYPE_INT) |
|
877 |
ifresult = val->int_value ? TRUE : FALSE; |
|
878 |
else if (val->str_value) |
|
879 |
ifresult = TRUE; |
|
880 |
else |
|
881 |
ifresult = FALSE; |
|
882 |
||
883 |
destroy_value (val); |
|
884 |
} |
|
885 |
||
886 |
static void do_then (char *arg) |
|
887 |
{ |
|
888 |
if (ifresult) |
|
889 |
process_command (arg, TRUE); |
|
890 |
} |
|
891 |
||
892 |
static void do_else (char *arg) |
|
893 |
{ |
|
894 |
if (!ifresult) |
|
895 |
process_command (arg, TRUE); |
|
896 |
} |
|
897 |
||
898 |
static void do_multi (char *arg) |
|
899 |
{ |
|
900 |
char *end; |
|
901 |
char *start = arg; |
|
902 |
||
903 |
for (end = strchr (start, ';'); end; end = strchr (start, ';')) { |
|
904 |
||
905 |
// execute command |
|
906 |
char *command = g_strndup (start, end - start); |
|
907 |
process_command (command, TRUE); |
|
908 |
g_free (command); |
|
909 |
||
910 |
// skip leading spaces |
|
911 |
for (start = end + 1; *start == ' '; ++start); |
|
912 |
} |
|
913 |
||
914 |
if (*start) |
|
915 |
process_command (start, TRUE); |
|
916 |
} |
|
917 |
||
8
2033d1c5d226
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
918 |
static void yaubil_init (void) |
0 | 919 |
{ |
920 |
cmd_add ("multi", "", COMPL_CMD, COMPL_CMD, do_multi, NULL); |
|
921 |
cmd_add ("if", "", 0, 0, do_if, NULL); |
|
922 |
cmd_add ("then", "", COMPL_CMD, COMPL_CMD, do_then, NULL); |
|
923 |
cmd_add ("else", "", COMPL_CMD, COMPL_CMD, do_else, NULL); |
|
924 |
cmd_add ("eval", "", 0, 0, do_eval, NULL); |
|
925 |
cmd_add ("let", "", 0, 0, do_let, NULL); |
|
926 |
} |
|
927 |
||
8
2033d1c5d226
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
928 |
static void yaubil_uninit (void) |
0 | 929 |
{ |
930 |
cmd_del ("multi"); |
|
931 |
cmd_del ("if"); |
|
932 |
cmd_del ("then"); |
|
933 |
cmd_del ("else"); |
|
934 |
cmd_del ("eval"); |
|
935 |
cmd_del ("let"); |
|
936 |
} |
|
937 |
||
938 |
/* The End */ |