medit/moo/mooutils/mooscript/mooscript-node.h

297 lines
8.3 KiB
C

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4; coding: utf-8 -*-
*
* mooscript-node.h
*
* 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.
*/
#ifndef __MOO_SCRIPT_NODE_H__
#define __MOO_SCRIPT_NODE_H__
#include "mooscript-func.h"
G_BEGIN_DECLS
typedef enum {
MS_TYPE_NODE_0,
MS_TYPE_NODE_LIST,
MS_TYPE_NODE_LIST_ELM,
MS_TYPE_NODE_VAR,
MS_TYPE_NODE_COMMAND,
MS_TYPE_NODE_IF_ELSE,
MS_TYPE_NODE_WHILE,
MS_TYPE_NODE_FOR,
MS_TYPE_NODE_ASSIGN,
MS_TYPE_NODE_LIST_ASSIGN,
MS_TYPE_NODE_VALUE,
MS_TYPE_NODE_VAL_LIST,
MS_TYPE_NODE_PYTHON,
MS_TYPE_NODE_RETURN,
MS_TYPE_NODE_BREAK,
MS_TYPE_NODE_LAST
} MSNodeType;
typedef struct _MSNode MSNode;
typedef struct _MSNodeList MSNodeList;
typedef struct _MSNodeListElm MSNodeListElm;
typedef struct _MSNodeVar MSNodeVar;
typedef struct _MSNodeCommand MSNodeCommand;
typedef struct _MSNodeIfElse MSNodeIfElse;
typedef struct _MSNodeWhile MSNodeWhile;
typedef struct _MSNodeFor MSNodeFor;
typedef struct _MSNodeAssign MSNodeAssign;
typedef struct _MSNodeListAssign MSNodeListAssign;
typedef struct _MSNodeValue MSNodeValue;
typedef struct _MSNodeValList MSNodeValList;
typedef struct _MSNodePython MSNodePython;
typedef struct _MSNodeBreak MSNodeBreak;
typedef struct _MSNodeReturn MSNodeReturn;
typedef MSValue* (*MSNodeEval) (MSNode *node, MSContext *ctx);
typedef void (*MSNodeDestroy) (MSNode *node);
struct _MSNode {
guint ref_count;
MSNodeEval eval;
MSNodeDestroy destroy;
MSNodeType type : 4;
};
#if 0
#define MS_NODE(node_) ((MSNode*) node_)
#else
inline static MSNode*
_ms_node_check (gpointer pnode)
{
MSNode *node = pnode;
g_assert (node != NULL);
g_assert (node->type && node->type < MS_TYPE_NODE_LAST);
return pnode;
}
#define MS_NODE(node_) _ms_node_check (node_)
#endif
#define MS_NODE_TYPE(node_) (MS_NODE(node_)->type)
#if 0
#define MS_NODE_CAST(node_,type_,Type_) ((Type_*) node_)
#else
inline static gpointer
_ms_node_check_type (gpointer pnode,
MSNodeType type)
{
MSNode *node = pnode;
g_assert (node != NULL);
g_assert (MS_NODE_TYPE (node) == type);
return pnode;
}
#define MS_NODE_CAST(node_,type_,Type_) \
((Type_*) _ms_node_check_type (node_, type_))
#endif
#define MS_NODE_LIST(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_LIST, MSNodeList)
#define MS_NODE_LIST_ELM(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_LIST_ELM, MSNodeListElm)
#define MS_NODE_VAR(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_VAR, MSNodeVar)
#define MS_NODE_COMMAND(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_COMMAND, MSNodeCommand)
#define MS_NODE_IF_ELSE(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_IF_ELSE, MSNodeIfElse)
#define MS_NODE_WHILE(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_WHILE, MSNodeWhile)
#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_LIST_ASSIGN(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_LIST_ASSIGN, MSNodeListAssign)
#define MS_NODE_VALUE(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_VALUE, MSNodeValue)
#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)
#define MS_NODE_RETURN(node_) MS_NODE_CAST (node_, MS_TYPE_NODE_RETURN, MSNodeReturn)
#define MS_IS_NODE_VAR(node) (node && MS_NODE_TYPE(node) == MS_TYPE_NODE_VAR)
#define MS_IS_NODE_VALUE(node) (node && MS_NODE_TYPE(node) == MS_TYPE_NODE_VALUE)
#define MS_IS_NODE_VAL_LIST(node) (node && MS_NODE_TYPE(node) == MS_TYPE_NODE_VAL_LIST)
#define MS_IS_NODE_LIST(node) (node && MS_NODE_TYPE(node) == MS_TYPE_NODE_LIST)
#define MS_IS_NODE_RETURN(node) (node && MS_NODE_TYPE(node) == MS_TYPE_NODE_RETURN)
#define MS_IS_NODE_BREAK(node) (node && MS_NODE_TYPE(node) == MS_TYPE_NODE_BREAK)
struct _MSNodeList {
MSNode node;
MSNode **nodes;
guint n_nodes;
guint n_nodes_allocd__;
};
struct _MSNodeListElm {
MSNode node;
MSNode *list;
MSNode *ind;
};
struct _MSNodeVar {
MSNode node;
char *name;
};
struct _MSNodeCommand {
MSNode node;
char *name;
MSNodeList *args;
};
typedef enum {
MS_COND_BEFORE,
MS_COND_AFTER
} MSCondType;
struct _MSNodeWhile {
MSNode node;
MSCondType type;
MSNode *condition;
MSNode *what;
};
struct _MSNodeFor {
MSNode node;
MSNode *variable;
MSNode *list;
MSNode *what;
};
struct _MSNodeIfElse {
MSNode node;
MSNode *condition;
MSNode *then_;
MSNode *else_;
};
struct _MSNodeAssign {
MSNode node;
MSNodeVar *var;
MSNode *val;
};
struct _MSNodeListAssign {
MSNode node;
MSNode *list;
MSNode *ind;
MSNode *val;
};
struct _MSNodeValue {
MSNode node;
MSValue *value;
};
struct _MSNodePython {
MSNode node;
char *script;
};
typedef enum {
MS_VAL_LIST,
MS_VAL_RANGE
} MSValListType;
struct _MSNodeValList {
MSNode node;
MSValListType type;
MSNodeList *elms;
MSNode *first;
MSNode *last;
};
typedef enum {
MS_BREAK_BREAK,
MS_BREAK_CONTINUE
} MSBreakType;
struct _MSNodeBreak {
MSNode node;
MSBreakType type;
};
struct _MSNodeReturn {
MSNode node;
MSNode *val;
};
gpointer ms_node_ref (gpointer node);
void ms_node_unref (gpointer node);
MSValue *ms_top_node_eval (MSNode *node,
MSContext *ctx);
MSValue *_ms_node_eval (MSNode *node,
MSContext *ctx);
MSNodeList *ms_node_list_new (void);
void ms_node_list_add (MSNodeList *list,
MSNode *node);
MSNodeCommand *ms_node_command_new (const char *name,
MSNodeList *args);
MSNodeCommand *ms_node_binary_op_new (MSBinaryOp op,
MSNode *lval,
MSNode *rval);
MSNodeCommand *ms_node_unary_op_new (MSUnaryOp op,
MSNode *val);
MSNodeIfElse *ms_node_if_else_new (MSNode *condition,
MSNode *then_,
MSNode *else_);
MSNodeWhile *ms_node_while_new (MSCondType type,
MSNode *cond,
MSNode *what);
MSNodeFor *ms_node_for_new (MSNode *var,
MSNode *list,
MSNode *what);
MSNodeAssign *ms_node_assign_new (MSNodeVar *var,
MSNode *val);
MSNodeValue *ms_node_value_new (MSValue *value);
MSNodeValList *ms_node_val_list_new (MSNodeList *list);
MSNodeValList *ms_node_val_range_new (MSNode *first,
MSNode *last);
MSNodeVar *ms_node_var_new (const char *name);
MSNodePython *ms_node_python_new (const char *script);
MSNodeListElm *ms_node_list_elm_new (MSNode *list,
MSNode *ind);
MSNodeListAssign *ms_node_list_assign_new (MSNode *list,
MSNode *ind,
MSNode *val);
MSNodeBreak *ms_node_break_new (MSBreakType type);
MSNodeReturn *ms_node_return_new (MSNode *val);
G_END_DECLS
#endif /* __MOO_SCRIPT_NODE_H__ */