2

I am using LUA as embedded language on a µC project, so the ressources are limited. To save some cycles and memory I do always only indexed based table access (table[1]) instead og hash-based access (table.someMeaning = 1). This saves a lot of memory.

The clear drawback of this is approach are the magic numbers thrughtout the code.

A Cpp-like preprocessor would help here to replace the number with named-constants.

Is there a good way to achieve this? A preprocessor in LUA itself, loading the script and editing the chunk and then loading it would be a variant, but I think this exhausts the ressources in the first place ...

wimalopaan
  • 4,838
  • 1
  • 21
  • 39
  • 1
    You could use `GNU M4`, this is a general purpose preprocessor, quite easy to use and very suitable to your use-case. Another solution could be to write a small script replacing your TOKENS with successive `sed` text substitutions. – Robert Nov 03 '21 at 08:11
  • 1
    The first example of `GNU M4` is interesting https://www.linuxjournal.com/article/5594#:~:text=The%20basic%20tool%20of%20m4%20is%20the%20macro.,surround%20a%20single%20word%20they%20inhibit%20macro%20expansion. – Robert Nov 03 '21 at 08:14
  • Then the next question for the m4 way: how to embed the m4 definitions into the lua script itself (more like using cpp) – wimalopaan Nov 03 '21 at 08:21
  • 1
    I think the easiest way is to use the cpp for lua ;-) – wimalopaan Nov 03 '21 at 08:29
  • 1
    That's probably also a good idea, as you don't need another tool. – Robert Nov 03 '21 at 09:35
  • 1
    Actually, there are also `Lua` general-purpose preprocessors, for example that one http://lua-users.org/wiki/SlightlyLessSimpleLuaPreprocessor – Robert Nov 03 '21 at 09:40
  • 1
    You can write a preprocessor for this easily using my token processor: https://web.tecgraf.puc-rio.br/~lhf/ftp/lua/#ltokenp – lhf Nov 03 '21 at 10:13
  • 1
    See also http://lua-users.org/lists/lua-l/2011-02/msg00881.html – lhf Nov 03 '21 at 13:57

1 Answers1

0

So, I found a simple solution: write your own preprocessor in Lua! It's probably the most easy thing to do.

First, define your symbols globally:

MySymbols = {
  FIELD_1 = 1,
  FIELD_2 = 2,
  FIELD_3 = 3,
}

Then you write your preprocessing function, which basically just replace the strings from MySymbols by their value.

function Preprocess (FilenameIn, FilenameOut)
  local FileIn     = io.open(FilenameIn, "r")
  local FileString = FileIn:read("*a") 

  for Name, Value in pairs(MySymbols) do
    FileString = FileString:gsub(Name, Value)
  end

  FileIn:close()

  local FileOut = io.open(FilenameOut, "w")
  FileOut:write(FileString)
  FileOut:close()
end

Then, if you try with this input file test.txt:

TEST FIELD_1
TEST FIELD_2
TEST FIELD_3

And call the following function:

Preprocess("test.txt", "test-out.lua")

You will get the fantastic output file:

TEST  1
TEST  2
TEST  3

I let you the joy to integrate it with your scripts/toolchain.

If you want to avoid attributing the number manually, you could just add a wonderful closure:

function MakeCounter ()
  local Count = 0
  return function ()
    Count = Count + 1
    return Count
  end
end

NewField = MakeCounter()

MySymbols = {
  FIELD_1 = NewField(),
  FIELD_2 = NewField(),
  FIELD_3 = NewField()
}
Robert
  • 2,711
  • 7
  • 15
  • This solution will replace names inside strings, which may or not be what you want. – lhf Nov 03 '21 at 10:12
  • You are correct, but for real-world usage for his micro-controller, this solution is pretty good. You don't do lot of string management in a micro-controller... He just need to find a name, probably with upper case and there will not be any name collision. – Robert Nov 03 '21 at 10:15
  • Sadly the whole LUA sourcecode does not fit into µC to be replaced on the µC – wimalopaan Nov 03 '21 at 10:58