Added $VAR thing to scripts

master
Yevgen Muntyan 2006-05-24 02:02:54 -05:00
parent 1abb512cb3
commit 874d5c971b
8 changed files with 591 additions and 412 deletions

View File

@ -14,6 +14,7 @@ BOOL:STRING,OBJECT,OBJECT,INT,INT,POINTER,UINT,UINT
BOOL:STRING,POINTER
BOOL:VOID
BOOL:VOID
BOXED:STRING
INT:VOID
OBJECT:VOID
OBJECT:OBJECT

View File

@ -13,6 +13,7 @@
#include "mooscript-context.h"
#include "mooscript-parser.h"
#include "mooutils/moomarshals.h"
#include <glib/gprintf.h>
#include <gtk/gtkwindow.h>
@ -25,6 +26,13 @@ enum {
PROP_ARGV
};
enum {
GET_ENV_VAR,
N_SIGNALS
};
static guint signals[N_SIGNALS];
G_DEFINE_TYPE (MSContext, ms_context, G_TYPE_OBJECT)
@ -191,6 +199,16 @@ ms_context_class_init (MSContextClass *klass)
"window",
GTK_TYPE_WINDOW,
G_PARAM_READWRITE));
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);
}
@ -585,6 +603,21 @@ ms_context_run_script (MSContext *ctx,
}
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;
}
/************************************************************************/
/* Python
*/

View File

@ -20,12 +20,12 @@
G_BEGIN_DECLS
#define MS_TYPE_CONTEXT (ms_context_get_type ())
#define MS_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), MS_TYPE_CONTEXT, MSContext))
#define MS_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MS_TYPE_CONTEXT, MSContextClass))
#define MS_IS_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), MS_TYPE_CONTEXT))
#define MS_IS_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MS_TYPE_CONTEXT))
#define MS_CONTEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MS_TYPE_CONTEXT, MSContextClass))
#define MS_TYPE_CONTEXT (ms_context_get_type ())
#define MS_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), MS_TYPE_CONTEXT, MSContext))
#define MS_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MS_TYPE_CONTEXT, MSContextClass))
#define MS_IS_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), MS_TYPE_CONTEXT))
#define MS_IS_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MS_TYPE_CONTEXT))
#define MS_CONTEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MS_TYPE_CONTEXT, MSContextClass))
typedef struct _MSContextClass MSContextClass;
typedef struct _MSVariable MSVariable;
@ -70,6 +70,9 @@ struct _MSContext {
struct _MSContextClass {
GObjectClass object_class;
MSValue* (*get_env_var) (MSContext *ctx,
const char *name);
};
@ -98,6 +101,9 @@ gboolean ms_context_assign_string (MSContext *ctx,
const char *name,
const char *value);
MSValue *ms_context_get_env_variable (MSContext *ctx,
const char *name);
MSVariable *ms_context_lookup_var (MSContext *ctx,
const char *name);
gboolean ms_context_set_var (MSContext *ctx,

View File

@ -181,6 +181,69 @@ ms_node_list_add (MSNodeList *list,
}
/****************************************************************************/
/* MSNodeEnvVar
*/
static void
ms_node_env_var_destroy (MSNode *node)
{
MSNodeEnvVar *var = MS_NODE_ENV_VAR (node);
ms_node_unref (var->name);
}
static MSValue *
ms_node_env_var_eval (MSNode *node,
MSContext *ctx)
{
MSValue *name, *ret;
MSNodeEnvVar *var = MS_NODE_ENV_VAR (node);
name = _ms_node_eval (var->name, ctx);
if (!name)
return NULL;
if (MS_VALUE_TYPE (name) != MS_VALUE_STRING || !name->str)
{
ms_context_format_error (ctx, MS_ERROR_TYPE,
"in $(%v): variable name must be a string",
name);
ms_value_unref (name);
return NULL;
}
ret = ms_context_get_env_variable (ctx, name->str);
ms_value_unref (name);
if (ret || ctx->error)
return ret;
return var->dflt ? _ms_node_eval (var->dflt, ctx) : ms_value_none ();
}
MSNodeEnvVar *
ms_node_env_var_new (MSNode *name,
MSNode *dflt)
{
MSNodeEnvVar *var;
g_return_val_if_fail (name != NULL, NULL);
var = NODE_NEW (MSNodeEnvVar,
MS_TYPE_NODE_ENV_VAR,
ms_node_env_var_eval,
ms_node_env_var_destroy);
var->name = ms_node_ref (name);
var->dflt = dflt ? ms_node_ref (dflt) : NULL;
return var;
}
/****************************************************************************/
/* MSNodeVar
*/

View File

@ -23,6 +23,7 @@ typedef enum {
MS_TYPE_NODE_0,
MS_TYPE_NODE_LIST,
MS_TYPE_NODE_VAR,
MS_TYPE_NODE_ENV_VAR,
MS_TYPE_NODE_FUNCTION,
MS_TYPE_NODE_IF_ELSE,
MS_TYPE_NODE_WHILE,
@ -52,6 +53,7 @@ typedef struct _MSNodeWhile MSNodeWhile;
typedef struct _MSNodeFor MSNodeFor;
typedef struct _MSNodeAssign MSNodeAssign;
typedef struct _MSNodeValue MSNodeValue;
typedef struct _MSNodeEnvVar MSNodeEnvVar;
typedef struct _MSNodeValList MSNodeValList;
typedef struct _MSNodePython MSNodePython;
typedef struct _MSNodeBreak MSNodeBreak;
@ -115,6 +117,7 @@ _ms_node_check_type (gpointer pnode,
#define MS_NODE_FOR(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_FOR, MSNodeFor)
#define MS_NODE_ASSIGN(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_ASSIGN, MSNodeAssign)
#define MS_NODE_VALUE(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_VALUE, MSNodeValue)
#define MS_NODE_ENV_VAR(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_ENV_VAR, MSNodeEnvVar)
#define MS_NODE_VAL_LIST(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_VAL_LIST, MSNodeValList)
#define MS_NODE_PYTHON(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_PYTHON, MSNodePython)
#define MS_NODE_BREAK(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_BREAK, MSNodeBreak)
@ -209,6 +212,13 @@ struct _MSNodeValue {
};
struct _MSNodeEnvVar {
MSNode node;
MSNode *name;
MSNode *dflt;
};
struct _MSNodePython {
MSNode node;
char *script;
@ -311,6 +321,8 @@ MSNodeValList *ms_node_val_list_new (MSNodeList *list);
MSNodeValList *ms_node_val_range_new (MSNode *first,
MSNode *last);
MSNodeEnvVar *ms_node_env_var_new (MSNode *name,
MSNode *dflt);
MSNodeVar *ms_node_var_new (const char *name);
MSNodePython *ms_node_python_new (const char *script);

File diff suppressed because it is too large Load Diff

View File

@ -91,7 +91,7 @@
#if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
#line 376 "/home/muntyan/projects/moo/moo/mooutils/mooscript/mooscript-yacc.y"
#line 390 "/home/muntyan/projects/moo/moo/mooutils/mooscript/mooscript-yacc.y"
typedef union YYSTYPE {
int ival;
const char *str;

View File

@ -337,6 +337,20 @@ node_continue (MSParser *parser)
}
static MSNode *
node_env_var (MSParser *parser,
MSNode *var,
MSNode *deflt)
{
MSNodeEnvVar *node;
node = ms_node_env_var_new (var, deflt);
_ms_parser_add_node (parser, node);
return MS_NODE (node);
}
static MSNode *
node_dict_elm (MSParser *parser,
MSNode *dict,
@ -504,6 +518,9 @@ simple_expr:
| simple_expr '(' list_elms ')' { $$ = node_function (parser, $1, $3 ? MS_NODE_LIST ($3) : NULL); }
| simple_expr '[' expr ']' { $$ = node_get_item (parser, $1, $3); }
| simple_expr '.' IDENTIFIER { $$ = node_dict_elm (parser, $1, $3); }
| '$' '(' stmt ')' { $$ = node_env_var (parser, $3, NULL); }
| '$' '(' stmt ',' stmt ')' { $$ = node_env_var (parser, $3, $5); }
| '$' IDENTIFIER { $$ = node_env_var (parser, node_string (parser, $2), NULL); }
;
list_elms: /* empty */ { $$ = NULL; }