librime-lua icon indicating copy to clipboard operation
librime-lua copied to clipboard

如何 在TranslationReg 加上 an<translation> pipe(translation, func(translation,env)

Open shewer opened this issue 3 years ago • 1 comments

https://github.com/hchunhui/librime-lua/blob/7bbf79ddda9a07b671b9bf0e94d6d6e94decaeab/src/types.cc#L191 lua->newthread(L, n) ; 是什麼意思? 指向 function ?? function 是否可以引用參數

以目前要用 transltion --> transltion --> transltion --> 我確定可以用 function() .... end
但不確定可以入參數嗎 如果可以 Translation( Translation(Translation( tran, func1), func2), func3) -- 但是這樣 巢狀呼叫不好 或是

local tab不是={cand1,cand2,cand3}
local t = Translation(function()  for _,cand in next tab end)
local t1 = Translation( function()  for cand in t:iter() do  yield(cand) end)
local t2 = Translation(function () for cand in t1:iter() do yield(cand) end)

-- 如果在 TranslationReg 增加method an< Translation > pipe( Translation &self , func, ... ); -- pipe 類似 make只是要有參數(translation &self, func)

據 就可以用

ex:

  local translation= Translation(function(tran,func) yield() end ):
              fifo(function(tran,func) for cand in tran:iter() do yield() end)
              fifo(function() yield() end)  

再加上 create filter translator 機制 就可以在 lua_filter lua_translation func中 調用 an an 現成工具

ex:

function init(env)
    env.t1= Translator(engine,"translatior" , "lua_translator@date")
    
end
function func(input,seg,env) 
     local t=env.t1:query(input,seg)
      :pipe( function(tran) for cand in tran:iter() do 
             .... yield(cand)  
              end 
       end)
       :pipe( function(tran) for cand in tran:iter() do 
               ... yield(cand)
             end
        end)
      for cand in t:iter() do 
              .... 
              yield
       end
end

shewer avatar Feb 11 '22 00:02 shewer

function M.func(input,seg, env)

  local t= Translation( function()
    print("--------------first" )
    for i=1,5 do
      print("------>", i)
      local cand=  Candidate( "test", seg.start,seg._end, tostring(i), "first")
      yield(cand)
    end信息
  end)
  local tt=t:pipe(function(tran)  <<------------------ function 出錯
    print("--------------second" )
    for cand in tran:iter() do
      cand.text= cand.text .. "-" .. cand.text
      yield(cand)
    end
  end)

  --[[
  end):pipe(function(tran) <<------------------ function 出錯
    print("-------------3-" )
    for cand in tran:iter() do
      cand.text= cand.text .. "-" .. cand.text
      yield(cand)
    end
  end)
  --]]
  print("----------------start ")
  for cand in tt:iter() do
    yield(cand)
  end

end

/*
// namespace TranslationReg 
 tran:pipe(func)  -- > raw_pipe(tran,func)  -- 
  func(tran)  
       for cand in tran:iter() do 
           ....   
           yield(cand)
      end 
 end
*/  
   int raw_pipe(lua_State *L){
     Lua *lua = Lua::from_state(L);
     int n = lua_gettop(L);

     if (n < 2)
       return 0;

     //  raw_pipe(tran, func,...)
     // replace (func, tran, ...)
     lua_replace(L, 2);
     lua_insert(L, 1);
     auto o = lua->newthreadx(L, n);
     an<Translation> r = New<LuaTranslation>(lua, o);
     LuaType<an<Translation>>::pushdata(L, r);
     return 1;
   }

錯誤是 stack 本來是要 func, tran , ... func 變成了 lua_State
717105 lua_gears.cc:15] LuaTranslation::Next error(2): [string "table.unpack = table.unpack or unpack..."]:4: attempt to call a 7LuaTypeIP9lua_StateE value (upvalue 'f')

shewer avatar Feb 11 '22 04:02 shewer