Commits

ZyX_I committed 6d6ce1f

@/fwc: Made it possible to use variables in subscripts

Comments (0)

Files changed (4)

plugin/frawor/fwc/compiler.vim

         endif
     endif
 endfunction
-"▶1 substostr      :: [subscript] → String
-function s:F.substostr(subscripts)
+"▶1 substostr      :: [subscript] + self → String
+function s:compiler.substostr(subscripts)
     let r=''
     for sub in a:subscripts
         let tsub=type(sub)
         elseif tsub==type(0)
             let r.='['.sub.']'
         elseif tsub==type([])
-            let r.='['.join(sub, ':').']'
+            if type(sub[0])==type([])
+                let r.='['.join(map(copy(sub), 'self.getvar(v:val)'), ':').']'
+            else
+                let r.='['.join(sub, ':').']'
+            endif
         endif
         unlet sub
     endfor
         endfor
         return join(map(chunks, 'v:key%2 ? v:val : string(v:val)'), '.')
     else
-        return self.argbase.s:F.substostr(get(a:000, 1, self.subs))
+        return self.argbase.(self.substostr(get(a:000, 1, self.subs)))
     endif
 endfunction
 "▶1 incsub         :: &self
 "▶1 getvar         :: varcontext + self → String
 function s:compiler.getvar(var)
     if a:var[0] is 'plugvar'
-        return '@%@.p.'.a:var[1].s:F.substostr(a:var[2:])
+        return '@%@.p.'.a:var[1].(self.substostr(a:var[2:]))
     elseif a:var[0] is 'expr'
         return self.getexpr(a:var)
     elseif a:var[0] is 'string'

plugin/frawor/fwc/parser.vim

             \'invssubs': 'invalid subscript in slice expression: %s',
             \ 'uoption': 'unknown option: %s',
             \  'argmis': 'missing arguments to %s',
-            \ 'subsmis': 'subscript range missing',
-            \   'nonum': 'number expected',
             \  'actmis': 'missing arguments description',
             \ 'typemis': 'missing type description',
             \ 'invtype': 'invalid type description: %s',
             \  'invreg': 'invalid regular expression: %s',
             \ 'wordend': 'regular expression cannot end with %s',
             \  'noexpr': 'expected expression, but got nothing',
-            \  'nosubs': 'expected subscript, but got %s'
         \}
 "▶1 s:args
 let s:args={}
 "        {subscript} :: {str}
 "                     | [0-9] {wordchar}*
 "                     | {wordchar}+
+"                     | "$" {var}
+"                     | {var}
 "                {n} :: "-"? {wordchar}+
+"                     | "$" {var}
+"                     | {var}
 " Output: add((String|Number|context(Number, Number))*)
 function s:parser.getsubscr()
     let requiresdot=0
             call self.addcon()
             " Start and end subscripts
             for i in range(0, 1)
-                if !self.len
-                    call self.throw('subsmis')
-                endif
                 let v=self.readc()
                 if v is '-'
-                    if !self.len
-                        call self.throw('nonum')
-                    endif
-                    let v.=self.readc()
+                    call self.add(+v.(self.readc()))
+                elseif v=~#'^\d'
+                    call self.add(+v)
+                elseif v is '$'
+                    call self.getvar()
+                else
+                    call self.ungetc(v).getvar()
                 endif
-                call self.add(+v)
             endfor
             call self.conclose()
+        elseif c is '$'
+            call self.addcon().getvar()
         else
-            call self.throw('nosubs', c)
+            call self.addcon().ungetc(c).getvar()
         endif
         let requiresdot=1
     endwhile
 plugin/frawor/fwc/compiler:nreg
 plugin/frawor/fwc/compiler:nindict
 plugin/frawor/fwc/compiler:typefail
+plugin/frawor/fwc/compiler:notfail
+::: Section <Checks/Built-in checks/in>
 plugin/frawor/fwc/compiler:ninlist
 plugin/frawor/fwc/compiler:ninlist
-plugin/frawor/fwc/compiler:notfail
+::: Section <Checks/Built-in checks/in/Subscripts checks>
+plugin/frawor/fwc/compiler:ninlist
+plugin/frawor/fwc/compiler:ninlist
+plugin/frawor/fwc/compiler:ninlist
+plugin/frawor/fwc/compiler:ninlist
+plugin/frawor/fwc/compiler:ninlist
+plugin/frawor/fwc/compiler:ninlist
 ::: Section <Checks/Built-in checks/dict>
 plugin/frawor/fwc/compiler:keynmatch
 plugin/frawor/fwc/compiler:typefail

test/fwctests.dat

 \function s:.ordered(str)
 \    return join(sort(split(a:str, '\v.@=')), '') is a:str
 :endfunction
+:let s:d={}
+:let s:d.list=range(4)
+:let s:listname='list'
+:let s:ss=1
+:let s:es=2
 #▶2 Function checks
 ['?*$"empty"', 'check'], ['abc'], 0
 ['?*$"empty"', 'check'], [''],    1
 ['key={"abc": 1}',  'check'], ['abc'],              1
 ['key={"abc": 1}',  'check'], ['ab'],               0
 ['key={"abc": 1}',  'check'], [0],                  0
+['not bool',        'check'], [0],                  0
+['not bool',        'check'], [2],                  1
+['_',               'check'], [[[[]]]],             1
+['any',             'check'], [[[[]]]],             1
+#▶3 in
 ['in [abc def]',    'check'], ['abc'],              1
 ['in =["a","b"]',   'check'], ['abc'],              0
 ['in =["a",0]',     'check'], ['a'],                1
 ['in =["a",0]',     'check'], ['0'],                0
 ['in =["a",0]',     'check'], [0],                  1
-['not bool',        'check'], [0],                  0
-['not bool',        'check'], [2],                  1
-['_',               'check'], [[[[]]]],             1
-['any',             'check'], [[[[]]]],             1
+#▶4 Subscripts checks
+['in d.list',         'check'], [0],                1
+['in d.list',         'check'], [5],                0
+['in d."list"',       'check'], [0],                1
+['in d."list"',       'check'], [5],                0
+['in d.''list''',     'check'], [0],                1
+['in d.''list''',     'check'], [5],                0
+['in d.$listname',    'check'], [0],                1
+['in d.$listname',    'check'], [5],                0
+['in d.list.:$ss$es', 'check'], [2],                1
+['in d.list.:$ss$es', 'check'], [0],                0
+['in d.list.:1 2',    'check'], [2],                1
+['in d.list.:1 2',    'check'], [0],                0
 #▶3 dict
 ['dict {}',                   'check'], [{}],              1
 ['dict {}',                   'check'], [{'a': 1}],        0
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.