Closed zaoqi closed 3 years ago
Hello, could clarify your command line for repeat that error?
Thank you for issue. I create a PR for solving the problem and after few minutes, I will merge in master branch. Results:
Calculate warnings in file: ./testdata/lang/lang.go
After transpiling : 314 warnings.
Go build : 2 warnings
Unsafe : 1029
After merging, try next and choose with minimal warnings:
c4go transpile lang.c
c4go transpile -s lang.c
$ gcc lang.c testmain.c -lm &&./a.out
...
#(符名 太始初核 (一類何物 (式形 (化滅 省略一物 化滅)) 特定其物))
$ c4go transpile -s lang.c testmain.c
$ go run lang.go
# command-line-arguments
./lang.go:48279:27: could not determine kind of name for C.__jmp_buf_tag
Yes, C.__jmp_buf_tag
from C header setjmp.h
is not implemented and it is cannot be implemented because Go language cannot jump from middle function to middle another function and back.
Lua 5.1:
/*
@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling.
** CHANGE them if you prefer to use longjmp/setjmp even with C++
** or if want/don't to use _longjmp/_setjmp instead of regular
** longjmp/setjmp. By default, Lua handles errors with exceptions when
** compiling as C++ code, with _longjmp/_setjmp when asked to use them,
** and with longjmp/setjmp otherwise.
*/
#if defined(__cplusplus)
/* C++ exceptions */
#define LUAI_THROW(L,c) throw(c)
#define LUAI_TRY(L,c,a) try { a } catch(...) \
{ if ((c)->status == 0) (c)->status = -1; }
#define luai_jmpbuf int /* dummy variable */
#elif defined(LUA_USE_ULONGJMP)
/* in Unix, try _longjmp/_setjmp (more efficient) */
#define LUAI_THROW(L,c) _longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
#define luai_jmpbuf jmp_buf
#else
/* default handling with long jumps */
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
#define luai_jmpbuf jmp_buf
#endif
Maybe defer
, panic
, recover
can handle this situation
Maybe, that jumps is not clear for my self. You can help with that : Create solution in PR or create some example on C and Go or ...
throw
panic(...)
catch
defer func() {
if e := recover(); e != nil {
//...
}
}()
Maybe add some non-standard extensions or C++
's try
and catch
can solve this problem.
Yes, I understood your approach. For myself - is not clear - how to implemented on real example http://www.c-cpp.ru/content/setjmp?
https://stackoverflow.com/questions/1713907/is-it-possible-to-do-a-call-cc-in-go http://www.inf.puc-rio.br/~roberto/docs/MCC15-04.pdf
Goroutines are equivalent to one-shot continuations.
Maybe setjmp can be implemented with Goroutines.
Dear @zaoqi-unsafe, Could you prepare example of transpilation for understood your approach? Example of C code : http://www.c-cpp.ru/content/setjmp
Dear @zaoqi,
In my point of view - main transpilation problem for LUA project is not enough C code - some base struct is not in file lang.c
.
Could you approve?
@Konstantin8105
$ gcc lang.c testmain.c -lm &&./a.out
What base struct?math.h?jmp_buf?
https://en.cppreference.com/w/c/program/longjmp If ignore undefined behavior, setjmp, longjmp and try, catch is equivalent
Then how to implement setjmp, longjmp is obvious.
@zaoqi-unsafe Don't know your meaning. Recover only make sense in defer, which means you cannot resume to the correct place. Can you show an equivalent go example to http://www.c-cpp.ru/content/setjmp this code?
#include <stdio.h>
#include <setjmp.h>
jmp_buf ebuf;
void f2 (void);
int main(void)
{
char first=1;
int i;
printf("1 ");
i = setjmp(ebuf);
if (first) {
first =! first;
f2();
printf ("this will not be printed");
}
printf("%d", i);
return 0;
}
void f2(void)
{
printf("2 ");
longjmp(ebuf, 3);
}
If you use defer+recover as setjmp, panic as longjmp, the main function will exit right after you recover the panic, but the C example will continue to run. Don't know if I misunderstand something.
https://gitlab.com/the-language/the-language/tree/7ade276ad241c7fdff0163cf41295e03e869e7a5/core/arch/c/lang.c