@@ -20,38 +20,7 @@ pub fn saveconfig_command(
2020 let mut symbols = crate :: kconfig:: SymbolTable :: new ( ) ;
2121
2222 // Extract symbols from AST and apply defaults
23- for entry in & ast. entries {
24- if let crate :: kconfig:: ast:: Entry :: Config ( config) = entry {
25- // Strip CONFIG_ prefix if present
26- let clean_name = config. name . strip_prefix ( "CONFIG_" ) . unwrap_or ( & config. name ) ;
27- symbols. add_symbol ( clean_name. to_string ( ) , config. symbol_type . clone ( ) ) ;
28-
29- // Apply default value if present
30- // Note: This is a simplified implementation that only handles simple
31- // constant and symbol expressions. Full expression evaluation
32- // (with dependencies and conditional defaults) is not yet implemented.
33- if let Some ( default_expr) = & config. properties . default {
34- // Extract value from simple expressions
35- if let crate :: kconfig:: ast:: Expr :: Const ( val) = default_expr {
36- symbols. set_value ( clean_name, val. clone ( ) ) ;
37- } else if let crate :: kconfig:: ast:: Expr :: Symbol ( sym) = default_expr {
38- // Handle default values like 'y' or 'n'
39- symbols. set_value ( clean_name, sym. clone ( ) ) ;
40- }
41- // Complex expressions (e.g., conditional defaults with 'if')
42- // would require full expression evaluation and are not handled here
43- } else {
44- // Set to 'n' if no default
45- match config. symbol_type {
46- crate :: kconfig:: ast:: SymbolType :: Bool |
47- crate :: kconfig:: ast:: SymbolType :: Tristate => {
48- symbols. set_value ( clean_name, "n" . to_string ( ) ) ;
49- }
50- _ => { }
51- }
52- }
53- }
54- }
23+ extract_symbols_from_entries ( & ast. entries , & mut symbols) ;
5524
5625 // Write .config file
5726 ConfigWriter :: write ( & output, & symbols) ?;
@@ -69,3 +38,71 @@ pub fn saveconfig_command(
6938
7039 Ok ( ( ) )
7140}
41+
42+ fn extract_symbols_from_entries ( entries : & [ crate :: kconfig:: ast:: Entry ] , symbols : & mut crate :: kconfig:: SymbolTable ) {
43+ use crate :: kconfig:: ast:: Entry ;
44+ use crate :: kconfig:: Expr ;
45+
46+ for entry in entries {
47+ match entry {
48+ Entry :: Config ( config) => {
49+ // Strip CONFIG_ prefix if present
50+ let clean_name = config. name . strip_prefix ( "CONFIG_" ) . unwrap_or ( & config. name ) ;
51+ symbols. add_symbol ( clean_name. to_string ( ) , config. symbol_type . clone ( ) ) ;
52+
53+ // Apply default value if present
54+ if let Some ( default_expr) = & config. properties . default {
55+ if let Expr :: Const ( val) = default_expr {
56+ symbols. set_value ( clean_name, val. clone ( ) ) ;
57+ } else if let Expr :: Symbol ( sym) = default_expr {
58+ // Handle default values like 'y' or 'n'
59+ symbols. set_value ( clean_name, sym. clone ( ) ) ;
60+ } else if let Expr :: ShellExpr ( shell_expr) = default_expr {
61+ // Evaluate shell expression
62+ if let Ok ( value) = crate :: kconfig:: shell_expr:: evaluate_shell_expr ( shell_expr, symbols) {
63+ if !value. is_empty ( ) {
64+ symbols. set_value ( clean_name, value) ;
65+ }
66+ }
67+ }
68+ } else {
69+ // Set to 'n' if no default
70+ match config. symbol_type {
71+ crate :: kconfig:: ast:: SymbolType :: Bool |
72+ crate :: kconfig:: ast:: SymbolType :: Tristate => {
73+ symbols. set_value ( clean_name, "n" . to_string ( ) ) ;
74+ }
75+ _ => { }
76+ }
77+ }
78+ }
79+ Entry :: MenuConfig ( menuconfig) => {
80+ let clean_name = menuconfig. name . strip_prefix ( "CONFIG_" ) . unwrap_or ( & menuconfig. name ) ;
81+ symbols. add_symbol ( clean_name. to_string ( ) , menuconfig. symbol_type . clone ( ) ) ;
82+ }
83+ Entry :: Choice ( choice) => {
84+ for option in & choice. options {
85+ let clean_name = option. name . strip_prefix ( "CONFIG_" ) . unwrap_or ( & option. name ) ;
86+ symbols. add_symbol ( clean_name. to_string ( ) , option. symbol_type . clone ( ) ) ;
87+ }
88+
89+ // Apply choice default if specified
90+ if let Some ( default_name) = & choice. default {
91+ let clean_default = default_name. strip_prefix ( "CONFIG_" ) . unwrap_or ( default_name) ;
92+ symbols. set_value ( clean_default, "y" . to_string ( ) ) ;
93+ } else if let Some ( first_option) = choice. options . first ( ) {
94+ // No default specified, select first option (standard Kconfig behavior)
95+ let clean_name = first_option. name . strip_prefix ( "CONFIG_" ) . unwrap_or ( & first_option. name ) ;
96+ symbols. set_value ( clean_name, "y" . to_string ( ) ) ;
97+ }
98+ }
99+ Entry :: Menu ( menu) => {
100+ extract_symbols_from_entries ( & menu. entries , symbols) ;
101+ }
102+ Entry :: If ( if_entry) => {
103+ extract_symbols_from_entries ( & if_entry. entries , symbols) ;
104+ }
105+ _ => { }
106+ }
107+ }
108+ }
0 commit comments