Commits

ZyX_I  committed 8a5120e

@/fwc: Improved s:F.string() function, moved it to @/fwc/constructor

  • Participants
  • Parent commits b4c304c

Comments (0)

Files changed (3)

File plugin/frawor/fwc/compiler.vim

 if has('float')
     call add(s:_messages._types, 'float')
 endif
-"▶1 string         :: String → String
-function s:F.string(str)
-    return substitute(substitute(substitute(substitute(string(a:str),
-                \"@",      '''."\\x40".''', 'g'),
-                \"\n",     '''."\\n".''',   'g'),
-                \"^''\\.", '',              '' ),
-                \"\\.''$", '',              '' )
-endfunction
 "▶1 cleanup        :: list::[_, {arg}*] → + list
 function s:F.cleanup(list)
     if len(a:list)>1
     for sub in a:subscripts
         let tsub=type(sub)
         if tsub==type('')
-            let r.=((sub=~#'\W')?('['.s:F.string(sub).']'):
+            let r.=((sub=~#'\W')?('['.self.string(sub).']'):
                         \        ('.'.sub))
         elseif tsub==type(0)
             let r.='['.sub.']'
                 if sub=~#'^\w\+$'
                     call s:F.addargchunk(chunks, '.'.sub, 0)
                 else
-                    call s:F.addargchunk(chunks, '['.s:F.string(sub).']', 0)
+                    call s:F.addargchunk(chunks, '['.self.string(sub).']', 0)
                 endif
             elseif tsub==type(0)
                 call s:F.addargchunk(chunks, '['.sub.']', 0)
             endif
             unlet sub
         endfor
-        return join(map(chunks, 'v:key%2 ? v:val : s:F.string(v:val)'), '.')
+        return join(map(chunks, 'v:key%2 ? v:val : self.string(v:val)'), '.')
     else
         return self.argbase.(self.getsubs(get(a:000, 1, self.subs)))
     endif
 endfunction
 "▶1 addthrow       :: msg::String, msgarg, needcurarg, ... + self → self + self
 function s:compiler.addthrow(msg, needcurarg, ...)
-    let args=s:F.string(a:msg).', '
+    let args=self.string(a:msg).', '
     if a:needcurarg
-        let args.=s:F.string(a:1).', '.self.argstr(1).', '.join(a:000[1:], ', ')
+        let args.=self.string(a:1).', '.self.argstr(1).', '.
+                    \join(a:000[1:], ', ')
     else
         let args.=join(a:000, ', ')
     endif
         let r.=', '.join(map(a:matcher[1][2:-2],
                     \        'type(v:val)=='.type([]).'?'.
                     \           'self.getvar(v:val, 0, a:ldstr, a:strstr):'.
-                    \           's:F.string(v:val)'),
+                    \           'self.string(v:val)'),
                     \    ', ')
     endif
     if self.type is 'check' || self.type is 'filter'
     elseif a:var[0] is 'expr'
         let r=[self.getexpr(a:var)]
     elseif a:var[0] is 'string'
-        let r=[s:F.string(a:var[1])]
+        let r=[self.string(a:var[1])]
     elseif a:var[0] is 'argument'
         let r=[self.argbase, [self.subs[0]+a:var[1]]+a:var[2:]]
     elseif a:var[0] is 'cur'
         let r=[self.argbase, self.subs[:-1-a:var[1]]+a:var[2:]]
     elseif a:var[0] is 'list'
         let r=['['.join(map(a:var[1:], 'type(v:val)=='.type('').'?'.
-                    \                       's:F.string(v:val):'.
+                    \                       'self.string(v:val):'.
                     \                       'self.getvar(v:val)'), ', ').']']
     elseif a:var[0] is 'evaluate'
         let r=[eval(substitute(self.getvar(a:var[1]), '@%@', 'self.vars', 'g'))]
         if type(r[0])!=type('')
-            let r[0]=s:F.string(r[0])
+            let r[0]=self.string(r[0])
         endif
     elseif a:var[0] is 'func'
         let r=[call(self.getfunc, [a:var, 0]+a:000[1:], self)]
         elseif msgarg[0] is 'curarg'
             call add(msg, curargstrstr)
         elseif msgarg[0] is 'curidx'
-            call add(msg, s:F.string(idx))
+            call add(msg, self.string(idx))
         else
             call add(msg, substitute(self.getvar(msgarg), '@#@',
                         \            escape(curargstrstr, '\&~'), 'g'))
         endif
     endfor
-    let msgstr=s:F.string(a:msg[1]).', '.join(msg, ', ')
+    let msgstr=self.string(a:msg[1]).', '.join(msg, ', ')
     call add(self.msgs.own, msgstr)
     return self
 endfunction
 "▶1 compilestring  :: {string} + self → self + self
 function s:compiler.compilestring(str)
     if type(a:str)==type('')
-        return s:F.string(a:str)
+        return self.string(a:str)
     endif
     return self.getvar(a:str)
 endfunction
             endif
             "▲3
             let prefstr=prefdictstr.self.getsubs([prefix])
-            let prefixstr=s:F.string(prefix)
+            let prefixstr=self.string(prefix)
             "▶3 Construct prefix condition
             let cond=astr.' is '.prefixstr
             if prefopts.list
             call self.addif(cond)
             "▶3 Process prefix arguments
             for i in range(1, prefopts.argnum)
-                call self.compilearg(args[i-1], idx.s:F.string(prefix))
+                call self.compilearg(args[i-1], idx.self.string(prefix))
                             \.incsub()
             endfor
             "▶3 Move prefix arguments to prefix dictionary
             call self.up()
             "▶3 Process `no{prefix}'
             if prefopts.alt
-                call self.addif(astr.' is '.s:F.string('no'.prefix))
+                call self.addif(astr.' is '.self.string('no'.prefix))
                             \.let(prefstr, 0)
                             \.up()
             endif
         call self.incsub()
         let astartsub=copy(self.subs[-1])
         for action in actions
-            call self.addif(actionstr.' is '.s:F.string(action[0]))
+            call self.addif(actionstr.' is '.self.string(action[0]))
             if len(action)>1
-                call self.compileadesc(action[1], idx.'.'.s:F.string(action[0]))
+                call self.compileadesc(action[1],idx.'.'.self.string(action[0]))
             endif
             call self.let(caidxstr, s:F.getsub(self.subs[-1]))
                         \.up()
     endif
     "▲2
     let vars=s:F.createvars(a:plugdict)
-    let a='"FWCfail", '.s:F.string(a:fname).', '.s:F.string(a:plugdict.id)
+    let a='"FWCfail", '.self.string(a:fname).', '.self.string(a:plugdict.id)
     let [opts, lines]=s:F.compilestr(vars, a:arg[0], a:arg[1], a)
     if opts.only
         call map(lines, 'substitute(v:val, "@@@", "@$@args", "g")')

File plugin/frawor/fwc/constructor.vim

 endif
 execute frawor#Setup('0.0', {'@/resources': '0.0'}, 1)
 let s:constructor={}
+"▶1 string     :: a → String
+function s:constructor.string(val)
+    if type(a:val)==type('') && a:val=~#"[\r\n@]"
+        return ('"'.substitute(substitute(substitute(escape(a:val, '\"'),
+                    \          "@",  '\\x40', 'g'),
+                    \          "\n", '\\n',   'g'),
+                    \          "\r", '\\r',   'g')
+                    \.'"')
+    endif
+    return substitute(substitute(substitute(string(a:val),
+                \"@",      '''."\\x40".''', 'g'),
+                \"\n",     '''."\\n".''',   'g'),
+                \"\r",     '''."\\r".''',   'g')
+endfunction
 "▶1 add        :: item, ... + self → self + self
 function s:constructor.add(...)
     let self.l+=a:000

File plugin/frawor/fwc/intfuncs.vim

 let s:cf='CHECKFAILED'
 let s:cfstr=string(s:cf)
 let s:cfreg='\v^'.s:cf.'$'
-"▶1 string         :: String → String
-function s:F.string(str)
-    return substitute(substitute(substitute(substitute(string(a:str),
-                \"@",      '''."\\x40".''', 'g'),
-                \"\n",     '''."\\n".''',   'g'),
-                \"^''\\.", '',              '' ),
-                \"\\.''$", '',              '' )
-endfunction
 "▶1 `func', `eval'
 let s:r.func={'args': ['func']}
 " Checks whether result of running {func}({argument}) isnot 0
     for check in a:dic[1][1:]
         let i+=1
         if check[0] is 'eq'
-            call self.addif(keystr.' is '.s:F.string(check[1]))
+            call self.addif(keystr.' is '.self.string(check[1]))
         elseif check[0] is 'regex'
-            call self.addif(keystr.'=~#'.s:F.string(check[1]))
+            call self.addif(keystr.'=~#'.self.string(check[1]))
         elseif check[0] is 'func'
             call self.addif(self.getfunc(check[1], 0, keystr).' isnot 0')
         elseif check[0] is 'expr'
     return self.addtypecond([type('')], a:idx)
                 \.let(curargstr, 'substitute('.curargstr.', '.
                 \((type(a:desc[1][1])==type(''))?
-                \       (s:F.string(a:desc[1][1])):
+                \       (self.string(a:desc[1][1])):
                 \       (self.getvar(a:desc[1][1]))).', '.
                 \join(map(a:desc[2:], 'self.compilestring(v:val)'), ', ').')')
 endfunction
     if len(a:desc[1])>1
         let keys='['.join(map(copy(a:desc[1]),
                     \         'type(v:val)==type("")?'.
-                    \               's:F.string(v:val):'.
+                    \               'self.string(v:val):'.
                     \               'self.getvar(v:val)'), ', ').']'
         let absentkeys='filter('.keys.', '.
                     \          string('!has_key('.self.argstr().', v:val)').')'
                     \                    'join('.absentkeys.', ", ")')
     elseif !empty(a:desc[1])
         let keystr=((type(a:desc[1][0])==type(''))?
-                    \       (s:F.string(a:desc[1][0])):
+                    \       (self.string(a:desc[1][0])):
                     \       (self.getvar(a:desc[1][0])))
         return self.nextthrow('!has_key('.self.argstr().', '.keystr.')',
                     \         'keymis', a:idx, keystr)
 let s:r.match={'args': ['reg']}
 function s:r.match.check(desc, idx, type)
     let regex=((type(a:desc[1][1])==type(''))?
-                \(s:F.string(a:desc[1][1])):
+                \(self.string(a:desc[1][1])):
                 \(self.getvar(a:desc[1][1])))
     let curargstr=self.argstr()
     return self.addtypecond([type('')], a:idx)