ZyX_I avatar ZyX_I committed 8b5bdfe

@/fwc/compiler: Added `not' optimizer

Comments (0)

Files changed (1)

plugin/frawor/fwc/compiler.vim

     let subidxs[-1]+=1
     return join(subidxs, '.')
 endfunction
+"▶1 optnot         :: &self(idx)
+" XXX low-level hacks here
+function s:compiler.optnot(idx)
+    if len(self.l[1])==2
+                \&& len(self.l[1][0])>2
+                \&& self.l[1][0][0] is 'if'
+                \&& ((self.l[1][0][-1] is 'endif'
+                \  && self.l[1][0][-3] isnot 'else')
+                \ || (self.l[1][0][-2] isnot 'else'))
+        let conditions=[]
+        let iftree=copy(self.l[1][0])
+        while !empty(iftree)
+            let type=remove(iftree, 0)
+            if type is 'if' || type is 'elseif'
+                let [condition, block]=remove(iftree, 0, 1)
+                if block!=#[['throw', s:cfstr]]
+                    return self
+                endif
+                call add(conditions, condition)
+            elseif type is 'endif'
+                break
+            else
+                return self
+            endif
+        endwhile
+        call remove(self.l, 0, -1)
+        call self.add('if', 0, [])
+        call self.nextthrow('!('.join(conditions, ' || ').')',
+                    \       'notfail', a:idx, '@#@')
+        call remove(self.l, 1, 3)
+    endif
+    return self
+endfunction
 "▶1 compilecheck   :: checkcontext, idx + self → self + self
 function s:compiler.compilecheck(check, idx)
     let curargstr=self.argstr()
                         \.throw("'NOTFAIL'")
                     \.catch(s:cfreg).up()
                     \.catch('\v^NOTFAIL$')
-                        \.addthrow('notfail', a:idx, '@#@').up()
+                        \.addthrow('notfail', a:idx, '@#@')
+                        \.optnot(a:idx)
         "▶3 `either'
         elseif desc[0] is 'either'
             let sucstr=self.getlvarid('succeeded')
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.