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

error while compiling when a source file starts with a number #259

Open sdx6 opened 3 months ago

sdx6 commented 3 months ago

Bug description

i was trying out using Nelua with an 8ball program, and by the end of it named it accordingly, 8ball.nelua, and it threw this compiler error

 [/home/leah/coding/nelua] (18:13:23)
 > nelua 8ball.nelua
/home/leah/.cache/nelua/8ball.c:598:22: error: invalid suffix "ball_responses" on integer constant
  598 | static nlstring_arr9 8ball_responses = {.v = {{(uint8_t*)"Yes.", 4}, {(uint8_t*)"No.", 3}, {(uint8_t*)"Maybe", 5}, {(uint8_t*)"Definitely.", 11}, {(uint8_t*)"Never.", 6}, {(uint8_t*)"Why do you ask...", 17}, {(uint8_t*)"Ew, I'm not responding to that.", 31}, {(uint8_t*)"OMG YES GIRLIE SLAAAAAYYYY", 26}, {(uint8_t*)"um no wHAT ???? DIE STUPID", 26}}};
      |                      ^~~~~~~~~~~~~~~
/home/leah/.cache/nelua/8ball.c:598:22: error: expected identifier or ‘(’ before numeric constant
/home/leah/.cache/nelua/8ball.c: In function ‘nelua_GC_registerroots’:
/home/leah/.cache/nelua/8ball.c:1752:43: error: invalid suffix "ball_responses" on integer constant
 1752 |   nelua_GC_register((&nelua_gc), (void*)(&8ball_responses), 144U, 131072U, (function_24MUm2mhxgB96C1FJ)NULL, (void*)NULL);
      |                                           ^~~~~~~~~~~~~~~
/home/leah/.cache/nelua/8ball.c: In function ‘nelua_main’:
/home/leah/.cache/nelua/8ball.c:2254:1: error: invalid suffix "ball_restart" on integer constant
 2254 | 8ball_restart:;
      | ^~~~~~~~~~~~~
/home/leah/.cache/nelua/8ball.c:2254:14: error: expected ‘;’ before ‘:’ token
 2254 | 8ball_restart:;
      |              ^
      |              ;
/home/leah/.cache/nelua/8ball.c:2259:12: error: invalid suffix "ball_quit" on integer constant
 2259 |       goto 8ball_quit;
      |            ^~~~~~~~~~
/home/leah/.cache/nelua/8ball.c:2259:12: error: expected identifier or ‘*’ before numeric constant
/home/leah/.cache/nelua/8ball.c:2261:19: error: invalid suffix "ball_responses.v" on integer constant
 2261 |     nelua_print_1(8ball_responses.v[nelua_assert_bounds_nlint64(nelua_math_random_1(0, 8), 9)]);
      |                   ^~~~~~~~~~~~~~~~~
/home/leah/.cache/nelua/8ball.c:2262:10: error: invalid suffix "ball_restart" on integer constant
 2262 |     goto 8ball_restart;
      |          ^~~~~~~~~~~~~
/home/leah/.cache/nelua/8ball.c:2262:10: error: expected identifier or ‘*’ before numeric constant
/home/leah/.cache/nelua/8ball.c:2264:1: error: invalid suffix "ball_quit" on integer constant
 2264 | 8ball_quit:;
      | ^~~~~~~~~~
/home/leah/.cache/nelua/8ball.c:2264:11: error: expected ‘;’ before ‘:’ token
 2264 | 8ball_quit:;
      |           ^
      |           ;
error: C compilation for '/home/leah/.cache/nelua/8ball' failed

Code example

no code for example, compiler issue

Expected behavior

the compilation finishes successfully and runs the code

Workaround

if you were to rename the file to eightball.nelua, or something else without a number the compilation will finish successfully

Environment

Nelua 0.2.0-dev Build number: 1615 Git date: 2024-04-20 09:45:08 -0300 Git hash: 9f75e009db190feda0f90ae858b48fd82f51b8b1 Semantic version: 0.2.0-dev.1615+9f75e009 Copyright (C) 2019-2024 Eduardo Bart (https://nelua.io/)

Linux

x86_64

gcc (Debian 12.2.0-14) 12.2.0 Copyright (C) 2022 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

jrfondren commented 3 months ago

The filename is used as a prefix for generated identifiers, so it has to be an identifier itself. "Has to be", but invalid characters are replaced with X. A simple fix ix probably to have that replacement include leading numerals.

There is a workaround:

## pragmas.unitname = 'eightball'
local function f()
  print 'from 8ball.nelia'
end
f()

This works as it generates eightball_f instead of 8ball_f