codereport / jsource

J Language Source Code. Livestream links ⬇️
https://www.youtube.com/playlist?list=PLVFrD1dmDdvfVhYLU_iKkV67X9XqCJLWe
Other
38 stars 20 forks source link

Convert #DEFINE const numbers to variables and enums. #97

Open Sebanisu opened 3 years ago

Sebanisu commented 3 years ago

Might be a bit too early to do this.

regular expression pattern: integer

#define\s+([\d\w_]+)\s+(-?\s*\d+)\s*?(?=$|//|/\*)

matches

#define EVRANK          -14
#define EVSPELL         16
#define EVSTACK         17 //blah
#define EVSTOP          18 /* blah

replace pattern

static int constexpr $1 = $2;

end result

static int constexpr EVRANK = -14;
static int constexpr EVSPELL = 16;
static int constexpr EVSTACK = 17; //blah
static int constexpr EVSTOP = 18; /* blah */

A basic enum creates something similar to above.

enum {
EVRANK = -14,
EVSPELL = 16,
EVSTACK = 17, //blah
EVSTOP = 18, /* blah */
}

Enum's can have names though in c all the members are global anyway. In c++ 11 we get scopes with enum class and set the base integral type. So in cases where we're defining a lot of constants maybe enums are better.


5 hits in 2 files

regular expression pattern: double

#define\s+([\d\w_]+)\s+(-?\s*\d+\.\d+)\s*?(?=$|//|/\*)

matches

#define EMAX            709.78271289338409
#define EMIN            -744.44007192138126
#define EMAX2           710.47586007394398
#define TMAX            19.066172834610153

replace pattern

static double constexpr $1 = $2;
static double constexpr EMAX = 709.78271289338409;
static double constexpr EMIN  = -744.44007192138126;
static double constexpr EMAX2 = 710.47586007394398;
static double constexpr TMAX = 19.066172834610153;