Commits

ZyX_I  committed ca673c7

@/fwc/compiler: Implemented `only' option

  • Participants
  • Parent commits 3ad2e8f

Comments (0)

Files changed (4)

File plugin/frawor/fwc/compiler.vim

 " XXX in order for this function to work self.argstr(self.subs[:-2]) should 
 " point to argument being checked and self.subs[-1] should be a number
 function s:compiler.compileadesc(adescr)
-    if !has_key(a:adescr, 'minimum')
-        call s:F.getlenrange(a:adescr)
-    endif
-    if !has_key(a:adescr, 'checkedfor')
-        call self.addlencheck(self.subs[-1]+a:adescr.minimum,
-                    \      self.subs[-1]+a:adescr.maximum)
-        let a:adescr.checkedfor=1
+    if !self.o.only
+        if !has_key(a:adescr, 'minimum')
+            call s:F.getlenrange(a:adescr)
+        endif
+        if !has_key(a:adescr, 'checkedfor')
+            call self.addlencheck(self.subs[-1]+a:adescr.minimum,
+                        \      self.subs[-1]+a:adescr.maximum)
+            let a:adescr.checkedfor=1
+        endif
     endif
     if has_key(a:adescr, 'arg')
         for arg in a:adescr.arg
-            call self.compilearg(arg, self.subs[-1])
-            call self.incsub()
+            call self.compilearg(arg, ((self.o.only)?(''):(self.subs[-1])))
+            if !self.o.only
+                call self.incsub()
+            endif
         endfor
     endif
     " TODO
     "▶2 Setup self
     let t=extend(s:_r.new_constructor(), {
                 \   'type': a:type,
-                \   'subs': [0],
+                \   'subs': [],
                 \   'vars': a:vars,
                 \   'vids': {},
                 \'argbase': '@@@',
                     \.let('@$@pmessages', '[]')
                     \.try()
         if !t.o.only
-            if t.type is 'check' || t.type is 'filter'
-                call t.compileadesc(tree[0])
-            else
-            endif
+            call add(t.subs, 0)
+        endif
+        if t.type is 'check' || t.type is 'filter'
+            call t.compileadesc(tree[0])
+        else
         endif
         call t.up().catch(s:cfreg)
                     \.for('@$@targs', '@$@messages')
             call t.return('@@@')
         endif
     endif
-    return t.tolist()
+    return [t.o, t.tolist()]
 endfunction
 "▶1 createvars     :: plugdict → id + s:lastid, s:vars
 function s:F.createvars(plugdict)
     let s:lastid+=1
     let s:vars[id]=s:F.createvars(a:plugdict)
     call add(a:fdict.ids, id)
+    let [opts, lines]=s:F.compilestr(s:vars[id], a:string, a:type, 1)
+    if opts.only
+        call insert(lines, 'let d={"arg": a:args}')
+    endif
     execute "function d.f(args)\n    ".
                 \substitute(substitute(substitute(
-                \join(s:F.compilestr(s:vars[id], a:string, a:type, 1),"\n    "),
+                \join(lines, "\n    "),
                 \'@@@', ((a:type is 'complete')?
                 \           ('variants'):
-                \           ('a:args')), 'g'),
+                \           ((opts.only)?('d.arg'):
+                \                        ('a:args'))), 'g'),
                 \'@%@', 's:vars.'.id,    'g'),
                 \'@$@', '',              'g')."\n".
             \"endfunction"
     "▲2
     let vars=s:F.createvars(a:plugdict)
     let a='"FWCfail", '.s:F.string(a:fname).', '.s:F.string(a:plugdict.id)
-    return [128, '@@@', vars, s:F.compilestr(vars, a:arg[0], a:arg[1], a), [],0]
+    let [opts, lines]=s:F.compilestr(s:vars[id], a:arg[0], a:arg[1], a)
+    if opts.only
+        call map(lines, 'substitute(v:val, "@@@", "@$@args", "g")')
+        call insert(lines, 'let @$@d={"args": @@@}')
+    endif
+    return [128, ((opts.only)?('@$@d.args'):('@@@')), vars, lines, [], 0]
 endfunction
 "▶1 Register decorator
 call s:_f.adddecorator('FWC', s:F.makedec)

File test/fwccheck.ok

 plugin/frawor/fwc/compiler:nbool
 Frawor:plugin/fwccheck:str:a:args[0]<
 plugin/frawor/fwc/compiler:nbool
+::: Section <Option `only'>
+plugin/frawor/fwc/compiler:nreg
+plugin/frawor/fwc/compiler:typefail
+plugin/frawor/fwc/compiler:invlstlen
+plugin/frawor/fwc/compiler:nbool
+plugin/frawor/fwc/compiler:nreg
+plugin/frawor/fwc/compiler:isnot
 <<< messages

File test/fwctests.dat

 ['#str(%) bool',                         'check'], [2],        0
 ['#str(%) bool (type "" #str(.) isreg)', 'check'], [1, '\v('], 0
 ['#str(*$"type"(@)) bool',               'check'], [2],        0
-['#str(*$"type"(@)) bool',               'check'], [""],       0
-['#str(%) ?_ #^ bool',                   'check'], [""],       0
-['#str(%) ?_ ## bool',                   'check'], [""],       0
-['#str(%) #str(.) #^ bool',              'check'], [""],       0
-['#str(%) #str(.) ## bool',              'check'], [""],       0
+['#str(*$"type"(@)) bool',               'check'], [''],       0
+['#str(%) ?_ #^ bool',                   'check'], [''],       0
+['#str(%) ?_ ## bool',                   'check'], [''],       0
+['#str(%) #str(.) #^ bool',              'check'], [''],       0
+['#str(%) #str(.) ## bool',              'check'], [''],       0
+#▶1 Option `only'
+['-(only) isreg',                        'check'], '\v(',      0
+['-(only) isreg',                        'check'], '\v(.)',    1
+['-(only) tuple bool, isreg',            'check'], '',         0
+['-(only) tuple bool, isreg',            'check'], [0],        0
+['-(only) tuple bool, isreg',            'check'], [2, ''],    0
+['-(only) tuple bool, isreg',            'check'], [1, '\v('], 0
+['-(only) tuple bool, isreg',            'check'], [1, '_'],   1
+['-(only) |bool is =1',                  'check'], [],         0
+['-(only) |bool is =1',                  'check'], [1],        1
+['-(only) bool',                        'filter'], [],         0
+['-(only) bool',                        'filter'], [1],        1
 # vim: cms=#%s fmr=▶,▲

File test/rtp/plugin/fwccheck.vim

         let prevsections=copy(sections)
         echom '::: Section <'.join(sections, '/').'>'
     endif
-    let [compargs, args, result]=eval('['.line.']')
-    let savedargs=deepcopy(args)
+    let [compargs, arg, result]=eval('['.line.']')
+    let savedarg=deepcopy(arg)
     if !empty($DEBUGTEST)
         debug let ChFunc=call(s:_f.fwc_compile, compargs, {})
-        debug let realres=call(ChFunc, [args], {})
+        debug let realres=call(ChFunc, [arg], {})
     else
         let ChFunc=call(s:_f.fwc_compile, compargs, {})
-        let realres=call(ChFunc, [args], {})
+        let realres=call(ChFunc, [arg], {})
     endif
     if type(realres)!=type(result) || realres!=result
         echom '[[[ Test #'.i.'('.j.') '.
                     \'in section <'.join(sections, '/').'> failed:'
         echom 'expected '.string(result).','
         echom 'but got  '.string(realres)
-        echom 'Checked arguments: '.string(savedargs)
+        echom 'Checked arguments: '.string(savedarg)
         echom 'FWC string: '.compargs[0]
         echom 'Check type: '.compargs[1]
         function g:.ChFunc
     endif
     let i+=1
     let j+=1
-    unlet realres result
+    unlet realres result arg savedarg
 endfor
 " vim: fmr=▶,▲