Skip to content

Commit

Permalink
Add (vim-eval)
Browse files Browse the repository at this point in the history
  • Loading branch information
kana committed Sep 13, 2008
1 parent 8fd0efd commit 16dfe8d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 15 deletions.
22 changes: 15 additions & 7 deletions runtime/doc/if_gosh.txt
Expand Up @@ -8,7 +8,7 @@ The Gauche Interface to Vim *gosh* *Gauche*

1. Commands |gauche-commands|
2. The vim module |gauche-vim|
3. Rules for converting values |gauche-conversion-rule|
3. Rules for converting values |gauche-vim-conversion-rule|
4. Remarks |gauche-remarks|

{Vi does not have any of these commands}
Expand Down Expand Up @@ -70,6 +70,7 @@ Function: (vim-execute {vim-script}) *(vim-execute)*
Function: (vim-eval {vim-script}) *(vim-eval)*
Evaluate a string as {vim-script}, then return the
result. This is equivalent to |eval()| in Vim script.
See also |gauche-vim-conversion-rules|.

Note that {vim-script} must be a valid one noted in
|expression-syntax|, for example, "1 + 2". It cannot
Expand All @@ -89,18 +90,25 @@ Function: (vim-echoerr-port) *(vim-echoerr-port)*
showed by |:echoerr|.

==============================================================================
3. Rules for converting values *gauche-conversion-rules*
3. Rules for converting values *gauche-vim-conversion-rules*

VIM TO GAUCHE *vim-to-gauche*

Vim script Gauhce Remarks ~
---------- ------ -------
Number <integer> error on overflow
Float <real> may lose the original value
{only when compiled with the |+float| feature}
String <string> immutable / may be incomplete string
List <vector> immutable
Dictionary <hash-table> immutable / type is string=?
Float <real> |+float| is required
String <string> may be incomplete string
List <list> -
Dictionary <hash-table> type is string=?
Funcref <vim-funcref> applicable

BUGS: Currently, lists and dictionaries which are circular cannot be
converted. If such values are exported to the Gauche level, Vim will be
crashed.

BUGS: Funcref is not supported yet.

==============================================================================
4. Remarks *gauche-remarks*

Expand Down
2 changes: 1 addition & 1 deletion src/eval.c
Expand Up @@ -5845,7 +5845,7 @@ list_equal(l1, l2, ic)
return item1 == NULL && item2 == NULL;
}

#if defined(FEAT_PYTHON) || defined(PROTO)
#if defined(FEAT_GAUCHE) || defined(FEAT_PYTHON) || defined(PROTO)
/*
* Return the dictitem that an entry in a hashtable points to.
*/
Expand Down
114 changes: 107 additions & 7 deletions src/if_gosh.c
Expand Up @@ -22,8 +22,7 @@


/* Common Utilities */ /*{{{1*/

/* Wrappers for Scm_Printf() - output by :echomsg or :echoerr */
/* Wrappers for Scm_Printf() - output by :echomsg or :echoerr */ /*{{{2*/

static void
Scm_Xmsgf(const char *fmt, va_list ap, int echoerrp)
Expand Down Expand Up @@ -58,6 +57,83 @@ Scm_Emsgf(const char* fmt, ...)



/* Conversion rules for values between Vim script and Gauche */ /*{{{2*/

static ScmObj
vim_to_gauche(typval_T* tv)
{
switch (tv->v_type)
{
default:
Scm_Error("Internal error: Unexpected tv->v_type: %d",
(int)(tv->v_type));
case VAR_NUMBER:
return Scm_MakeInteger(tv->vval.v_number);
#ifdef FEAT_FLOAT
case VAR_FLOAT:
return Scm_MakeFlonum(tv->vval.v_float);
#endif
case VAR_STRING:
return SCM_MAKE_STR_COPYING((char *)(tv->vval.v_string));

case VAR_LIST: /* TODO: Support circular list */
{
list_T *l = tv->vval.v_list;
listitem_T *li;
ScmObj slist = SCM_NIL;

if (l != NULL)
{
for (li = l->lv_last; li != NULL; li = li->li_prev)
{
slist = Scm_Cons(vim_to_gauche(&(li->li_tv)), slist);
}
}
return slist;
}
case VAR_DICT: /* TODO: Support circular dictionary */
{
dict_T *vdict = tv->vval.v_dict;
ScmObj shash;

if (vdict != NULL)
{
hashtab_T *ht = &(vdict->dv_hashtab);
hashitem_T *hi;
dictitem_T *di;
long_u todo = ht->ht_used;
shash = Scm_MakeHashTableSimple(SCM_HASH_STRING, (int)todo);

for (hi = ht->ht_array; 0 < todo; hi++)
{
if (!HASHITEM_EMPTY(hi))
{
todo--;
di = dict_lookup(hi);
Scm_HashTableSet(
SCM_HASH_TABLE(shash),
SCM_MAKE_STR_COPYING((char *)(hi->hi_key)),
vim_to_gauche(&(di->di_tv)),
0
);
}
}
}
else
{
shash = Scm_MakeHashTableSimple(SCM_HASH_STRING, 0);
}
return shash;
}
case VAR_FUNC:
Scm_Error("Funcref is not supported yet"); /* TODO */
}
return SCM_NIL;
}







Expand Down Expand Up @@ -243,7 +319,33 @@ static SCM_DEFINE_SUBR(vim_echoerr_port_STUB, 0, 0,



/* vim-execute and vim-eval */ /*{{{2*/
/* vim-eval and vim-execute */ /*{{{2*/
/* TODO: provide choice for caller - do :echoerr by Vim or raise a condition
* for Gauche if an error is occured in the given vim script. */

static ScmObj
vim_eval_proc(ScmObj *args, int nargs, void *data)
{
ScmObj s = args[0];
typval_T *tv;
ScmObj result;

if (!SCM_STRINGP(s))
Scm_TypeError("vim-eval", "string", s);

tv = eval_expr((char_u *)Scm_GetString(SCM_STRING(s)), NULL);
if (tv == NULL)
Scm_Error("Invalid expression: %S", s);

result = vim_to_gauche(tv);

free_tv(tv);
return result;
}
static SCM_DEFINE_STRING_CONST(vim_eval_NAME, "vim-eval", 8, 8);
static SCM_DEFINE_SUBR(vim_eval_STUB, 1, 0,
SCM_OBJ(&vim_eval_NAME), vim_eval_proc,
NULL, NULL);

static ScmObj
vim_execute_proc(ScmObj *args, int nargs, void *data)
Expand All @@ -253,9 +355,6 @@ vim_execute_proc(ScmObj *args, int nargs, void *data)
if (!SCM_STRINGP(s))
Scm_TypeError("vim-execute", "string", s);

/* TODO: provide choice for caller - do :echoerr by Vim or raise
* a condition for Gauche if an error is occured in the given vim
* script. */
do_cmdline_cmd((char_u*)Scm_GetString(SCM_STRING(s)));
return SCM_UNDEFINED;
}
Expand Down Expand Up @@ -323,7 +422,8 @@ gauche_init()
SCM_PORT(Scm_MakeVirtualPort(SCM_CLASS_PORT, SCM_PORT_OUTPUT,
&scm_null_port_vtable)));

/* (vim-execute) and (vim-eval) */
/* (vim-eval) and (vim-execute) */
SCM_DEFINE(Scm_UserModule(), "vim-eval", SCM_OBJ(&vim_eval_STUB));
SCM_DEFINE(Scm_UserModule(), "vim-execute", SCM_OBJ(&vim_execute_STUB));
}

Expand Down

0 comments on commit 16dfe8d

Please sign in to comment.