11--[[ HOTRELOAD
22 run_lua("test/tests/nattlua/c_declarations/preprocessor.lua")
33]]
4- local META = require (" nattlua.parser.base" )()
4+ local Lexer = require (" nattlua.definitions.lua.ffi.preprocessor.lexer" ).New
5+ local Code = require (" nattlua.code" ).New
56local buffer = require (" string.buffer" )
7+ local META = require (" nattlua.parser.base" )()
68
79-- Deep copy tokens to prevent shared state corruption
810-- This is CRITICAL because:
2224
2325local old = META .New
2426
25- function META .New (...)
26- local obj = old (... )
27- obj .defines = {}
28- obj .define_stack = {}
29- obj .expansion_stack = {}
30- obj .conditional_stack = {} -- Track nested #if/#ifdef/#ifndef states
31- obj .position_stack = {} -- Track positions for directive token removal
32- return obj
27+ function META .New (tokens , code , config )
28+ config = config or {}
29+ config .working_directory = config .working_directory or os.getenv (" PWD" ) or " ."
30+ config .defines = config .defines or {}
31+ config .include_paths = config .include_paths or {}
32+ config .max_include_depth = config .max_include_depth or 100
33+ config .on_include = config .on_include -- Optional callback for includes
34+ config .system_include_paths = config .system_include_paths or {}
35+
36+ if config .add_standard_defines ~= false then
37+ local standard_defines = {
38+ __STDC__ = 1 ,
39+ __STDC_VERSION__ = " 201710L" ,
40+ __STDC_HOSTED__ = 1 ,
41+ __GNUC__ = 4 ,
42+ __GNUC_MINOR__ = 2 ,
43+ __GNUC_PATCHLEVEL__ = 1 ,
44+ }
45+
46+ for name , value in pairs (standard_defines ) do
47+ if config .defines [name ] == nil then config .defines [name ] = value end
48+ end
49+ end
50+
51+ local self = old (tokens , code , config )
52+ self .defines = {}
53+ self .define_stack = {}
54+ self .conditional_stack = {} -- Track nested #if/#ifdef/#ifndef states
55+ self .position_stack = {} -- Track positions for directive token removal
56+ self .include_depth = 0 -- Track current include depth
57+ -- Add predefined macros
58+ for name , value in pairs (config .defines ) do
59+ if type (value ) == " boolean" then
60+ value = value and " 1" or " 0"
61+ else
62+ value = tostring (value )
63+ end
64+
65+ local value_tokens = Lexer (Code (value , " define" )):GetTokens ()
66+ assert (value_tokens [# value_tokens ].type == " end_of_file" )
67+ table.remove (value_tokens )
68+ self :Define (name , nil , value_tokens )
69+ end
70+
71+ return self
3372end
3473
3574-- Helper functions for saving/restoring positions when removing directive tokens
@@ -745,7 +784,7 @@ do -- #include directive
745784 local fs = require (" nattlua.other.fs" )
746785
747786 local function resolve_include_path (self , filename , is_system_include )
748- local opts = self .preprocess_options
787+ local opts = self .config
749788
750789 if not opts then return nil , " No preprocessor options available" end
751790
@@ -874,8 +913,8 @@ do -- #include directive
874913 end
875914
876915 -- Callback for tracking includes
877- if self .preprocess_options .on_include then
878- self .preprocess_options .on_include (filename , full_path )
916+ if self .config .on_include then
917+ self .config .on_include (filename , full_path )
879918 end
880919
881920 -- Preprocess the included file recursively
@@ -898,14 +937,14 @@ do -- #include directive
898937 -- Create a copy of options with updated working directory
899938 local include_opts = {}
900939
901- for k , v in pairs (self .preprocess_options ) do
940+ for k , v in pairs (self .config ) do
902941 include_opts [k ] = v
903942 end
904943
905944 -- Set working directory to the directory of the included file
906945 -- so relative includes from that file work correctly
907- include_opts .working_directory = full_path :match (" (.*/)" ) or self .preprocess_options .working_directory
908- include_parser .preprocess_options = include_opts
946+ include_opts .working_directory = full_path :match (" (.*/)" ) or self .config .working_directory
947+ include_parser .config = include_opts
909948 include_parser .include_depth = self .include_depth + 1
910949 -- Process the included file
911950 include_parser :Parse ()
0 commit comments