edubart / nelua-lang

Minimal, efficient, statically-typed and meta-programmable systems programming language heavily inspired by Lua, which compiles to C and native code.
https://nelua.io
MIT License
1.99k stars 64 forks source link

Invalid unitname generated for directories that look like numeric literals #240

Open jrfondren opened 10 months ago

jrfondren commented 10 months ago

Code example

local function f() <nodce> end

When compiled in a directory named '001-example', an invalid unitname is generated:

$ nelua 001-example/test.nelua
/home/jfondren/.cache/nelua/test.c:79:13: error: invalid suffix "_example_test_f" on integer constant
   79 | static void 001_example_test_f(void);
      |             ^~~~~~~~~~~~~~~~~~
/home/jfondren/.cache/nelua/test.c:79:13: error: expected identifier or ‘(’ before numeric constant
/home/jfondren/.cache/nelua/test.c:81:6: error: invalid suffix "_example_test_f" on integer constant
   81 | void 001_example_test_f(void) {
      |      ^~~~~~~~~~~~~~~~~~
/home/jfondren/.cache/nelua/test.c:81:6: error: expected identifier or ‘(’ before numeric constant
error: C compilation for '/home/jfondren/.cache/nelua/test' failed

Expected behavior

I expected a unitname like XXX_example, as is done with non-Latin characters.

Workaround

Files can set an explicit unitname with

## pragmas.unitname = 'X001_example'

Environment

x86_64 linux Nelua 0.2.0-dev Build number: 1605 Git date: 2023-11-05 16:12:59 -0300 Git hash: decf713ca4bfd99b7bb9ae3c5f66b87761fd8621 Semantic version: 0.2.0-dev.1605+decf713c Copyright (C) 2019-2023 Eduardo Bart (https://nelua.io/)

stefanos82 commented 10 months ago

Can you provide a full demo to check it on my side? I have tested the code excerpt you shared and could not reproduce the behavior you shared above.

My bad; I forgot to run the script with 001-example included.

Indeed it would be handy to create something like first-character-breadcrumb appended to script's parent directory; in my case, /home/stefanos/nelua/various/ could become hsnv that could get appended to 001-example.

In other words, it should become hsnv_001_example_f_f(void).

Update as of 16-11-2023: Coincidentally, I have discovered by accident that Crystal language creates a caching directory for improving re-building compilation speeds inside $HOME/.cache/, much like Nelua does, but with the following format: home-stefanos-code-crystal-hello_world.cr/; this is my $HOME/stefanos/code/crystal/ directory with filename hello_world.cr.

This type of format could also be used to avoid the aforementioned buggy behavior while maintaining readability for developers that want to investigate the generate C code and know which file came from where exactly.