Comments (10)
In Overview documentation, in Pointer section we can read
local n = nilptr -- a generic pointer, initialized to nilptr
local p: pointer -- a generic pointer to anything, initialized to nilptr
local i: *integer -- pointer to an integer
which in other words, what your code is actually doing is indeed trying to assign NULL
inside NULL
; but if you replace your auto
to pointer
, it compiles as expected.
This is the generated C code with the usage of auto
:
static void tmp_chars_1(nlstring s, void* state);
static int nelua_main(int argc, char** argv);
/* ------------------------------ DEFINITIONS ------------------------------- */
void tmp_chars_1(nlstring s, void* state) {
for(intptr_t i = 1, _end = ((intptr_t)(s).size); i <= _end; i += 1) {
NULL = NULL;
}
}
and this is with the usage of pointer
instead:
static void tmp_chars(nlstring s, void* state);
static int nelua_main(int argc, char** argv);
/* ------------------------------ DEFINITIONS ------------------------------- */
void tmp_chars(nlstring s, void* state) {
for(intptr_t i = 1, _end = ((intptr_t)(s).size); i <= _end; i += 1) {
state = (void*)NULL;
}
}
When I read your code I said to myself: "wait, isn't state
going to become nilptr
and you want to assign nilptr
in it? Is this even allowed?" and decided to run the aforementioned tests.
from nelua-lang.
I don't see how that part of the overview implies what you say it does. I know that nilptr is NULL. What I wanted to do was pass NULL to the function. The state
parameter, though, is not NULL: it is a variable with the value of NULL, and assignment to it should only replace that value and not write to NULL. This is akin to replacing to contents of a CPU register, not writing to memory. Similar to this C program:
include <stdlib.h>
void f(void *p) { p = NULL; }
int main() {
f(NULL);
return 0;
}
Or, maybe with auto
it isn't a variable with the value of NULL. I don't get that part. But it seems like an error that Nelua and not the C compiler shouldn't be reporting.
from nelua-lang.
Hmm...yes, you have a point 🤔
Update: The following code helps me understand the logic a bit more, but I could be wrong here.
Only @edubart can enlighten us with this behavior.
do
local function typenameof(x: auto): string
return #[tostring(x.type)]#
end
local function tellmemytype(x: auto)
print('my type is:', typenameof(x))
x = nilptr
print('my type now is:', typenameof(x))
end
-- local foo = nilptr
-- tellmemytype(foo)
tellmemytype(nilptr)
end
The way I interpret tellmemytype()
's behavior is that I pass it a type nilptr
whereas I should have had passed it a variable, because this is what the auto
does behind the scenes; it deduces to type based on user input as value.
If I comment it out and uncomment the two lines above it, it returns pointer
as printed type.
I hope I've got it right 😕
from nelua-lang.
So, after some further investigation, it seems like that indeed nilptr
is an rvalue
, it's not a type; its type should be pointer
.
What helped me realized this was the flag --print-analyzed-ast
.
Call {
attr = {
calleesym = "tellmemytype: function(x: nilptr): void",
calleetype = "function(x: nilptr): void",
polyeval = "table: 0x7f5114df7a00",
pseudoargattrs = "table: 0x7f5114dfae40",
pseudoargtypes = "table: 0x7f5114dfae00",
type = "void",
},
{
Nilptr {
attr = {
comptime = true,
type = "nilptr",
},
}
},
...
from nelua-lang.
Fixed in a60b9fe
from nelua-lang.
@edubart I have just tested this commit and the generated C code for definition looks like this:
void tmp_chars_1(nlstring s, void* state) {
bool stop = false;
for(intptr_t i = 1, _end = ((intptr_t)(s).size); i <= _end; i += 1) {
bool _asgntmp_1 = false;
stop = _asgntmp_1;
}
}
Excuse my ignorance, but shouldn't the aforementioned code look something like
void tmp_chars_1(nlstring s, void* state) {
bool stop = false;
for(intptr_t i = 1, _end = ((intptr_t)(s).size); i <= _end; i += 1) {
bool _asgntmp_1 = false;
stop = _asgntmp_1;
state = (void *) NULL;
}
}
from nelua-lang.
Pass some other pointers. The function that doesn't assign state
to NULL is the one that's specialized to state
already being NULL.
from nelua-lang.
Pass some other pointers. The function that doesn't assign
state
to NULL is the one that's specialized tostate
already being NULL.
In other words, in
local function chars(s: string, state: auto)
local stop = false
for i=1, #s do
stop, state = false, nilptr
end
end
chars('abc', nilptr)
because we pass a nilptr
in chars()
, the compiler gets informed to ignore the ,nilptr
part from the for
loop?
from nelua-lang.
because we pass a
nilptr
inchars()
, the compiler gets informed to ignore the,nilptr
part from thefor
loop?
Correct, the thing is already nilptr
, so the compiler eliminates the code.
from nelua-lang.
...wait, you have applied Dead Code Elimination in AST mechanism, before emitting C code?! Bruh! O.o
from nelua-lang.
Related Issues (20)
- (@span(byte)){...} ordered field cast permits invalid, rejects valid construction HOT 1
- Executables reflect compile-time isatty conditions HOT 3
- <comptime> record parameters can't be provided; fields can't be used in runtime code HOT 1
- Copyright timespan needs update HOT 3
- Invalid unitname generated for directories that look like numeric literals HOT 1
- No line number in error if a hashmap index has a type error
- Range check error messages point to library code (point of check) rather than user code (point of error) HOT 1
- Function variables can collide with C identifiers HOT 1
- Confusing errors with varargs
- Permit function returns in generic instantiation, not just types
- Broken preprocessor after 9faaa66e HOT 3
- Comparison of a pointer with nil generates unexpected C code HOT 2
- we need nelua debugger(vscode) or tell us how to debug my app? HOT 2
- Compatible with Lua C plug-in HOT 2
- LFS: can't change directory with `lfs.chdir` HOT 1
- error while compiling when a source file starts with a number HOT 1
- Compiler hangs when cgenerator.lua errors
- Can't compile fibonacci example
- Can't compile fibonacci example
- Support actual closures via struct
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nelua-lang.