--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/yaubil.c Tue Nov 10 18:27:16 2009 +0200
@@ -0,0 +1,852 @@
+/*
+ * yaubil.c -- Yet Another Useless Built-In Language
+ *
+ * Copyrigth (C) 2009 Myhailo Danylenko <isbear@ukrpost.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#include <glib.h>
+#include <gmodule.h>
+#include <string.h>
+
+#include "commands.h"
+#include "compl.h"
+#include "logprint.h"
+#include "settings.h"
+
+static gboolean ifresult = TRUE;
+
+#define MSGPREFIX "yaubil: "
+
+#define TYPE_UNDEF ( 0 )
+#define TYPE_STR ( 1 )
+#define TYPE_INT ( 2 )
+
+#define STATE_LVALUE ( 1 )
+#define STATE_OP ( 2 )
+#define STATE_RVALUE ( 3 )
+
+typedef struct {
+ int type;
+ int int_value;
+ char *str_value;
+} value_t;
+
+typedef struct {
+ char op;
+ gboolean (*handler) (value_t *l, value_t *r);
+} op_t;
+
+static int check_value_type (const char *value)
+{
+ if (value) {
+ const char *e;
+ gboolean integer = (*value == '-') ? FALSE : (g_ascii_isdigit (*value) ? TRUE : FALSE);
+
+ if (integer) {
+ for (e = value + 1; *e; ++e) {
+ if (g_ascii_isdigit (*e))
+ integer = TRUE;
+ else {
+ integer = FALSE;
+ break;
+ }
+ }
+ }
+
+ if (integer)
+ return TYPE_INT;
+ else
+ return TYPE_STR;
+ } else
+ return TYPE_UNDEF;
+}
+
+static gboolean op_concat (value_t *l, value_t *r)
+{
+ GString *res = g_string_new (NULL);;
+
+ if (l->type == TYPE_INT)
+ g_string_append_printf (res, "%d", l->int_value);
+ else if (l->type == TYPE_STR)
+ g_string_append (res, l->str_value);
+
+ if (r->type == TYPE_INT)
+ g_string_append_printf (res, "%d", r->int_value);
+ else if (r->type == TYPE_STR)
+ g_string_append (res, r->str_value);
+
+ l->type = TYPE_STR;
+ g_free (l->str_value);
+ l->str_value = g_string_free (res, FALSE);
+
+ return TRUE;
+}
+
+static gboolean op_plus (value_t *l, value_t *r)
+{
+ if (l->type == TYPE_UNDEF) {
+ l->type = TYPE_INT;
+ l->int_value = 0;
+ }
+ if (r->type == TYPE_UNDEF) {
+ r->type = TYPE_INT;
+ r->int_value = 0;
+ }
+
+ if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
+ l->type = TYPE_INT;
+ l->int_value = atoi (l->str_value);
+ // not free value here
+ }
+ if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
+ r->type = TYPE_INT;
+ r->int_value = atoi (r->str_value);
+ // not free value here
+ }
+
+ if (l->type == TYPE_INT && r->type == TYPE_INT) {
+ // integer
+ l->int_value += r->int_value;
+ g_free (l->str_value);
+ l->str_value = NULL;
+ g_free (r->str_value);
+ r->str_value = NULL;
+ } else {
+ // convert both to strings
+ if (l->type == TYPE_INT) {
+ l->type = TYPE_STR;
+ if (!l->str_value)
+ l->str_value = g_strdup_printf ("%d", l->int_value);
+ }
+ if (r->type == TYPE_INT) {
+ r->type = TYPE_STR;
+ if (!r->str_value)
+ r->str_value = g_strdup_printf ("%d", r->int_value);
+ }
+
+ {
+ char *tmp = l->str_value;
+ l->str_value = g_strdup_printf ("%s%s", tmp, r->str_value);
+ g_free (tmp);
+ }
+ }
+
+ return TRUE;
+}
+
+static gboolean op_minus (value_t *l, value_t *r)
+{
+ if (l->type == TYPE_UNDEF) {
+ l->type = TYPE_INT;
+ l->int_value = 0;
+ }
+ if (r->type == TYPE_UNDEF) {
+ r->type = TYPE_INT;
+ r->int_value = 0;
+ }
+
+ if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
+ l->type = TYPE_INT;
+ l->int_value = atoi (l->str_value);
+ g_free (l->str_value);
+ l->str_value = NULL;
+ }
+ if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
+ r->type = TYPE_INT;
+ r->int_value = atoi (r->str_value);
+ g_free (r->str_value);
+ r->str_value = NULL;
+ }
+
+ if (l->type == TYPE_INT && r->type == TYPE_INT)
+ l->int_value -= r->int_value;
+ else
+ return FALSE;
+
+ return TRUE;
+}
+
+static gboolean op_multiply (value_t *l, value_t *r)
+{
+ if (l->type == TYPE_UNDEF) {
+ l->type = TYPE_INT;
+ l->int_value = 0;
+ }
+ if (r->type == TYPE_UNDEF) {
+ r->type = TYPE_INT;
+ r->int_value = 0;
+ }
+
+ if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
+ l->type = TYPE_INT;
+ l->int_value = atoi (l->str_value);
+ // not free value here
+ }
+ if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
+ r->type = TYPE_INT;
+ r->int_value = atoi (r->str_value);
+ g_free (r->str_value);
+ r->str_value = NULL;
+ }
+
+ if (l->type == TYPE_INT && r->type == TYPE_INT) {
+ l->int_value *= r->int_value;
+ g_free (l->str_value);
+ l->str_value = NULL;
+ } else if (r->type == TYPE_INT) {
+ GString *res = g_string_new (NULL);
+ int i;
+ for (i = r->int_value; i; --i)
+ g_string_append (res, l->str_value);
+ g_free (l->str_value);
+ l->str_value = g_string_free (res, FALSE);
+ } else
+ return FALSE;
+
+ return TRUE;
+}
+
+static gboolean op_divide (value_t *l, value_t *r)
+{
+ if (l->type == TYPE_UNDEF) {
+ l->type = TYPE_INT;
+ l->int_value = 0;
+ }
+ if (r->type == TYPE_UNDEF) {
+ r->type = TYPE_INT;
+ r->int_value = 0;
+ }
+
+ if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
+ l->type = TYPE_INT;
+ l->int_value = atoi (l->str_value);
+ g_free (l->str_value);
+ l->str_value = NULL;
+ }
+ if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
+ r->type = TYPE_INT;
+ r->int_value = atoi (r->str_value);
+ g_free (r->str_value);
+ r->str_value = NULL;
+ }
+
+ if (l->type == TYPE_INT && r->type == TYPE_INT) {
+ if (r->int_value == 0) {
+ scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "/: Error: division by zero.");
+ return FALSE;
+ }
+ l->int_value /= r->int_value;
+ } else
+ return FALSE;
+
+ return TRUE;
+}
+
+static gboolean op_remain (value_t *l, value_t *r)
+{
+ if (l->type == TYPE_UNDEF) {
+ l->type = TYPE_INT;
+ l->int_value = 0;
+ }
+ if (r->type == TYPE_UNDEF) {
+ r->type = TYPE_INT;
+ r->int_value = 0;
+ }
+
+ if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
+ l->type = TYPE_INT;
+ l->int_value = atoi (l->str_value);
+ g_free (l->str_value);
+ l->str_value = NULL;
+ }
+ if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
+ r->type = TYPE_INT;
+ r->int_value = atoi (r->str_value);
+ g_free (r->str_value);
+ r->str_value = NULL;
+ }
+
+ if (l->type == TYPE_INT && r->type == TYPE_INT) {
+ if (r->int_value == 0) {
+ scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "%%: Error: division by zero.");
+ return FALSE;
+ }
+ l->int_value %= r->int_value;
+ } else
+ return FALSE;
+
+ return TRUE;
+}
+
+static gboolean op_equal (value_t *l, value_t *r)
+{
+ if (l->type == TYPE_UNDEF) {
+ l->type = TYPE_INT;
+ l->int_value = 0;
+ }
+ if (r->type == TYPE_UNDEF) {
+ r->type = TYPE_INT;
+ r->int_value = 0;
+ }
+
+ if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
+ l->type = TYPE_INT;
+ l->int_value = atoi (l->str_value);
+ // not free value here
+ }
+ if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
+ r->type = TYPE_INT;
+ r->int_value = atoi (r->str_value);
+ // not free value here
+ }
+
+ if (l->type == TYPE_INT && r->type == TYPE_INT) {
+ // integer
+ l->int_value = (l->int_value == r->int_value) ? 1 : 0;
+ g_free (l->str_value);
+ l->str_value = NULL;
+ g_free (r->str_value);
+ r->str_value = NULL;
+ } else {
+ // convert both to strings
+ if (l->type == TYPE_INT) {
+ l->type = TYPE_STR;
+ if (!l->str_value)
+ l->str_value = g_strdup_printf ("%d", l->int_value);
+ }
+ if (r->type == TYPE_INT) {
+ r->type = TYPE_STR;
+ if (!r->str_value)
+ r->str_value = g_strdup_printf ("%d", r->int_value);
+ }
+
+ {
+ char *tmp = l->str_value;
+ l->type = TYPE_INT;
+ l->int_value = (g_strcmp0 (l->str_value, r->str_value) == 0) ? 1 : 0;
+ g_free (tmp);
+ }
+ }
+
+ return TRUE;
+}
+
+static gboolean op_lt (value_t *l, value_t *r)
+{
+ if (l->type == TYPE_UNDEF) {
+ l->type = TYPE_INT;
+ l->int_value = 0;
+ }
+ if (r->type == TYPE_UNDEF) {
+ r->type = TYPE_INT;
+ r->int_value = 0;
+ }
+
+ if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
+ l->type = TYPE_INT;
+ l->int_value = atoi (l->str_value);
+ // not free value here
+ }
+ if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
+ r->type = TYPE_INT;
+ r->int_value = atoi (r->str_value);
+ // not free value here
+ }
+
+ if (l->type == TYPE_INT && r->type == TYPE_INT) {
+ // integer
+ l->int_value = (l->int_value < r->int_value) ? 1 : 0;
+ g_free (l->str_value);
+ l->str_value = NULL;
+ g_free (r->str_value);
+ r->str_value = NULL;
+ } else {
+ // convert both to strings
+ if (l->type == TYPE_INT) {
+ l->type = TYPE_STR;
+ if (!l->str_value)
+ l->str_value = g_strdup_printf ("%d", l->int_value);
+ }
+ if (r->type == TYPE_INT) {
+ r->type = TYPE_STR;
+ if (!r->str_value)
+ r->str_value = g_strdup_printf ("%d", r->int_value);
+ }
+
+ {
+ char *tmp = l->str_value;
+ l->type = TYPE_INT;
+ l->int_value = (g_strcmp0 (l->str_value, r->str_value) < 0) ? 1 : 0;
+ g_free (tmp);
+ }
+ }
+
+ return TRUE;
+}
+
+static gboolean op_gt (value_t *l, value_t *r)
+{
+ if (l->type == TYPE_UNDEF) {
+ l->type = TYPE_INT;
+ l->int_value = 0;
+ }
+ if (r->type == TYPE_UNDEF) {
+ r->type = TYPE_INT;
+ r->int_value = 0;
+ }
+
+ if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) {
+ l->type = TYPE_INT;
+ l->int_value = atoi (l->str_value);
+ // not free value here
+ }
+ if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) {
+ r->type = TYPE_INT;
+ r->int_value = atoi (r->str_value);
+ // not free value here
+ }
+
+ if (l->type == TYPE_INT && r->type == TYPE_INT) {
+ // integer
+ l->int_value = (l->int_value > r->int_value) ? 1 : 0;
+ g_free (l->str_value);
+ l->str_value = NULL;
+ g_free (r->str_value);
+ r->str_value = NULL;
+ } else {
+ // convert both to strings
+ if (l->type == TYPE_INT) {
+ l->type = TYPE_STR;
+ if (!l->str_value)
+ l->str_value = g_strdup_printf ("%d", l->int_value);
+ }
+ if (r->type == TYPE_INT) {
+ r->type = TYPE_STR;
+ if (!r->str_value)
+ r->str_value = g_strdup_printf ("%d", r->int_value);
+ }
+
+ {
+ char *tmp = l->str_value;
+ l->type = TYPE_INT;
+ l->int_value = (g_strcmp0 (l->str_value, r->str_value) > 0) ? 1 : 0;
+ g_free (tmp);
+ }
+ }
+
+ return TRUE;
+}
+
+static op_t operators[] = {
+ { '.', op_concat },
+ { '+', op_plus },
+ { '-', op_minus },
+ { '*', op_multiply },
+ { '/', op_divide },
+ { '%', op_remain },
+ { '=', op_equal },
+ { '<', op_lt },
+ { '>', op_gt },
+ { 0, NULL },
+};
+
+static void destroy_value (value_t *value)
+{
+ if (value->str_value)
+ g_free (value->str_value);
+ g_free (value);
+}
+
+static value_t *process_expression (const char *str, gsize len)
+{
+ const char *strend = str + len;
+ const char *p;
+ op_t *op = NULL;
+ int state = STATE_LVALUE;
+ value_t val = {
+ .type = TYPE_UNDEF,
+ .int_value = 0,
+ .str_value = NULL,
+ };
+
+ for (p = str; *p && p < strend; ++p) {
+ switch (state) {
+ case STATE_LVALUE:
+ case STATE_RVALUE:
+
+ if (*p == ' ')
+ break;
+
+ {
+ value_t rval = {
+ .type = TYPE_UNDEF,
+ .int_value = 0,
+ .str_value = NULL,
+ };
+
+ if (g_ascii_isdigit (*p) || *p == '-') { // integer // XXX: no unary operators for now...
+
+ const char *e;
+
+ rval.type = TYPE_INT;
+
+ for (e = p + 1; g_ascii_isdigit (*e) && e < strend; ++e); // TODO: does atoi handle 0x etc? then also allow this?
+
+ {
+ char *v = g_strndup (p, e - p);
+ rval.int_value = atoi (v);
+ g_free (v);
+ }
+
+ p = e - 1;
+
+ } else if (*p == '"') { // string
+
+ const char *e;
+
+ rval.type = TYPE_STR;
+
+ {
+ gboolean finished = FALSE;
+ gboolean escape = FALSE;
+ GString *v = g_string_new (NULL);
+
+ for (e = p + 1; *e && e < strend; ++e) {
+ switch (*e) {
+ case '\\':
+ if (!escape)
+ escape = TRUE;
+ else
+ escape = FALSE;
+ break;
+ case '"':
+ if (!escape) {
+ finished = TRUE;
+ break;
+ } else
+ escape = FALSE;
+ break;
+ default:
+ escape = FALSE;
+ break;
+ }
+
+ if (finished)
+ break;
+
+ if (!escape)
+ g_string_append_c (v, *e);
+ };
+
+ if (!finished) {
+ scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: Unmatched quote.");
+ g_string_free (v, TRUE);
+ g_free (val.str_value);
+ return NULL;
+ }
+
+ rval.str_value = g_string_free (v, FALSE);
+ }
+
+ p = e;
+
+ } else if (g_ascii_isalpha (*p)) { // variable (MUST start from alpha)
+
+ const char *e;
+
+ for (e = p + 1; (g_ascii_isalnum (*e) || *e == '-' || *e == '_') && e < strend; ++e);
+
+ char *name = g_strndup (p, e - p);
+ const char *value = settings_opt_get (name);
+ g_free (name);
+
+ rval.type = check_value_type (value);
+ if (rval.type == TYPE_INT)
+ rval.int_value = atoi (value);
+ else if (rval.type == TYPE_STR)
+ rval.str_value = g_strdup (value);
+
+ p = e - 1;
+
+ } else if (*p == '(') {
+
+ const char *e;
+
+ {
+ int l = 0;
+ gboolean finished = FALSE;
+
+ // XXX maybe we should just pass end of line and let it return real length...
+ for (e = p + 1; e < strend; ++e) {
+ if (*e == '(')
+ ++l;
+ else if (*e == ')') {
+ --l;
+ if (l < 0) {
+ finished = TRUE;
+ break;
+ }
+ }
+ }
+
+ if (!finished) {
+ scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: Unmatched parenthesis.");
+ g_free (val.str_value);
+ return NULL;
+ }
+ }
+
+ {
+ value_t *n = process_expression (p + 1, e - p - 1);
+
+ if (!n) {
+ scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: Error in subexpression.");
+ g_free (val.str_value);
+ return NULL;
+ }
+
+ rval.type = n->type;
+ rval.int_value = n->int_value;
+ rval.str_value = g_strdup (n->str_value);
+ destroy_value (n);
+ }
+
+ p = e;
+
+ } else {
+ scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: unrecognized symbols.");
+ g_free (val.str_value);
+ return NULL;
+ }
+
+ if (state == STATE_RVALUE) {
+ if (op->handler) {
+ if (!op->handler (&val, &rval)) {
+ scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: operand argument types mismatch.");
+ g_free (val.str_value);
+ g_free (rval.str_value);
+ return NULL;
+ }
+ }
+ g_free (rval.str_value);
+ } else {
+ val.type = rval.type;
+ val.int_value = rval.int_value;
+ val.str_value = rval.str_value;
+ }
+
+ state = STATE_OP;
+ }
+
+ break;
+
+ case STATE_OP:
+
+ if (*p == ' ')
+ break;
+
+ {
+ op_t *operator;
+ gboolean found = FALSE;
+
+ for (operator = operators; operator->op; ++operator) {
+ if (operator->op == *p) {
+ op = operator;
+ found = TRUE;
+ break;
+ }
+ }
+
+ if (!found) {
+ scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: unknown operator.");
+ g_free (val.str_value);
+ }
+ }
+
+ state = STATE_RVALUE;
+
+ break;
+ default:
+ break;
+ }
+ }
+
+ { // return value
+ value_t *rval = g_new (value_t, 1);
+
+ rval->type = val.type;
+ rval->int_value = val.int_value;
+ rval->str_value = val.str_value;
+
+ return rval;
+ }
+}
+
+static void do_eval (char *arg)
+{
+ value_t *val = process_expression (arg, strlen (arg));
+
+ if (!val) {
+ scr_LogPrint (LPRINT_NORMAL, "eval: Evaluation error.");
+ return;
+ }
+
+ if (val->type == TYPE_STR)
+ process_command (val->str_value, TRUE);
+ else
+ scr_LogPrint (LPRINT_NORMAL, "eval: Expression does not result in string.");
+
+ destroy_value (val);
+}
+
+static void do_let (char *arg)
+{
+ char *varname;
+ value_t *value;
+ int namelen;
+ char *val = strchr (arg, '=');
+
+ if (!val) {
+ scr_LogPrint (LPRINT_NORMAL, "let: Syntax error: no equal sign in line.");
+ return;
+ }
+
+ {
+ char *p = val;
+
+ for (p = val - 1; p >= arg && *p == ' '; --p);
+ if (p < arg) {
+ scr_LogPrint (LPRINT_NORMAL, "let: Syntax error: no destination variable name specified.");
+ return;
+ }
+
+ namelen = p + 1 - arg;
+ }
+
+ // evaluate expression
+ value = process_expression (val + 1, strlen (val + 1));
+
+ if (!value) {
+ scr_LogPrint (LPRINT_NORMAL, "let: Evaluation error.");
+ return;
+ }
+
+ if (value->type == TYPE_INT) {
+ value->type = TYPE_STR;
+ value->str_value = g_strdup_printf ("%d", value->int_value);
+ }
+
+ { // assign value
+ char *varname = g_strndup (arg, namelen);
+
+ if (value->str_value)
+ settings_set (SETTINGS_TYPE_OPTION, varname, value->str_value);
+ else
+ settings_del (SETTINGS_TYPE_OPTION, varname);
+
+ g_free (varname);
+ }
+
+ destroy_value (value);
+}
+
+static void do_if (char *arg)
+{
+ value_t *val = process_expression (arg, strlen (arg));
+
+ if (!val) {
+ scr_LogPrint (LPRINT_NORMAL, "if: Evaluation error.");
+ return;
+ }
+
+ if (val->type == TYPE_UNDEF) {
+ val->type = TYPE_INT;
+ val->int_value = 0;
+ }
+ if (val->type == TYPE_STR && check_value_type (val->str_value) == TYPE_INT) {
+ val->type = TYPE_INT;
+ val->int_value = atoi (val->str_value);
+ g_free (val->str_value);
+ val->str_value = NULL;
+ }
+
+ if (val->type == TYPE_INT)
+ ifresult = val->int_value ? TRUE : FALSE;
+ else if (val->str_value)
+ ifresult = TRUE;
+ else
+ ifresult = FALSE;
+
+ destroy_value (val);
+}
+
+static void do_then (char *arg)
+{
+ if (ifresult)
+ process_command (arg, TRUE);
+}
+
+static void do_else (char *arg)
+{
+ if (!ifresult)
+ process_command (arg, TRUE);
+}
+
+static void do_multi (char *arg)
+{
+ char *end;
+ char *start = arg;
+
+ for (end = strchr (start, ';'); end; end = strchr (start, ';')) {
+
+ // execute command
+ char *command = g_strndup (start, end - start);
+ process_command (command, TRUE);
+ g_free (command);
+
+ // skip leading spaces
+ for (start = end + 1; *start == ' '; ++start);
+ }
+
+ if (*start)
+ process_command (start, TRUE);
+}
+
+const gchar *g_module_check_init (GModule *module)
+{
+ cmd_add ("multi", "", COMPL_CMD, COMPL_CMD, do_multi, NULL);
+ cmd_add ("if", "", 0, 0, do_if, NULL);
+ cmd_add ("then", "", COMPL_CMD, COMPL_CMD, do_then, NULL);
+ cmd_add ("else", "", COMPL_CMD, COMPL_CMD, do_else, NULL);
+ cmd_add ("eval", "", 0, 0, do_eval, NULL);
+ cmd_add ("let", "", 0, 0, do_let, NULL);
+
+ return NULL;
+}
+
+void g_module_unload (GModule *module)
+{
+ cmd_del ("multi");
+ cmd_del ("if");
+ cmd_del ("then");
+ cmd_del ("else");
+ cmd_del ("eval");
+ cmd_del ("let");
+}
+
+/* The End */