Replies: 1 comment
-
|
The reason why it's not working as expected is because this code ##[=[
print [[
static void greeting(void) {
puts("Hello, world!");
}
]]
]=]takes place during preprocessing process. In other words, you are using your preprocessor's Your code ##[=[
cemit [[
greeting();
]]
]=]gets emitted inside your generated C code. This code should not compile because it sees it as static void greeting(void) {
puts("Hello, world!");
}
/* ------------------------------ DIRECTIVES -------------------------------- */
/* Disable some warnings that the generated code can trigger. */
#if defined(__clang__) && __clang_major__ >= 3
#pragma clang diagnostic ignored "-Wtype-limits"
#pragma clang diagnostic ignored "-Wwrite-strings"
#pragma clang diagnostic ignored "-Wunused"
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#pragma clang diagnostic ignored "-Wparentheses-equality"
#pragma clang diagnostic ignored "-Wtautological-compare"
#pragma clang diagnostic ignored "-Wmissing-braces"
#ifndef __cplusplus
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#pragma clang diagnostic error "-Wimplicit-function-declaration"
#pragma clang diagnostic error "-Wimplicit-int"
#else
#pragma clang diagnostic ignored "-Wnarrowing"
#pragma clang diagnostic ignored "-Wc99-designator"
#endif
#elif defined(__GNUC__) && __GNUC__ >= 5
#pragma GCC diagnostic ignored "-Wtype-limits"
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-value"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#ifndef __cplusplus
#pragma GCC diagnostic ignored "-Wmissing-braces"
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
#pragma GCC diagnostic error "-Wimplicit-function-declaration"
#pragma GCC diagnostic error "-Wimplicit-int"
#else
#pragma GCC diagnostic ignored "-Wnarrowing"
#endif
#endif
#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
/* Macro used to perform compile-time checks. */
#if __STDC_VERSION__ >= 201112L
#define NELUA_STATIC_ASSERT _Static_assert
#elif __cplusplus >= 201103L
#define NELUA_STATIC_ASSERT static_assert
#else
#define NELUA_STATIC_ASSERT(x, y)
#endif
/* Macro used to get alignment of a type. */
#if __STDC_VERSION__ >= 201112L
#define NELUA_ALIGNOF _Alignof
#elif __cplusplus >= 201103L
#define NELUA_ALIGNOF alignof
#elif defined(__GNUC__)
#define NELUA_ALIGNOF __alignof__
#elif defined(_MSC_VER)
#define NELUA_ALIGNOF __alignof
#else
#define NELUA_ALIGNOF(x)
#endif
/* Checks if Nelua and C agrees on pointer size. */
NELUA_STATIC_ASSERT(sizeof(void*) == 8 && NELUA_ALIGNOF(void*) == 8, "Nelua and C disagree on pointer size or alignment");
/* Enable 64 bit offsets for stdio APIs. */
#if !defined(_FILE_OFFSET_BITS) && __SIZEOF_LONG__ >= 8
#define _FILE_OFFSET_BITS 64
#endif
/* Enable POSIX APIs in included headers. */
#if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) && !defined(_GNU_SOURCE) && !defined(_DEFAULT_SOURCE)
#if defined(__gnu_linux__)
#define _GNU_SOURCE
#else
#define _XOPEN_SOURCE 600
#endif
#endif
/* ------------------------------ DECLARATIONS ------------------------------ */
static int nelua_main(int argc, char** argv);
/* ------------------------------ DEFINITIONS ------------------------------- */
int nelua_main(int argc, char** argv) {
greeting();
return 0;
}
int main(int argc, char** argv) {
return nelua_main(argc, argv);
}and if you attempt to run it, you should expect to see something like Now, if you replace your ## cinclude '<stdio.h>'
##[=[
cemitdefn [[
static void greeting(void) {
puts("Hello, world!");
}
]]
]=]
##[=[
cemit [[
greeting();
]]
]=]with /* ------------------------------ DIRECTIVES -------------------------------- */
/* Disable some warnings that the generated code can trigger. */
#if defined(__clang__) && __clang_major__ >= 3
#pragma clang diagnostic ignored "-Wtype-limits"
#pragma clang diagnostic ignored "-Wwrite-strings"
#pragma clang diagnostic ignored "-Wunused"
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#pragma clang diagnostic ignored "-Wparentheses-equality"
#pragma clang diagnostic ignored "-Wtautological-compare"
#pragma clang diagnostic ignored "-Wmissing-braces"
#ifndef __cplusplus
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#pragma clang diagnostic error "-Wimplicit-function-declaration"
#pragma clang diagnostic error "-Wimplicit-int"
#else
#pragma clang diagnostic ignored "-Wnarrowing"
#pragma clang diagnostic ignored "-Wc99-designator"
#endif
#elif defined(__GNUC__) && __GNUC__ >= 5
#pragma GCC diagnostic ignored "-Wtype-limits"
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-value"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#ifndef __cplusplus
#pragma GCC diagnostic ignored "-Wmissing-braces"
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
#pragma GCC diagnostic error "-Wimplicit-function-declaration"
#pragma GCC diagnostic error "-Wimplicit-int"
#else
#pragma GCC diagnostic ignored "-Wnarrowing"
#endif
#endif
#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
/* Macro used to perform compile-time checks. */
#if __STDC_VERSION__ >= 201112L
#define NELUA_STATIC_ASSERT _Static_assert
#elif __cplusplus >= 201103L
#define NELUA_STATIC_ASSERT static_assert
#else
#define NELUA_STATIC_ASSERT(x, y)
#endif
/* Macro used to get alignment of a type. */
#if __STDC_VERSION__ >= 201112L
#define NELUA_ALIGNOF _Alignof
#elif __cplusplus >= 201103L
#define NELUA_ALIGNOF alignof
#elif defined(__GNUC__)
#define NELUA_ALIGNOF __alignof__
#elif defined(_MSC_VER)
#define NELUA_ALIGNOF __alignof
#else
#define NELUA_ALIGNOF(x)
#endif
/* Checks if Nelua and C agrees on pointer size. */
NELUA_STATIC_ASSERT(sizeof(void*) == 8 && NELUA_ALIGNOF(void*) == 8, "Nelua and C disagree on pointer size or alignment");
/* Enable 64 bit offsets for stdio APIs. */
#if !defined(_FILE_OFFSET_BITS) && __SIZEOF_LONG__ >= 8
#define _FILE_OFFSET_BITS 64
#endif
/* Enable POSIX APIs in included headers. */
#if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) && !defined(_GNU_SOURCE) && !defined(_DEFAULT_SOURCE)
#if defined(__gnu_linux__)
#define _GNU_SOURCE
#else
#define _XOPEN_SOURCE 600
#endif
#endif
#include <stdio.h>
/* ------------------------------ DECLARATIONS ------------------------------ */
static int nelua_main(int argc, char** argv);
/* ------------------------------ DEFINITIONS ------------------------------- */
static void greeting(void) {
puts("Hello, world!");
}
int nelua_main(int argc, char** argv) {
greeting();
return 0;
}
int main(int argc, char** argv) {
return nelua_main(argc, argv);
}which produces the right message output |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This code usually fails to compile.
However, If you create the C code by giving the
--print-codeoption, it leaves the following function definition at the begining of the source code. Example command linenelua --print-code test.nelua > output.cAnd this code succeeds in C compiling.
One solution is to define the function as follows.
Is there no other solution that this for now?
Beta Was this translation helpful? Give feedback.
All reactions