medit/moo/mooscript/mooscript-context.c

679 lines
15 KiB
C
Raw Normal View History

2006-05-21 16:11:05 -07:00
/*
* mooscript-context.c
*
* Copyright (C) 2004-2006 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* 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.
*
* See COPYING file that comes with this distribution.
*/
2006-08-20 12:38:20 -07:00
#include "mooscript-context-private.h"
2006-05-14 16:45:13 -07:00
#include "mooscript-parser.h"
2006-08-20 12:38:20 -07:00
#include "mooscript-func-private.h"
2006-05-24 00:02:54 -07:00
#include "mooutils/moomarshals.h"
#include <glib/gprintf.h>
2006-02-26 01:30:57 -08:00
#include <gtk/gtkwindow.h>
#define N_POS_VARS 20
2006-08-20 12:38:20 -07:00
typedef void (*MSPrintFunc) (const char *string,
MSContext *ctx);
struct _MSContextPrivate {
GHashTable *vars;
MSError error;
char *error_msg;
MSPrintFunc print_func;
MSValue *return_val;
guint break_set : 1;
guint continue_set : 1;
guint return_set : 1;
int argc;
char **argv;
char *name;
};
2006-02-26 01:30:57 -08:00
enum {
PROP_0,
PROP_WINDOW,
PROP_NAME,
PROP_ARGV
2006-02-26 01:30:57 -08:00
};
2006-05-24 00:02:54 -07:00
enum {
GET_ENV_VAR,
N_SIGNALS
};
static guint signals[N_SIGNALS];
2006-02-24 20:39:12 -08:00
G_DEFINE_TYPE (MSContext, ms_context, G_TYPE_OBJECT)
2006-05-19 22:09:09 -07:00
#if GLIB_CHECK_VERSION(2,10,0)
#define ms_variable_alloc() g_slice_new0 (MSVariable)
#define ms_variable_free(v) g_slice_free (MSVariable, v)
#else
#define ms_variable_alloc() g_new0 (MSVariable, 1)
#define ms_variable_free(v) g_free (v)
#endif
2006-02-26 01:30:57 -08:00
static void
ms_context_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
MSContext *ctx = MS_CONTEXT (object);
switch (prop_id)
{
case PROP_WINDOW:
ctx->window = g_value_get_object (value);
g_object_notify (object, "window");
break;
case PROP_NAME:
2006-08-20 12:38:20 -07:00
g_free (ctx->priv->name);
ctx->window = g_strdup (g_value_get_string (value));
g_object_notify (object, "name");
break;
case PROP_ARGV:
2006-08-20 12:38:20 -07:00
g_strfreev (ctx->priv->argv);
ctx->priv->argv = g_strdupv (g_value_get_pointer (value));
ctx->priv->argc = ctx->priv->argv ? g_strv_length (ctx->priv->argv) : 0;
g_object_notify (object, "argv");
break;
2006-02-26 01:30:57 -08:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
ms_context_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
MSContext *ctx = MS_CONTEXT (object);
switch (prop_id)
{
case PROP_WINDOW:
g_value_set_object (value, ctx->window);
break;
case PROP_NAME:
2006-08-20 12:38:20 -07:00
g_value_set_string (value, ctx->priv->name);
break;
case PROP_ARGV:
2006-08-20 12:38:20 -07:00
g_value_set_pointer (value, ctx->priv->argv);
break;
2006-02-26 01:30:57 -08:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
default_print_func (const char *string,
2006-02-24 20:39:12 -08:00
G_GNUC_UNUSED MSContext *ctx)
{
g_print ("%s", string);
}
static GObject *
ms_context_constructor (GType type,
guint n_props,
GObjectConstructParam *props)
{
GObject *obj;
MSContext *ctx;
obj = G_OBJECT_CLASS(ms_context_parent_class)->constructor (type, n_props, props);
ctx = MS_CONTEXT (obj);
2006-08-20 12:38:20 -07:00
if (!ctx->priv->name)
{
2006-08-20 12:38:20 -07:00
if (ctx->priv->argv && ctx->priv->argc)
ctx->priv->name = g_strdup (ctx->priv->argv[0]);
else
2006-08-20 12:38:20 -07:00
ctx->priv->name = g_strdup ("script");
}
2006-08-20 12:38:20 -07:00
if (!ctx->priv->argv || !ctx->priv->argc)
{
2006-08-20 12:38:20 -07:00
g_strfreev (ctx->priv->argv);
ctx->priv->argv = g_new0 (char*, 2);
ctx->priv->argv[0] = g_strdup (ctx->priv->name);
ctx->priv->argc = 1;
}
return obj;
}
static void
2006-02-24 20:39:12 -08:00
ms_context_init (MSContext *ctx)
{
2006-08-20 12:38:20 -07:00
ctx->priv = G_TYPE_INSTANCE_GET_PRIVATE (ctx, MS_TYPE_CONTEXT, MSContextPrivate);
ctx->priv->vars = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
(GDestroyNotify) ms_variable_unref);
2006-08-20 12:38:20 -07:00
ctx->priv->print_func = default_print_func;
_ms_context_add_builtin (ctx);
}
static void
2006-02-24 20:39:12 -08:00
ms_context_finalize (GObject *object)
{
2006-02-24 20:39:12 -08:00
MSContext *ctx = MS_CONTEXT (object);
2006-08-20 12:38:20 -07:00
g_hash_table_destroy (ctx->priv->vars);
g_free (ctx->priv->error_msg);
2006-08-20 12:38:20 -07:00
ms_value_unref (ctx->priv->return_val);
2006-08-20 12:38:20 -07:00
g_free (ctx->priv->name);
g_strfreev (ctx->priv->argv);
2006-02-24 20:39:12 -08:00
G_OBJECT_CLASS(ms_context_parent_class)->finalize (object);
}
static void
2006-02-24 20:39:12 -08:00
ms_context_class_init (MSContextClass *klass)
{
2006-02-26 01:30:57 -08:00
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ms_context_finalize;
object_class->set_property = ms_context_set_property;
object_class->get_property = ms_context_get_property;
object_class->constructor = ms_context_constructor;
2006-02-26 01:30:57 -08:00
2006-08-20 12:38:20 -07:00
g_type_class_add_private (klass, sizeof (MSContextPrivate));
2006-03-07 23:10:49 -08:00
ms_type_init ();
2006-02-26 01:30:57 -08:00
g_object_class_install_property (object_class,
PROP_WINDOW,
g_param_spec_object ("window",
"window",
"window",
GTK_TYPE_WINDOW,
G_PARAM_READWRITE));
2006-05-24 00:02:54 -07:00
signals[GET_ENV_VAR] =
g_signal_new ("get-env-var",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (MSContextClass, get_env_var),
NULL, NULL,
_moo_marshal_BOXED__STRING,
MS_TYPE_VALUE, 1,
G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
}
static MSVariable *
ms_context_lookup_var (MSContext *ctx,
const char *name)
{
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
g_return_val_if_fail (name != NULL, NULL);
return g_hash_table_lookup (ctx->priv->vars, name);
}
2006-02-24 20:39:12 -08:00
MSValue *
ms_context_eval_variable (MSContext *ctx,
const char *name)
{
2006-02-24 20:39:12 -08:00
MSVariable *var;
2006-02-24 20:39:12 -08:00
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
g_return_val_if_fail (name != NULL, NULL);
2006-02-24 20:39:12 -08:00
var = ms_context_lookup_var (ctx, name);
if (!var)
return ms_context_format_error (ctx, MS_ERROR_NAME,
"no variable named '%s'",
name);
if (var->value)
2006-02-24 20:39:12 -08:00
return ms_value_ref (var->value);
2006-08-20 12:38:20 -07:00
return _ms_func_call (var->func, NULL, 0, ctx);
}
gboolean
ms_context_assign_variable (MSContext *ctx,
const char *name,
MSValue *value)
{
2006-02-24 20:39:12 -08:00
MSVariable *var;
2006-02-24 20:39:12 -08:00
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
g_return_val_if_fail (name != NULL, FALSE);
2006-02-24 20:39:12 -08:00
var = ms_context_lookup_var (ctx, name);
if (value)
{
if (var)
{
if (var->func)
{
g_object_unref (var->func);
var->func = NULL;
}
if (var->value != value)
{
2006-02-24 20:39:12 -08:00
ms_value_unref (var->value);
var->value = ms_value_ref (value);
}
}
else
{
2006-02-24 20:39:12 -08:00
var = ms_variable_new_value (value);
ms_context_set_var (ctx, name, var);
ms_variable_unref (var);
}
}
else if (var)
{
2006-02-24 20:39:12 -08:00
ms_context_set_var (ctx, name, NULL);
}
return TRUE;
}
2006-04-19 11:06:05 -07:00
gboolean
ms_context_assign_string (MSContext *ctx,
const char *name,
const char *str_value)
{
MSValue *value = NULL;
gboolean retval;
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
g_return_val_if_fail (name != NULL, FALSE);
if (str_value)
value = ms_value_string (str_value);
retval = ms_context_assign_variable (ctx, name, value);
ms_value_unref (value);
return retval;
}
gboolean
ms_context_assign_positional (MSContext *ctx,
guint n,
MSValue *value)
{
char *name;
gboolean result;
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
2006-12-02 23:49:14 -08:00
name = g_strdup_printf ("_%u", n);
result = ms_context_assign_variable (ctx, name, value);
g_free (name);
return result;
}
gboolean
2006-02-24 20:39:12 -08:00
ms_context_set_var (MSContext *ctx,
const char *name,
2006-02-24 20:39:12 -08:00
MSVariable *var)
{
2006-02-24 20:39:12 -08:00
MSVariable *old;
2006-02-24 20:39:12 -08:00
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
g_return_val_if_fail (name != NULL, FALSE);
2006-08-20 12:38:20 -07:00
old = g_hash_table_lookup (ctx->priv->vars, name);
if (var != old)
{
if (var)
2006-08-20 12:38:20 -07:00
g_hash_table_insert (ctx->priv->vars,
g_strdup (name),
2006-02-24 20:39:12 -08:00
ms_variable_ref (var));
else
2006-08-20 12:38:20 -07:00
g_hash_table_remove (ctx->priv->vars, name);
}
return TRUE;
}
gboolean
2006-02-24 20:39:12 -08:00
ms_context_set_func (MSContext *ctx,
const char *name,
2006-02-24 20:39:12 -08:00
MSFunc *func)
{
2006-03-07 22:00:49 -08:00
MSValue *vfunc;
gboolean ret;
2006-02-24 20:39:12 -08:00
g_return_val_if_fail (MS_IS_CONTEXT (ctx), FALSE);
g_return_val_if_fail (name != NULL, FALSE);
2006-02-24 20:39:12 -08:00
g_return_val_if_fail (!func || MS_IS_FUNC (func), FALSE);
2006-03-07 22:00:49 -08:00
vfunc = ms_value_func (func);
ret = ms_context_assign_variable (ctx, name, vfunc);
ms_value_unref (vfunc);
2006-03-07 22:00:49 -08:00
return ret;
}
2006-02-24 20:39:12 -08:00
MSValue *
ms_context_set_error (MSContext *ctx,
MSError error,
const char *message)
{
const char *errname;
2006-02-24 20:39:12 -08:00
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
2006-08-20 12:38:20 -07:00
g_return_val_if_fail (!ctx->priv->error && error, NULL);
g_return_val_if_fail (!ctx->priv->error_msg, NULL);
2006-08-20 12:38:20 -07:00
ctx->priv->error = error;
2006-02-24 20:39:12 -08:00
errname = ms_context_get_error_msg (ctx);
if (message && *message)
2006-08-20 12:38:20 -07:00
ctx->priv->error_msg = g_strdup_printf ("%s: %s", errname, message);
else
2006-08-20 12:38:20 -07:00
ctx->priv->error_msg = g_strdup (message);
return NULL;
}
2006-02-24 20:39:12 -08:00
MSValue *
ms_context_format_error (MSContext *ctx,
MSError error,
const char *format,
...)
{
va_list args;
2006-03-14 14:27:40 -08:00
char *string;
2006-02-24 20:39:12 -08:00
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
2006-08-20 12:38:20 -07:00
g_return_val_if_fail (!ctx->priv->error && error, NULL);
g_return_val_if_fail (!ctx->priv->error_msg, NULL);
if (!format || !format[0])
2006-03-14 12:59:07 -08:00
{
2006-02-24 20:39:12 -08:00
ms_context_set_error (ctx, error, NULL);
2006-03-14 12:59:07 -08:00
return NULL;
}
va_start (args, format);
2006-08-20 12:38:20 -07:00
string = _ms_vaprintf (format, args);
va_end (args);
2006-03-14 14:27:40 -08:00
ms_context_set_error (ctx, error, string);
g_free (string);
return NULL;
}
void
2006-02-24 20:39:12 -08:00
ms_context_clear_error (MSContext *ctx)
{
2006-02-24 20:39:12 -08:00
g_return_if_fail (MS_IS_CONTEXT (ctx));
2006-08-20 12:38:20 -07:00
ctx->priv->error = MS_ERROR_NONE;
g_free (ctx->priv->error_msg);
ctx->priv->error_msg = NULL;
}
const char *
2006-02-24 20:39:12 -08:00
ms_context_get_error_msg (MSContext *ctx)
{
static const char *msgs[MS_ERROR_LAST] = {
NULL, "Type error", "Value error", "Name error",
"Runtime error"
};
2006-02-24 20:39:12 -08:00
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
2006-08-20 12:38:20 -07:00
g_return_val_if_fail (ctx->priv->error < MS_ERROR_LAST, NULL);
g_return_val_if_fail (ctx->priv->error != MS_ERROR_NONE, "ERROR");
2006-08-20 12:38:20 -07:00
if (ctx->priv->error_msg)
return ctx->priv->error_msg;
else
2006-08-20 12:38:20 -07:00
return msgs[ctx->priv->error];
}
2006-02-24 20:39:12 -08:00
static MSVariable *
ms_variable_new (void)
{
2006-05-19 22:09:09 -07:00
MSVariable *var = ms_variable_alloc ();
var->ref_count = 1;
return var;
}
2006-02-24 20:39:12 -08:00
MSVariable *
ms_variable_new_value (MSValue *value)
{
2006-02-24 20:39:12 -08:00
MSVariable *var;
g_return_val_if_fail (value != NULL, NULL);
2006-02-24 20:39:12 -08:00
var = ms_variable_new ();
var->value = ms_value_ref (value);
return var;
}
#if 0
2006-02-24 20:39:12 -08:00
MSVariable *
ms_variable_new_func (MSFunc *func)
{
2006-02-24 20:39:12 -08:00
MSVariable *var;
2006-02-24 20:39:12 -08:00
g_return_val_if_fail (MS_IS_FUNC (func), NULL);
2006-02-24 20:39:12 -08:00
var = ms_variable_new ();
var->func = g_object_ref (func);
return var;
}
#endif
2006-02-24 20:39:12 -08:00
MSVariable *
ms_variable_ref (MSVariable *var)
{
g_return_val_if_fail (var != NULL, NULL);
var->ref_count++;
return var;
}
void
2006-02-24 20:39:12 -08:00
ms_variable_unref (MSVariable *var)
{
g_return_if_fail (var != NULL);
if (!--var->ref_count)
{
2006-03-07 23:10:49 -08:00
ms_value_unref (var->value);
if (var->func)
g_object_unref (var->func);
2006-05-19 22:09:09 -07:00
ms_variable_free (var);
}
}
void
2006-08-20 12:38:20 -07:00
_ms_context_set_return (MSContext *ctx,
MSValue *val)
{
2006-08-20 12:38:20 -07:00
g_assert (MS_IS_CONTEXT (ctx));
g_return_if_fail (!ctx->priv->return_set);
ctx->priv->return_set = TRUE;
ctx->priv->return_val = val ? ms_value_ref (val) : ms_value_none ();
}
MSValue *
_ms_context_get_return (MSContext *ctx)
{
g_assert (MS_IS_CONTEXT (ctx));
g_return_val_if_fail (ctx->priv->return_set, NULL);
return ms_value_ref (ctx->priv->return_val);
}
void
2006-08-20 12:38:20 -07:00
_ms_context_set_break (MSContext *ctx)
{
2006-08-20 12:38:20 -07:00
g_assert (MS_IS_CONTEXT (ctx));
g_return_if_fail (!ctx->priv->break_set);
ctx->priv->break_set = TRUE;
}
void
2006-08-20 12:38:20 -07:00
_ms_context_set_continue (MSContext *ctx)
{
2006-08-20 12:38:20 -07:00
g_assert (MS_IS_CONTEXT (ctx));
g_return_if_fail (!ctx->priv->continue_set);
ctx->priv->continue_set = TRUE;
}
void
2006-08-20 12:38:20 -07:00
_ms_context_unset_return (MSContext *ctx)
{
2006-08-20 12:38:20 -07:00
g_assert (MS_IS_CONTEXT (ctx));
g_return_if_fail (ctx->priv->return_set);
ctx->priv->return_set = FALSE;
ms_value_unref (ctx->priv->return_val);
ctx->priv->return_val = NULL;
}
void
2006-08-20 12:38:20 -07:00
_ms_context_unset_break (MSContext *ctx)
{
2006-08-20 12:38:20 -07:00
g_assert (MS_IS_CONTEXT (ctx));
g_return_if_fail (ctx->priv->break_set);
ctx->priv->break_set = FALSE;
}
void
2006-08-20 12:38:20 -07:00
_ms_context_unset_continue (MSContext *ctx)
{
2006-08-20 12:38:20 -07:00
g_assert (MS_IS_CONTEXT (ctx));
g_return_if_fail (ctx->priv->continue_set);
ctx->priv->continue_set = FALSE;
}
gboolean
_ms_context_return_set (MSContext *ctx)
{
g_assert (MS_IS_CONTEXT (ctx));
return ctx->priv->return_set;
}
gboolean
_ms_context_break_set (MSContext *ctx)
{
g_assert (MS_IS_CONTEXT (ctx));
return ctx->priv->break_set;
}
gboolean
_ms_context_continue_set (MSContext *ctx)
{
g_assert (MS_IS_CONTEXT (ctx));
return ctx->priv->continue_set;
}
gboolean
_ms_context_error_set (MSContext *ctx)
{
g_assert (MS_IS_CONTEXT (ctx));
return ctx->priv->error != 0;
}
2006-03-14 12:59:07 -08:00
#if 0
2006-05-14 16:45:13 -07:00
MSValue *
ms_context_run_script (MSContext *ctx,
const char *script)
{
MSNode *node;
MSValue *result;
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
g_return_val_if_fail (script != NULL, NULL);
node = ms_script_parse (script);
g_return_val_if_fail (node != NULL, NULL);
result = ms_top_node_eval (node, ctx);
ms_node_unref (node);
return result;
}
#endif
2006-05-14 16:45:13 -07:00
2006-05-24 00:02:54 -07:00
MSValue *
ms_context_get_env_variable (MSContext *ctx,
const char *name)
{
MSValue *val = NULL;
g_return_val_if_fail (MS_IS_CONTEXT (ctx), NULL);
g_return_val_if_fail (name != NULL, NULL);
g_signal_emit (ctx, signals[GET_ENV_VAR], 0, name, &val);
return val;
}
2006-08-20 12:38:20 -07:00
void
_ms_context_print (MSContext *ctx,
const char *string)
{
g_assert (MS_IS_CONTEXT (ctx));
ctx->priv->print_func (string, ctx);
}