Skip to content

Commit

Permalink
operator: add operators for accessing the socpe stack
Browse files Browse the repository at this point in the history
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed May 2, 2021
1 parent f13d16e commit 7f9e4f2
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Units/optscript.r/op-scope.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--langdef=X
--map-X=.unknown
--kinddef-X=p,package,packages
--kinddef-X=d,def,definitions

--regex-X=/def[ ]+([a-z]+)[ ]*:[ ]*([a-z]+)/\1/d/{{
. [ (typename) \2 ] typeref:
. _scopetop {
} if scope:
}}

--regex-X=/package[ ]+([A-Z]+)[ ]*/\1/p/{{
. _scopeset
}}
5 changes: 5 additions & 0 deletions Units/optscript.r/op-scope.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
X input.unknown /^package X$/;" p
Y input.unknown /^package Y$/;" p
a input.unknown /^def a:int = 1$/;" d package:X typeref:typename:int
b input.unknown /^def b:str = "abc"$/;" d package:X typeref:typename:str
c input.unknown /^def c:name = name$/;" d package:Y typeref:typename:name
9 changes: 9 additions & 0 deletions Units/optscript.r/op-scope.d/input.unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package X

def a:int = 1
def b:str = "abc"

package Y

def c:name = name

157 changes: 157 additions & 0 deletions main/lregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -3224,6 +3224,126 @@ static EsObject* lrop_get_match_string (OptVM *vm, EsObject *name)
return es_false;
}

static EsObject* lrop_set_scope (OptVM *vm, EsObject *name)
{
EsObject *corkIndex = opt_vm_ostack_top (vm);
if (!es_integer_p (corkIndex))
return OPT_ERR_TYPECHECK;

int n = es_integer_get (corkIndex);
if (n < 0)
return OPT_ERR_RANGECHECK;

if (n >= countEntryInCorkQueue())
return OPT_ERR_RANGECHECK;

struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
lcb->currentScope = n;

opt_vm_ostack_pop (vm);

return es_false;
}

static EsObject* lrop_pop_scope (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
if (lcb->currentScope != CORK_NIL)
{
tagEntryInfo *e = getEntryInCorkQueue (lcb->currentScope);
if (e)
lcb->currentScope = e->extensionFields.scopeIndex;
}
return es_false;
}

static EsObject* lrop_clear_scope (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
lcb->currentScope = CORK_NIL;
return es_false;
}

static EsObject* lrop_ref0_scope (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);

if (lcb->currentScope == 0)
{
opt_vm_ostack_push (vm, es_false);
return es_false;
}

EsObject *q = es_integer_new (lcb->currentScope);

if (es_error_p (q))
return q;

opt_vm_ostack_push (vm, q);
es_object_unref (q);
opt_vm_ostack_push (vm, es_true);
return es_false;
}

static EsObject* lrop_refN_scope (OptVM *vm, EsObject *name)
{
EsObject *nobj = opt_vm_ostack_top (vm);
if (!es_integer_p (nobj))
return OPT_ERR_TYPECHECK;

int n = es_integer_get(nobj);

struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
int scope = lcb->currentScope;

while (n--)
{
if (scope == CORK_NIL)
break;
tagEntryInfo *e = getEntryInCorkQueue (scope);
if (e == NULL)
break;

scope = e->extensionFields.scopeIndex;
}

EsObject *q = es_integer_new (scope);
if (es_error_p(q))
return q;

opt_vm_ostack_pop (vm);
opt_vm_ostack_push (vm, q);
es_object_unref (q);

return es_false;
}

static EsObject* lrop_get_scope_depth (OptVM *vm, EsObject *name)
{
int n = 0;

struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
int scope = lcb->currentScope;

while (scope != CORK_NIL)
{
tagEntryInfo *e = getEntryInCorkQueue (scope);
if (!e)
break;

scope = e->extensionFields.scopeIndex;
n++;
}

EsObject *q = es_integer_new (scope);
if (es_error_p(q))
return q;

opt_vm_ostack_push (vm, q);
es_object_unref (q);
return es_false;
}

static struct optscriptOperatorRegistration lropOperators [] = {
{
.name = "_matchloc",
Expand All @@ -3245,6 +3365,43 @@ static struct optscriptOperatorRegistration lropOperators [] = {
.arity = 1,
.help_str = "tag _COMMIT int",
},
{
.name = "_scopeset",
.fn = lrop_set_scope,
.arity = 1,
.help_str = "int _SCOPESET -",
},
{
.name = "_scopepop",
.fn = lrop_pop_scope,
.arity = 0,
.help_str = "- _SCOPEPOP -",
},
{
.name = "_scopeclear",
.fn = lrop_clear_scope,
.arity = 0,
.help_str = "- _SCOPECLEAR -",
},
{
.name = "_scopetop",
.fn = lrop_ref0_scope,
.arity = 0,
.help_str = "- _SCOPETOP int true%"
"- _SCOPETOP false",
},
{
.name = "_scopeNth",
.fn = lrop_refN_scope,
.arity = 1,
.help_str = "index:int _SCOPENTH int",
},
{
.name = "_scopedepth",
.fn = lrop_get_scope_depth,
.arity = 0,
.help_str = "- _SCOPEDEPTH int",
},
};

extern void initRegexOptscript (void)
Expand Down

0 comments on commit 7f9e4f2

Please sign in to comment.