Пример #1
0
def fun_macro_grammar(klass1, klass2):
  return (
  # fun. a(x)[...],  fun. a(x) <= at[...][...]
    (getattr(vv.name)+
     any(~call(__.args) +assign(__.args, getvalue_default(__.args, ()))
          + some(getitem_to_list(__.body), __.body, __.bodies), 
          (__.args, __.bodies), vv.rules)
        +eoi+make_fun1(vv.name, vv.rules, klass2))
  # fun. a(x) >= [...],  fun. a(x) <= at[...][...]
  | (getattr(vv.name)+call(vv.args)+ge(vv.body)+eoi
     +make_fun2(vv.name,vv.args, vv.body, klass2))
  # fun. a(x) <= [...],  fun. a(x) <= at[...][...]
  | (getattr(vv.name)+call(vv.args)+le(vv.body)+eoi
     +make_fun3(vv.name,vv.args, vv.body, klass2))
  #  fun. a== at(..)[...]
  | (getattr(vv.name)+eq(vv.rules)+eoi
     +make_fun4(vv.name, vv.rules, klass1))
  #  fun. a>= at(..)[...]
  | (getattr(vv.name)+ge(vv.rules)+eoi+make_fun5(vv.name,vv.rules, klass2))
  #  fun. a<= at(..)[...]
  | (getattr(vv.name)+le(vv.rules)+eoi+make_fun6(vv.name,vv.rules, klass2))
  #  fun(args) [...](args)[...][...]
  | (some(may(call(__.args)) +assign(__.args, getvalue_default(__.args, ()))
          + some(getitem_to_list(__.body), __.body, __.bodies), 
          (__.args, __.bodies), vv.rules)
        +eoi+make_fun7(vv.rules, klass1))
  #   - fun.a/3,
  | (getattr(vv.name)+neg+div(vv.arity)+eoi
     +pycall(abolish, getvar(vv.name), vv.arity))
  #- fun.a(x),
  | (getattr(vv.name)+call(vv.args)+neg+eoi
     +make_fun8(vv.name, vv.args, klass2)) #retractall
  #- fun.a(x)[1],
##  | (getattr(vv.name)+call(vv.args)+getitem(vv.index)+neg+assign(result, retract(vv.name, vv.args)))
  )
Пример #2
0
def make_fun4(name, rules, klass): 
  fun = varcache(name)
  rules = preparse(rules)
  if isinstance(rules, AtForm): 
    rules1 = []
    for head, bodies in rules.clauses:
      if head is None: head = ()
      for body in bodies:
        rules1.append((head,)+tuple(body))
    return assign(fun, klass(*rules1))
  elif isinstance(rules, list):
    return assign(fun, klass(((), rules)))
  else: raise DinpySyntaxError()
Пример #3
0
def make_fun4(name, rules, klass):
    fun = varcache(name)
    rules = preparse(rules)
    if isinstance(rules, AtForm):
        rules1 = []
        for head, bodies in rules.clauses:
            if head is None: head = ()
            for body in bodies:
                rules1.append((head, ) + tuple(body))
        return assign(fun, klass(*rules1))
    elif isinstance(rules, list):
        return assign(fun, klass(((), rules)))
    else:
        raise DinpySyntaxError()
Пример #4
0
def fun_macro_grammar(klass1, klass2):
    return (
        # fun. a(x)[...],  fun. a(x) <= at[...][...]
        (getattr(vv.name) + any(
            ~call(__.args) + assign(__.args, getvalue_default(__.args, ())) +
            some(getitem_to_list(__.body), __.body, __.bodies),
            (__.args, __.bodies), vv.rules) + eoi +
         make_fun1(vv.name, vv.rules, klass2))
        # fun. a(x) >= [...],  fun. a(x) <= at[...][...]
        | (getattr(vv.name) + call(vv.args) + ge(vv.body) + eoi +
           make_fun2(vv.name, vv.args, vv.body, klass2))
        # fun. a(x) <= [...],  fun. a(x) <= at[...][...]
        | (getattr(vv.name) + call(vv.args) + le(vv.body) + eoi +
           make_fun3(vv.name, vv.args, vv.body, klass2))
        #  fun. a== at(..)[...]
        | (getattr(vv.name) + eq(vv.rules) + eoi +
           make_fun4(vv.name, vv.rules, klass1))
        #  fun. a>= at(..)[...]
        | (getattr(vv.name) + ge(vv.rules) + eoi +
           make_fun5(vv.name, vv.rules, klass2))
        #  fun. a<= at(..)[...]
        | (getattr(vv.name) + le(vv.rules) + eoi +
           make_fun6(vv.name, vv.rules, klass2))
        #  fun(args) [...](args)[...][...]
        | (some(
            may(call(__.args)) + assign(__.args, getvalue_default(__.args,
                                                                  ())) +
            some(getitem_to_list(__.body), __.body, __.bodies),
            (__.args, __.bodies), vv.rules) + eoi +
           make_fun7(vv.rules, klass1))
        #   - fun.a/3,
        | (getattr(vv.name) + neg + div(vv.arity) + eoi +
           pycall(abolish, getvar(vv.name), vv.arity))
        #- fun.a(x),
        | (getattr(vv.name) + call(vv.args) + neg + eoi +
           make_fun8(vv.name, vv.args, klass2))  #retractall
        #- fun.a(x)[1],
        ##  | (getattr(vv.name)+call(vv.args)+getitem(vv.index)+neg+assign(result, retract(vv.name, vv.args)))
    )
Пример #5
0
    return varcache(name, klass)


# my.a, globl.a
## my = element(some(getattr(__._), L('local', __._), y)+eoi)
## globl = element(some(getattr(__._), L('globl', __._), y)+eoi)

## use_item = attr|div(var)|div(str)
## use = 'use'+some(use_item)+mayional(use_block)|any(use_item)+use_block\
##          |some(use_item)+div+use_block

# put.a = 1, put.i.j==(1,2)
put = element(
    'put',
    # put.i.j<<(1,2)
    (getattr(__._) + assign(vv.x, getvar(getvalue(__._))))[1:] % vv.x * vv.vars
    + lshift(vv.value) + eoi +
    pycall(special.set_list, vv.vars, pycall(preparse, vv.value)))


@builtin.function('make_begin')
def make_begin(body):
    return special.begin(*preparse(body))


do = element('do', getitem_to_list(vv.body) + eoi + make_begin(vv.body))

_do, _of, _at, _loop, _always = words('do, of, at, loop, always')


def get_let_vars(binary, klass):
Пример #6
0
@builtin.function('getvar')
def getvar(name, klass=Var): 
  return varcache(name, klass)

# my.a, globl.a
## my = element(some(getattr(__._), L('local', __._), y)+eoi)
## globl = element(some(getattr(__._), L('globl', __._), y)+eoi)

## use_item = attr|div(var)|div(str)
## use = 'use'+some(use_item)+mayional(use_block)|any(use_item)+use_block\
##          |some(use_item)+div+use_block

# put.a = 1, put.i.j==(1,2)
put = element('put',
  # put.i.j<<(1,2)
  (getattr(__._)+assign(vv.x, getvar(__._)))[1:]%vv.x*vv.vars
        +lshift(vv.value)+eoi
        +pycall(special.set_list, vv.vars, pycall(preparse,vv.value))
  )

@builtin.function('make_begin')
def make_begin(body):
  return special.begin(*preparse(body))

do = element('do',
  getitem_to_list(vv.body)+eoi+make_begin(vv.body))

_do, _of, _at, _loop, _always = words('do, of, at, loop, always')

def get_let_vars(binary, klass):
  if not isinstance(binary.y, _VarSymbol): raise DinpySyntaxError()