@@ -1136,16 +1136,23 @@ impl AstHostFunctions for TypedAstBuilder {
11361136 fn create_assignment ( & mut self , target : NodeHandle , value : NodeHandle ) -> NodeHandle {
11371137 let span = self . default_span ( ) ;
11381138
1139- // Assignment is represented as an expression statement with binary op
1140- // For simplicity, we'll create an expression statement
1139+ // Get target and value expressions
11411140 let target_expr = self . get_expr ( target)
11421141 . unwrap_or_else ( || self . inner . variable ( "target" , Type :: Primitive ( PrimitiveType :: I32 ) , span) ) ;
11431142 let value_expr = self . get_expr ( value)
11441143 . unwrap_or_else ( || self . inner . int_literal ( 0 , span) ) ;
11451144
1146- // For now, wrap as expression (proper assignment would need TypedAssignment)
1147- let _ = target_expr; // TODO: proper assignment
1148- let stmt = self . inner . expression_statement ( value_expr, span) ;
1145+ // Create assignment as a binary expression: target = value
1146+ let assign_expr = self . inner . binary (
1147+ BinaryOp :: Assign ,
1148+ target_expr,
1149+ value_expr,
1150+ Type :: Primitive ( PrimitiveType :: Unit ) ,
1151+ span,
1152+ ) ;
1153+
1154+ // Wrap in expression statement
1155+ let stmt = self . inner . expression_statement ( assign_expr, span) ;
11491156 self . store_stmt ( stmt)
11501157 }
11511158
@@ -1174,24 +1181,28 @@ impl AstHostFunctions for TypedAstBuilder {
11741181 let cond_expr = self . get_expr ( condition)
11751182 . unwrap_or_else ( || self . inner . bool_literal ( true , span) ) ;
11761183
1177- let then_stmts = if let Some ( stmt) = self . get_stmt ( then_branch) {
1178- vec ! [ stmt]
1184+ // Get the then block - check for block first, then statement, then expression
1185+ let then_block = if let Some ( block) = self . get_block ( then_branch) {
1186+ block
1187+ } else if let Some ( stmt) = self . get_stmt ( then_branch) {
1188+ TypedBlock { statements : vec ! [ stmt] , span }
11791189 } else if let Some ( expr) = self . get_expr ( then_branch) {
1180- vec ! [ self . inner. expression_statement( expr, span) ]
1190+ TypedBlock { statements : vec ! [ self . inner. expression_statement( expr, span) ] , span }
11811191 } else {
1182- vec ! [ ]
1192+ TypedBlock { statements : vec ! [ ] , span }
11831193 } ;
1184- let then_block = self . inner . block ( then_stmts, span) ;
11851194
1195+ // Get the else block if present
11861196 let else_block = else_branch. map ( |h| {
1187- let else_stmts = if let Some ( stmt) = self . get_stmt ( h) {
1188- vec ! [ stmt]
1197+ if let Some ( block) = self . get_block ( h) {
1198+ block
1199+ } else if let Some ( stmt) = self . get_stmt ( h) {
1200+ TypedBlock { statements : vec ! [ stmt] , span }
11891201 } else if let Some ( expr) = self . get_expr ( h) {
1190- vec ! [ self . inner. expression_statement( expr, span) ]
1202+ TypedBlock { statements : vec ! [ self . inner. expression_statement( expr, span) ] , span }
11911203 } else {
1192- vec ! [ ]
1193- } ;
1194- self . inner . block ( else_stmts, span)
1204+ TypedBlock { statements : vec ! [ ] , span }
1205+ }
11951206 } ) ;
11961207
11971208 let stmt = self . inner . if_statement ( cond_expr, then_block, else_block, span) ;
@@ -1204,14 +1215,16 @@ impl AstHostFunctions for TypedAstBuilder {
12041215 let cond_expr = self . get_expr ( condition)
12051216 . unwrap_or_else ( || self . inner . bool_literal ( true , span) ) ;
12061217
1207- let body_stmts = if let Some ( stmt) = self . get_stmt ( body) {
1208- vec ! [ stmt]
1218+ // Get body block - check for block first, then statement, then expression
1219+ let body_block = if let Some ( block) = self . get_block ( body) {
1220+ block
1221+ } else if let Some ( stmt) = self . get_stmt ( body) {
1222+ TypedBlock { statements : vec ! [ stmt] , span }
12091223 } else if let Some ( expr) = self . get_expr ( body) {
1210- vec ! [ self . inner. expression_statement( expr, span) ]
1224+ TypedBlock { statements : vec ! [ self . inner. expression_statement( expr, span) ] , span }
12111225 } else {
1212- vec ! [ ]
1226+ TypedBlock { statements : vec ! [ ] , span }
12131227 } ;
1214- let body_block = self . inner . block ( body_stmts, span) ;
12151228
12161229 let stmt = self . inner . while_loop ( cond_expr, body_block, span) ;
12171230 self . store_stmt ( stmt)
@@ -1223,14 +1236,16 @@ impl AstHostFunctions for TypedAstBuilder {
12231236 let iter_expr = self . get_expr ( iterable)
12241237 . unwrap_or_else ( || self . inner . variable ( "iter" , Type :: Primitive ( PrimitiveType :: I32 ) , span) ) ;
12251238
1226- let body_stmts = if let Some ( stmt) = self . get_stmt ( body) {
1227- vec ! [ stmt]
1239+ // Get body block - check for block first, then statement, then expression
1240+ let body_block = if let Some ( block) = self . get_block ( body) {
1241+ block
1242+ } else if let Some ( stmt) = self . get_stmt ( body) {
1243+ TypedBlock { statements : vec ! [ stmt] , span }
12281244 } else if let Some ( expr) = self . get_expr ( body) {
1229- vec ! [ self . inner. expression_statement( expr, span) ]
1245+ TypedBlock { statements : vec ! [ self . inner. expression_statement( expr, span) ] , span }
12301246 } else {
1231- vec ! [ ]
1247+ TypedBlock { statements : vec ! [ ] , span }
12321248 } ;
1233- let body_block = self . inner . block ( body_stmts, span) ;
12341249
12351250 let stmt = self . inner . for_loop ( iterator, iter_expr, body_block, span) ;
12361251 self . store_stmt ( stmt)
@@ -1746,6 +1761,7 @@ impl<'a, H: AstHostFunctions> CommandInterpreter<'a, H> {
17461761 "bool_literal" => {
17471762 let value = match args. get ( "value" ) {
17481763 Some ( RuntimeValue :: Bool ( b) ) => * b,
1764+ Some ( RuntimeValue :: String ( s) ) => s == "true" ,
17491765 _ => false ,
17501766 } ;
17511767 let handle = self . host . create_bool_literal ( value) ;
@@ -1904,8 +1920,13 @@ impl<'a, H: AstHostFunctions> CommandInterpreter<'a, H> {
19041920 }
19051921
19061922 "call_expr" | "call" => {
1923+ // Callee can be either a Node (expression) or String (identifier)
19071924 let callee = match args. get ( "callee" ) {
19081925 Some ( RuntimeValue :: Node ( h) ) => * h,
1926+ Some ( RuntimeValue :: String ( name) ) => {
1927+ // Create a variable reference for the callee
1928+ self . host . create_variable ( name)
1929+ }
19091930 _ => return Err ( crate :: error:: ZynPegError :: CodeGenError ( "call: missing callee" . into ( ) ) ) ,
19101931 } ;
19111932 let call_args: Vec < NodeHandle > = match args. get ( "args" ) {
@@ -1917,6 +1938,8 @@ impl<'a, H: AstHostFunctions> CommandInterpreter<'a, H> {
19171938 } )
19181939 . collect ( )
19191940 }
1941+ Some ( RuntimeValue :: Null ) => vec ! [ ] ,
1942+ None => vec ! [ ] ,
19201943 _ => vec ! [ ] ,
19211944 } ;
19221945 let handle = self . host . create_call ( callee, call_args) ;
@@ -2066,8 +2089,13 @@ impl<'a, H: AstHostFunctions> CommandInterpreter<'a, H> {
20662089 }
20672090
20682091 "assignment" | "assign" => {
2092+ // Target can be either a Node (expression) or String (identifier)
20692093 let target = match args. get ( "target" ) {
20702094 Some ( RuntimeValue :: Node ( h) ) => * h,
2095+ Some ( RuntimeValue :: String ( name) ) => {
2096+ // Create a variable reference for the target
2097+ self . host . create_variable ( name)
2098+ }
20712099 _ => return Err ( crate :: error:: ZynPegError :: CodeGenError ( "assignment: missing target" . into ( ) ) ) ,
20722100 } ;
20732101 let value = match args. get ( "value" ) {
@@ -2083,11 +2111,11 @@ impl<'a, H: AstHostFunctions> CommandInterpreter<'a, H> {
20832111 Some ( RuntimeValue :: Node ( h) ) => * h,
20842112 _ => return Err ( crate :: error:: ZynPegError :: CodeGenError ( "if: missing condition" . into ( ) ) ) ,
20852113 } ;
2086- let then_block = match args. get ( "then" ) . or ( args. get ( "then_block" ) ) {
2114+ let then_block = match args. get ( "then" ) . or ( args. get ( "then_block" ) ) . or ( args . get ( "then_branch" ) ) {
20872115 Some ( RuntimeValue :: Node ( h) ) => * h,
20882116 _ => return Err ( crate :: error:: ZynPegError :: CodeGenError ( "if: missing then block" . into ( ) ) ) ,
20892117 } ;
2090- let else_block = match args. get ( "else" ) . or ( args. get ( "else_block" ) ) {
2118+ let else_block = match args. get ( "else" ) . or ( args. get ( "else_block" ) ) . or ( args . get ( "else_branch" ) ) {
20912119 Some ( RuntimeValue :: Node ( h) ) => Some ( * h) ,
20922120 _ => None ,
20932121 } ;
0 commit comments