| 1 | module eval |
| 2 | |
| 3 | import os |
| 4 | import strconv |
| 5 | import time |
| 6 | import v3.flat |
| 7 | import v3.parser |
| 8 | import v3.pref |
| 9 | |
| 10 | pub type Value = ArrayValue |
| 11 | | EnumValue |
| 12 | | FnValue |
| 13 | | MapValue |
| 14 | | ModuleValue |
| 15 | | RangeValue |
| 16 | | StructValue |
| 17 | | SumValue |
| 18 | | TupleValue |
| 19 | | TypeValue |
| 20 | | VoidValue |
| 21 | | bool |
| 22 | | f64 |
| 23 | | i64 |
| 24 | | string |
| 25 | |
| 26 | pub struct ArrayValue { |
| 27 | pub mut: |
| 28 | elem_type_name string |
| 29 | values []Value |
| 30 | } |
| 31 | |
| 32 | pub struct EnumValue { |
| 33 | pub: |
| 34 | type_name string |
| 35 | value i64 |
| 36 | } |
| 37 | |
| 38 | pub struct FnValue { |
| 39 | node flat.NodeId |
| 40 | module_name string |
| 41 | file_name string |
| 42 | captures map[string]Value |
| 43 | capture_types map[string]string |
| 44 | } |
| 45 | |
| 46 | pub struct MapEntry { |
| 47 | pub: |
| 48 | key Value |
| 49 | pub mut: |
| 50 | value Value |
| 51 | } |
| 52 | |
| 53 | pub struct MapValue { |
| 54 | pub mut: |
| 55 | key_type_name string |
| 56 | value_type_name string |
| 57 | default_value Value |
| 58 | entries []MapEntry |
| 59 | } |
| 60 | |
| 61 | pub struct ModuleValue { |
| 62 | pub: |
| 63 | name string |
| 64 | } |
| 65 | |
| 66 | pub struct RangeValue { |
| 67 | pub: |
| 68 | start i64 |
| 69 | end i64 |
| 70 | inclusive bool |
| 71 | } |
| 72 | |
| 73 | pub struct StructValue { |
| 74 | pub: |
| 75 | type_name string |
| 76 | pub mut: |
| 77 | fields map[string]Value |
| 78 | } |
| 79 | |
| 80 | pub struct SumValue { |
| 81 | pub: |
| 82 | type_name string |
| 83 | variant_name string |
| 84 | pub mut: |
| 85 | payload Value |
| 86 | } |
| 87 | |
| 88 | pub struct TupleValue { |
| 89 | pub: |
| 90 | values []Value |
| 91 | } |
| 92 | |
| 93 | pub struct TypeValue { |
| 94 | pub: |
| 95 | name string |
| 96 | } |
| 97 | |
| 98 | pub struct VoidValue {} |
| 99 | |
| 100 | struct FunctionDef { |
| 101 | node flat.NodeId |
| 102 | name string |
| 103 | module_name string |
| 104 | file_name string |
| 105 | } |
| 106 | |
| 107 | struct ConstEntry { |
| 108 | node flat.NodeId |
| 109 | module_name string |
| 110 | file_name string |
| 111 | mut: |
| 112 | cached bool |
| 113 | evaluating bool |
| 114 | value Value |
| 115 | } |
| 116 | |
| 117 | struct GlobalEntry { |
| 118 | name string |
| 119 | typ string |
| 120 | default_node flat.NodeId |
| 121 | module_name string |
| 122 | file_name string |
| 123 | } |
| 124 | |
| 125 | struct EnumInitEntry { |
| 126 | node flat.NodeId |
| 127 | module_name string |
| 128 | file_name string |
| 129 | } |
| 130 | |
| 131 | struct TopLevelStmt { |
| 132 | node flat.NodeId |
| 133 | module_name string |
| 134 | file_name string |
| 135 | } |
| 136 | |
| 137 | struct StructInfo { |
| 138 | module_name string |
| 139 | name string |
| 140 | mut: |
| 141 | fields []FieldInfo |
| 142 | } |
| 143 | |
| 144 | struct TypeAliasInfo { |
| 145 | module_name string |
| 146 | target string |
| 147 | } |
| 148 | |
| 149 | struct FieldInfo { |
| 150 | name string |
| 151 | typ string |
| 152 | default_node flat.NodeId |
| 153 | module_name string |
| 154 | file_name string |
| 155 | } |
| 156 | |
| 157 | struct ScopeFrame { |
| 158 | mut: |
| 159 | vars map[string]Value |
| 160 | types map[string]string |
| 161 | defer_stmts []flat.NodeId |
| 162 | } |
| 163 | |
| 164 | struct CallFrame { |
| 165 | module_name string |
| 166 | file_name string |
| 167 | fn_name string |
| 168 | return_type string |
| 169 | scope_idx int = -1 |
| 170 | } |
| 171 | |
| 172 | struct MaybeValue { |
| 173 | found bool |
| 174 | value Value |
| 175 | } |
| 176 | |
| 177 | struct SmartcastBinding { |
| 178 | name string |
| 179 | value Value |
| 180 | type_name string |
| 181 | } |
| 182 | |
| 183 | struct CallResult { |
| 184 | values []Value |
| 185 | mutated_args map[int]Value |
| 186 | fn_value_changed bool |
| 187 | fn_value FnValue |
| 188 | } |
| 189 | |
| 190 | struct MethodCallResult { |
| 191 | value Value |
| 192 | receiver_changed bool |
| 193 | receiver Value |
| 194 | mutated_args map[int]Value |
| 195 | } |
| 196 | |
| 197 | struct LvalueStep { |
| 198 | kind flat.NodeKind |
| 199 | container Value |
| 200 | index Value |
| 201 | field_name string |
| 202 | } |
| 203 | |
| 204 | struct ResolvedLvalue { |
| 205 | mut: |
| 206 | signal FlowSignal |
| 207 | root_id flat.NodeId |
| 208 | value Value |
| 209 | steps []LvalueStep |
| 210 | } |
| 211 | |
| 212 | struct InfixOperatorCallInfo { |
| 213 | target FunctionDef |
| 214 | reverse bool |
| 215 | negate bool |
| 216 | } |
| 217 | |
| 218 | enum FlowKind { |
| 219 | normal |
| 220 | break_ |
| 221 | continue_ |
| 222 | return_ |
| 223 | } |
| 224 | |
| 225 | struct FlowSignal { |
| 226 | kind FlowKind |
| 227 | label string |
| 228 | values []Value |
| 229 | mut_lvalues map[int]ResolvedLvalue |
| 230 | } |
| 231 | |
| 232 | // Eval interprets v3 flat AST nodes directly for a practical subset of V. |
| 233 | pub struct Eval { |
| 234 | pub mut: |
| 235 | capture_output bool |
| 236 | prefs pref.Preferences |
| 237 | mut: |
| 238 | a &flat.FlatAst = unsafe { nil } |
| 239 | stdout_data string |
| 240 | stderr_data string |
| 241 | functions map[string]map[string]FunctionDef |
| 242 | consts map[string]map[string]ConstEntry |
| 243 | globals map[string]map[string]Value |
| 244 | global_types map[string]map[string]string |
| 245 | global_inits []GlobalEntry |
| 246 | enum_inits []EnumInitEntry |
| 247 | implicit_main []TopLevelStmt |
| 248 | structs map[string]StructInfo |
| 249 | enum_fields map[string][]string |
| 250 | sum_types map[string][]string |
| 251 | type_aliases map[string]TypeAliasInfo |
| 252 | type_names map[string]map[string]bool |
| 253 | module_imports map[string][]string |
| 254 | module_order []string |
| 255 | file_import_alias map[string]map[string]string |
| 256 | modules map[string]bool |
| 257 | scopes []ScopeFrame |
| 258 | call_stack []CallFrame |
| 259 | } |
| 260 | |
| 261 | // new returns a new evaluator configured for direct execution. |
| 262 | pub fn new(prefs_ &pref.Preferences) Eval { |
| 263 | return Eval{ |
| 264 | prefs: *prefs_ |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // create returns a capturing evaluator convenient for tests. |
| 269 | pub fn create() Eval { |
| 270 | return Eval{ |
| 271 | capture_output: true |
| 272 | prefs: pref.new_preferences() |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // stdout returns the captured stdout stream. |
| 277 | pub fn (e &Eval) stdout() string { |
| 278 | return e.stdout_data |
| 279 | } |
| 280 | |
| 281 | // stderr returns the captured stderr stream. |
| 282 | pub fn (e &Eval) stderr() string { |
| 283 | return e.stderr_data |
| 284 | } |
| 285 | |
| 286 | // run_text parses and executes a single V source string. |
| 287 | pub fn (mut e Eval) run_text(code string) ![]Value { |
| 288 | tmp_file := os.join_path(os.temp_dir(), 'v3_eval_${os.getpid()}_${time.now().unix_micro()}.v') |
| 289 | os.write_file(tmp_file, code)! |
| 290 | defer { |
| 291 | os.rm(tmp_file) or {} |
| 292 | } |
| 293 | mut p := parser.Parser.new(&e.prefs) |
| 294 | p.parse_into(tmp_file) |
| 295 | return e.run_files(p.a) |
| 296 | } |
| 297 | |
| 298 | // run_files executes parsed flat AST files and invokes `main.main`. |
| 299 | pub fn (mut e Eval) run_files(a &flat.FlatAst) ![]Value { |
| 300 | e.reset(a) |
| 301 | e.register_files()! |
| 302 | e.run_inits()! |
| 303 | if e.has_main_function() { |
| 304 | return e.call_function('main', 'main', []Value{})!.values |
| 305 | } |
| 306 | return e.run_implicit_main() |
| 307 | } |
| 308 | |
| 309 | fn void_value() Value { |
| 310 | return VoidValue{} |
| 311 | } |
| 312 | |
| 313 | fn normal_flow() FlowSignal { |
| 314 | return FlowSignal{ |
| 315 | kind: .normal |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | fn value_flow(value Value) FlowSignal { |
| 320 | return FlowSignal{ |
| 321 | values: [value] |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | fn (mut e Eval) reset(a &flat.FlatAst) { |
| 326 | e.a = unsafe { a } |
| 327 | e.stdout_data = '' |
| 328 | e.stderr_data = '' |
| 329 | e.functions = map[string]map[string]FunctionDef{} |
| 330 | e.consts = map[string]map[string]ConstEntry{} |
| 331 | e.globals = map[string]map[string]Value{} |
| 332 | e.global_types = map[string]map[string]string{} |
| 333 | e.global_inits = []GlobalEntry{} |
| 334 | e.enum_inits = []EnumInitEntry{} |
| 335 | e.implicit_main = []TopLevelStmt{} |
| 336 | e.structs = map[string]StructInfo{} |
| 337 | e.enum_fields = map[string][]string{} |
| 338 | e.sum_types = map[string][]string{} |
| 339 | e.type_aliases = map[string]TypeAliasInfo{} |
| 340 | e.type_names = map[string]map[string]bool{} |
| 341 | e.module_imports = map[string][]string{} |
| 342 | e.module_order = []string{} |
| 343 | e.file_import_alias = map[string]map[string]string{} |
| 344 | e.modules = map[string]bool{} |
| 345 | e.scopes = []ScopeFrame{} |
| 346 | e.call_stack = []CallFrame{} |
| 347 | } |
| 348 | |
| 349 | fn (e &Eval) node(id flat.NodeId) &flat.Node { |
| 350 | return e.a.node(id) |
| 351 | } |
| 352 | |
| 353 | fn (e &Eval) child(node &flat.Node, index int) flat.NodeId { |
| 354 | return e.a.child(node, index) |
| 355 | } |
| 356 | |
| 357 | fn (e &Eval) child_node(node &flat.Node, index int) &flat.Node { |
| 358 | return e.a.child_node(node, index) |
| 359 | } |
| 360 | |
| 361 | fn (e &Eval) children(node &flat.Node) []flat.NodeId { |
| 362 | return e.a.children_of(node) |
| 363 | } |
| 364 | |
| 365 | fn (mut e Eval) ensure_module_maps(module_name string) { |
| 366 | if module_name !in e.functions { |
| 367 | e.functions[module_name] = map[string]FunctionDef{} |
| 368 | } |
| 369 | if module_name !in e.consts { |
| 370 | e.consts[module_name] = map[string]ConstEntry{} |
| 371 | } |
| 372 | if module_name !in e.globals { |
| 373 | e.globals[module_name] = map[string]Value{} |
| 374 | } |
| 375 | if module_name !in e.global_types { |
| 376 | e.global_types[module_name] = map[string]string{} |
| 377 | } |
| 378 | if module_name !in e.type_names { |
| 379 | e.type_names[module_name] = map[string]bool{} |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | fn (e &Eval) top_level_registration_children(node &flat.Node) []flat.NodeId { |
| 384 | mut ids := []flat.NodeId{} |
| 385 | for child_id in e.children(node) { |
| 386 | child := e.node(child_id) |
| 387 | if child.kind == .block { |
| 388 | ids << e.top_level_registration_children(child) |
| 389 | continue |
| 390 | } |
| 391 | ids << child_id |
| 392 | } |
| 393 | return ids |
| 394 | } |
| 395 | |
| 396 | fn (e &Eval) has_main_function() bool { |
| 397 | return 'main' in e.functions && 'main' in e.functions['main'] |
| 398 | } |
| 399 | |
| 400 | fn is_top_level_declaration_kind(kind flat.NodeKind) bool { |
| 401 | match kind { |
| 402 | .empty, .module_decl, .import_decl, .directive, .fn_decl, .c_fn_decl, .struct_decl, |
| 403 | .field_decl, .global_decl, .const_decl, .const_field, .enum_decl, .enum_field, .type_decl, |
| 404 | .interface_decl, .interface_field, .param, .comptime_if, .comptime_for { |
| 405 | return true |
| 406 | } |
| 407 | else { |
| 408 | return false |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | fn (mut e Eval) register_implicit_main_stmt(module_name string, file_name string, id flat.NodeId, node &flat.Node) { |
| 414 | if module_name != 'main' || is_top_level_declaration_kind(node.kind) { |
| 415 | return |
| 416 | } |
| 417 | e.implicit_main << TopLevelStmt{ |
| 418 | node: id |
| 419 | module_name: module_name |
| 420 | file_name: file_name |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | fn (mut e Eval) register_files() ! { |
| 425 | for file_id, file_node in e.a.nodes { |
| 426 | if file_node.kind != .file || file_node.children_count == 0 { |
| 427 | continue |
| 428 | } |
| 429 | file_name := file_node.value |
| 430 | top_level_children := e.top_level_registration_children(&file_node) |
| 431 | mut module_name := 'main' |
| 432 | for child_id in top_level_children { |
| 433 | child := e.node(child_id) |
| 434 | if child.kind == .module_decl { |
| 435 | module_name = child.value |
| 436 | break |
| 437 | } |
| 438 | } |
| 439 | if module_name !in e.modules { |
| 440 | e.module_order << module_name |
| 441 | } |
| 442 | e.modules[module_name] = true |
| 443 | e.ensure_module_maps(module_name) |
| 444 | if module_name !in e.module_imports { |
| 445 | e.module_imports[module_name] = []string{} |
| 446 | } |
| 447 | mut import_aliases := map[string]string{} |
| 448 | for child_id in top_level_children { |
| 449 | child := e.node(child_id) |
| 450 | if child.kind != .import_decl { |
| 451 | continue |
| 452 | } |
| 453 | imported_module := child.value.all_after_last('.') |
| 454 | if imported_module.len > 0 && imported_module != module_name |
| 455 | && imported_module !in e.module_imports[module_name] { |
| 456 | e.module_imports[module_name] << imported_module |
| 457 | } |
| 458 | import_aliases[child.typ] = imported_module |
| 459 | import_aliases[imported_module] = imported_module |
| 460 | } |
| 461 | e.file_import_alias[file_name] = import_aliases.clone() |
| 462 | for child_id in top_level_children { |
| 463 | child := e.node(child_id) |
| 464 | match child.kind { |
| 465 | .import_decl {} |
| 466 | .const_decl { |
| 467 | for field_id in e.children(child) { |
| 468 | field := e.node(field_id) |
| 469 | if field.kind != .const_field { |
| 470 | continue |
| 471 | } |
| 472 | e.consts[module_name][field.value] = ConstEntry{ |
| 473 | node: field_id |
| 474 | module_name: module_name |
| 475 | file_name: file_name |
| 476 | value: void_value() |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | .global_decl { |
| 481 | for field_id in e.children(child) { |
| 482 | field := e.node(field_id) |
| 483 | if field.kind != .field_decl { |
| 484 | continue |
| 485 | } |
| 486 | e.global_inits << GlobalEntry{ |
| 487 | name: field.value |
| 488 | typ: field.typ |
| 489 | default_node: if field.children_count > 0 { |
| 490 | e.child(field, 0) |
| 491 | } else { |
| 492 | flat.empty_node |
| 493 | } |
| 494 | module_name: module_name |
| 495 | file_name: file_name |
| 496 | } |
| 497 | e.global_types[module_name][field.value] = e.qualify_nested_type_name(module_name, |
| 498 | field.typ) |
| 499 | } |
| 500 | } |
| 501 | .enum_decl { |
| 502 | e.type_names[module_name][child.value] = true |
| 503 | enum_name := e.qualify_type_name(module_name, child.value) |
| 504 | mut enum_fields := []string{} |
| 505 | for field_id in e.children(child) { |
| 506 | field := e.node(field_id) |
| 507 | if field.kind != .enum_field { |
| 508 | continue |
| 509 | } |
| 510 | enum_fields << field.value |
| 511 | } |
| 512 | e.enum_fields[enum_name] = enum_fields |
| 513 | if module_name in ['main', 'builtin'] { |
| 514 | e.enum_fields[child.value] = enum_fields |
| 515 | } |
| 516 | e.enum_inits << EnumInitEntry{ |
| 517 | node: child_id |
| 518 | module_name: module_name |
| 519 | file_name: file_name |
| 520 | } |
| 521 | } |
| 522 | .fn_decl { |
| 523 | e.register_function(module_name, file_name, child_id, child) |
| 524 | } |
| 525 | .struct_decl { |
| 526 | e.type_names[module_name][child.value] = true |
| 527 | mut fields := []FieldInfo{} |
| 528 | for field_id in e.children(child) { |
| 529 | field := e.node(field_id) |
| 530 | if field.kind == .field_decl { |
| 531 | fields << FieldInfo{ |
| 532 | name: field.value |
| 533 | typ: field.typ |
| 534 | default_node: if field.children_count > 0 { |
| 535 | e.child(field, 0) |
| 536 | } else { |
| 537 | flat.empty_node |
| 538 | } |
| 539 | module_name: module_name |
| 540 | file_name: file_name |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | info := StructInfo{ |
| 545 | module_name: module_name |
| 546 | name: child.value |
| 547 | fields: fields |
| 548 | } |
| 549 | qualified_name := e.qualify_type_name(module_name, child.value) |
| 550 | e.structs[qualified_name] = info |
| 551 | if module_name in ['main', 'builtin'] { |
| 552 | e.structs[child.value] = info |
| 553 | } |
| 554 | } |
| 555 | .type_decl { |
| 556 | e.type_names[module_name][child.value] = true |
| 557 | if child.children_count > 0 { |
| 558 | mut variants := []string{} |
| 559 | for variant_id in e.children(child) { |
| 560 | variant := e.node(variant_id) |
| 561 | variants << e.qualify_type_name(module_name, variant.value) |
| 562 | } |
| 563 | qualified_name := e.qualify_type_name(module_name, child.value) |
| 564 | e.sum_types[qualified_name] = variants |
| 565 | if module_name in ['main', 'builtin'] { |
| 566 | e.sum_types[child.value] = variants |
| 567 | } |
| 568 | } else if child.typ.len > 0 { |
| 569 | info := TypeAliasInfo{ |
| 570 | module_name: module_name |
| 571 | target: child.typ |
| 572 | } |
| 573 | qualified_name := e.qualify_type_name(module_name, child.value) |
| 574 | e.type_aliases[qualified_name] = info |
| 575 | if module_name in ['main', 'builtin'] { |
| 576 | e.type_aliases[child.value] = info |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | else { |
| 581 | e.register_implicit_main_stmt(module_name, file_name, child_id, child) |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | _ = file_id |
| 586 | } |
| 587 | e.evaluate_enum_inits()! |
| 588 | e.evaluate_global_inits()! |
| 589 | if !e.has_main_function() && e.implicit_main.len == 0 { |
| 590 | return error('v3.eval: missing main.main entry point') |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | fn (mut e Eval) evaluate_enum_inits() ! { |
| 595 | for entry in e.enum_inits { |
| 596 | node := e.node(entry.node) |
| 597 | mut next_value := if node.typ == 'flag' { i64(1) } else { i64(0) } |
| 598 | for field_id in e.children(node) { |
| 599 | field := e.node(field_id) |
| 600 | if field.kind != .enum_field { |
| 601 | continue |
| 602 | } |
| 603 | value := if field.children_count > 0 { |
| 604 | e.value_as_int(e.eval_expr_in_module(e.child(field, 0), entry.module_name, |
| 605 | entry.file_name, '<enum ${node.value}.${field.value}>')!)! |
| 606 | } else { |
| 607 | next_value |
| 608 | } |
| 609 | e.consts[entry.module_name][field.value] = ConstEntry{ |
| 610 | node: field_id |
| 611 | module_name: entry.module_name |
| 612 | file_name: entry.file_name |
| 613 | cached: true |
| 614 | value: Value(value) |
| 615 | } |
| 616 | e.consts[entry.module_name]['${node.value}.${field.value}'] = ConstEntry{ |
| 617 | node: field_id |
| 618 | module_name: entry.module_name |
| 619 | file_name: entry.file_name |
| 620 | cached: true |
| 621 | value: Value(value) |
| 622 | } |
| 623 | next_value = if node.typ == 'flag' { |
| 624 | if value <= 0 { i64(1) } else { value * 2 } |
| 625 | } else { |
| 626 | value + 1 |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | fn (mut e Eval) run_implicit_main() ![]Value { |
| 633 | e.open_scope() |
| 634 | scope_idx := e.scopes.len - 1 |
| 635 | mut frame := CallFrame{ |
| 636 | module_name: 'main' |
| 637 | file_name: if e.implicit_main.len > 0 { e.implicit_main[0].file_name } else { '' } |
| 638 | fn_name: 'main' |
| 639 | return_type: 'void' |
| 640 | scope_idx: scope_idx |
| 641 | } |
| 642 | e.call_stack << frame |
| 643 | mut values := [void_value()] |
| 644 | mut escaped := normal_flow() |
| 645 | for stmt in e.implicit_main { |
| 646 | frame = CallFrame{ |
| 647 | module_name: stmt.module_name |
| 648 | file_name: stmt.file_name |
| 649 | fn_name: 'main' |
| 650 | return_type: 'void' |
| 651 | scope_idx: scope_idx |
| 652 | } |
| 653 | e.call_stack[e.call_stack.len - 1] = frame |
| 654 | signal := e.exec_stmt(stmt.node)! |
| 655 | if signal.kind == .return_ { |
| 656 | values = flow_values_or_void(signal.values) |
| 657 | break |
| 658 | } |
| 659 | if signal.kind != .normal { |
| 660 | escaped = signal |
| 661 | break |
| 662 | } |
| 663 | } |
| 664 | e.run_deferred_stmts()! |
| 665 | e.close_scope()! |
| 666 | e.call_stack.delete(e.call_stack.len - 1) |
| 667 | if escaped.kind != .normal { |
| 668 | return error('v3.eval: unexpected `${escaped.kind}` escaped implicit main') |
| 669 | } |
| 670 | return values |
| 671 | } |
| 672 | |
| 673 | fn (mut e Eval) evaluate_global_inits() ! { |
| 674 | mut entries_by_module := map[string][]GlobalEntry{} |
| 675 | for entry in e.global_inits { |
| 676 | mut entries := entries_by_module[entry.module_name] |
| 677 | entries << entry |
| 678 | entries_by_module[entry.module_name] = entries |
| 679 | } |
| 680 | mut visited := map[string]bool{} |
| 681 | mut visiting := map[string]bool{} |
| 682 | e.evaluate_module_global_inits('main', entries_by_module, mut visited, mut visiting)! |
| 683 | for module_name in e.module_order { |
| 684 | e.evaluate_module_global_inits(module_name, entries_by_module, mut visited, mut visiting)! |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | fn (mut e Eval) evaluate_module_global_inits(module_name string, entries_by_module map[string][]GlobalEntry, mut visited map[string]bool, mut visiting map[string]bool) ! { |
| 689 | if module_name in visited { |
| 690 | return |
| 691 | } |
| 692 | if module_name in visiting { |
| 693 | return |
| 694 | } |
| 695 | visiting[module_name] = true |
| 696 | if module_name in e.module_imports { |
| 697 | for imported_module in e.module_imports[module_name] { |
| 698 | e.evaluate_module_global_inits(imported_module, entries_by_module, mut visited, mut |
| 699 | visiting)! |
| 700 | } |
| 701 | } |
| 702 | visiting.delete(module_name) |
| 703 | visited[module_name] = true |
| 704 | if module_name !in entries_by_module { |
| 705 | return |
| 706 | } |
| 707 | for entry in entries_by_module[module_name] { |
| 708 | e.evaluate_global_init(entry)! |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | fn (mut e Eval) evaluate_global_init(entry GlobalEntry) ! { |
| 713 | e.call_stack << CallFrame{ |
| 714 | module_name: entry.module_name |
| 715 | file_name: entry.file_name |
| 716 | fn_name: '<global ${entry.name}>' |
| 717 | } |
| 718 | value := if int(entry.default_node) >= 0 { |
| 719 | e.eval_expr(entry.default_node)! |
| 720 | } else { |
| 721 | e.zero_value_for_type_name_in_module(entry.typ, entry.module_name) |
| 722 | } |
| 723 | e.call_stack.delete(e.call_stack.len - 1) |
| 724 | e.globals[entry.module_name][entry.name] = e.adapt_value_to_type_name(value, entry.typ) |
| 725 | } |
| 726 | |
| 727 | fn (mut e Eval) eval_expr_in_module(id flat.NodeId, module_name string, file_name string, label string) !Value { |
| 728 | e.call_stack << CallFrame{ |
| 729 | module_name: module_name |
| 730 | file_name: file_name |
| 731 | fn_name: label |
| 732 | } |
| 733 | defer { |
| 734 | e.call_stack.delete(e.call_stack.len - 1) |
| 735 | } |
| 736 | return e.eval_expr(id) |
| 737 | } |
| 738 | |
| 739 | fn (mut e Eval) register_function(module_name string, file_name string, id flat.NodeId, node &flat.Node) { |
| 740 | def := FunctionDef{ |
| 741 | node: id |
| 742 | name: node.value |
| 743 | module_name: module_name |
| 744 | file_name: file_name |
| 745 | } |
| 746 | e.functions[module_name][node.value] = def |
| 747 | if module_name != 'main' && module_name != 'builtin' { |
| 748 | e.functions[module_name]['${module_name}.${node.value}'] = def |
| 749 | } |
| 750 | if node.value.contains('.') { |
| 751 | short := node.value.all_after_last('.') |
| 752 | receiver := node.value.all_before_last('.').all_after_last('.') |
| 753 | e.functions[module_name]['${receiver}.${short}'] = def |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | fn (mut e Eval) run_inits() ! { |
| 758 | mut visited := map[string]bool{} |
| 759 | mut visiting := map[string]bool{} |
| 760 | e.run_module_init('main', mut visited, mut visiting)! |
| 761 | for module_name in e.module_order { |
| 762 | e.run_module_init(module_name, mut visited, mut visiting)! |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | fn (mut e Eval) run_module_init(module_name string, mut visited map[string]bool, mut visiting map[string]bool) ! { |
| 767 | if module_name in visited { |
| 768 | return |
| 769 | } |
| 770 | if module_name in visiting { |
| 771 | return |
| 772 | } |
| 773 | visiting[module_name] = true |
| 774 | if module_name in e.module_imports { |
| 775 | for imported_module in e.module_imports[module_name] { |
| 776 | e.run_module_init(imported_module, mut visited, mut visiting)! |
| 777 | } |
| 778 | } |
| 779 | visiting.delete(module_name) |
| 780 | visited[module_name] = true |
| 781 | if module_name in e.functions && 'init' in e.functions[module_name] { |
| 782 | e.call_function(module_name, 'init', []Value{})! |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | fn (mut e Eval) open_scope() { |
| 787 | e.scopes << ScopeFrame{ |
| 788 | vars: map[string]Value{} |
| 789 | types: map[string]string{} |
| 790 | defer_stmts: []flat.NodeId{} |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | fn (mut e Eval) close_scope() ! { |
| 795 | if e.scopes.len > 0 { |
| 796 | e.run_deferred_stmts()! |
| 797 | e.scopes.delete(e.scopes.len - 1) |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | fn (mut e Eval) declare_var(name string, value Value) { |
| 802 | e.declare_var_typed(name, value, '') |
| 803 | } |
| 804 | |
| 805 | fn (mut e Eval) declare_var_typed(name string, value Value, type_name string) { |
| 806 | if name == '_' { |
| 807 | return |
| 808 | } |
| 809 | if e.scopes.len == 0 { |
| 810 | e.open_scope() |
| 811 | } |
| 812 | e.scopes[e.scopes.len - 1].vars[name] = value |
| 813 | normalized := e.normalize_type_name(type_name) |
| 814 | if normalized.len > 0 { |
| 815 | e.scopes[e.scopes.len - 1].types[name] = normalized |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | fn (mut e Eval) set_var(name string, value Value) ! { |
| 820 | if name == '_' { |
| 821 | return |
| 822 | } |
| 823 | for i := e.scopes.len - 1; i >= 0; i-- { |
| 824 | if name in e.scopes[i].vars { |
| 825 | e.scopes[i].vars[name] = value |
| 826 | return |
| 827 | } |
| 828 | } |
| 829 | module_name := e.current_module_name() |
| 830 | if module_name in e.globals && name in e.globals[module_name] { |
| 831 | e.globals[module_name][name] = value |
| 832 | return |
| 833 | } |
| 834 | return e.unknown_variable_error(name) |
| 835 | } |
| 836 | |
| 837 | fn (mut e Eval) set_var_type(name string, type_name string) { |
| 838 | normalized := e.normalize_type_name(type_name) |
| 839 | if normalized.len == 0 { |
| 840 | return |
| 841 | } |
| 842 | for i := e.scopes.len - 1; i >= 0; i-- { |
| 843 | if name in e.scopes[i].vars { |
| 844 | e.scopes[i].types[name] = normalized |
| 845 | return |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | fn (e &Eval) lookup_var(name string) MaybeValue { |
| 851 | for i := e.scopes.len - 1; i >= 0; i-- { |
| 852 | if name in e.scopes[i].vars { |
| 853 | return MaybeValue{ |
| 854 | found: true |
| 855 | value: e.scopes[i].vars[name] or { void_value() } |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | module_name := e.current_module_name() |
| 860 | if module_name in e.globals && name in e.globals[module_name] { |
| 861 | return MaybeValue{ |
| 862 | found: true |
| 863 | value: e.globals[module_name][name] or { void_value() } |
| 864 | } |
| 865 | } |
| 866 | return MaybeValue{} |
| 867 | } |
| 868 | |
| 869 | fn (e &Eval) lookup_var_type(name string) ?string { |
| 870 | for i := e.scopes.len - 1; i >= 0; i-- { |
| 871 | if name in e.scopes[i].types { |
| 872 | return e.scopes[i].types[name] |
| 873 | } |
| 874 | } |
| 875 | module_name := e.current_module_name() |
| 876 | if module_name in e.global_types && name in e.global_types[module_name] { |
| 877 | return e.global_types[module_name][name] |
| 878 | } |
| 879 | return none |
| 880 | } |
| 881 | |
| 882 | fn (e &Eval) current_module_name() string { |
| 883 | if e.call_stack.len == 0 { |
| 884 | return 'main' |
| 885 | } |
| 886 | return e.call_stack[e.call_stack.len - 1].module_name |
| 887 | } |
| 888 | |
| 889 | fn (e &Eval) current_file_name() string { |
| 890 | if e.call_stack.len == 0 { |
| 891 | return '' |
| 892 | } |
| 893 | return e.call_stack[e.call_stack.len - 1].file_name |
| 894 | } |
| 895 | |
| 896 | fn (e &Eval) current_function_label() string { |
| 897 | if e.call_stack.len == 0 { |
| 898 | return '<top-level>' |
| 899 | } |
| 900 | frame := e.call_stack[e.call_stack.len - 1] |
| 901 | return '${frame.module_name}.${frame.fn_name}' |
| 902 | } |
| 903 | |
| 904 | fn (e &Eval) call_stack_trace() string { |
| 905 | return e.call_stack.map('${it.module_name}.${it.fn_name}').join(' -> ') |
| 906 | } |
| 907 | |
| 908 | fn (e &Eval) unknown_variable_error(name string) IError { |
| 909 | return error('v3.eval: unknown variable `${name}` in `${e.current_function_label()}` stack `${e.call_stack_trace()}`') |
| 910 | } |
| 911 | |
| 912 | fn (mut e Eval) call_function(module_name string, fn_name string, args []Value) !CallResult { |
| 913 | builtin := e.maybe_call_builtin(module_name, fn_name, args)! |
| 914 | if builtin.found { |
| 915 | return CallResult{ |
| 916 | values: [builtin.value] |
| 917 | } |
| 918 | } |
| 919 | target_module := if module_name == '' { e.current_module_name() } else { module_name } |
| 920 | if target_module !in e.functions { |
| 921 | return error('v3.eval: unknown module `${target_module}`') |
| 922 | } |
| 923 | if fn_name !in e.functions[target_module] { |
| 924 | return error('v3.eval: unknown function `${target_module}.${fn_name}`') |
| 925 | } |
| 926 | def := e.functions[target_module][fn_name] |
| 927 | node := e.node(def.node) |
| 928 | mut params := []flat.NodeId{} |
| 929 | mut body_start := 0 |
| 930 | for child_id in e.children(node) { |
| 931 | child := e.node(child_id) |
| 932 | if child.kind == .param { |
| 933 | params << child_id |
| 934 | body_start++ |
| 935 | continue |
| 936 | } |
| 937 | break |
| 938 | } |
| 939 | min_args := e.minimum_arg_count(params) |
| 940 | if args.len < min_args { |
| 941 | return error('v3.eval: `${target_module}.${fn_name}` expected ${min_args} arguments, got ${args.len}') |
| 942 | } |
| 943 | e.open_scope() |
| 944 | e.call_stack << CallFrame{ |
| 945 | module_name: target_module |
| 946 | file_name: def.file_name |
| 947 | fn_name: fn_name |
| 948 | return_type: node.typ |
| 949 | scope_idx: e.scopes.len - 1 |
| 950 | } |
| 951 | e.bind_call_params(params, args)! |
| 952 | mut body_ids := e.children(node) |
| 953 | if body_start > 0 { |
| 954 | body_ids = body_ids[body_start..].clone() |
| 955 | } |
| 956 | signal := e.exec_stmts(body_ids)! |
| 957 | e.run_deferred_stmts()! |
| 958 | mutated_args := e.collect_mutated_param_args(params, args) |
| 959 | adapted_values := if signal.kind == .return_ { |
| 960 | e.adapt_return_values(signal.values, node.typ) |
| 961 | } else { |
| 962 | []Value{} |
| 963 | } |
| 964 | e.close_scope()! |
| 965 | e.call_stack.delete(e.call_stack.len - 1) |
| 966 | if signal.kind == .return_ { |
| 967 | return CallResult{ |
| 968 | values: adapted_values |
| 969 | mutated_args: mutated_args |
| 970 | } |
| 971 | } |
| 972 | if signal.kind != .normal { |
| 973 | return error('v3.eval: unexpected `${signal.kind}` escaped `${target_module}.${fn_name}`') |
| 974 | } |
| 975 | return CallResult{ |
| 976 | values: [void_value()] |
| 977 | mutated_args: mutated_args |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | fn (mut e Eval) exec_stmts(stmts []flat.NodeId) !FlowSignal { |
| 982 | mut i := 0 |
| 983 | for i < stmts.len { |
| 984 | stmt_id := stmts[i] |
| 985 | if int(stmt_id) >= 0 { |
| 986 | node := e.node(stmt_id) |
| 987 | if node.kind == .label_stmt && i + 1 < stmts.len { |
| 988 | next_id := stmts[i + 1] |
| 989 | if int(next_id) >= 0 { |
| 990 | next_node := e.node(next_id) |
| 991 | if next_node.kind == .for_stmt { |
| 992 | signal := e.exec_for(next_node, node.value)! |
| 993 | if signal.kind != .normal { |
| 994 | return signal |
| 995 | } |
| 996 | i += 2 |
| 997 | continue |
| 998 | } |
| 999 | if next_node.kind == .for_in_stmt { |
| 1000 | signal := e.exec_for_in(next_node, node.value)! |
| 1001 | if signal.kind != .normal { |
| 1002 | return signal |
| 1003 | } |
| 1004 | i += 2 |
| 1005 | continue |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | signal := e.exec_stmt(stmt_id)! |
| 1011 | if signal.kind != .normal { |
| 1012 | return signal |
| 1013 | } |
| 1014 | i++ |
| 1015 | } |
| 1016 | return normal_flow() |
| 1017 | } |
| 1018 | |
| 1019 | fn (mut e Eval) exec_block(node &flat.Node) !FlowSignal { |
| 1020 | e.open_scope() |
| 1021 | signal := e.exec_stmts(e.children(node))! |
| 1022 | e.close_scope()! |
| 1023 | return signal |
| 1024 | } |
| 1025 | |
| 1026 | fn (mut e Eval) exec_stmt(id flat.NodeId) !FlowSignal { |
| 1027 | if int(id) < 0 { |
| 1028 | return normal_flow() |
| 1029 | } |
| 1030 | node := e.node(id) |
| 1031 | match node.kind { |
| 1032 | .block { |
| 1033 | return e.exec_block(node) |
| 1034 | } |
| 1035 | .expr_stmt { |
| 1036 | if node.children_count > 0 { |
| 1037 | expr_id := e.child(node, 0) |
| 1038 | expr := e.node(expr_id) |
| 1039 | if expr.kind == .infix && expr.op == .left_shift { |
| 1040 | return e.exec_array_append(expr) |
| 1041 | } else { |
| 1042 | signal := e.eval_expr_flow(expr_id)! |
| 1043 | if signal.kind != .normal { |
| 1044 | return signal |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | return normal_flow() |
| 1049 | } |
| 1050 | .decl_assign { |
| 1051 | return e.exec_assign(node, true) |
| 1052 | } |
| 1053 | .assign, .selector_assign, .index_assign { |
| 1054 | return e.exec_assign(node, false) |
| 1055 | } |
| 1056 | .return_stmt { |
| 1057 | mut values := []Value{} |
| 1058 | return_type := e.current_return_type() |
| 1059 | return_types := split_multi_return_types(return_type) |
| 1060 | child_count := node.children_count |
| 1061 | mut child_index := 0 |
| 1062 | for child_id in e.children(node) { |
| 1063 | expected_type := return_child_expected_type(return_type, return_types, child_index, |
| 1064 | child_count) |
| 1065 | signal := e.eval_expr_flow_expected(child_id, expected_type)! |
| 1066 | if signal.kind != .normal { |
| 1067 | return signal |
| 1068 | } |
| 1069 | expr_values := flow_values_or_void(signal.values) |
| 1070 | if expr_values.len > 1 { |
| 1071 | values << expr_values |
| 1072 | continue |
| 1073 | } |
| 1074 | value := expr_values[0] |
| 1075 | if value is TupleValue { |
| 1076 | values << value.values |
| 1077 | } else { |
| 1078 | values << value |
| 1079 | } |
| 1080 | child_index++ |
| 1081 | } |
| 1082 | if values.len == 0 { |
| 1083 | values << void_value() |
| 1084 | } |
| 1085 | return FlowSignal{ |
| 1086 | kind: .return_ |
| 1087 | values: values |
| 1088 | } |
| 1089 | } |
| 1090 | .if_expr { |
| 1091 | return e.exec_if(node) |
| 1092 | } |
| 1093 | .match_stmt { |
| 1094 | return e.exec_match(node) |
| 1095 | } |
| 1096 | .for_stmt { |
| 1097 | return e.exec_for(node, '') |
| 1098 | } |
| 1099 | .for_in_stmt { |
| 1100 | return e.exec_for_in(node, '') |
| 1101 | } |
| 1102 | .break_stmt { |
| 1103 | return FlowSignal{ |
| 1104 | kind: .break_ |
| 1105 | label: node.value |
| 1106 | } |
| 1107 | } |
| 1108 | .continue_stmt { |
| 1109 | return FlowSignal{ |
| 1110 | kind: .continue_ |
| 1111 | label: node.value |
| 1112 | } |
| 1113 | } |
| 1114 | .assert_stmt { |
| 1115 | cond_signal := e.eval_expr_flow(e.child(node, 0))! |
| 1116 | if cond_signal.kind != .normal { |
| 1117 | return cond_signal |
| 1118 | } |
| 1119 | cond := flow_value(cond_signal) |
| 1120 | if !e.value_as_bool(cond)! { |
| 1121 | mut msg := 'assertion failed' |
| 1122 | if node.children_count > 1 { |
| 1123 | msg = e.display_string(e.eval_expr(e.child(node, 1))!)! |
| 1124 | } |
| 1125 | return error('v3.eval: ${msg}') |
| 1126 | } |
| 1127 | return normal_flow() |
| 1128 | } |
| 1129 | .defer_stmt { |
| 1130 | e.register_defer_stmt(id) |
| 1131 | return normal_flow() |
| 1132 | } |
| 1133 | .asm_stmt, .empty, .label_stmt { |
| 1134 | return normal_flow() |
| 1135 | } |
| 1136 | else { |
| 1137 | _ = e.eval_expr(id)! |
| 1138 | return normal_flow() |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | fn (mut e Eval) register_defer_stmt(id flat.NodeId) { |
| 1144 | if e.scopes.len == 0 { |
| 1145 | e.open_scope() |
| 1146 | } |
| 1147 | node := e.node(id) |
| 1148 | scope_idx := if node.value == 'function' && e.call_stack.len > 0 |
| 1149 | && e.call_stack[e.call_stack.len - 1].scope_idx >= 0 { |
| 1150 | e.call_stack[e.call_stack.len - 1].scope_idx |
| 1151 | } else { |
| 1152 | e.scopes.len - 1 |
| 1153 | } |
| 1154 | e.scopes[scope_idx].defer_stmts << id |
| 1155 | } |
| 1156 | |
| 1157 | fn (mut e Eval) run_deferred_stmts() ! { |
| 1158 | if e.scopes.len == 0 { |
| 1159 | return |
| 1160 | } |
| 1161 | scope_idx := e.scopes.len - 1 |
| 1162 | defer_stmts := e.scopes[scope_idx].defer_stmts.clone() |
| 1163 | e.scopes[scope_idx].defer_stmts = []flat.NodeId{} |
| 1164 | for i := defer_stmts.len; i > 0; i-- { |
| 1165 | defer_id := defer_stmts[i - 1] |
| 1166 | defer_node := e.node(defer_id) |
| 1167 | for child_id in e.children(defer_node) { |
| 1168 | signal := e.exec_stmt(child_id)! |
| 1169 | if signal.kind != .normal { |
| 1170 | return error('v3.eval: unexpected `${signal.kind}` escaped defer') |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | fn (mut e Eval) exec_assign(node &flat.Node, declare bool) !FlowSignal { |
| 1177 | children := e.children(node) |
| 1178 | if children.len == 0 { |
| 1179 | return normal_flow() |
| 1180 | } |
| 1181 | if children.len == 2 { |
| 1182 | lhs_id := children[0] |
| 1183 | rhs_id := children[1] |
| 1184 | if node.op != .assign && node.op != .none && e.target_needs_single_eval(lhs_id) { |
| 1185 | return e.exec_compound_assignment_pair_flow(node.op, lhs_id, rhs_id, declare) |
| 1186 | } |
| 1187 | target_type := e.assignment_target_type_name(lhs_id, declare) |
| 1188 | signal := e.assignment_value_flow(node.op, lhs_id, rhs_id, target_type)! |
| 1189 | if signal.kind != .normal { |
| 1190 | return signal |
| 1191 | } |
| 1192 | assign_signal := e.assign_target_typed_flow(lhs_id, flow_value(signal), declare, if target_type.len > 0 { |
| 1193 | target_type |
| 1194 | } else { |
| 1195 | e.infer_expr_type_name(rhs_id) |
| 1196 | })! |
| 1197 | if assign_signal.kind != .normal { |
| 1198 | return assign_signal |
| 1199 | } |
| 1200 | return normal_flow() |
| 1201 | } |
| 1202 | if children.len > 2 { |
| 1203 | first_target_type := e.assignment_target_type_name(children[0], declare) |
| 1204 | first_signal := e.assignment_value_flow(node.op, children[0], children[1], |
| 1205 | first_target_type)! |
| 1206 | if first_signal.kind != .normal { |
| 1207 | return first_signal |
| 1208 | } |
| 1209 | value := flow_value(first_signal) |
| 1210 | mut lhs_ids := [children[0]] |
| 1211 | for i := 2; i < children.len; i++ { |
| 1212 | lhs_ids << children[i] |
| 1213 | } |
| 1214 | if value is TupleValue { |
| 1215 | for i, lhs_id in lhs_ids { |
| 1216 | item := if i < value.values.len { value.values[i] } else { void_value() } |
| 1217 | assign_signal := e.assign_target_flow(lhs_id, item, declare)! |
| 1218 | if assign_signal.kind != .normal { |
| 1219 | return assign_signal |
| 1220 | } |
| 1221 | } |
| 1222 | return normal_flow() |
| 1223 | } |
| 1224 | if children.len % 2 == 1 { |
| 1225 | for i, lhs_id in lhs_ids { |
| 1226 | item := if i == 0 { value } else { void_value() } |
| 1227 | assign_signal := e.assign_target_flow(lhs_id, item, declare)! |
| 1228 | if assign_signal.kind != .normal { |
| 1229 | return assign_signal |
| 1230 | } |
| 1231 | } |
| 1232 | return normal_flow() |
| 1233 | } |
| 1234 | mut assign_lhs_ids := [children[0]] |
| 1235 | mut values := [value] |
| 1236 | mut i := 2 |
| 1237 | for i < children.len { |
| 1238 | lhs_id := children[i] |
| 1239 | rhs_id := if i + 1 < children.len { children[i + 1] } else { flat.empty_node } |
| 1240 | item := if int(rhs_id) >= 0 { |
| 1241 | target_type := e.assignment_target_type_name(lhs_id, declare) |
| 1242 | signal := e.assignment_value_flow(node.op, lhs_id, rhs_id, target_type)! |
| 1243 | if signal.kind != .normal { |
| 1244 | return signal |
| 1245 | } |
| 1246 | flow_value(signal) |
| 1247 | } else { |
| 1248 | void_value() |
| 1249 | } |
| 1250 | assign_lhs_ids << lhs_id |
| 1251 | values << item |
| 1252 | i += 2 |
| 1253 | } |
| 1254 | for idx, lhs_id in assign_lhs_ids { |
| 1255 | assign_signal := e.assign_target_flow(lhs_id, values[idx], declare)! |
| 1256 | if assign_signal.kind != .normal { |
| 1257 | return assign_signal |
| 1258 | } |
| 1259 | } |
| 1260 | return normal_flow() |
| 1261 | } |
| 1262 | mut i := 0 |
| 1263 | for i < children.len { |
| 1264 | lhs_id := children[i] |
| 1265 | rhs_id := if i + 1 < children.len { children[i + 1] } else { flat.empty_node } |
| 1266 | value := if int(rhs_id) >= 0 { |
| 1267 | target_type := e.assignment_target_type_name(lhs_id, declare) |
| 1268 | signal := e.assignment_value_flow(node.op, lhs_id, rhs_id, target_type)! |
| 1269 | if signal.kind != .normal { |
| 1270 | return signal |
| 1271 | } |
| 1272 | flow_value(signal) |
| 1273 | } else { |
| 1274 | void_value() |
| 1275 | } |
| 1276 | assign_signal := e.assign_target_flow(lhs_id, value, declare)! |
| 1277 | if assign_signal.kind != .normal { |
| 1278 | return assign_signal |
| 1279 | } |
| 1280 | i += 2 |
| 1281 | } |
| 1282 | return normal_flow() |
| 1283 | } |
| 1284 | |
| 1285 | fn (e &Eval) target_needs_single_eval(id flat.NodeId) bool { |
| 1286 | if int(id) < 0 { |
| 1287 | return false |
| 1288 | } |
| 1289 | return e.node(id).kind in [.index, .selector] |
| 1290 | } |
| 1291 | |
| 1292 | fn (mut e Eval) exec_compound_assignment_pair_flow(op flat.Op, lhs_id flat.NodeId, rhs_id flat.NodeId, declare bool) !FlowSignal { |
| 1293 | target_type := e.assignment_target_type_name(lhs_id, declare) |
| 1294 | rhs_signal := e.eval_expr_flow_expected(rhs_id, target_type)! |
| 1295 | if rhs_signal.kind != .normal { |
| 1296 | return rhs_signal |
| 1297 | } |
| 1298 | rhs := flow_value(rhs_signal) |
| 1299 | return e.apply_compound_target_flow(op, lhs_id, rhs, declare, if target_type.len > 0 { |
| 1300 | target_type |
| 1301 | } else { |
| 1302 | e.infer_expr_type_name(rhs_id) |
| 1303 | }) |
| 1304 | } |
| 1305 | |
| 1306 | fn (mut e Eval) apply_compound_target_flow(op flat.Op, lhs_id flat.NodeId, rhs Value, declare bool, type_name string) !FlowSignal { |
| 1307 | node := e.node(lhs_id) |
| 1308 | match node.kind { |
| 1309 | .index { |
| 1310 | return e.update_resolved_lvalue_with_op_flow(lhs_id, op, rhs) |
| 1311 | } |
| 1312 | .selector { |
| 1313 | return e.update_resolved_lvalue_with_op_flow(lhs_id, op, rhs) |
| 1314 | } |
| 1315 | else { |
| 1316 | left_signal := e.eval_expr_flow(lhs_id)! |
| 1317 | if left_signal.kind != .normal { |
| 1318 | return left_signal |
| 1319 | } |
| 1320 | value := e.apply_assignment_op(op, flow_value(left_signal), rhs)! |
| 1321 | return e.assign_target_typed_flow(lhs_id, value, declare, type_name) |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | fn (mut e Eval) assignment_value(op flat.Op, lhs_id flat.NodeId, rhs_id flat.NodeId) !Value { |
| 1327 | rhs := e.eval_expr(rhs_id)! |
| 1328 | if op == .assign || op == .none { |
| 1329 | return rhs |
| 1330 | } |
| 1331 | left := e.eval_expr(lhs_id) or { e.zero_value_like(rhs) } |
| 1332 | return match op { |
| 1333 | .plus_assign { e.apply_infix(.plus, left, rhs)! } |
| 1334 | .minus_assign { e.apply_infix(.minus, left, rhs)! } |
| 1335 | .mul_assign { e.apply_infix(.mul, left, rhs)! } |
| 1336 | .div_assign { e.apply_infix(.div, left, rhs)! } |
| 1337 | .mod_assign { e.apply_infix(.mod, left, rhs)! } |
| 1338 | .amp_assign { e.apply_infix(.amp, left, rhs)! } |
| 1339 | .pipe_assign { e.apply_infix(.pipe, left, rhs)! } |
| 1340 | .xor_assign { e.apply_infix(.xor, left, rhs)! } |
| 1341 | .left_shift_assign { e.apply_infix(.left_shift, left, rhs)! } |
| 1342 | .right_shift_assign { e.apply_infix(.right_shift, left, rhs)! } |
| 1343 | .right_shift_unsigned_assign { e.apply_infix(.right_shift_unsigned, left, rhs)! } |
| 1344 | else { rhs } |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | fn (mut e Eval) assignment_value_flow(op flat.Op, lhs_id flat.NodeId, rhs_id flat.NodeId, expected_type string) !FlowSignal { |
| 1349 | rhs_signal := e.eval_expr_flow_expected(rhs_id, expected_type)! |
| 1350 | if rhs_signal.kind != .normal { |
| 1351 | return rhs_signal |
| 1352 | } |
| 1353 | rhs := flow_value(rhs_signal) |
| 1354 | if op == .assign || op == .none { |
| 1355 | return FlowSignal{ |
| 1356 | values: [rhs] |
| 1357 | } |
| 1358 | } |
| 1359 | left_signal := e.eval_expr_flow(lhs_id)! |
| 1360 | if left_signal.kind != .normal { |
| 1361 | return left_signal |
| 1362 | } |
| 1363 | left := flow_value(left_signal) |
| 1364 | return FlowSignal{ |
| 1365 | values: [e.apply_assignment_op(op, left, rhs)!] |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | fn (mut e Eval) apply_assignment_op(op flat.Op, left Value, rhs Value) !Value { |
| 1370 | return match op { |
| 1371 | .plus_assign { e.apply_infix(.plus, left, rhs)! } |
| 1372 | .minus_assign { e.apply_infix(.minus, left, rhs)! } |
| 1373 | .mul_assign { e.apply_infix(.mul, left, rhs)! } |
| 1374 | .div_assign { e.apply_infix(.div, left, rhs)! } |
| 1375 | .mod_assign { e.apply_infix(.mod, left, rhs)! } |
| 1376 | .amp_assign { e.apply_infix(.amp, left, rhs)! } |
| 1377 | .pipe_assign { e.apply_infix(.pipe, left, rhs)! } |
| 1378 | .xor_assign { e.apply_infix(.xor, left, rhs)! } |
| 1379 | .left_shift_assign { e.apply_infix(.left_shift, left, rhs)! } |
| 1380 | .right_shift_assign { e.apply_infix(.right_shift, left, rhs)! } |
| 1381 | .right_shift_unsigned_assign { e.apply_infix(.right_shift_unsigned, left, rhs)! } |
| 1382 | else { rhs } |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | fn (mut e Eval) assignment_target_type_name(lhs_id flat.NodeId, declare bool) string { |
| 1387 | if int(lhs_id) < 0 { |
| 1388 | return '' |
| 1389 | } |
| 1390 | node := e.node(lhs_id) |
| 1391 | match node.kind { |
| 1392 | .ident { |
| 1393 | if !declare { |
| 1394 | if typ := e.lookup_var_type(node.value) { |
| 1395 | return typ |
| 1396 | } |
| 1397 | } |
| 1398 | return node.typ |
| 1399 | } |
| 1400 | .selector { |
| 1401 | if node.children_count > 0 { |
| 1402 | base_type := e.infer_expr_type_name(e.child(node, 0)) |
| 1403 | if base_type.len > 0 { |
| 1404 | return e.struct_field_type_name_by_type(base_type, node.value) |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | .index { |
| 1409 | if node.children_count > 0 { |
| 1410 | container_type := e.infer_expr_type_name(e.child(node, 0)) |
| 1411 | if container_type.starts_with('[]') { |
| 1412 | return container_type[2..] |
| 1413 | } |
| 1414 | if is_fixed_array_type_name(container_type) { |
| 1415 | return fixed_array_elem_type_name(container_type) |
| 1416 | } |
| 1417 | if container_type.starts_with('map[') { |
| 1418 | _, value_type := split_map_type(container_type) |
| 1419 | return value_type |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | else {} |
| 1424 | } |
| 1425 | |
| 1426 | return '' |
| 1427 | } |
| 1428 | |
| 1429 | fn flow_value(signal FlowSignal) Value { |
| 1430 | if signal.values.len > 0 { |
| 1431 | return signal.values[0] |
| 1432 | } |
| 1433 | return void_value() |
| 1434 | } |
| 1435 | |
| 1436 | fn flow_values_value(values []Value) Value { |
| 1437 | if values.len == 0 { |
| 1438 | return void_value() |
| 1439 | } |
| 1440 | if values.len == 1 { |
| 1441 | return values[0] |
| 1442 | } |
| 1443 | return TupleValue{ |
| 1444 | values: values.clone() |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | fn flow_values_or_void(values []Value) []Value { |
| 1449 | if values.len > 0 { |
| 1450 | return values.clone() |
| 1451 | } |
| 1452 | return [void_value()] |
| 1453 | } |
| 1454 | |
| 1455 | fn (mut e Eval) assign_target(id flat.NodeId, value Value, declare bool) ! { |
| 1456 | signal := e.assign_target_typed_flow(id, value, declare, '')! |
| 1457 | if signal.kind != .normal { |
| 1458 | return error('v3.eval: unexpected `${signal.kind}` escaped assignment target') |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | fn (mut e Eval) assign_target_typed(id flat.NodeId, value Value, declare bool, type_name string) ! { |
| 1463 | signal := e.assign_target_typed_flow(id, value, declare, type_name)! |
| 1464 | if signal.kind != .normal { |
| 1465 | return error('v3.eval: unexpected `${signal.kind}` escaped assignment target') |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | fn (mut e Eval) assign_target_flow(id flat.NodeId, value Value, declare bool) !FlowSignal { |
| 1470 | return e.assign_target_typed_flow(id, value, declare, '') |
| 1471 | } |
| 1472 | |
| 1473 | fn (mut e Eval) assign_target_typed_flow(id flat.NodeId, value Value, declare bool, type_name string) !FlowSignal { |
| 1474 | node := e.node(id) |
| 1475 | match node.kind { |
| 1476 | .ident { |
| 1477 | if declare { |
| 1478 | decl_type := if type_name.len > 0 { type_name } else { node.typ } |
| 1479 | e.declare_var_typed(node.value, e.adapt_value_to_type_name(value, decl_type), |
| 1480 | decl_type) |
| 1481 | } else { |
| 1482 | target_type := if existing := e.lookup_var_type(node.value) { |
| 1483 | existing |
| 1484 | } else { |
| 1485 | if type_name.len > 0 { type_name } else { node.typ } |
| 1486 | } |
| 1487 | e.set_var(node.value, e.adapt_value_to_type_name(value, target_type))! |
| 1488 | e.set_var_type(node.value, target_type) |
| 1489 | } |
| 1490 | } |
| 1491 | .selector { |
| 1492 | return e.update_resolved_lvalue_flow(id, value) |
| 1493 | } |
| 1494 | .index { |
| 1495 | return e.update_resolved_lvalue_flow(id, value) |
| 1496 | } |
| 1497 | else { |
| 1498 | return error('v3.eval: unsupported assignment target `${node.kind}`') |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | return normal_flow() |
| 1503 | } |
| 1504 | |
| 1505 | fn (e &Eval) infer_expr_type_name(id flat.NodeId) string { |
| 1506 | if int(id) < 0 { |
| 1507 | return '' |
| 1508 | } |
| 1509 | node := e.node(id) |
| 1510 | if node.kind in [.struct_init, .cast_expr, .as_expr] { |
| 1511 | return e.normalize_type_name(node.value) |
| 1512 | } |
| 1513 | if node.typ.len > 0 { |
| 1514 | return e.normalize_type_name(node.typ) |
| 1515 | } |
| 1516 | match node.kind { |
| 1517 | .int_literal { |
| 1518 | return 'int' |
| 1519 | } |
| 1520 | .float_literal { |
| 1521 | return 'f64' |
| 1522 | } |
| 1523 | .bool_literal { |
| 1524 | return 'bool' |
| 1525 | } |
| 1526 | .char_literal { |
| 1527 | return 'char' |
| 1528 | } |
| 1529 | .string_literal, .string_interp { |
| 1530 | return 'string' |
| 1531 | } |
| 1532 | .ident { |
| 1533 | if typ := e.lookup_var_type(node.value) { |
| 1534 | return typ |
| 1535 | } |
| 1536 | } |
| 1537 | .selector { |
| 1538 | if node.children_count > 0 { |
| 1539 | if typ := e.type_value_name_from_expr(e.child(node, 0)) { |
| 1540 | return typ |
| 1541 | } |
| 1542 | base_type := e.infer_expr_type_name(e.child(node, 0)) |
| 1543 | if base_type.len > 0 { |
| 1544 | return e.struct_field_type_name_by_type(base_type, node.value) |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | .index { |
| 1549 | if node.children_count > 0 { |
| 1550 | container_type := e.infer_expr_type_name(e.child(node, 0)) |
| 1551 | if container_type.starts_with('[]') { |
| 1552 | return container_type[2..] |
| 1553 | } |
| 1554 | if is_fixed_array_type_name(container_type) { |
| 1555 | return fixed_array_elem_type_name(container_type) |
| 1556 | } |
| 1557 | if container_type.starts_with('map[') { |
| 1558 | _, value_type := split_map_type(container_type) |
| 1559 | return value_type |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | .array_literal { |
| 1564 | elem_type_name := e.array_literal_elem_type_name(node) |
| 1565 | if elem_type_name.len > 0 { |
| 1566 | return '[]${elem_type_name}' |
| 1567 | } |
| 1568 | } |
| 1569 | else {} |
| 1570 | } |
| 1571 | |
| 1572 | return '' |
| 1573 | } |
| 1574 | |
| 1575 | fn (e &Eval) array_literal_elem_type_name(node &flat.Node) string { |
| 1576 | if node.typ.len > 0 { |
| 1577 | return array_elem_type_name_from_type(node.typ) |
| 1578 | } |
| 1579 | if node.value.len > 0 { |
| 1580 | return array_elem_type_name_from_type(node.value) |
| 1581 | } |
| 1582 | if node.children_count > 0 { |
| 1583 | return e.infer_expr_type_name(e.child(node, 0)) |
| 1584 | } |
| 1585 | return '' |
| 1586 | } |
| 1587 | |
| 1588 | fn array_elem_type_name_from_type(type_name string) string { |
| 1589 | if type_name.starts_with('[]') { |
| 1590 | return type_name[2..] |
| 1591 | } |
| 1592 | if is_fixed_array_type_name(type_name) { |
| 1593 | return fixed_array_elem_type_name(type_name) |
| 1594 | } |
| 1595 | return '' |
| 1596 | } |
| 1597 | |
| 1598 | fn (mut e Eval) update_target(id flat.NodeId, value Value) ! { |
| 1599 | signal := e.update_target_flow(id, value)! |
| 1600 | if signal.kind != .normal { |
| 1601 | return error('v3.eval: unexpected `${signal.kind}` escaped assignment target') |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | fn (mut e Eval) update_target_flow(id flat.NodeId, value Value) !FlowSignal { |
| 1606 | return e.assign_target_flow(id, value, false) |
| 1607 | } |
| 1608 | |
| 1609 | fn (mut e Eval) update_resolved_lvalue_flow(id flat.NodeId, value Value) !FlowSignal { |
| 1610 | resolved := e.resolve_lvalue_flow(id)! |
| 1611 | if resolved.signal.kind != .normal { |
| 1612 | return resolved.signal |
| 1613 | } |
| 1614 | return e.write_resolved_lvalue_flow(resolved, value) |
| 1615 | } |
| 1616 | |
| 1617 | fn (mut e Eval) update_resolved_lvalue_with_op_flow(id flat.NodeId, op flat.Op, rhs Value) !FlowSignal { |
| 1618 | resolved := e.resolve_lvalue_flow(id)! |
| 1619 | if resolved.signal.kind != .normal { |
| 1620 | return resolved.signal |
| 1621 | } |
| 1622 | value := e.apply_assignment_op(op, resolved.value, rhs)! |
| 1623 | return e.write_resolved_lvalue_flow(resolved, value) |
| 1624 | } |
| 1625 | |
| 1626 | fn (mut e Eval) update_resolved_lvalue_postfix_flow(id flat.NodeId, op flat.Op) !FlowSignal { |
| 1627 | resolved := e.resolve_lvalue_flow(id)! |
| 1628 | if resolved.signal.kind != .normal { |
| 1629 | return resolved.signal |
| 1630 | } |
| 1631 | value := e.apply_postfix_op(op, resolved.value)! |
| 1632 | update_signal := e.write_resolved_lvalue_flow(resolved, value)! |
| 1633 | if update_signal.kind != .normal { |
| 1634 | return update_signal |
| 1635 | } |
| 1636 | return value_flow(resolved.value) |
| 1637 | } |
| 1638 | |
| 1639 | fn (mut e Eval) resolve_lvalue_flow(id flat.NodeId) !ResolvedLvalue { |
| 1640 | node := e.node(id) |
| 1641 | match node.kind { |
| 1642 | .selector { |
| 1643 | mut base := e.resolve_lvalue_flow(e.child(node, 0))! |
| 1644 | if base.signal.kind != .normal { |
| 1645 | return base |
| 1646 | } |
| 1647 | value := e.eval_selector_value(base.value, node.value)! |
| 1648 | base.steps << LvalueStep{ |
| 1649 | kind: .selector |
| 1650 | container: base.value |
| 1651 | index: void_value() |
| 1652 | field_name: node.value |
| 1653 | } |
| 1654 | base.value = value |
| 1655 | return base |
| 1656 | } |
| 1657 | .index { |
| 1658 | mut base := e.resolve_lvalue_flow(e.child(node, 0))! |
| 1659 | if base.signal.kind != .normal { |
| 1660 | return base |
| 1661 | } |
| 1662 | index_signal := if base.value is MapValue { |
| 1663 | e.eval_expr_flow_expected(e.child(node, 1), base.value.key_type_name)! |
| 1664 | } else { |
| 1665 | e.eval_expr_flow(e.child(node, 1))! |
| 1666 | } |
| 1667 | if index_signal.kind != .normal { |
| 1668 | return ResolvedLvalue{ |
| 1669 | signal: index_signal |
| 1670 | } |
| 1671 | } |
| 1672 | index := flow_value(index_signal) |
| 1673 | value := e.index_value(base.value, index)! |
| 1674 | base.steps << LvalueStep{ |
| 1675 | kind: .index |
| 1676 | container: base.value |
| 1677 | index: index |
| 1678 | } |
| 1679 | base.value = value |
| 1680 | return base |
| 1681 | } |
| 1682 | else { |
| 1683 | signal := e.eval_expr_flow(id)! |
| 1684 | if signal.kind != .normal { |
| 1685 | return ResolvedLvalue{ |
| 1686 | signal: signal |
| 1687 | } |
| 1688 | } |
| 1689 | return ResolvedLvalue{ |
| 1690 | root_id: id |
| 1691 | value: flow_value(signal) |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | fn (mut e Eval) write_resolved_lvalue_flow(resolved ResolvedLvalue, value Value) !FlowSignal { |
| 1698 | mut new_value := value |
| 1699 | for i := resolved.steps.len - 1; i >= 0; i-- { |
| 1700 | step := resolved.steps[i] |
| 1701 | match step.kind { |
| 1702 | .index { |
| 1703 | new_value = e.set_index_value(step.container, step.index, new_value)! |
| 1704 | } |
| 1705 | .selector { |
| 1706 | new_value = e.set_selector_value(step.container, step.field_name, new_value)! |
| 1707 | } |
| 1708 | else {} |
| 1709 | } |
| 1710 | } |
| 1711 | return e.update_target_flow(resolved.root_id, new_value) |
| 1712 | } |
| 1713 | |
| 1714 | fn (mut e Eval) set_index_value(container Value, index Value, value Value) !Value { |
| 1715 | match container { |
| 1716 | ArrayValue { |
| 1717 | mut arr := ArrayValue{ |
| 1718 | elem_type_name: container.elem_type_name |
| 1719 | values: container.values.clone() |
| 1720 | } |
| 1721 | idx := int(e.value_as_int(index)!) |
| 1722 | if idx < 0 || idx >= arr.values.len { |
| 1723 | return error('v3.eval: array index out of bounds') |
| 1724 | } |
| 1725 | arr.values[idx] = e.adapt_value_to_type_name(value, arr.elem_type_name) |
| 1726 | return arr |
| 1727 | } |
| 1728 | MapValue { |
| 1729 | return e.map_set_value(container, index, value) |
| 1730 | } |
| 1731 | else { |
| 1732 | return error('v3.eval: indexed assignment expects array or map') |
| 1733 | } |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | fn (mut e Eval) set_selector_value(container Value, field_name string, value Value) !Value { |
| 1738 | match container { |
| 1739 | StructValue { |
| 1740 | mut st := container |
| 1741 | st.fields[field_name] = e.adapt_value_to_type_name(value, e.struct_field_type_name(container, |
| 1742 | field_name)) |
| 1743 | return st |
| 1744 | } |
| 1745 | SumValue { |
| 1746 | mut sv := container |
| 1747 | sv.payload = e.set_selector_value(sv.payload, field_name, value)! |
| 1748 | return sv |
| 1749 | } |
| 1750 | else { |
| 1751 | return error('v3.eval: selector assignment expects struct, got `${e.runtime_type_name(container)}`') |
| 1752 | } |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | fn (mut e Eval) exec_array_append(node &flat.Node) !FlowSignal { |
| 1757 | lhs_id := e.child(node, 0) |
| 1758 | rhs_id := e.child(node, 1) |
| 1759 | resolved := e.resolve_lvalue_flow(lhs_id)! |
| 1760 | if resolved.signal.kind != .normal { |
| 1761 | return resolved.signal |
| 1762 | } |
| 1763 | current := resolved.value |
| 1764 | if current !is ArrayValue { |
| 1765 | return error('v3.eval: `<<` is only supported for arrays') |
| 1766 | } |
| 1767 | mut arr := current as ArrayValue |
| 1768 | rhs_type_name := e.infer_expr_type_name(rhs_id) |
| 1769 | rhs_expected_type := if rhs_type_name.len > 0 |
| 1770 | && e.array_type_spreads_into_elem(rhs_type_name, arr.elem_type_name) { |
| 1771 | rhs_type_name |
| 1772 | } else { |
| 1773 | arr.elem_type_name |
| 1774 | } |
| 1775 | rhs_signal := e.eval_expr_flow_expected(rhs_id, rhs_expected_type)! |
| 1776 | if rhs_signal.kind != .normal { |
| 1777 | return rhs_signal |
| 1778 | } |
| 1779 | rhs := flow_value(rhs_signal) |
| 1780 | if rhs is ArrayValue && e.array_append_rhs_spreads(rhs_id, rhs, arr.elem_type_name) { |
| 1781 | for item in rhs.values { |
| 1782 | arr.values << e.adapt_value_to_type_name(item, arr.elem_type_name) |
| 1783 | } |
| 1784 | } else { |
| 1785 | arr.values << e.adapt_value_to_type_name(rhs, arr.elem_type_name) |
| 1786 | } |
| 1787 | return e.write_resolved_lvalue_flow(resolved, arr) |
| 1788 | } |
| 1789 | |
| 1790 | fn (e &Eval) array_append_rhs_spreads(rhs_id flat.NodeId, rhs ArrayValue, elem_type_name string) bool { |
| 1791 | rhs_type_name := e.infer_expr_type_name(rhs_id) |
| 1792 | if rhs_type_name.len > 0 { |
| 1793 | return e.array_type_spreads_into_elem(rhs_type_name, elem_type_name) |
| 1794 | } |
| 1795 | if rhs.elem_type_name.len > 0 { |
| 1796 | return e.type_name_matches(rhs.elem_type_name, elem_type_name) |
| 1797 | } |
| 1798 | return false |
| 1799 | } |
| 1800 | |
| 1801 | fn (e &Eval) array_type_spreads_into_elem(array_type_name string, elem_type_name string) bool { |
| 1802 | rhs_elem_type_name := array_elem_type_name_from_type(array_type_name) |
| 1803 | if rhs_elem_type_name.len == 0 { |
| 1804 | return false |
| 1805 | } |
| 1806 | return e.type_name_matches(rhs_elem_type_name, elem_type_name) |
| 1807 | } |
| 1808 | |
| 1809 | fn (mut e Eval) exec_if(node &flat.Node) !FlowSignal { |
| 1810 | if node.children_count < 2 { |
| 1811 | return normal_flow() |
| 1812 | } |
| 1813 | cond_id := e.child(node, 0) |
| 1814 | cond_is_guard := e.node(cond_id).kind == .decl_assign |
| 1815 | if cond_is_guard { |
| 1816 | e.open_scope() |
| 1817 | } |
| 1818 | cond_signal := e.eval_condition_flow(cond_id)! |
| 1819 | if cond_signal.kind != .normal { |
| 1820 | if cond_is_guard { |
| 1821 | e.close_scope()! |
| 1822 | } |
| 1823 | return cond_signal |
| 1824 | } |
| 1825 | if e.value_as_bool(flow_value(cond_signal))! { |
| 1826 | mut smartcast_scope := false |
| 1827 | if !cond_is_guard { |
| 1828 | if binding := e.smartcast_binding_from_condition(cond_id) { |
| 1829 | e.open_scope() |
| 1830 | e.declare_var_typed(binding.name, binding.value, binding.type_name) |
| 1831 | smartcast_scope = true |
| 1832 | } |
| 1833 | } |
| 1834 | signal := e.exec_stmt(e.child(node, 1))! |
| 1835 | if smartcast_scope { |
| 1836 | e.close_scope()! |
| 1837 | } |
| 1838 | if cond_is_guard { |
| 1839 | e.close_scope()! |
| 1840 | } |
| 1841 | return signal |
| 1842 | } |
| 1843 | if cond_is_guard { |
| 1844 | e.close_scope()! |
| 1845 | } |
| 1846 | if node.children_count > 2 { |
| 1847 | return e.exec_stmt(e.child(node, 2)) |
| 1848 | } |
| 1849 | return normal_flow() |
| 1850 | } |
| 1851 | |
| 1852 | fn (mut e Eval) smartcast_binding_from_condition(cond_id flat.NodeId) ?SmartcastBinding { |
| 1853 | if int(cond_id) < 0 { |
| 1854 | return none |
| 1855 | } |
| 1856 | node := e.node(cond_id) |
| 1857 | if node.kind == .paren { |
| 1858 | return e.smartcast_binding_from_condition(e.child(node, 0)) |
| 1859 | } |
| 1860 | if node.kind == .infix && node.op == .logical_and { |
| 1861 | return e.smartcast_binding_from_condition(e.child(node, 0)) |
| 1862 | } |
| 1863 | if node.kind != .is_expr || node.children_count == 0 { |
| 1864 | return none |
| 1865 | } |
| 1866 | left_id := e.child(node, 0) |
| 1867 | left := e.node(left_id) |
| 1868 | if left.kind != .ident { |
| 1869 | return none |
| 1870 | } |
| 1871 | found := e.lookup_var(left.value) |
| 1872 | if !found.found { |
| 1873 | return none |
| 1874 | } |
| 1875 | if value := e.smartcast_value(found.value, node.value) { |
| 1876 | return SmartcastBinding{ |
| 1877 | name: left.value |
| 1878 | value: value |
| 1879 | type_name: e.normalize_type_name(node.value) |
| 1880 | } |
| 1881 | } |
| 1882 | return none |
| 1883 | } |
| 1884 | |
| 1885 | fn (e &Eval) smartcast_value(value Value, target_type string) ?Value { |
| 1886 | if value is SumValue { |
| 1887 | if e.type_name_matches(value.variant_name, target_type) |
| 1888 | || e.value_matches_type_name(value.payload, target_type) { |
| 1889 | return value.payload |
| 1890 | } |
| 1891 | } |
| 1892 | return none |
| 1893 | } |
| 1894 | |
| 1895 | fn (mut e Eval) eval_condition(id flat.NodeId) !bool { |
| 1896 | signal := e.eval_condition_flow(id)! |
| 1897 | return e.value_as_bool(flow_value(signal)) |
| 1898 | } |
| 1899 | |
| 1900 | fn (mut e Eval) eval_condition_flow(id flat.NodeId) !FlowSignal { |
| 1901 | node := e.node(id) |
| 1902 | if node.kind == .decl_assign { |
| 1903 | rhs_id := e.child(node, 1) |
| 1904 | rhs_node := e.node(rhs_id) |
| 1905 | mut ok := false |
| 1906 | mut bind_value := Value(void_value()) |
| 1907 | mut bind_type := '' |
| 1908 | if rhs_node.kind == .index && rhs_node.value != 'range' && rhs_node.children_count > 1 { |
| 1909 | container_signal := e.eval_expr_flow(e.child(rhs_node, 0))! |
| 1910 | if container_signal.kind != .normal { |
| 1911 | return container_signal |
| 1912 | } |
| 1913 | container := flow_value(container_signal) |
| 1914 | index_signal := if container is MapValue { |
| 1915 | e.eval_expr_flow_expected(e.child(rhs_node, 1), container.key_type_name)! |
| 1916 | } else { |
| 1917 | e.eval_expr_flow(e.child(rhs_node, 1))! |
| 1918 | } |
| 1919 | if index_signal.kind != .normal { |
| 1920 | return index_signal |
| 1921 | } |
| 1922 | if container is MapValue { |
| 1923 | value, found := e.map_lookup(container, flow_value(index_signal)) |
| 1924 | ok = found |
| 1925 | bind_value = value |
| 1926 | bind_type = container.value_type_name |
| 1927 | } else { |
| 1928 | value := e.index_value(container, flow_value(index_signal))! |
| 1929 | ok = e.value_is_truthy(value) |
| 1930 | bind_value = e.unwrap_option_like(value) |
| 1931 | bind_type = e.guard_binding_type_name(rhs_id, value) |
| 1932 | } |
| 1933 | } else { |
| 1934 | rhs_signal := e.eval_expr_flow(rhs_id)! |
| 1935 | if rhs_signal.kind != .normal { |
| 1936 | return rhs_signal |
| 1937 | } |
| 1938 | value := flow_value(rhs_signal) |
| 1939 | ok = e.value_is_truthy(value) |
| 1940 | bind_value = e.unwrap_option_like(value) |
| 1941 | bind_type = e.guard_binding_type_name(rhs_id, value) |
| 1942 | } |
| 1943 | if ok { |
| 1944 | e.assign_target_typed(e.child(node, 0), bind_value, true, bind_type)! |
| 1945 | } |
| 1946 | return value_flow(Value(ok)) |
| 1947 | } |
| 1948 | signal := e.eval_expr_flow(id)! |
| 1949 | if signal.kind != .normal { |
| 1950 | return signal |
| 1951 | } |
| 1952 | return value_flow(Value(e.value_as_bool(flow_value(signal))!)) |
| 1953 | } |
| 1954 | |
| 1955 | fn (e &Eval) guard_binding_type_name(rhs_id flat.NodeId, value Value) string { |
| 1956 | rhs_type := e.infer_expr_type_name(rhs_id) |
| 1957 | name := rhs_type.trim_left('&') |
| 1958 | if name.starts_with('?') || name.starts_with('!') { |
| 1959 | return name[1..] |
| 1960 | } |
| 1961 | if name.len > 0 { |
| 1962 | return name |
| 1963 | } |
| 1964 | return e.runtime_type_name(e.unwrap_option_like(value)) |
| 1965 | } |
| 1966 | |
| 1967 | fn (e &Eval) unwrap_option_like(value Value) Value { |
| 1968 | if value is StructValue { |
| 1969 | if 'data' in value.fields { |
| 1970 | return value.fields['data'] or { void_value() } |
| 1971 | } |
| 1972 | } |
| 1973 | return value |
| 1974 | } |
| 1975 | |
| 1976 | fn (e &Eval) value_is_truthy(value Value) bool { |
| 1977 | match value { |
| 1978 | bool { |
| 1979 | return value |
| 1980 | } |
| 1981 | VoidValue { |
| 1982 | return false |
| 1983 | } |
| 1984 | StructValue { |
| 1985 | state := value.fields['state'] or { void_value() } |
| 1986 | if state is i64 { |
| 1987 | return state == 0 |
| 1988 | } |
| 1989 | is_error := value.fields['is_error'] or { void_value() } |
| 1990 | if is_error is bool { |
| 1991 | return !is_error |
| 1992 | } |
| 1993 | return true |
| 1994 | } |
| 1995 | else { |
| 1996 | return true |
| 1997 | } |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | fn (mut e Eval) exec_for(node &flat.Node, loop_label string) !FlowSignal { |
| 2002 | if node.children_count < 3 { |
| 2003 | return normal_flow() |
| 2004 | } |
| 2005 | init_id := e.child(node, 0) |
| 2006 | cond_id := e.child(node, 1) |
| 2007 | post_id := e.child(node, 2) |
| 2008 | e.open_scope() |
| 2009 | if int(init_id) >= 0 && e.node(init_id).kind != .empty { |
| 2010 | init_signal := e.exec_stmt(init_id)! |
| 2011 | if init_signal.kind != .normal { |
| 2012 | e.close_scope()! |
| 2013 | return init_signal |
| 2014 | } |
| 2015 | } |
| 2016 | body := e.children(node) |
| 2017 | for { |
| 2018 | if int(cond_id) >= 0 && e.node(cond_id).kind != .empty { |
| 2019 | cond_signal := e.eval_condition_flow(cond_id)! |
| 2020 | if cond_signal.kind != .normal { |
| 2021 | e.close_scope()! |
| 2022 | return cond_signal |
| 2023 | } |
| 2024 | if !e.value_as_bool(flow_value(cond_signal))! { |
| 2025 | break |
| 2026 | } |
| 2027 | } |
| 2028 | e.open_scope() |
| 2029 | signal := e.exec_stmts(body[3..])! |
| 2030 | e.close_scope()! |
| 2031 | if signal.kind == .break_ { |
| 2032 | if e.flow_targets_loop(signal, loop_label) { |
| 2033 | e.close_scope()! |
| 2034 | return normal_flow() |
| 2035 | } |
| 2036 | e.close_scope()! |
| 2037 | return signal |
| 2038 | } |
| 2039 | if signal.kind == .continue_ { |
| 2040 | if !e.flow_targets_loop(signal, loop_label) { |
| 2041 | e.close_scope()! |
| 2042 | return signal |
| 2043 | } |
| 2044 | } |
| 2045 | if signal.kind == .return_ { |
| 2046 | e.close_scope()! |
| 2047 | return signal |
| 2048 | } |
| 2049 | if int(post_id) >= 0 && e.node(post_id).kind != .empty { |
| 2050 | post_signal := e.exec_stmt(post_id)! |
| 2051 | if post_signal.kind != .normal { |
| 2052 | e.close_scope()! |
| 2053 | return post_signal |
| 2054 | } |
| 2055 | } |
| 2056 | } |
| 2057 | e.close_scope()! |
| 2058 | return normal_flow() |
| 2059 | } |
| 2060 | |
| 2061 | fn (mut e Eval) exec_for_in(node &flat.Node, loop_label string) !FlowSignal { |
| 2062 | header_count := if node.value.int() > 0 { node.value.int() } else { 3 } |
| 2063 | key_id := e.child(node, 0) |
| 2064 | val_id := e.child(node, 1) |
| 2065 | container_id := e.child(node, 2) |
| 2066 | is_mut_loop := node.op == .amp |
| 2067 | mut resolved_container := ResolvedLvalue{} |
| 2068 | mut has_resolved_container := false |
| 2069 | mut container := Value(void_value()) |
| 2070 | if is_mut_loop && e.is_assignable_target(container_id) { |
| 2071 | resolved_container = e.resolve_lvalue_flow(container_id)! |
| 2072 | if resolved_container.signal.kind != .normal { |
| 2073 | return resolved_container.signal |
| 2074 | } |
| 2075 | container = resolved_container.value |
| 2076 | has_resolved_container = true |
| 2077 | } else { |
| 2078 | container_signal := e.eval_expr_flow(container_id)! |
| 2079 | if container_signal.kind != .normal { |
| 2080 | return container_signal |
| 2081 | } |
| 2082 | container = flow_value(container_signal) |
| 2083 | } |
| 2084 | body := e.children(node)[header_count..] |
| 2085 | if header_count == 4 { |
| 2086 | start := e.value_as_int(container)! |
| 2087 | end_signal := e.eval_expr_flow(e.child(node, 3))! |
| 2088 | if end_signal.kind != .normal { |
| 2089 | return end_signal |
| 2090 | } |
| 2091 | end := e.value_as_int(flow_value(end_signal))! |
| 2092 | for i := start; i < end; i++ { |
| 2093 | e.open_scope() |
| 2094 | e.assign_loop_vars(key_id, val_id, Value(i), void_value(), false) |
| 2095 | signal := e.exec_stmts(body)! |
| 2096 | e.close_scope()! |
| 2097 | if signal.kind == .break_ { |
| 2098 | if e.flow_targets_loop(signal, loop_label) { |
| 2099 | return normal_flow() |
| 2100 | } |
| 2101 | return signal |
| 2102 | } |
| 2103 | if signal.kind == .continue_ { |
| 2104 | if e.flow_targets_loop(signal, loop_label) { |
| 2105 | continue |
| 2106 | } |
| 2107 | return signal |
| 2108 | } |
| 2109 | if signal.kind == .return_ { |
| 2110 | return signal |
| 2111 | } |
| 2112 | } |
| 2113 | return normal_flow() |
| 2114 | } |
| 2115 | match container { |
| 2116 | RangeValue { |
| 2117 | last := if container.inclusive { container.end + 1 } else { container.end } |
| 2118 | for i := container.start; i < last; i++ { |
| 2119 | e.open_scope() |
| 2120 | e.assign_loop_vars(key_id, val_id, Value(i), void_value(), false) |
| 2121 | signal := e.exec_stmts(body)! |
| 2122 | e.close_scope()! |
| 2123 | if signal.kind == .break_ { |
| 2124 | if e.flow_targets_loop(signal, loop_label) { |
| 2125 | return normal_flow() |
| 2126 | } |
| 2127 | return signal |
| 2128 | } |
| 2129 | if signal.kind == .continue_ { |
| 2130 | if e.flow_targets_loop(signal, loop_label) { |
| 2131 | continue |
| 2132 | } |
| 2133 | return signal |
| 2134 | } |
| 2135 | if signal.kind == .return_ { |
| 2136 | return signal |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | ArrayValue { |
| 2141 | mut arr := ArrayValue{ |
| 2142 | elem_type_name: container.elem_type_name |
| 2143 | values: container.values.clone() |
| 2144 | } |
| 2145 | for i, item in arr.values { |
| 2146 | e.open_scope() |
| 2147 | e.assign_loop_vars(key_id, val_id, Value(i64(i)), item, true) |
| 2148 | signal := e.exec_stmts(body)! |
| 2149 | if is_mut_loop { |
| 2150 | if value := e.loop_mut_value(key_id, val_id, true) { |
| 2151 | arr.values[i] = e.adapt_value_to_type_name(value, arr.elem_type_name) |
| 2152 | mut write_signal := FlowSignal{} |
| 2153 | if has_resolved_container { |
| 2154 | write_signal = e.write_resolved_lvalue_flow(resolved_container, arr) or { |
| 2155 | e.close_scope()! |
| 2156 | return err |
| 2157 | } |
| 2158 | } else { |
| 2159 | write_signal = e.update_target_flow(container_id, arr) or { |
| 2160 | e.close_scope()! |
| 2161 | return err |
| 2162 | } |
| 2163 | } |
| 2164 | if write_signal.kind != .normal { |
| 2165 | e.close_scope()! |
| 2166 | return write_signal |
| 2167 | } |
| 2168 | } |
| 2169 | } |
| 2170 | e.close_scope()! |
| 2171 | if signal.kind == .break_ { |
| 2172 | if e.flow_targets_loop(signal, loop_label) { |
| 2173 | return normal_flow() |
| 2174 | } |
| 2175 | return signal |
| 2176 | } |
| 2177 | if signal.kind == .continue_ { |
| 2178 | if e.flow_targets_loop(signal, loop_label) { |
| 2179 | continue |
| 2180 | } |
| 2181 | return signal |
| 2182 | } |
| 2183 | if signal.kind == .return_ { |
| 2184 | return signal |
| 2185 | } |
| 2186 | } |
| 2187 | } |
| 2188 | MapValue { |
| 2189 | mut m := MapValue{ |
| 2190 | key_type_name: container.key_type_name |
| 2191 | value_type_name: container.value_type_name |
| 2192 | default_value: container.default_value |
| 2193 | entries: container.entries.clone() |
| 2194 | } |
| 2195 | for i, entry in m.entries { |
| 2196 | e.open_scope() |
| 2197 | e.assign_loop_vars(key_id, val_id, entry.key, entry.value, true) |
| 2198 | signal := e.exec_stmts(body)! |
| 2199 | if is_mut_loop { |
| 2200 | if value := e.loop_mut_value(key_id, val_id, true) { |
| 2201 | m.entries[i].value = e.adapt_value_to_type_name(value, m.value_type_name) |
| 2202 | mut write_signal := FlowSignal{} |
| 2203 | if has_resolved_container { |
| 2204 | write_signal = e.write_resolved_lvalue_flow(resolved_container, m) or { |
| 2205 | e.close_scope()! |
| 2206 | return err |
| 2207 | } |
| 2208 | } else { |
| 2209 | write_signal = e.update_target_flow(container_id, m) or { |
| 2210 | e.close_scope()! |
| 2211 | return err |
| 2212 | } |
| 2213 | } |
| 2214 | if write_signal.kind != .normal { |
| 2215 | e.close_scope()! |
| 2216 | return write_signal |
| 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | e.close_scope()! |
| 2221 | if signal.kind == .break_ { |
| 2222 | if e.flow_targets_loop(signal, loop_label) { |
| 2223 | return normal_flow() |
| 2224 | } |
| 2225 | return signal |
| 2226 | } |
| 2227 | if signal.kind == .continue_ { |
| 2228 | if e.flow_targets_loop(signal, loop_label) { |
| 2229 | continue |
| 2230 | } |
| 2231 | return signal |
| 2232 | } |
| 2233 | if signal.kind == .return_ { |
| 2234 | return signal |
| 2235 | } |
| 2236 | } |
| 2237 | } |
| 2238 | string { |
| 2239 | for i, ch in container { |
| 2240 | e.open_scope() |
| 2241 | e.assign_loop_vars(key_id, val_id, Value(i64(i)), Value(i64(ch)), true) |
| 2242 | signal := e.exec_stmts(body)! |
| 2243 | e.close_scope()! |
| 2244 | if signal.kind == .break_ { |
| 2245 | if e.flow_targets_loop(signal, loop_label) { |
| 2246 | return normal_flow() |
| 2247 | } |
| 2248 | return signal |
| 2249 | } |
| 2250 | if signal.kind == .continue_ { |
| 2251 | if e.flow_targets_loop(signal, loop_label) { |
| 2252 | continue |
| 2253 | } |
| 2254 | return signal |
| 2255 | } |
| 2256 | if signal.kind == .return_ { |
| 2257 | return signal |
| 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | else { |
| 2262 | return error('v3.eval: unsupported for-in iterable `${e.runtime_type_name(container)}`') |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | return normal_flow() |
| 2267 | } |
| 2268 | |
| 2269 | fn (e &Eval) flow_targets_loop(signal FlowSignal, loop_label string) bool { |
| 2270 | return signal.label.len == 0 || signal.label == loop_label |
| 2271 | } |
| 2272 | |
| 2273 | fn (mut e Eval) assign_loop_vars(key_id flat.NodeId, val_id flat.NodeId, key Value, value Value, has_value bool) { |
| 2274 | if int(val_id) >= 0 && e.node(val_id).kind != .empty { |
| 2275 | e.assign_target(key_id, key, true) or {} |
| 2276 | e.assign_target(val_id, value, true) or {} |
| 2277 | } else if has_value { |
| 2278 | e.assign_target(key_id, value, true) or {} |
| 2279 | } else { |
| 2280 | e.assign_target(key_id, key, true) or {} |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | fn (e &Eval) loop_mut_value(key_id flat.NodeId, val_id flat.NodeId, has_value bool) ?Value { |
| 2285 | mut value_id := key_id |
| 2286 | if int(val_id) >= 0 && e.node(val_id).kind != .empty { |
| 2287 | value_id = val_id |
| 2288 | } else if !has_value { |
| 2289 | return none |
| 2290 | } |
| 2291 | value_node := e.node(value_id) |
| 2292 | if value_node.kind != .ident { |
| 2293 | return none |
| 2294 | } |
| 2295 | found := e.lookup_var(value_node.value) |
| 2296 | if !found.found { |
| 2297 | return none |
| 2298 | } |
| 2299 | return found.value |
| 2300 | } |
| 2301 | |
| 2302 | fn (mut e Eval) eval_expr(id flat.NodeId) !Value { |
| 2303 | if int(id) < 0 { |
| 2304 | return void_value() |
| 2305 | } |
| 2306 | node := e.node(id) |
| 2307 | match node.kind { |
| 2308 | .empty { |
| 2309 | return void_value() |
| 2310 | } |
| 2311 | .int_literal { |
| 2312 | return Value(strconv.parse_int(clean_number_literal(node.value), 0, 64) or { i64(0) }) |
| 2313 | } |
| 2314 | .float_literal { |
| 2315 | return Value(strconv.atof64(clean_number_literal(node.value)) or { 0.0 }) |
| 2316 | } |
| 2317 | .bool_literal { |
| 2318 | return Value(node.value == 'true') |
| 2319 | } |
| 2320 | .char_literal { |
| 2321 | return Value(e.char_literal_value(node.value)) |
| 2322 | } |
| 2323 | .string_literal { |
| 2324 | return Value(node.value) |
| 2325 | } |
| 2326 | .string_interp { |
| 2327 | return flow_value(e.eval_string_interp_flow(node)!) |
| 2328 | } |
| 2329 | .ident { |
| 2330 | return e.eval_ident(node.value) |
| 2331 | } |
| 2332 | .enum_val { |
| 2333 | return e.lookup_const(e.current_module_name(), node.value) or { Value(i64(0)) } |
| 2334 | } |
| 2335 | .paren { |
| 2336 | return e.eval_expr(e.child(node, 0)) |
| 2337 | } |
| 2338 | .prefix { |
| 2339 | return e.eval_prefix(node) |
| 2340 | } |
| 2341 | .postfix { |
| 2342 | return e.eval_postfix(node) |
| 2343 | } |
| 2344 | .infix { |
| 2345 | if node.op == .logical_and { |
| 2346 | left := e.eval_expr(e.child(node, 0))! |
| 2347 | if !e.value_as_bool(left)! { |
| 2348 | return Value(false) |
| 2349 | } |
| 2350 | return Value(e.value_as_bool(e.eval_expr(e.child(node, 1))!)!) |
| 2351 | } |
| 2352 | if node.op == .logical_or { |
| 2353 | left := e.eval_expr(e.child(node, 0))! |
| 2354 | if e.value_as_bool(left)! { |
| 2355 | return Value(true) |
| 2356 | } |
| 2357 | return Value(e.value_as_bool(e.eval_expr(e.child(node, 1))!)!) |
| 2358 | } |
| 2359 | return e.apply_infix(node.op, e.eval_expr(e.child(node, 0))!, e.eval_expr(e.child(node, |
| 2360 | 1))!) |
| 2361 | } |
| 2362 | .call { |
| 2363 | return flow_value(e.eval_call_flow(id, node)!) |
| 2364 | } |
| 2365 | .selector { |
| 2366 | return e.eval_selector(node) |
| 2367 | } |
| 2368 | .index { |
| 2369 | return e.eval_index(node) |
| 2370 | } |
| 2371 | .if_expr { |
| 2372 | return e.eval_if_value(node) |
| 2373 | } |
| 2374 | .block { |
| 2375 | return e.eval_block_value(node) |
| 2376 | } |
| 2377 | .array_literal { |
| 2378 | mut values := []Value{} |
| 2379 | elem_type_name := e.array_literal_elem_type_name(node) |
| 2380 | for child_id in e.children(node) { |
| 2381 | values << e.adapt_value_to_type_name(e.eval_expr_expected(child_id, elem_type_name)!, |
| 2382 | elem_type_name) |
| 2383 | } |
| 2384 | return ArrayValue{ |
| 2385 | elem_type_name: elem_type_name |
| 2386 | values: values |
| 2387 | } |
| 2388 | } |
| 2389 | .array_init { |
| 2390 | return e.eval_array_init(node) |
| 2391 | } |
| 2392 | .map_init { |
| 2393 | return flow_value(e.eval_map_init_flow(node)!) |
| 2394 | } |
| 2395 | .struct_init { |
| 2396 | return e.eval_struct_init(node) |
| 2397 | } |
| 2398 | .assoc { |
| 2399 | return e.eval_assoc(node) |
| 2400 | } |
| 2401 | .range { |
| 2402 | return RangeValue{ |
| 2403 | start: e.value_as_int(e.eval_expr(e.child(node, 0))!)! |
| 2404 | end: e.value_as_int(e.eval_expr(e.child(node, 1))!)! |
| 2405 | } |
| 2406 | } |
| 2407 | .cast_expr, .as_expr { |
| 2408 | value := e.eval_expr(e.child(node, 0))! |
| 2409 | return e.cast_value(value, node.value) |
| 2410 | } |
| 2411 | .is_expr { |
| 2412 | value := e.eval_expr(e.child(node, 0))! |
| 2413 | return Value(e.value_matches_type_name(value, node.value)) |
| 2414 | } |
| 2415 | .in_expr { |
| 2416 | left := e.eval_expr(e.child(node, 0))! |
| 2417 | right := e.eval_expr(e.child(node, 1))! |
| 2418 | return Value(e.value_in(left, right)) |
| 2419 | } |
| 2420 | .or_expr { |
| 2421 | return e.eval_or_expr(node) |
| 2422 | } |
| 2423 | .match_stmt { |
| 2424 | return e.eval_match_value(node) |
| 2425 | } |
| 2426 | .none_expr, .nil_literal { |
| 2427 | return void_value() |
| 2428 | } |
| 2429 | .sizeof_expr { |
| 2430 | return Value(e.sizeof_type_name(node.value)) |
| 2431 | } |
| 2432 | .typeof_expr { |
| 2433 | value := e.eval_expr(e.child(node, 0))! |
| 2434 | return TypeValue{ |
| 2435 | name: e.runtime_type_name(value) |
| 2436 | } |
| 2437 | } |
| 2438 | .fn_literal { |
| 2439 | return Value(e.eval_fn_literal(id, node)!) |
| 2440 | } |
| 2441 | else { |
| 2442 | return error('v3.eval: unsupported expression `${node.kind}`') |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | fn (mut e Eval) eval_fn_literal(id flat.NodeId, node &flat.Node) !FnValue { |
| 2448 | mut captures := map[string]Value{} |
| 2449 | mut capture_types := map[string]string{} |
| 2450 | for child_id in e.children(node) { |
| 2451 | child := e.node(child_id) |
| 2452 | if child.kind != .ident { |
| 2453 | break |
| 2454 | } |
| 2455 | found := e.lookup_var(child.value) |
| 2456 | if !found.found { |
| 2457 | return error('v3.eval: unknown captured variable `${child.value}`') |
| 2458 | } |
| 2459 | captures[child.value] = found.value |
| 2460 | if typ := e.lookup_var_type(child.value) { |
| 2461 | capture_types[child.value] = typ |
| 2462 | } |
| 2463 | } |
| 2464 | return FnValue{ |
| 2465 | node: id |
| 2466 | module_name: e.current_module_name() |
| 2467 | file_name: e.current_file_name() |
| 2468 | captures: captures |
| 2469 | capture_types: capture_types |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | fn (mut e Eval) eval_expr_flow(id flat.NodeId) !FlowSignal { |
| 2474 | if int(id) < 0 { |
| 2475 | return FlowSignal{ |
| 2476 | values: [void_value()] |
| 2477 | } |
| 2478 | } |
| 2479 | node := e.node(id) |
| 2480 | match node.kind { |
| 2481 | .or_expr { |
| 2482 | return e.eval_or_expr_flow(node) |
| 2483 | } |
| 2484 | .call { |
| 2485 | return e.eval_call_flow(id, node) |
| 2486 | } |
| 2487 | .index { |
| 2488 | return e.eval_index_flow(node) |
| 2489 | } |
| 2490 | .if_expr { |
| 2491 | return e.eval_if_value_flow(node) |
| 2492 | } |
| 2493 | .match_stmt { |
| 2494 | return e.eval_match_value_flow(node) |
| 2495 | } |
| 2496 | .selector { |
| 2497 | left_signal := e.eval_expr_flow(e.child(node, 0))! |
| 2498 | if left_signal.kind != .normal { |
| 2499 | return left_signal |
| 2500 | } |
| 2501 | return value_flow(e.eval_selector_value(flow_value(left_signal), node.value)!) |
| 2502 | } |
| 2503 | .paren { |
| 2504 | return e.eval_expr_flow(e.child(node, 0)) |
| 2505 | } |
| 2506 | .prefix { |
| 2507 | value_signal := e.eval_expr_flow(e.child(node, 0))! |
| 2508 | if value_signal.kind != .normal { |
| 2509 | return value_signal |
| 2510 | } |
| 2511 | return value_flow(e.apply_prefix(node.op, flow_value(value_signal))!) |
| 2512 | } |
| 2513 | .postfix { |
| 2514 | return e.eval_postfix_flow(node) |
| 2515 | } |
| 2516 | .infix { |
| 2517 | return e.eval_infix_flow(node) |
| 2518 | } |
| 2519 | .range { |
| 2520 | start_signal := e.eval_expr_flow(e.child(node, 0))! |
| 2521 | if start_signal.kind != .normal { |
| 2522 | return start_signal |
| 2523 | } |
| 2524 | end_signal := e.eval_expr_flow(e.child(node, 1))! |
| 2525 | if end_signal.kind != .normal { |
| 2526 | return end_signal |
| 2527 | } |
| 2528 | return value_flow(RangeValue{ |
| 2529 | start: e.value_as_int(flow_value(start_signal))! |
| 2530 | end: e.value_as_int(flow_value(end_signal))! |
| 2531 | }) |
| 2532 | } |
| 2533 | .cast_expr, .as_expr { |
| 2534 | value_signal := e.eval_expr_flow(e.child(node, 0))! |
| 2535 | if value_signal.kind != .normal { |
| 2536 | return value_signal |
| 2537 | } |
| 2538 | return value_flow(e.cast_value(flow_value(value_signal), node.value)!) |
| 2539 | } |
| 2540 | .is_expr { |
| 2541 | value_signal := e.eval_expr_flow(e.child(node, 0))! |
| 2542 | if value_signal.kind != .normal { |
| 2543 | return value_signal |
| 2544 | } |
| 2545 | return value_flow(Value(e.value_matches_type_name(flow_value(value_signal), node.value))) |
| 2546 | } |
| 2547 | .in_expr { |
| 2548 | left_signal := e.eval_expr_flow(e.child(node, 0))! |
| 2549 | if left_signal.kind != .normal { |
| 2550 | return left_signal |
| 2551 | } |
| 2552 | right_signal := e.eval_expr_flow(e.child(node, 1))! |
| 2553 | if right_signal.kind != .normal { |
| 2554 | return right_signal |
| 2555 | } |
| 2556 | return value_flow(Value(e.value_in(flow_value(left_signal), flow_value(right_signal)))) |
| 2557 | } |
| 2558 | .array_literal { |
| 2559 | mut values := []Value{} |
| 2560 | elem_type_name := e.array_literal_elem_type_name(node) |
| 2561 | for child_id in e.children(node) { |
| 2562 | child_signal := e.eval_expr_flow_expected(child_id, elem_type_name)! |
| 2563 | if child_signal.kind != .normal { |
| 2564 | return child_signal |
| 2565 | } |
| 2566 | values << e.adapt_value_to_type_name(flow_value(child_signal), elem_type_name) |
| 2567 | } |
| 2568 | return value_flow(ArrayValue{ |
| 2569 | elem_type_name: elem_type_name |
| 2570 | values: values |
| 2571 | }) |
| 2572 | } |
| 2573 | .array_init { |
| 2574 | return e.eval_array_init_flow(node) |
| 2575 | } |
| 2576 | .struct_init { |
| 2577 | return e.eval_struct_init_flow(node) |
| 2578 | } |
| 2579 | .map_init { |
| 2580 | return e.eval_map_init_flow(node) |
| 2581 | } |
| 2582 | .assoc { |
| 2583 | return e.eval_assoc_flow(node) |
| 2584 | } |
| 2585 | .string_interp { |
| 2586 | return e.eval_string_interp_flow(node) |
| 2587 | } |
| 2588 | else { |
| 2589 | return FlowSignal{ |
| 2590 | values: [e.eval_expr(id)!] |
| 2591 | } |
| 2592 | } |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | fn (mut e Eval) eval_expr_expected(id flat.NodeId, expected_type string) !Value { |
| 2597 | signal := e.eval_expr_flow_expected(id, expected_type)! |
| 2598 | if signal.kind != .normal { |
| 2599 | return flow_value(signal) |
| 2600 | } |
| 2601 | return flow_value(signal) |
| 2602 | } |
| 2603 | |
| 2604 | fn (mut e Eval) eval_expr_flow_expected(id flat.NodeId, expected_type string) !FlowSignal { |
| 2605 | if int(id) >= 0 { |
| 2606 | node := e.node(id) |
| 2607 | if node.kind == .enum_val && expected_type.len > 0 { |
| 2608 | if value := e.lookup_enum_value(expected_type, node.value) { |
| 2609 | return value_flow(value) |
| 2610 | } |
| 2611 | } |
| 2612 | if expected_type.len > 0 { |
| 2613 | match node.kind { |
| 2614 | .if_expr { |
| 2615 | return e.eval_if_value_flow_expected(node, expected_type) |
| 2616 | } |
| 2617 | .match_stmt { |
| 2618 | return e.eval_match_value_flow_expected(node, expected_type) |
| 2619 | } |
| 2620 | .block { |
| 2621 | return e.eval_block_value_flow_expected(node, expected_type) |
| 2622 | } |
| 2623 | .paren { |
| 2624 | return e.eval_expr_flow_expected(e.child(node, 0), expected_type) |
| 2625 | } |
| 2626 | else {} |
| 2627 | } |
| 2628 | } |
| 2629 | } |
| 2630 | return e.eval_expr_flow(id) |
| 2631 | } |
| 2632 | |
| 2633 | fn (e &Eval) current_return_type() string { |
| 2634 | if e.call_stack.len == 0 { |
| 2635 | return '' |
| 2636 | } |
| 2637 | return e.call_stack[e.call_stack.len - 1].return_type |
| 2638 | } |
| 2639 | |
| 2640 | fn return_child_expected_type(return_type string, return_types []string, child_index int, child_count int) string { |
| 2641 | if return_types.len > 0 { |
| 2642 | if child_count == return_types.len && child_index < return_types.len { |
| 2643 | return return_types[child_index] |
| 2644 | } |
| 2645 | return '' |
| 2646 | } |
| 2647 | if child_count == 1 { |
| 2648 | return return_type |
| 2649 | } |
| 2650 | return '' |
| 2651 | } |
| 2652 | |
| 2653 | fn (mut e Eval) eval_infix_flow(node &flat.Node) !FlowSignal { |
| 2654 | left_signal := e.eval_expr_flow(e.child(node, 0))! |
| 2655 | if left_signal.kind != .normal { |
| 2656 | return left_signal |
| 2657 | } |
| 2658 | left := flow_value(left_signal) |
| 2659 | if node.op == .logical_and { |
| 2660 | if !e.value_as_bool(left)! { |
| 2661 | return value_flow(Value(false)) |
| 2662 | } |
| 2663 | mut smartcast_scope := false |
| 2664 | if binding := e.smartcast_binding_from_condition(e.child(node, 0)) { |
| 2665 | e.open_scope() |
| 2666 | e.declare_var_typed(binding.name, binding.value, binding.type_name) |
| 2667 | smartcast_scope = true |
| 2668 | } |
| 2669 | right_signal := e.eval_expr_flow(e.child(node, 1))! |
| 2670 | if smartcast_scope { |
| 2671 | e.close_scope()! |
| 2672 | } |
| 2673 | if right_signal.kind != .normal { |
| 2674 | return right_signal |
| 2675 | } |
| 2676 | return value_flow(Value(e.value_as_bool(flow_value(right_signal))!)) |
| 2677 | } |
| 2678 | if node.op == .logical_or { |
| 2679 | if e.value_as_bool(left)! { |
| 2680 | return value_flow(Value(true)) |
| 2681 | } |
| 2682 | right_signal := e.eval_expr_flow(e.child(node, 1))! |
| 2683 | if right_signal.kind != .normal { |
| 2684 | return right_signal |
| 2685 | } |
| 2686 | return value_flow(Value(e.value_as_bool(flow_value(right_signal))!)) |
| 2687 | } |
| 2688 | right_signal := e.eval_expr_flow(e.child(node, 1))! |
| 2689 | if right_signal.kind != .normal { |
| 2690 | return right_signal |
| 2691 | } |
| 2692 | return value_flow(e.apply_infix(node.op, left, flow_value(right_signal))!) |
| 2693 | } |
| 2694 | |
| 2695 | fn clean_number_literal(value string) string { |
| 2696 | return value.replace('_', '') |
| 2697 | } |
| 2698 | |
| 2699 | fn (e &Eval) char_literal_value(raw string) i64 { |
| 2700 | mut s := raw |
| 2701 | if s.len >= 2 && ((s[0] == `'` && s[s.len - 1] == `'`) |
| 2702 | || (s[0] == `\`` && s[s.len - 1] == `\``)) { |
| 2703 | s = s[1..s.len - 1] |
| 2704 | } |
| 2705 | if s.len == 0 { |
| 2706 | return 0 |
| 2707 | } |
| 2708 | if s.len >= 2 && s[0] == `\\` { |
| 2709 | return match s[1] { |
| 2710 | `n` { i64(`\n`) } |
| 2711 | `t` { i64(`\t`) } |
| 2712 | `r` { i64(`\r`) } |
| 2713 | `\\` { i64(`\\`) } |
| 2714 | `'` { i64(`'`) } |
| 2715 | `"` { i64(`"`) } |
| 2716 | `$` { i64(`$`) } |
| 2717 | `0` { i64(0) } |
| 2718 | `a` { i64(7) } |
| 2719 | `b` { i64(8) } |
| 2720 | `f` { i64(12) } |
| 2721 | `v` { i64(11) } |
| 2722 | else { i64(s[0]) } |
| 2723 | } |
| 2724 | } |
| 2725 | return i64(s[0]) |
| 2726 | } |
| 2727 | |
| 2728 | fn (mut e Eval) eval_ident(name string) !Value { |
| 2729 | if name == '_' { |
| 2730 | return void_value() |
| 2731 | } |
| 2732 | found := e.lookup_var(name) |
| 2733 | if found.found { |
| 2734 | return found.value |
| 2735 | } |
| 2736 | if value := e.lookup_const(e.current_module_name(), name) { |
| 2737 | return value |
| 2738 | } |
| 2739 | mod_name := e.resolve_module_name(name) |
| 2740 | if mod_name in e.modules || name in ['os', 'time', 'strings'] { |
| 2741 | return ModuleValue{ |
| 2742 | name: mod_name |
| 2743 | } |
| 2744 | } |
| 2745 | if e.has_type_name(e.current_module_name(), name) { |
| 2746 | return TypeValue{ |
| 2747 | name: e.qualify_type_name(e.current_module_name(), name) |
| 2748 | } |
| 2749 | } |
| 2750 | return e.unknown_variable_error(name) |
| 2751 | } |
| 2752 | |
| 2753 | fn (e &Eval) resolve_module_name(name string) string { |
| 2754 | file_name := e.current_file_name() |
| 2755 | if file_name in e.file_import_alias { |
| 2756 | aliases := e.file_import_alias[file_name].clone() |
| 2757 | if name in aliases { |
| 2758 | return aliases[name] |
| 2759 | } |
| 2760 | } |
| 2761 | return name |
| 2762 | } |
| 2763 | |
| 2764 | fn (e &Eval) has_type_name(module_name string, name string) bool { |
| 2765 | if module_name in e.type_names && name in e.type_names[module_name] { |
| 2766 | return true |
| 2767 | } |
| 2768 | if name.contains('.') { |
| 2769 | mod := name.all_before_last('.') |
| 2770 | short := name.all_after_last('.') |
| 2771 | return mod in e.type_names && short in e.type_names[mod] |
| 2772 | } |
| 2773 | return name in e.structs || name in e.sum_types |
| 2774 | } |
| 2775 | |
| 2776 | fn (mut e Eval) lookup_const(module_name string, name string) ?Value { |
| 2777 | if module_name in e.consts && name in e.consts[module_name] { |
| 2778 | mut entry := e.consts[module_name][name] |
| 2779 | if entry.cached { |
| 2780 | return entry.value |
| 2781 | } |
| 2782 | if entry.evaluating { |
| 2783 | return none |
| 2784 | } |
| 2785 | entry.evaluating = true |
| 2786 | e.consts[module_name][name] = entry |
| 2787 | mut value := Value(void_value()) |
| 2788 | if e.node(entry.node).children_count > 0 { |
| 2789 | e.call_stack << CallFrame{ |
| 2790 | module_name: entry.module_name |
| 2791 | file_name: entry.file_name |
| 2792 | fn_name: '<const ${name}>' |
| 2793 | } |
| 2794 | value = e.eval_expr(e.child(e.node(entry.node), 0)) or { |
| 2795 | e.call_stack.delete(e.call_stack.len - 1) |
| 2796 | entry.evaluating = false |
| 2797 | e.consts[module_name][name] = entry |
| 2798 | return none |
| 2799 | } |
| 2800 | e.call_stack.delete(e.call_stack.len - 1) |
| 2801 | } |
| 2802 | entry.cached = true |
| 2803 | entry.evaluating = false |
| 2804 | entry.value = value |
| 2805 | e.consts[module_name][name] = entry |
| 2806 | return value |
| 2807 | } |
| 2808 | for mod_name in e.consts.keys() { |
| 2809 | if name in e.consts[mod_name] { |
| 2810 | return e.lookup_const(mod_name, name) |
| 2811 | } |
| 2812 | } |
| 2813 | return none |
| 2814 | } |
| 2815 | |
| 2816 | fn (mut e Eval) eval_call_flow(id flat.NodeId, node &flat.Node) !FlowSignal { |
| 2817 | if node.children_count == 0 { |
| 2818 | return FlowSignal{ |
| 2819 | values: [void_value()] |
| 2820 | } |
| 2821 | } |
| 2822 | callee_id := e.child(node, 0) |
| 2823 | callee := e.node(callee_id) |
| 2824 | if value := e.disabled_call_value(node, callee) { |
| 2825 | return FlowSignal{ |
| 2826 | values: [value] |
| 2827 | } |
| 2828 | } |
| 2829 | if callee.kind == .ident { |
| 2830 | fn_name := callee.value |
| 2831 | found := e.lookup_var(fn_name) |
| 2832 | if found.found && found.value is FnValue { |
| 2833 | fv := found.value as FnValue |
| 2834 | args_signal := e.eval_call_arg_values(node, e.fn_value_param_type_names(fv))! |
| 2835 | if args_signal.kind != .normal { |
| 2836 | return args_signal |
| 2837 | } |
| 2838 | result := e.call_fn_value(fv, args_signal.values)! |
| 2839 | e.write_back_fn_value(callee_id, result)! |
| 2840 | e.write_back_mutated_arg_values(node, result.mutated_args, args_signal.mut_lvalues)! |
| 2841 | return FlowSignal{ |
| 2842 | values: [e.call_result_value(result)] |
| 2843 | } |
| 2844 | } |
| 2845 | expected_types := if target := e.function_def(e.current_module_name(), fn_name) { |
| 2846 | e.function_param_type_names(target, 0) |
| 2847 | } else { |
| 2848 | []string{} |
| 2849 | } |
| 2850 | args_signal := e.eval_call_arg_values(node, expected_types)! |
| 2851 | if args_signal.kind != .normal { |
| 2852 | return args_signal |
| 2853 | } |
| 2854 | result := e.call_function(e.current_module_name(), fn_name, args_signal.values)! |
| 2855 | e.write_back_mutated_args(node, result, args_signal.mut_lvalues)! |
| 2856 | return FlowSignal{ |
| 2857 | values: [e.call_result_value(result)] |
| 2858 | } |
| 2859 | } |
| 2860 | if callee.kind == .selector { |
| 2861 | receiver_id := e.child(callee, 0) |
| 2862 | receiver_resolved := e.resolve_lvalue_flow(receiver_id)! |
| 2863 | if receiver_resolved.signal.kind != .normal { |
| 2864 | return receiver_resolved.signal |
| 2865 | } |
| 2866 | left := receiver_resolved.value |
| 2867 | if left is ModuleValue { |
| 2868 | expected_types := if target := e.function_def(left.name, callee.value) { |
| 2869 | e.function_param_type_names(target, 0) |
| 2870 | } else { |
| 2871 | []string{} |
| 2872 | } |
| 2873 | args_signal := e.eval_call_arg_values(node, expected_types)! |
| 2874 | if args_signal.kind != .normal { |
| 2875 | return args_signal |
| 2876 | } |
| 2877 | result := e.call_function(left.name, callee.value, args_signal.values)! |
| 2878 | e.write_back_mutated_args(node, result, args_signal.mut_lvalues)! |
| 2879 | return FlowSignal{ |
| 2880 | values: [e.call_result_value(result)] |
| 2881 | } |
| 2882 | } |
| 2883 | if left is TypeValue { |
| 2884 | static_name := '${left.name.all_after_last('.')}.${callee.value}' |
| 2885 | static_module := e.type_value_module_name(left) |
| 2886 | expected_types := if target := e.function_def(static_module, static_name) { |
| 2887 | e.function_param_type_names(target, 0) |
| 2888 | } else { |
| 2889 | []string{} |
| 2890 | } |
| 2891 | args_signal := e.eval_call_arg_values(node, expected_types)! |
| 2892 | if args_signal.kind != .normal { |
| 2893 | return args_signal |
| 2894 | } |
| 2895 | result := e.call_function(static_module, static_name, args_signal.values)! |
| 2896 | e.write_back_mutated_args(node, result, args_signal.mut_lvalues)! |
| 2897 | return FlowSignal{ |
| 2898 | values: [e.call_result_value(result)] |
| 2899 | } |
| 2900 | } |
| 2901 | selector_value := e.eval_selector_value(left, callee.value) or { void_value() } |
| 2902 | if selector_value is FnValue { |
| 2903 | args_signal := |
| 2904 | e.eval_call_arg_values(node, e.fn_value_param_type_names(selector_value))! |
| 2905 | if args_signal.kind != .normal { |
| 2906 | return args_signal |
| 2907 | } |
| 2908 | result := e.call_fn_value(selector_value, args_signal.values)! |
| 2909 | e.write_back_fn_value(callee_id, result)! |
| 2910 | e.write_back_mutated_arg_values(node, result.mutated_args, args_signal.mut_lvalues)! |
| 2911 | return FlowSignal{ |
| 2912 | values: [e.call_result_value(result)] |
| 2913 | } |
| 2914 | } |
| 2915 | receiver_type_name := e.infer_expr_type_name(receiver_id) |
| 2916 | expected_types := e.method_call_param_type_names(left, receiver_type_name, callee.value) |
| 2917 | args_signal := e.eval_call_arg_values(node, expected_types)! |
| 2918 | if args_signal.kind != .normal { |
| 2919 | return args_signal |
| 2920 | } |
| 2921 | result := e.call_value_method(left, receiver_type_name, callee.value, args_signal.values)! |
| 2922 | if result.receiver_changed { |
| 2923 | if e.is_assignable_target(receiver_id) { |
| 2924 | write_signal := e.write_resolved_lvalue_flow(receiver_resolved, result.receiver)! |
| 2925 | if write_signal.kind != .normal { |
| 2926 | return write_signal |
| 2927 | } |
| 2928 | } |
| 2929 | } |
| 2930 | e.write_back_mutated_arg_values(node, result.mutated_args, args_signal.mut_lvalues)! |
| 2931 | return FlowSignal{ |
| 2932 | values: [result.value] |
| 2933 | } |
| 2934 | } |
| 2935 | if callee.kind == .fn_literal { |
| 2936 | fv := e.eval_fn_literal(callee_id, callee)! |
| 2937 | args_signal := e.eval_call_arg_values(node, e.fn_literal_param_type_names(callee))! |
| 2938 | if args_signal.kind != .normal { |
| 2939 | return args_signal |
| 2940 | } |
| 2941 | result := e.call_fn_value(fv, args_signal.values)! |
| 2942 | e.write_back_mutated_arg_values(node, result.mutated_args, args_signal.mut_lvalues)! |
| 2943 | return FlowSignal{ |
| 2944 | values: [e.call_result_value(result)] |
| 2945 | } |
| 2946 | } |
| 2947 | value := e.eval_expr(callee_id)! |
| 2948 | if value is FnValue { |
| 2949 | args_signal := e.eval_call_arg_values(node, e.fn_value_param_type_names(value))! |
| 2950 | if args_signal.kind != .normal { |
| 2951 | return args_signal |
| 2952 | } |
| 2953 | result := e.call_fn_value(value, args_signal.values)! |
| 2954 | e.write_back_fn_value(callee_id, result)! |
| 2955 | e.write_back_mutated_arg_values(node, result.mutated_args, args_signal.mut_lvalues)! |
| 2956 | return FlowSignal{ |
| 2957 | values: [e.call_result_value(result)] |
| 2958 | } |
| 2959 | } |
| 2960 | _ = id |
| 2961 | return error('v3.eval: unsupported call target `${callee.kind}`') |
| 2962 | } |
| 2963 | |
| 2964 | fn (mut e Eval) disabled_call_value(call_node &flat.Node, callee &flat.Node) ?Value { |
| 2965 | match callee.kind { |
| 2966 | .ident { |
| 2967 | found := e.lookup_var(callee.value) |
| 2968 | if found.found && found.value is FnValue { |
| 2969 | return none |
| 2970 | } |
| 2971 | if value := e.disabled_function_zero_value(e.current_module_name(), callee.value, |
| 2972 | call_node) |
| 2973 | { |
| 2974 | return value |
| 2975 | } |
| 2976 | } |
| 2977 | .selector { |
| 2978 | if callee.children_count == 0 { |
| 2979 | return none |
| 2980 | } |
| 2981 | base_id := e.child(callee, 0) |
| 2982 | base := e.node(base_id) |
| 2983 | receiver_type := e.infer_expr_type_name(base_id) |
| 2984 | if receiver_type.len > 0 { |
| 2985 | if target := e.resolve_method_target(receiver_type, callee.value) { |
| 2986 | if e.is_disabled_fn_name_in_module(target.module_name, target.name) { |
| 2987 | return e.zero_value_for_disabled_function(call_node, target) |
| 2988 | } |
| 2989 | } |
| 2990 | } |
| 2991 | if base.kind == .ident { |
| 2992 | full_name := '${base.value}.${callee.value}' |
| 2993 | if value := e.disabled_function_zero_value(e.current_module_name(), full_name, |
| 2994 | call_node) |
| 2995 | { |
| 2996 | return value |
| 2997 | } |
| 2998 | found := e.lookup_var(base.value) |
| 2999 | if !found.found { |
| 3000 | module_name := e.resolve_module_name(base.value) |
| 3001 | if value := e.disabled_function_zero_value(module_name, callee.value, call_node) { |
| 3002 | return value |
| 3003 | } |
| 3004 | } |
| 3005 | } |
| 3006 | } |
| 3007 | else {} |
| 3008 | } |
| 3009 | |
| 3010 | return none |
| 3011 | } |
| 3012 | |
| 3013 | fn (e &Eval) function_def(module_name string, fn_name string) ?FunctionDef { |
| 3014 | target_module := if module_name == '' { e.current_module_name() } else { module_name } |
| 3015 | if target_module !in e.functions { |
| 3016 | return none |
| 3017 | } |
| 3018 | if fn_name in e.functions[target_module] { |
| 3019 | return e.functions[target_module][fn_name] |
| 3020 | } |
| 3021 | if target_module !in ['main', 'builtin'] && !fn_name.contains('.') { |
| 3022 | qualified := '${target_module}.${fn_name}' |
| 3023 | if qualified in e.functions[target_module] { |
| 3024 | return e.functions[target_module][qualified] |
| 3025 | } |
| 3026 | } |
| 3027 | return none |
| 3028 | } |
| 3029 | |
| 3030 | fn (e &Eval) function_param_type_names(def FunctionDef, skip int) []string { |
| 3031 | node := e.node(def.node) |
| 3032 | mut types := []string{} |
| 3033 | mut param_index := 0 |
| 3034 | for child_id in e.children(node) { |
| 3035 | child := e.node(child_id) |
| 3036 | if child.kind != .param { |
| 3037 | break |
| 3038 | } |
| 3039 | if param_index >= skip { |
| 3040 | types << e.qualify_expected_type_name(def.module_name, child.typ) |
| 3041 | } |
| 3042 | param_index++ |
| 3043 | } |
| 3044 | return types |
| 3045 | } |
| 3046 | |
| 3047 | fn (e &Eval) qualify_expected_type_name(module_name string, type_name string) string { |
| 3048 | name := type_name.trim_space() |
| 3049 | if name == '' { |
| 3050 | return '' |
| 3051 | } |
| 3052 | if name.starts_with('&') { |
| 3053 | return '&${e.qualify_expected_type_name(module_name, name[1..])}' |
| 3054 | } |
| 3055 | if name.starts_with('...') { |
| 3056 | return '...${e.qualify_expected_type_name(module_name, name[3..])}' |
| 3057 | } |
| 3058 | if name.starts_with('mut ') { |
| 3059 | return '&${e.qualify_expected_type_name(module_name, name[4..])}' |
| 3060 | } |
| 3061 | return e.qualify_nested_type_name(module_name, name) |
| 3062 | } |
| 3063 | |
| 3064 | fn (e &Eval) fn_value_param_type_names(fv FnValue) []string { |
| 3065 | return e.fn_literal_param_type_names_in_module(e.node(fv.node), fv.module_name) |
| 3066 | } |
| 3067 | |
| 3068 | fn (e &Eval) fn_literal_param_type_names(node &flat.Node) []string { |
| 3069 | return e.fn_literal_param_type_names_in_module(node, e.current_module_name()) |
| 3070 | } |
| 3071 | |
| 3072 | fn (e &Eval) fn_literal_param_type_names_in_module(node &flat.Node, module_name string) []string { |
| 3073 | children := e.children(node) |
| 3074 | mut i := 0 |
| 3075 | for i < children.len { |
| 3076 | child := e.node(children[i]) |
| 3077 | if child.kind != .ident { |
| 3078 | break |
| 3079 | } |
| 3080 | i++ |
| 3081 | } |
| 3082 | mut types := []string{} |
| 3083 | for i < children.len { |
| 3084 | child := e.node(children[i]) |
| 3085 | if child.kind != .param { |
| 3086 | break |
| 3087 | } |
| 3088 | types << e.qualify_expected_type_name(module_name, child.typ) |
| 3089 | i++ |
| 3090 | } |
| 3091 | return types |
| 3092 | } |
| 3093 | |
| 3094 | fn (e &Eval) method_call_param_type_names(receiver Value, receiver_type_name string, method_name string) []string { |
| 3095 | static_type_name := e.normalize_type_name(receiver_type_name) |
| 3096 | if static_type_name.len > 0 { |
| 3097 | if target := e.resolve_method_target(static_type_name, method_name) { |
| 3098 | return e.function_param_type_names(target, 1) |
| 3099 | } |
| 3100 | } |
| 3101 | if receiver is StructValue { |
| 3102 | if target := e.resolve_method_target(receiver.type_name, method_name) { |
| 3103 | return e.function_param_type_names(target, 1) |
| 3104 | } |
| 3105 | } |
| 3106 | if receiver is ArrayValue { |
| 3107 | return e.array_method_param_type_names(receiver, method_name) |
| 3108 | } |
| 3109 | if receiver is MapValue { |
| 3110 | return e.map_method_param_type_names(receiver, method_name) |
| 3111 | } |
| 3112 | if receiver is SumValue { |
| 3113 | return e.method_call_param_type_names(receiver.payload, receiver.variant_name, method_name) |
| 3114 | } |
| 3115 | return []string{} |
| 3116 | } |
| 3117 | |
| 3118 | fn (e &Eval) array_method_param_type_names(receiver ArrayValue, method_name string) []string { |
| 3119 | return match method_name { |
| 3120 | 'contains', 'index' { [receiver.elem_type_name] } |
| 3121 | else { []string{} } |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | fn (e &Eval) map_method_param_type_names(receiver MapValue, method_name string) []string { |
| 3126 | return match method_name { |
| 3127 | 'delete' { [receiver.key_type_name] } |
| 3128 | else { []string{} } |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | fn (mut e Eval) eval_call_arg_values(node &flat.Node, expected_types []string) !FlowSignal { |
| 3133 | mut args := []Value{} |
| 3134 | mut mut_lvalues := map[int]ResolvedLvalue{} |
| 3135 | for child_index in 1 .. node.children_count { |
| 3136 | arg_index := child_index - 1 |
| 3137 | expected_type := call_arg_expected_type(expected_types, arg_index) |
| 3138 | value_expected_type := call_arg_value_expected_type(expected_type) |
| 3139 | arg_id := e.child(node, child_index) |
| 3140 | if expected_type.starts_with('&') && e.is_assignable_target(arg_id) { |
| 3141 | resolved := e.resolve_lvalue_flow(arg_id)! |
| 3142 | if resolved.signal.kind != .normal { |
| 3143 | return resolved.signal |
| 3144 | } |
| 3145 | args << resolved.value |
| 3146 | mut_lvalues[arg_index] = resolved |
| 3147 | continue |
| 3148 | } |
| 3149 | signal := e.eval_expr_flow_expected(arg_id, value_expected_type)! |
| 3150 | if signal.kind != .normal { |
| 3151 | return signal |
| 3152 | } |
| 3153 | args << flow_value(signal) |
| 3154 | } |
| 3155 | return FlowSignal{ |
| 3156 | values: args |
| 3157 | mut_lvalues: mut_lvalues |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | fn call_arg_expected_type(expected_types []string, index int) string { |
| 3162 | if index < expected_types.len { |
| 3163 | typ := expected_types[index] |
| 3164 | if typ.starts_with('...') { |
| 3165 | return typ[3..] |
| 3166 | } |
| 3167 | return typ |
| 3168 | } |
| 3169 | if expected_types.len > 0 { |
| 3170 | last := expected_types[expected_types.len - 1] |
| 3171 | if last.starts_with('...') { |
| 3172 | return last[3..] |
| 3173 | } |
| 3174 | } |
| 3175 | return '' |
| 3176 | } |
| 3177 | |
| 3178 | fn call_arg_value_expected_type(type_name string) string { |
| 3179 | name := type_name.trim_space() |
| 3180 | if name.starts_with('&') { |
| 3181 | return call_arg_value_expected_type(name[1..]) |
| 3182 | } |
| 3183 | if name.starts_with('mut ') { |
| 3184 | return call_arg_value_expected_type(name[4..]) |
| 3185 | } |
| 3186 | if name.starts_with('...') { |
| 3187 | return call_arg_value_expected_type(name[3..]) |
| 3188 | } |
| 3189 | return name |
| 3190 | } |
| 3191 | |
| 3192 | fn (mut e Eval) disabled_function_zero_value(module_name string, fn_name string, call_node &flat.Node) ?Value { |
| 3193 | target_module := if module_name == '' { e.current_module_name() } else { module_name } |
| 3194 | mut candidates := []string{cap: 3} |
| 3195 | candidates << fn_name |
| 3196 | if target_module.len > 0 && target_module !in ['main', 'builtin'] && !fn_name.contains('.') { |
| 3197 | candidates << '${target_module}.${fn_name}' |
| 3198 | } |
| 3199 | if target_module in e.functions { |
| 3200 | for candidate in candidates { |
| 3201 | if candidate in e.functions[target_module] { |
| 3202 | target := e.functions[target_module][candidate] |
| 3203 | if e.is_disabled_fn_name_in_module(target.module_name, target.name) |
| 3204 | || e.is_disabled_fn_name_in_module(target_module, candidate) { |
| 3205 | return e.zero_value_for_disabled_function(call_node, target) |
| 3206 | } |
| 3207 | } |
| 3208 | } |
| 3209 | } |
| 3210 | for candidate in candidates { |
| 3211 | if e.is_disabled_fn_name_in_module(target_module, candidate) { |
| 3212 | return e.zero_value_for_disabled_return_type(call_node.typ, target_module) |
| 3213 | } |
| 3214 | } |
| 3215 | return none |
| 3216 | } |
| 3217 | |
| 3218 | fn (mut e Eval) zero_value_for_disabled_function(call_node &flat.Node, target FunctionDef) Value { |
| 3219 | target_node := e.node(target.node) |
| 3220 | return e.zero_value_for_disabled_return_type(if call_node.typ.len > 0 { |
| 3221 | call_node.typ |
| 3222 | } else { |
| 3223 | target_node.typ |
| 3224 | }, target.module_name) |
| 3225 | } |
| 3226 | |
| 3227 | fn (mut e Eval) zero_value_for_disabled_return_type(return_type string, module_name string) Value { |
| 3228 | if return_type.len == 0 || return_type == 'void' { |
| 3229 | return void_value() |
| 3230 | } |
| 3231 | return e.zero_value_for_type_name_in_module(return_type, module_name) |
| 3232 | } |
| 3233 | |
| 3234 | fn (e &Eval) is_disabled_fn_name_in_module(module_name string, fn_name string) bool { |
| 3235 | if fn_name in e.a.disabled_fns { |
| 3236 | return true |
| 3237 | } |
| 3238 | if !fn_name.contains('.') && module_name.len > 0 && module_name !in ['main', 'builtin'] { |
| 3239 | return '${module_name}.${fn_name}' in e.a.disabled_fns |
| 3240 | } |
| 3241 | return false |
| 3242 | } |
| 3243 | |
| 3244 | fn (e &Eval) call_result_value(result CallResult) Value { |
| 3245 | if result.values.len == 1 { |
| 3246 | return result.values[0] |
| 3247 | } |
| 3248 | return TupleValue{ |
| 3249 | values: result.values |
| 3250 | } |
| 3251 | } |
| 3252 | |
| 3253 | fn (mut e Eval) write_back_mutated_args(node &flat.Node, result CallResult, mut_lvalues map[int]ResolvedLvalue) ! { |
| 3254 | e.write_back_mutated_arg_values(node, result.mutated_args, mut_lvalues)! |
| 3255 | } |
| 3256 | |
| 3257 | fn (mut e Eval) adapt_return_values(values []Value, return_type string) []Value { |
| 3258 | return_types := split_multi_return_types(return_type) |
| 3259 | if return_types.len > 0 { |
| 3260 | mut adapted := []Value{cap: values.len} |
| 3261 | for i, value in values { |
| 3262 | adapted << if i < return_types.len { |
| 3263 | e.adapt_value_to_type_name(value, return_types[i]) |
| 3264 | } else { |
| 3265 | value |
| 3266 | } |
| 3267 | } |
| 3268 | return adapted |
| 3269 | } |
| 3270 | if values.len == 1 && return_type.len > 0 { |
| 3271 | return [e.adapt_value_to_type_name(values[0], return_type)] |
| 3272 | } |
| 3273 | return values.clone() |
| 3274 | } |
| 3275 | |
| 3276 | fn split_multi_return_types(return_type string) []string { |
| 3277 | if !return_type.starts_with('(') || !return_type.ends_with(')') { |
| 3278 | return []string{} |
| 3279 | } |
| 3280 | inner := return_type[1..return_type.len - 1] |
| 3281 | mut types := []string{} |
| 3282 | mut start := 0 |
| 3283 | mut paren_depth := 0 |
| 3284 | mut bracket_depth := 0 |
| 3285 | for i := 0; i < inner.len; i++ { |
| 3286 | ch := inner[i] |
| 3287 | match ch { |
| 3288 | `(` { |
| 3289 | paren_depth++ |
| 3290 | } |
| 3291 | `)` { |
| 3292 | if paren_depth > 0 { |
| 3293 | paren_depth-- |
| 3294 | } |
| 3295 | } |
| 3296 | `[` { |
| 3297 | bracket_depth++ |
| 3298 | } |
| 3299 | `]` { |
| 3300 | if bracket_depth > 0 { |
| 3301 | bracket_depth-- |
| 3302 | } |
| 3303 | } |
| 3304 | `,` { |
| 3305 | if paren_depth == 0 && bracket_depth == 0 { |
| 3306 | types << inner[start..i].trim_space() |
| 3307 | start = i + 1 |
| 3308 | } |
| 3309 | } |
| 3310 | else {} |
| 3311 | } |
| 3312 | } |
| 3313 | last := inner[start..].trim_space() |
| 3314 | if last.len > 0 { |
| 3315 | types << last |
| 3316 | } |
| 3317 | return if types.len > 1 { types } else { []string{} } |
| 3318 | } |
| 3319 | |
| 3320 | fn (e &Eval) minimum_arg_count(params []flat.NodeId) int { |
| 3321 | if params.len == 0 { |
| 3322 | return 0 |
| 3323 | } |
| 3324 | last_param := e.node(params[params.len - 1]) |
| 3325 | if last_param.typ.starts_with('...') { |
| 3326 | return params.len - 1 |
| 3327 | } |
| 3328 | return params.len |
| 3329 | } |
| 3330 | |
| 3331 | fn (mut e Eval) bind_call_params(params []flat.NodeId, args []Value) ! { |
| 3332 | for i, param_id in params { |
| 3333 | param := e.node(param_id) |
| 3334 | if param.typ.starts_with('...') { |
| 3335 | elem_type := param.typ[3..] |
| 3336 | mut values := []Value{cap: if args.len > i { args.len - i } else { 0 }} |
| 3337 | for j := i; j < args.len; j++ { |
| 3338 | values << e.adapt_value_to_type_name(args[j], elem_type) |
| 3339 | } |
| 3340 | if param.value.len > 0 { |
| 3341 | e.declare_var_typed(param.value, ArrayValue{ |
| 3342 | elem_type_name: elem_type |
| 3343 | values: values |
| 3344 | }, '[]${elem_type}') |
| 3345 | } |
| 3346 | return |
| 3347 | } |
| 3348 | if i < args.len { |
| 3349 | e.declare_var_typed(param.value, e.adapt_value_to_type_name(args[i], param.typ), |
| 3350 | param.typ) |
| 3351 | } |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | fn (mut e Eval) collect_mutated_param_args(params []flat.NodeId, args []Value) map[int]Value { |
| 3356 | mut mutated_args := map[int]Value{} |
| 3357 | for i, param_id in params { |
| 3358 | param := e.node(param_id) |
| 3359 | if param.value.len == 0 { |
| 3360 | continue |
| 3361 | } |
| 3362 | if param.typ.starts_with('...') { |
| 3363 | continue |
| 3364 | } |
| 3365 | found := e.lookup_var(param.value) |
| 3366 | if !found.found { |
| 3367 | continue |
| 3368 | } |
| 3369 | if param.typ.starts_with('&') { |
| 3370 | mutated_args[i] = found.value |
| 3371 | continue |
| 3372 | } |
| 3373 | if i < args.len && args[i] is FnValue && found.value is FnValue { |
| 3374 | fn_value := found.value as FnValue |
| 3375 | if fn_value.captures.len > 0 { |
| 3376 | mutated_args[i] = found.value |
| 3377 | } |
| 3378 | } |
| 3379 | } |
| 3380 | return mutated_args |
| 3381 | } |
| 3382 | |
| 3383 | fn (mut e Eval) write_back_fn_value(callee_id flat.NodeId, result CallResult) ! { |
| 3384 | if result.fn_value_changed && e.is_assignable_target(callee_id) { |
| 3385 | e.update_target(callee_id, Value(result.fn_value))! |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | fn (mut e Eval) write_back_mutated_arg_values(node &flat.Node, mutated_args map[int]Value, mut_lvalues map[int]ResolvedLvalue) ! { |
| 3390 | for child_index := 1; child_index < node.children_count; child_index++ { |
| 3391 | arg_index := child_index - 1 |
| 3392 | if arg_index !in mutated_args { |
| 3393 | continue |
| 3394 | } |
| 3395 | value := mutated_args[arg_index] or { continue } |
| 3396 | if resolved := mut_lvalues[arg_index] { |
| 3397 | signal := e.write_resolved_lvalue_flow(resolved, value)! |
| 3398 | if signal.kind != .normal { |
| 3399 | return error('v3.eval: unexpected `${signal.kind}` escaped mut argument writeback') |
| 3400 | } |
| 3401 | continue |
| 3402 | } |
| 3403 | arg_id := e.child(node, child_index) |
| 3404 | if e.is_assignable_target(arg_id) { |
| 3405 | e.update_target(arg_id, value)! |
| 3406 | } |
| 3407 | } |
| 3408 | } |
| 3409 | |
| 3410 | fn (e &Eval) is_assignable_target(id flat.NodeId) bool { |
| 3411 | if int(id) < 0 { |
| 3412 | return false |
| 3413 | } |
| 3414 | node := e.node(id) |
| 3415 | return node.kind in [.ident, .selector, .index] |
| 3416 | } |
| 3417 | |
| 3418 | fn (mut e Eval) call_fn_value(fv FnValue, args []Value) !CallResult { |
| 3419 | node := e.node(fv.node) |
| 3420 | mut params := []flat.NodeId{} |
| 3421 | mut body_start := 0 |
| 3422 | children := e.children(node) |
| 3423 | for body_start < children.len { |
| 3424 | child := e.node(children[body_start]) |
| 3425 | if child.kind != .ident { |
| 3426 | break |
| 3427 | } |
| 3428 | body_start++ |
| 3429 | } |
| 3430 | for body_start < children.len { |
| 3431 | child_id := children[body_start] |
| 3432 | child := e.node(child_id) |
| 3433 | if child.kind == .param { |
| 3434 | params << child_id |
| 3435 | body_start++ |
| 3436 | continue |
| 3437 | } |
| 3438 | break |
| 3439 | } |
| 3440 | e.open_scope() |
| 3441 | e.call_stack << CallFrame{ |
| 3442 | module_name: fv.module_name |
| 3443 | file_name: fv.file_name |
| 3444 | fn_name: '<anonymous>' |
| 3445 | return_type: node.typ |
| 3446 | scope_idx: e.scopes.len - 1 |
| 3447 | } |
| 3448 | for name, value in fv.captures { |
| 3449 | e.declare_var_typed(name, value, fv.capture_types[name] or { '' }) |
| 3450 | } |
| 3451 | e.bind_call_params(params, args)! |
| 3452 | signal := e.exec_stmts(children[body_start..])! |
| 3453 | e.run_deferred_stmts()! |
| 3454 | mutated_args := e.collect_mutated_param_args(params, args) |
| 3455 | mut updated_fn_value := fv |
| 3456 | if fv.captures.len > 0 { |
| 3457 | mut captures := fv.captures.clone() |
| 3458 | for name, _ in fv.captures { |
| 3459 | found := e.lookup_var(name) |
| 3460 | if found.found { |
| 3461 | captures[name] = found.value |
| 3462 | } |
| 3463 | } |
| 3464 | updated_fn_value = FnValue{ |
| 3465 | node: fv.node |
| 3466 | module_name: fv.module_name |
| 3467 | file_name: fv.file_name |
| 3468 | captures: captures |
| 3469 | capture_types: fv.capture_types.clone() |
| 3470 | } |
| 3471 | } |
| 3472 | adapted_values := if signal.kind == .return_ { |
| 3473 | e.adapt_return_values(signal.values, node.typ) |
| 3474 | } else { |
| 3475 | []Value{} |
| 3476 | } |
| 3477 | e.close_scope()! |
| 3478 | e.call_stack.delete(e.call_stack.len - 1) |
| 3479 | if signal.kind == .return_ { |
| 3480 | return CallResult{ |
| 3481 | values: adapted_values |
| 3482 | mutated_args: mutated_args |
| 3483 | fn_value_changed: fv.captures.len > 0 |
| 3484 | fn_value: updated_fn_value |
| 3485 | } |
| 3486 | } |
| 3487 | if signal.kind != .normal { |
| 3488 | return error('v3.eval: unexpected `${signal.kind}` escaped anonymous function') |
| 3489 | } |
| 3490 | return CallResult{ |
| 3491 | values: [void_value()] |
| 3492 | mutated_args: mutated_args |
| 3493 | fn_value_changed: fv.captures.len > 0 |
| 3494 | fn_value: updated_fn_value |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | fn (mut e Eval) eval_selector(node &flat.Node) !Value { |
| 3499 | left := e.eval_expr(e.child(node, 0))! |
| 3500 | return e.eval_selector_value(left, node.value) |
| 3501 | } |
| 3502 | |
| 3503 | fn (mut e Eval) eval_selector_value(left Value, field string) !Value { |
| 3504 | match left { |
| 3505 | ModuleValue { |
| 3506 | if value := e.lookup_const(left.name, field) { |
| 3507 | return value |
| 3508 | } |
| 3509 | return TypeValue{ |
| 3510 | name: '${left.name}.${field}' |
| 3511 | } |
| 3512 | } |
| 3513 | TypeValue { |
| 3514 | if field == 'name' { |
| 3515 | return Value(left.name) |
| 3516 | } |
| 3517 | mod := e.type_value_module_name(left) |
| 3518 | enum_key := '${left.name.all_after_last('.')}.${field}' |
| 3519 | if value := e.lookup_const(mod, enum_key) { |
| 3520 | return e.enum_value(left.name, value) |
| 3521 | } |
| 3522 | if value := e.lookup_const(mod, field) { |
| 3523 | return e.enum_value(left.name, value) |
| 3524 | } |
| 3525 | return TypeValue{ |
| 3526 | name: '${left.name}.${field}' |
| 3527 | } |
| 3528 | } |
| 3529 | ArrayValue { |
| 3530 | if field == 'len' { |
| 3531 | return Value(i64(left.values.len)) |
| 3532 | } |
| 3533 | if field == 'cap' { |
| 3534 | return Value(i64(left.values.cap)) |
| 3535 | } |
| 3536 | } |
| 3537 | MapValue { |
| 3538 | if field == 'len' { |
| 3539 | return Value(i64(left.entries.len)) |
| 3540 | } |
| 3541 | } |
| 3542 | string { |
| 3543 | if field == 'len' { |
| 3544 | return Value(i64(left.len)) |
| 3545 | } |
| 3546 | } |
| 3547 | StructValue { |
| 3548 | if field in left.fields { |
| 3549 | return left.fields[field] or { void_value() } |
| 3550 | } |
| 3551 | } |
| 3552 | SumValue { |
| 3553 | return e.eval_sum_selector(left, field) |
| 3554 | } |
| 3555 | else {} |
| 3556 | } |
| 3557 | |
| 3558 | return error('v3.eval: unsupported selector `${field}` on `${e.runtime_type_name(left)}`') |
| 3559 | } |
| 3560 | |
| 3561 | fn (e &Eval) eval_sum_selector(value SumValue, field string) !Value { |
| 3562 | if field == '_typ' { |
| 3563 | return Value(i64(e.sum_variant_index(value.type_name, value.variant_name))) |
| 3564 | } |
| 3565 | if value.payload is StructValue { |
| 3566 | payload := value.payload as StructValue |
| 3567 | if field in payload.fields { |
| 3568 | return payload.fields[field] or { void_value() } |
| 3569 | } |
| 3570 | } |
| 3571 | return error('v3.eval: unknown sum type field `${field}` on `${value.type_name}`') |
| 3572 | } |
| 3573 | |
| 3574 | fn (e &Eval) sum_variant_index(type_name string, variant_name string) int { |
| 3575 | if type_name in e.sum_types { |
| 3576 | variants := e.sum_types[type_name] |
| 3577 | sum_module := if type_name.contains('.') { |
| 3578 | type_name.all_before_last('.') |
| 3579 | } else { |
| 3580 | e.current_module_name() |
| 3581 | } |
| 3582 | expected_name := e.qualify_type_name(sum_module, variant_name) |
| 3583 | for i, variant in variants { |
| 3584 | if variant == expected_name || variant == variant_name { |
| 3585 | return i |
| 3586 | } |
| 3587 | } |
| 3588 | mut short_match_index := -1 |
| 3589 | for i, variant in variants { |
| 3590 | if variant.all_after_last('.') == variant_name.all_after_last('.') { |
| 3591 | if short_match_index >= 0 { |
| 3592 | return 0 |
| 3593 | } |
| 3594 | short_match_index = i |
| 3595 | } |
| 3596 | } |
| 3597 | if short_match_index >= 0 { |
| 3598 | return short_match_index |
| 3599 | } |
| 3600 | } |
| 3601 | return 0 |
| 3602 | } |
| 3603 | |
| 3604 | fn (mut e Eval) eval_index(node &flat.Node) !Value { |
| 3605 | container := e.eval_expr(e.child(node, 0))! |
| 3606 | if node.value == 'range' { |
| 3607 | start := if node.children_count > 1 && e.node(e.child(node, 1)).kind != .empty { |
| 3608 | int(e.value_as_int(e.eval_expr(e.child(node, 1))!)!) |
| 3609 | } else { |
| 3610 | 0 |
| 3611 | } |
| 3612 | mut end := 0 |
| 3613 | match container { |
| 3614 | ArrayValue { end = container.values.len } |
| 3615 | string { end = container.len } |
| 3616 | else {} |
| 3617 | } |
| 3618 | |
| 3619 | if node.children_count > 2 { |
| 3620 | end = int(e.value_as_int(e.eval_expr(e.child(node, 2))!)!) |
| 3621 | } |
| 3622 | return e.slice_value(container, start, end)! |
| 3623 | } |
| 3624 | index := if container is MapValue { |
| 3625 | e.eval_expr_expected(e.child(node, 1), container.key_type_name)! |
| 3626 | } else { |
| 3627 | e.eval_expr(e.child(node, 1))! |
| 3628 | } |
| 3629 | return e.index_value(container, index) |
| 3630 | } |
| 3631 | |
| 3632 | fn (mut e Eval) eval_index_flow(node &flat.Node) !FlowSignal { |
| 3633 | container_signal := e.eval_expr_flow(e.child(node, 0))! |
| 3634 | if container_signal.kind != .normal { |
| 3635 | return container_signal |
| 3636 | } |
| 3637 | container := flow_value(container_signal) |
| 3638 | if node.value == 'range' { |
| 3639 | mut start := 0 |
| 3640 | if node.children_count > 1 && e.node(e.child(node, 1)).kind != .empty { |
| 3641 | start_signal := e.eval_expr_flow(e.child(node, 1))! |
| 3642 | if start_signal.kind != .normal { |
| 3643 | return start_signal |
| 3644 | } |
| 3645 | start = int(e.value_as_int(flow_value(start_signal))!) |
| 3646 | } |
| 3647 | mut end := 0 |
| 3648 | match container { |
| 3649 | ArrayValue { end = container.values.len } |
| 3650 | string { end = container.len } |
| 3651 | else {} |
| 3652 | } |
| 3653 | |
| 3654 | if node.children_count > 2 { |
| 3655 | end_signal := e.eval_expr_flow(e.child(node, 2))! |
| 3656 | if end_signal.kind != .normal { |
| 3657 | return end_signal |
| 3658 | } |
| 3659 | end = int(e.value_as_int(flow_value(end_signal))!) |
| 3660 | } |
| 3661 | return value_flow(e.slice_value(container, start, end)!) |
| 3662 | } |
| 3663 | index_signal := if container is MapValue { |
| 3664 | e.eval_expr_flow_expected(e.child(node, 1), container.key_type_name)! |
| 3665 | } else { |
| 3666 | e.eval_expr_flow(e.child(node, 1))! |
| 3667 | } |
| 3668 | if index_signal.kind != .normal { |
| 3669 | return index_signal |
| 3670 | } |
| 3671 | return value_flow(e.index_value(container, flow_value(index_signal))!) |
| 3672 | } |
| 3673 | |
| 3674 | fn (mut e Eval) index_value(container Value, index Value) !Value { |
| 3675 | match container { |
| 3676 | ArrayValue { |
| 3677 | idx := int(e.value_as_int(index)!) |
| 3678 | if idx < 0 || idx >= container.values.len { |
| 3679 | return error('v3.eval: array index out of bounds') |
| 3680 | } |
| 3681 | return container.values[idx] |
| 3682 | } |
| 3683 | MapValue { |
| 3684 | value, ok := e.map_lookup(container, e.adapt_value_to_type_name(index, |
| 3685 | container.key_type_name)) |
| 3686 | if ok { |
| 3687 | return value |
| 3688 | } |
| 3689 | return container.default_value |
| 3690 | } |
| 3691 | string { |
| 3692 | idx := int(e.value_as_int(index)!) |
| 3693 | if idx < 0 || idx >= container.len { |
| 3694 | return error('v3.eval: string index out of bounds') |
| 3695 | } |
| 3696 | return Value(i64(container[idx])) |
| 3697 | } |
| 3698 | else { |
| 3699 | return error('v3.eval: unsupported index target `${e.runtime_type_name(container)}`') |
| 3700 | } |
| 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | fn (e &Eval) slice_value(container Value, start int, end int) !Value { |
| 3705 | match container { |
| 3706 | ArrayValue { |
| 3707 | mut arr := container |
| 3708 | arr.values = arr.values[start..end].clone() |
| 3709 | return arr |
| 3710 | } |
| 3711 | string { |
| 3712 | sliced := container[start..end] |
| 3713 | return Value(sliced) |
| 3714 | } |
| 3715 | else { |
| 3716 | return error('v3.eval: slicing is only supported for arrays and strings') |
| 3717 | } |
| 3718 | } |
| 3719 | } |
| 3720 | |
| 3721 | fn (mut e Eval) eval_if_value(node &flat.Node) !Value { |
| 3722 | signal := e.eval_if_value_flow(node)! |
| 3723 | return flow_values_value(signal.values) |
| 3724 | } |
| 3725 | |
| 3726 | fn (mut e Eval) eval_if_value_flow(node &flat.Node) !FlowSignal { |
| 3727 | return e.eval_if_value_flow_expected(node, '') |
| 3728 | } |
| 3729 | |
| 3730 | fn (mut e Eval) eval_if_value_flow_expected(node &flat.Node, expected_type string) !FlowSignal { |
| 3731 | if node.children_count < 2 { |
| 3732 | return value_flow(void_value()) |
| 3733 | } |
| 3734 | cond_id := e.child(node, 0) |
| 3735 | cond_is_guard := e.node(cond_id).kind == .decl_assign |
| 3736 | if cond_is_guard { |
| 3737 | e.open_scope() |
| 3738 | } |
| 3739 | cond_signal := e.eval_condition_flow(cond_id)! |
| 3740 | if cond_signal.kind != .normal { |
| 3741 | if cond_is_guard { |
| 3742 | e.close_scope()! |
| 3743 | } |
| 3744 | return cond_signal |
| 3745 | } |
| 3746 | if e.value_as_bool(flow_value(cond_signal))! { |
| 3747 | mut smartcast_scope := false |
| 3748 | if !cond_is_guard { |
| 3749 | if binding := e.smartcast_binding_from_condition(cond_id) { |
| 3750 | e.open_scope() |
| 3751 | e.declare_var_typed(binding.name, binding.value, binding.type_name) |
| 3752 | smartcast_scope = true |
| 3753 | } |
| 3754 | } |
| 3755 | signal := e.eval_value_expr_flow_expected(e.child(node, 1), expected_type)! |
| 3756 | if smartcast_scope { |
| 3757 | e.close_scope()! |
| 3758 | } |
| 3759 | if cond_is_guard { |
| 3760 | e.close_scope()! |
| 3761 | } |
| 3762 | return signal |
| 3763 | } |
| 3764 | if cond_is_guard { |
| 3765 | e.close_scope()! |
| 3766 | } |
| 3767 | if node.children_count > 2 { |
| 3768 | return e.eval_value_expr_flow_expected(e.child(node, 2), expected_type) |
| 3769 | } |
| 3770 | return value_flow(void_value()) |
| 3771 | } |
| 3772 | |
| 3773 | fn (mut e Eval) eval_value_expr_flow(id flat.NodeId) !FlowSignal { |
| 3774 | return e.eval_value_expr_flow_expected(id, '') |
| 3775 | } |
| 3776 | |
| 3777 | fn (mut e Eval) eval_value_expr_flow_expected(id flat.NodeId, expected_type string) !FlowSignal { |
| 3778 | if int(id) >= 0 { |
| 3779 | node := e.node(id) |
| 3780 | if node.kind == .block { |
| 3781 | return e.eval_block_value_flow_expected(node, expected_type) |
| 3782 | } |
| 3783 | } |
| 3784 | return e.eval_expr_flow_expected(id, expected_type) |
| 3785 | } |
| 3786 | |
| 3787 | fn (mut e Eval) eval_block_value(node &flat.Node) !Value { |
| 3788 | signal := e.eval_block_value_flow(node)! |
| 3789 | if signal.kind == .return_ && signal.values.len > 0 { |
| 3790 | return flow_values_value(signal.values) |
| 3791 | } |
| 3792 | return flow_values_value(signal.values) |
| 3793 | } |
| 3794 | |
| 3795 | fn (mut e Eval) eval_block_value_flow(node &flat.Node) !FlowSignal { |
| 3796 | return e.eval_block_value_flow_expected(node, '') |
| 3797 | } |
| 3798 | |
| 3799 | fn (mut e Eval) eval_block_value_flow_expected(node &flat.Node, expected_type string) !FlowSignal { |
| 3800 | return e.eval_block_value_flow_collect(node, false, expected_type) |
| 3801 | } |
| 3802 | |
| 3803 | fn (mut e Eval) eval_block_value_flow_collect(node &flat.Node, collect_expr_values bool, expected_type string) !FlowSignal { |
| 3804 | e.open_scope() |
| 3805 | mut values := []Value{} |
| 3806 | for child_id in e.children(node) { |
| 3807 | child := e.node(child_id) |
| 3808 | if child.kind == .expr_stmt && child.children_count > 0 { |
| 3809 | expr_id := e.child(child, 0) |
| 3810 | expr := e.node(expr_id) |
| 3811 | if expr.kind == .infix && expr.op == .left_shift { |
| 3812 | signal := e.exec_stmt(child_id)! |
| 3813 | if signal.kind != .normal { |
| 3814 | e.close_scope()! |
| 3815 | return signal |
| 3816 | } |
| 3817 | continue |
| 3818 | } |
| 3819 | signal := e.eval_expr_flow_expected(expr_id, expected_type)! |
| 3820 | if signal.kind != .normal { |
| 3821 | e.close_scope()! |
| 3822 | return signal |
| 3823 | } |
| 3824 | expr_values := flow_values_or_void(signal.values) |
| 3825 | if collect_expr_values { |
| 3826 | for expr_value in expr_values { |
| 3827 | if expr_value !is VoidValue { |
| 3828 | values << expr_value |
| 3829 | } |
| 3830 | } |
| 3831 | } else { |
| 3832 | values = expr_values.clone() |
| 3833 | } |
| 3834 | continue |
| 3835 | } |
| 3836 | if child.kind == .block { |
| 3837 | collect_child_expr_values := node.children_count == 1 |
| 3838 | && e.block_has_only_expr_stmts(child) && child.children_count > 1 |
| 3839 | signal := e.eval_block_value_flow_collect(child, collect_child_expr_values, |
| 3840 | expected_type)! |
| 3841 | if signal.kind != .normal { |
| 3842 | e.close_scope()! |
| 3843 | return signal |
| 3844 | } |
| 3845 | values = flow_values_or_void(signal.values) |
| 3846 | continue |
| 3847 | } |
| 3848 | signal := e.exec_stmt(child_id)! |
| 3849 | if signal.kind != .normal { |
| 3850 | e.close_scope()! |
| 3851 | return signal |
| 3852 | } |
| 3853 | } |
| 3854 | e.close_scope()! |
| 3855 | return FlowSignal{ |
| 3856 | values: flow_values_or_void(values) |
| 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | fn (e &Eval) block_has_only_expr_stmts(node &flat.Node) bool { |
| 3861 | if node.children_count == 0 { |
| 3862 | return false |
| 3863 | } |
| 3864 | for child_id in e.children(node) { |
| 3865 | if e.node(child_id).kind != .expr_stmt { |
| 3866 | return false |
| 3867 | } |
| 3868 | } |
| 3869 | return true |
| 3870 | } |
| 3871 | |
| 3872 | fn (mut e Eval) eval_array_init(node &flat.Node) !Value { |
| 3873 | return flow_value(e.eval_array_init_flow(node)!) |
| 3874 | } |
| 3875 | |
| 3876 | fn (mut e Eval) eval_array_init_flow(node &flat.Node) !FlowSignal { |
| 3877 | array_type_name := if node.typ.len > 0 { node.typ } else { node.value } |
| 3878 | is_fixed_array := is_fixed_array_type_name(array_type_name) |
| 3879 | elem_type_name := if is_fixed_array { |
| 3880 | fixed_array_elem_type_name(array_type_name) |
| 3881 | } else { |
| 3882 | node.value |
| 3883 | } |
| 3884 | mut len := 0 |
| 3885 | mut cap := 0 |
| 3886 | mut has_cap := false |
| 3887 | mut init_id := flat.empty_node |
| 3888 | mut has_init := false |
| 3889 | mut values := []Value{} |
| 3890 | for child_id in e.children(node) { |
| 3891 | child := e.node(child_id) |
| 3892 | if child.kind == .field_init { |
| 3893 | if child.value == 'len' { |
| 3894 | signal := e.eval_expr_flow(e.child(child, 0))! |
| 3895 | if signal.kind != .normal { |
| 3896 | return signal |
| 3897 | } |
| 3898 | len = int(e.value_as_int(flow_value(signal))!) |
| 3899 | } else if child.value == 'cap' { |
| 3900 | signal := e.eval_expr_flow(e.child(child, 0))! |
| 3901 | if signal.kind != .normal { |
| 3902 | return signal |
| 3903 | } |
| 3904 | cap = int(e.value_as_int(flow_value(signal))!) |
| 3905 | has_cap = true |
| 3906 | } else if child.value == 'init' { |
| 3907 | init_id = e.child(child, 0) |
| 3908 | has_init = true |
| 3909 | } |
| 3910 | } else { |
| 3911 | signal := e.eval_expr_flow_expected(child_id, elem_type_name)! |
| 3912 | if signal.kind != .normal { |
| 3913 | return signal |
| 3914 | } |
| 3915 | values << e.adapt_value_to_type_name(flow_value(signal), elem_type_name) |
| 3916 | } |
| 3917 | } |
| 3918 | if len > 0 || has_cap { |
| 3919 | array_cap := if has_cap { cap } else { len } |
| 3920 | return e.eval_array_init_values(len, array_cap, has_init, init_id, elem_type_name) |
| 3921 | } |
| 3922 | if is_fixed_array && values.len == 0 { |
| 3923 | fixed_len := e.fixed_array_len(array_type_name)! |
| 3924 | return e.eval_array_init_values(fixed_len, fixed_len, has_init, init_id, elem_type_name) |
| 3925 | } |
| 3926 | return value_flow(ArrayValue{ |
| 3927 | elem_type_name: elem_type_name |
| 3928 | values: values |
| 3929 | }) |
| 3930 | } |
| 3931 | |
| 3932 | fn (mut e Eval) eval_array_init_values(len int, cap int, has_init bool, init_id flat.NodeId, elem_type_name string) !FlowSignal { |
| 3933 | mut values := []Value{cap: cap} |
| 3934 | for i in 0 .. len { |
| 3935 | if has_init { |
| 3936 | e.open_scope() |
| 3937 | e.declare_var_typed('index', Value(i64(i)), 'int') |
| 3938 | signal := e.eval_expr_flow_expected(init_id, elem_type_name) or { |
| 3939 | e.close_scope()! |
| 3940 | return err |
| 3941 | } |
| 3942 | e.close_scope()! |
| 3943 | if signal.kind != .normal { |
| 3944 | return signal |
| 3945 | } |
| 3946 | values << e.adapt_value_to_type_name(flow_value(signal), elem_type_name) |
| 3947 | } else { |
| 3948 | values << e.zero_value_for_type_name(elem_type_name) |
| 3949 | } |
| 3950 | } |
| 3951 | return value_flow(ArrayValue{ |
| 3952 | elem_type_name: elem_type_name |
| 3953 | values: values |
| 3954 | }) |
| 3955 | } |
| 3956 | |
| 3957 | fn is_fixed_array_type_name(type_name string) bool { |
| 3958 | if type_name.starts_with('[]') || type_name.starts_with('map[') { |
| 3959 | return false |
| 3960 | } |
| 3961 | return type_name.contains('[') && (type_name.ends_with(']') || type_name.starts_with('[')) |
| 3962 | } |
| 3963 | |
| 3964 | fn (mut e Eval) fixed_array_len(type_name string) !int { |
| 3965 | return e.fixed_array_len_in_module(type_name, e.current_module_name()) |
| 3966 | } |
| 3967 | |
| 3968 | fn (mut e Eval) fixed_array_len_in_module(type_name string, module_name string) !int { |
| 3969 | text := fixed_array_len_text(type_name) |
| 3970 | if len := fixed_array_len_literal(text) { |
| 3971 | return len |
| 3972 | } |
| 3973 | if value := e.lookup_const(module_name, text) { |
| 3974 | return int(e.value_as_int(value)!) |
| 3975 | } |
| 3976 | if text.contains('.') { |
| 3977 | mod_name := text.all_before_last('.') |
| 3978 | const_name := text.all_after_last('.') |
| 3979 | if value := e.lookup_const(mod_name, const_name) { |
| 3980 | return int(e.value_as_int(value)!) |
| 3981 | } |
| 3982 | } |
| 3983 | return 0 |
| 3984 | } |
| 3985 | |
| 3986 | fn fixed_array_len_literal(text string) ?int { |
| 3987 | normalized := text.replace('_', '') |
| 3988 | if normalized.len == 0 { |
| 3989 | return none |
| 3990 | } |
| 3991 | mut len := 0 |
| 3992 | for ch in normalized { |
| 3993 | if ch < `0` || ch > `9` { |
| 3994 | return none |
| 3995 | } |
| 3996 | len = len * 10 + int(ch - `0`) |
| 3997 | } |
| 3998 | return len |
| 3999 | } |
| 4000 | |
| 4001 | fn fixed_array_len_text(type_name string) string { |
| 4002 | return type_name.all_after('[').all_before(']').trim_space() |
| 4003 | } |
| 4004 | |
| 4005 | fn fixed_array_elem_type_name(type_name string) string { |
| 4006 | if type_name.starts_with('[') { |
| 4007 | return type_name.all_after(']') |
| 4008 | } |
| 4009 | return type_name.all_before('[') |
| 4010 | } |
| 4011 | |
| 4012 | fn (mut e Eval) eval_map_init(node &flat.Node) !Value { |
| 4013 | return flow_value(e.eval_map_init_flow(node)!) |
| 4014 | } |
| 4015 | |
| 4016 | fn (mut e Eval) eval_map_init_flow(node &flat.Node) !FlowSignal { |
| 4017 | mut key_type, mut value_type := split_map_type(node.value) |
| 4018 | children := e.children(node) |
| 4019 | mut keys := []Value{} |
| 4020 | mut values := []Value{} |
| 4021 | mut i := 0 |
| 4022 | for i + 1 < children.len { |
| 4023 | key_signal := e.eval_expr_flow_expected(children[i], key_type)! |
| 4024 | if key_signal.kind != .normal { |
| 4025 | return key_signal |
| 4026 | } |
| 4027 | key := flow_value(key_signal) |
| 4028 | if key_type.len == 0 { |
| 4029 | key_type = e.infer_expr_type_name(children[i]) |
| 4030 | if key_type.len == 0 { |
| 4031 | key_type = e.runtime_type_name(key) |
| 4032 | } |
| 4033 | } |
| 4034 | typed_key := e.adapt_value_to_type_name(key, key_type) |
| 4035 | value_signal := e.eval_expr_flow_expected(children[i + 1], value_type)! |
| 4036 | if value_signal.kind != .normal { |
| 4037 | return value_signal |
| 4038 | } |
| 4039 | value := flow_value(value_signal) |
| 4040 | if value_type.len == 0 { |
| 4041 | value_type = e.infer_expr_type_name(children[i + 1]) |
| 4042 | if value_type.len == 0 { |
| 4043 | value_type = e.runtime_type_name(value) |
| 4044 | } |
| 4045 | } |
| 4046 | keys << typed_key |
| 4047 | values << value |
| 4048 | i += 2 |
| 4049 | } |
| 4050 | mut m := MapValue{ |
| 4051 | key_type_name: key_type |
| 4052 | value_type_name: value_type |
| 4053 | default_value: e.zero_value_for_type_name(value_type) |
| 4054 | } |
| 4055 | for j, key in keys { |
| 4056 | m = e.map_set_value(m, key, values[j]) |
| 4057 | } |
| 4058 | return value_flow(m) |
| 4059 | } |
| 4060 | |
| 4061 | fn (mut e Eval) eval_string_interp_flow(node &flat.Node) !FlowSignal { |
| 4062 | mut out := '' |
| 4063 | for child_id in e.children(node) { |
| 4064 | child := e.node(child_id) |
| 4065 | if child.kind == .string_literal { |
| 4066 | out += child.value |
| 4067 | continue |
| 4068 | } |
| 4069 | signal := e.eval_expr_flow(child_id)! |
| 4070 | if signal.kind != .normal { |
| 4071 | return signal |
| 4072 | } |
| 4073 | out += e.display_string(flow_value(signal))! |
| 4074 | } |
| 4075 | return value_flow(Value(out)) |
| 4076 | } |
| 4077 | |
| 4078 | fn split_map_type(type_name string) (string, string) { |
| 4079 | if !type_name.starts_with('map[') { |
| 4080 | return '', '' |
| 4081 | } |
| 4082 | inner := type_name[4..] |
| 4083 | if idx := inner.index(']') { |
| 4084 | return inner[..idx], inner[idx + 1..] |
| 4085 | } |
| 4086 | return '', '' |
| 4087 | } |
| 4088 | |
| 4089 | fn (mut e Eval) eval_struct_init(node &flat.Node) !Value { |
| 4090 | return flow_value(e.eval_struct_init_flow(node)!) |
| 4091 | } |
| 4092 | |
| 4093 | fn (mut e Eval) eval_struct_init_flow(node &flat.Node) !FlowSignal { |
| 4094 | type_name := e.normalize_type_name(node.value) |
| 4095 | mut st := e.zero_struct_value(type_name) |
| 4096 | mut positional := 0 |
| 4097 | for child_id in e.children(node) { |
| 4098 | child := e.node(child_id) |
| 4099 | if child.kind != .field_init { |
| 4100 | continue |
| 4101 | } |
| 4102 | if child.value.len > 0 { |
| 4103 | field_type := e.struct_field_type_name(st, child.value) |
| 4104 | value_signal := e.eval_expr_flow_expected(e.child(child, 0), field_type)! |
| 4105 | if value_signal.kind != .normal { |
| 4106 | return value_signal |
| 4107 | } |
| 4108 | value := flow_value(value_signal) |
| 4109 | st.fields[child.value] = e.adapt_value_to_type_name(value, e.struct_field_type_name(st, |
| 4110 | child.value)) |
| 4111 | } else { |
| 4112 | fields := e.struct_fields(type_name) |
| 4113 | if positional < fields.len { |
| 4114 | field := fields[positional] |
| 4115 | field_type := e.qualify_nested_type_name(field.module_name, field.typ) |
| 4116 | value_signal := e.eval_expr_flow_expected(e.child(child, 0), field_type)! |
| 4117 | if value_signal.kind != .normal { |
| 4118 | return value_signal |
| 4119 | } |
| 4120 | value := flow_value(value_signal) |
| 4121 | st.fields[field.name] = e.adapt_value_to_type_name(value, field_type) |
| 4122 | } |
| 4123 | positional++ |
| 4124 | } |
| 4125 | } |
| 4126 | return value_flow(st) |
| 4127 | } |
| 4128 | |
| 4129 | fn (mut e Eval) eval_assoc(node &flat.Node) !Value { |
| 4130 | return flow_value(e.eval_assoc_flow(node)!) |
| 4131 | } |
| 4132 | |
| 4133 | fn (mut e Eval) eval_assoc_flow(node &flat.Node) !FlowSignal { |
| 4134 | if node.children_count == 0 { |
| 4135 | return value_flow(e.zero_struct_value(node.value)) |
| 4136 | } |
| 4137 | base_signal := e.eval_expr_flow(e.child(node, 0))! |
| 4138 | if base_signal.kind != .normal { |
| 4139 | return base_signal |
| 4140 | } |
| 4141 | base := flow_value(base_signal) |
| 4142 | if base !is StructValue { |
| 4143 | return error('v3.eval: assoc base must be struct') |
| 4144 | } |
| 4145 | mut st := base as StructValue |
| 4146 | for i in 1 .. node.children_count { |
| 4147 | field := e.child_node(node, i) |
| 4148 | if field.kind == .field_init { |
| 4149 | field_type := e.struct_field_type_name(st, field.value) |
| 4150 | value_signal := e.eval_expr_flow_expected(e.child(field, 0), field_type)! |
| 4151 | if value_signal.kind != .normal { |
| 4152 | return value_signal |
| 4153 | } |
| 4154 | st.fields[field.value] = e.adapt_value_to_type_name(flow_value(value_signal), |
| 4155 | field_type) |
| 4156 | } |
| 4157 | } |
| 4158 | return value_flow(st) |
| 4159 | } |
| 4160 | |
| 4161 | fn (mut e Eval) eval_prefix(node &flat.Node) !Value { |
| 4162 | value := e.eval_expr(e.child(node, 0))! |
| 4163 | return e.apply_prefix(node.op, value) |
| 4164 | } |
| 4165 | |
| 4166 | fn (e &Eval) apply_prefix(op flat.Op, value Value) !Value { |
| 4167 | match op { |
| 4168 | .minus { |
| 4169 | if value is f64 { |
| 4170 | negated := -value |
| 4171 | return Value(negated) |
| 4172 | } |
| 4173 | return Value(-e.value_as_int(value)!) |
| 4174 | } |
| 4175 | .not { |
| 4176 | return Value(!e.value_as_bool(value)!) |
| 4177 | } |
| 4178 | .bit_not { |
| 4179 | return Value(~e.value_as_int(value)!) |
| 4180 | } |
| 4181 | .amp, .mul { |
| 4182 | return value |
| 4183 | } |
| 4184 | else { |
| 4185 | return value |
| 4186 | } |
| 4187 | } |
| 4188 | } |
| 4189 | |
| 4190 | fn (mut e Eval) eval_postfix(node &flat.Node) !Value { |
| 4191 | return flow_value(e.eval_postfix_flow(node)!) |
| 4192 | } |
| 4193 | |
| 4194 | fn (mut e Eval) eval_postfix_flow(node &flat.Node) !FlowSignal { |
| 4195 | target_id := e.child(node, 0) |
| 4196 | return e.apply_postfix_target_flow(target_id, node.op) |
| 4197 | } |
| 4198 | |
| 4199 | fn (mut e Eval) apply_postfix_target_flow(target_id flat.NodeId, op flat.Op) !FlowSignal { |
| 4200 | target := e.node(target_id) |
| 4201 | match target.kind { |
| 4202 | .index, .selector { |
| 4203 | return e.update_resolved_lvalue_postfix_flow(target_id, op) |
| 4204 | } |
| 4205 | else { |
| 4206 | old_signal := e.eval_expr_flow(target_id)! |
| 4207 | if old_signal.kind != .normal { |
| 4208 | return old_signal |
| 4209 | } |
| 4210 | old := flow_value(old_signal) |
| 4211 | value := e.apply_postfix_op(op, old)! |
| 4212 | update_signal := e.update_target_flow(target_id, value)! |
| 4213 | if update_signal.kind != .normal { |
| 4214 | return update_signal |
| 4215 | } |
| 4216 | return value_flow(old) |
| 4217 | } |
| 4218 | } |
| 4219 | } |
| 4220 | |
| 4221 | fn (mut e Eval) apply_postfix_op(op flat.Op, old Value) !Value { |
| 4222 | if op == .inc { |
| 4223 | return e.apply_infix(.plus, old, Value(i64(1))) |
| 4224 | } |
| 4225 | if op == .dec { |
| 4226 | return e.apply_infix(.minus, old, Value(i64(1))) |
| 4227 | } |
| 4228 | return old |
| 4229 | } |
| 4230 | |
| 4231 | fn (mut e Eval) eval_or_expr(node &flat.Node) !Value { |
| 4232 | signal := e.eval_or_expr_flow(node)! |
| 4233 | if signal.kind == .return_ && signal.values.len > 0 { |
| 4234 | return signal.values[0] |
| 4235 | } |
| 4236 | return flow_value(signal) |
| 4237 | } |
| 4238 | |
| 4239 | fn (mut e Eval) eval_or_expr_flow(node &flat.Node) !FlowSignal { |
| 4240 | left_id := e.child(node, 0) |
| 4241 | left_node := e.node(left_id) |
| 4242 | if left_node.kind == .index { |
| 4243 | container_signal := e.eval_expr_flow(e.child(left_node, 0))! |
| 4244 | if container_signal.kind != .normal { |
| 4245 | return container_signal |
| 4246 | } |
| 4247 | container := flow_value(container_signal) |
| 4248 | if container is MapValue { |
| 4249 | index_signal := e.eval_expr_flow_expected(e.child(left_node, 1), |
| 4250 | container.key_type_name)! |
| 4251 | if index_signal.kind != .normal { |
| 4252 | return index_signal |
| 4253 | } |
| 4254 | value, ok := e.map_lookup(container, flow_value(index_signal)) |
| 4255 | if ok { |
| 4256 | return value_flow(e.unwrap_option_like(value)) |
| 4257 | } |
| 4258 | return e.eval_or_failure(node, container.default_value) |
| 4259 | } |
| 4260 | } |
| 4261 | left_signal := e.eval_expr_flow(left_id)! |
| 4262 | if left_signal.kind != .normal { |
| 4263 | return left_signal |
| 4264 | } |
| 4265 | left := flow_value(left_signal) |
| 4266 | if e.value_is_truthy(left) { |
| 4267 | return FlowSignal{ |
| 4268 | values: [e.unwrap_option_like(left)] |
| 4269 | } |
| 4270 | } |
| 4271 | return e.eval_or_failure(node, left) |
| 4272 | } |
| 4273 | |
| 4274 | fn (mut e Eval) eval_or_failure(node &flat.Node, value Value) !FlowSignal { |
| 4275 | if node.children_count > 1 && e.node(e.child(node, 1)).kind != .empty { |
| 4276 | or_id := e.child(node, 1) |
| 4277 | or_node := e.node(or_id) |
| 4278 | if or_node.kind == .block { |
| 4279 | e.open_scope() |
| 4280 | e.declare_var('err', e.or_block_err_value(value)) |
| 4281 | signal := e.eval_block_value_flow(or_node) or { |
| 4282 | e.close_scope() or {} |
| 4283 | return err |
| 4284 | } |
| 4285 | e.close_scope()! |
| 4286 | return signal |
| 4287 | } |
| 4288 | return FlowSignal{ |
| 4289 | values: [e.eval_expr(or_id)!] |
| 4290 | } |
| 4291 | } |
| 4292 | if node.value == '?' || node.value == '!' { |
| 4293 | return FlowSignal{ |
| 4294 | kind: .return_ |
| 4295 | values: [value] |
| 4296 | } |
| 4297 | } |
| 4298 | return FlowSignal{ |
| 4299 | values: [void_value()] |
| 4300 | } |
| 4301 | } |
| 4302 | |
| 4303 | fn (e &Eval) or_block_err_value(value Value) Value { |
| 4304 | if value is VoidValue { |
| 4305 | return Value('') |
| 4306 | } |
| 4307 | return value |
| 4308 | } |
| 4309 | |
| 4310 | fn (mut e Eval) exec_match(node &flat.Node) !FlowSignal { |
| 4311 | target_id := e.child(node, 0) |
| 4312 | target_signal := e.eval_expr_flow(target_id)! |
| 4313 | if target_signal.kind != .normal { |
| 4314 | return target_signal |
| 4315 | } |
| 4316 | target := flow_value(target_signal) |
| 4317 | for i in 1 .. node.children_count { |
| 4318 | branch := e.child_node(node, i) |
| 4319 | if branch.kind != .match_branch { |
| 4320 | continue |
| 4321 | } |
| 4322 | cond_count := if branch.value == 'else' { 0 } else { branch.value.int() } |
| 4323 | mut matched := branch.value == 'else' |
| 4324 | mut matched_cond := Value(void_value()) |
| 4325 | for j in 0 .. cond_count { |
| 4326 | cond_signal := e.eval_match_condition_flow(e.child(branch, j), target_id)! |
| 4327 | if cond_signal.kind != .normal { |
| 4328 | return cond_signal |
| 4329 | } |
| 4330 | cond := flow_value(cond_signal) |
| 4331 | if e.match_condition_matches(target, cond) { |
| 4332 | matched = true |
| 4333 | matched_cond = cond |
| 4334 | break |
| 4335 | } |
| 4336 | } |
| 4337 | if matched { |
| 4338 | e.open_scope() |
| 4339 | if binding := e.smartcast_binding_from_match(target_id, target, matched_cond) { |
| 4340 | e.declare_var_typed(binding.name, binding.value, binding.type_name) |
| 4341 | } |
| 4342 | signal := e.exec_stmts(e.children(branch)[cond_count..])! |
| 4343 | e.close_scope()! |
| 4344 | if signal.kind != .normal { |
| 4345 | return signal |
| 4346 | } |
| 4347 | return normal_flow() |
| 4348 | } |
| 4349 | } |
| 4350 | return normal_flow() |
| 4351 | } |
| 4352 | |
| 4353 | fn (mut e Eval) eval_match_value(node &flat.Node) !Value { |
| 4354 | signal := e.eval_match_value_flow(node)! |
| 4355 | if signal.kind == .return_ && signal.values.len > 0 { |
| 4356 | return flow_values_value(signal.values) |
| 4357 | } |
| 4358 | return flow_values_value(signal.values) |
| 4359 | } |
| 4360 | |
| 4361 | fn (mut e Eval) eval_match_value_flow(node &flat.Node) !FlowSignal { |
| 4362 | return e.eval_match_value_flow_expected(node, '') |
| 4363 | } |
| 4364 | |
| 4365 | fn (mut e Eval) eval_match_value_flow_expected(node &flat.Node, expected_type string) !FlowSignal { |
| 4366 | target_id := e.child(node, 0) |
| 4367 | target_signal := e.eval_expr_flow(target_id)! |
| 4368 | if target_signal.kind != .normal { |
| 4369 | return target_signal |
| 4370 | } |
| 4371 | target := flow_value(target_signal) |
| 4372 | for i in 1 .. node.children_count { |
| 4373 | branch := e.child_node(node, i) |
| 4374 | if branch.kind != .match_branch { |
| 4375 | continue |
| 4376 | } |
| 4377 | cond_count := if branch.value == 'else' { 0 } else { branch.value.int() } |
| 4378 | mut matched := branch.value == 'else' |
| 4379 | mut matched_cond := Value(void_value()) |
| 4380 | for j in 0 .. cond_count { |
| 4381 | cond_signal := e.eval_match_condition_flow(e.child(branch, j), target_id)! |
| 4382 | if cond_signal.kind != .normal { |
| 4383 | return cond_signal |
| 4384 | } |
| 4385 | cond := flow_value(cond_signal) |
| 4386 | if e.match_condition_matches(target, cond) { |
| 4387 | matched = true |
| 4388 | matched_cond = cond |
| 4389 | break |
| 4390 | } |
| 4391 | } |
| 4392 | if matched { |
| 4393 | e.open_scope() |
| 4394 | if binding := e.smartcast_binding_from_match(target_id, target, matched_cond) { |
| 4395 | e.declare_var_typed(binding.name, binding.value, binding.type_name) |
| 4396 | } |
| 4397 | mut values := []Value{} |
| 4398 | for j in cond_count .. branch.children_count { |
| 4399 | stmt_id := e.child(branch, j) |
| 4400 | stmt := e.node(stmt_id) |
| 4401 | if stmt.kind == .expr_stmt && stmt.children_count > 0 { |
| 4402 | expr_id := e.child(stmt, 0) |
| 4403 | expr := e.node(expr_id) |
| 4404 | if expr.kind == .infix && expr.op == .left_shift { |
| 4405 | signal := e.exec_stmt(stmt_id)! |
| 4406 | if signal.kind != .normal { |
| 4407 | e.close_scope()! |
| 4408 | return signal |
| 4409 | } |
| 4410 | continue |
| 4411 | } |
| 4412 | signal := e.eval_expr_flow_expected(expr_id, expected_type)! |
| 4413 | if signal.kind != .normal { |
| 4414 | e.close_scope()! |
| 4415 | return signal |
| 4416 | } |
| 4417 | values = flow_values_or_void(signal.values) |
| 4418 | } else { |
| 4419 | signal := e.exec_stmt(stmt_id)! |
| 4420 | if signal.kind != .normal { |
| 4421 | e.close_scope()! |
| 4422 | return signal |
| 4423 | } |
| 4424 | } |
| 4425 | } |
| 4426 | e.close_scope()! |
| 4427 | return FlowSignal{ |
| 4428 | values: flow_values_or_void(values) |
| 4429 | } |
| 4430 | } |
| 4431 | } |
| 4432 | return value_flow(void_value()) |
| 4433 | } |
| 4434 | |
| 4435 | fn (e &Eval) match_condition_matches(target Value, cond Value) bool { |
| 4436 | if cond is RangeValue { |
| 4437 | return e.value_in(target, cond) |
| 4438 | } |
| 4439 | if cond is TypeValue { |
| 4440 | return e.value_matches_type_name(target, cond.name) |
| 4441 | } |
| 4442 | return e.value_eq(target, cond) |
| 4443 | } |
| 4444 | |
| 4445 | fn (e &Eval) smartcast_binding_from_match(target_id flat.NodeId, target Value, cond Value) ?SmartcastBinding { |
| 4446 | if cond !is TypeValue { |
| 4447 | return none |
| 4448 | } |
| 4449 | type_cond := cond as TypeValue |
| 4450 | target_node := e.node(target_id) |
| 4451 | if target_node.kind != .ident { |
| 4452 | return none |
| 4453 | } |
| 4454 | if value := e.smartcast_value(target, type_cond.name) { |
| 4455 | return SmartcastBinding{ |
| 4456 | name: target_node.value |
| 4457 | value: value |
| 4458 | type_name: e.normalize_type_name(type_cond.name) |
| 4459 | } |
| 4460 | } |
| 4461 | return none |
| 4462 | } |
| 4463 | |
| 4464 | fn (mut e Eval) eval_match_condition(cond_id flat.NodeId, target_id flat.NodeId) !Value { |
| 4465 | return flow_value(e.eval_match_condition_flow(cond_id, target_id)!) |
| 4466 | } |
| 4467 | |
| 4468 | fn (mut e Eval) eval_match_condition_flow(cond_id flat.NodeId, target_id flat.NodeId) !FlowSignal { |
| 4469 | cond := e.node(cond_id) |
| 4470 | if cond.kind == .enum_val { |
| 4471 | if enum_type := e.match_target_enum_type_name(target_id) { |
| 4472 | if value := e.lookup_enum_value(enum_type, cond.value) { |
| 4473 | return value_flow(value) |
| 4474 | } |
| 4475 | } |
| 4476 | } |
| 4477 | if cond.kind == .ident && is_builtin_type_name(cond.value) { |
| 4478 | return value_flow(TypeValue{ |
| 4479 | name: cond.value |
| 4480 | }) |
| 4481 | } |
| 4482 | signal := e.eval_expr_flow(cond_id)! |
| 4483 | if signal.kind != .normal { |
| 4484 | return signal |
| 4485 | } |
| 4486 | value := flow_value(signal) |
| 4487 | if value is RangeValue { |
| 4488 | return value_flow(RangeValue{ |
| 4489 | start: value.start |
| 4490 | end: value.end |
| 4491 | inclusive: true |
| 4492 | }) |
| 4493 | } |
| 4494 | return value_flow(value) |
| 4495 | } |
| 4496 | |
| 4497 | fn (e &Eval) match_target_enum_type_name(target_id flat.NodeId) ?string { |
| 4498 | target := e.node(target_id) |
| 4499 | match target.kind { |
| 4500 | .ident { |
| 4501 | if typ := e.lookup_var_type(target.value) { |
| 4502 | return typ |
| 4503 | } |
| 4504 | if target.typ.len > 0 { |
| 4505 | return e.normalize_type_name(target.typ) |
| 4506 | } |
| 4507 | } |
| 4508 | .selector { |
| 4509 | if target.children_count > 0 { |
| 4510 | return e.type_value_name_from_expr(e.child(target, 0)) |
| 4511 | } |
| 4512 | } |
| 4513 | else {} |
| 4514 | } |
| 4515 | |
| 4516 | return none |
| 4517 | } |
| 4518 | |
| 4519 | fn (e &Eval) type_value_name_from_expr(id flat.NodeId) ?string { |
| 4520 | node := e.node(id) |
| 4521 | match node.kind { |
| 4522 | .ident { |
| 4523 | if e.has_type_name(e.current_module_name(), node.value) { |
| 4524 | return e.qualify_type_name(e.current_module_name(), node.value) |
| 4525 | } |
| 4526 | } |
| 4527 | .selector { |
| 4528 | if node.children_count > 0 { |
| 4529 | left := e.node(e.child(node, 0)) |
| 4530 | if left.kind == .ident { |
| 4531 | mod := e.resolve_module_name(left.value) |
| 4532 | if e.has_type_name(mod, node.value) { |
| 4533 | return e.qualify_type_name(mod, node.value) |
| 4534 | } |
| 4535 | } |
| 4536 | } |
| 4537 | } |
| 4538 | else {} |
| 4539 | } |
| 4540 | |
| 4541 | return none |
| 4542 | } |
| 4543 | |
| 4544 | fn (mut e Eval) lookup_enum_value(enum_type_name string, field string) ?Value { |
| 4545 | mod := if enum_type_name.contains('.') { |
| 4546 | enum_type_name.all_before_last('.') |
| 4547 | } else { |
| 4548 | e.current_module_name() |
| 4549 | } |
| 4550 | enum_key := '${enum_type_name.all_after_last('.')}.${field}' |
| 4551 | if value := e.lookup_const(mod, enum_key) { |
| 4552 | return e.enum_value(e.qualify_type_name(mod, enum_type_name), value) |
| 4553 | } |
| 4554 | return none |
| 4555 | } |
| 4556 | |
| 4557 | fn (e &Eval) enum_value(type_name string, value Value) Value { |
| 4558 | return EnumValue{ |
| 4559 | type_name: e.normalize_type_name(type_name) |
| 4560 | value: e.value_as_int(value) or { i64(0) } |
| 4561 | } |
| 4562 | } |
| 4563 | |
| 4564 | fn (mut e Eval) maybe_call_builtin(module_name string, fn_name string, args []Value) !MaybeValue { |
| 4565 | if module_name in ['', 'main', e.current_module_name()] { |
| 4566 | match fn_name { |
| 4567 | 'print' { |
| 4568 | if args.len > 0 { |
| 4569 | e.write_stdout(e.display_string(args[0])!) |
| 4570 | } |
| 4571 | return MaybeValue{ |
| 4572 | found: true |
| 4573 | value: void_value() |
| 4574 | } |
| 4575 | } |
| 4576 | 'println' { |
| 4577 | if args.len > 0 { |
| 4578 | e.write_stdout(e.display_string(args[0])! + '\n') |
| 4579 | } else { |
| 4580 | e.write_stdout('\n') |
| 4581 | } |
| 4582 | return MaybeValue{ |
| 4583 | found: true |
| 4584 | value: void_value() |
| 4585 | } |
| 4586 | } |
| 4587 | 'eprint' { |
| 4588 | if args.len > 0 { |
| 4589 | e.write_stderr(e.display_string(args[0])!) |
| 4590 | } |
| 4591 | return MaybeValue{ |
| 4592 | found: true |
| 4593 | value: void_value() |
| 4594 | } |
| 4595 | } |
| 4596 | 'eprintln' { |
| 4597 | if args.len > 0 { |
| 4598 | e.write_stderr(e.display_string(args[0])! + '\n') |
| 4599 | } else { |
| 4600 | e.write_stderr('\n') |
| 4601 | } |
| 4602 | return MaybeValue{ |
| 4603 | found: true |
| 4604 | value: void_value() |
| 4605 | } |
| 4606 | } |
| 4607 | 'int_str', 'i64_str', 'u64_str' { |
| 4608 | return MaybeValue{ |
| 4609 | found: true |
| 4610 | value: Value(e.value_as_int(args[0] or { Value(i64(0)) })!.str()) |
| 4611 | } |
| 4612 | } |
| 4613 | 'str' { |
| 4614 | return MaybeValue{ |
| 4615 | found: true |
| 4616 | value: Value(e.display_string(args[0] or { void_value() })!) |
| 4617 | } |
| 4618 | } |
| 4619 | 'panic' { |
| 4620 | return error(e.display_string(args[0] or { Value('panic') })!) |
| 4621 | } |
| 4622 | else {} |
| 4623 | } |
| 4624 | } |
| 4625 | if module_name == 'os' { |
| 4626 | return e.maybe_call_os_builtin(fn_name, args) |
| 4627 | } |
| 4628 | return MaybeValue{} |
| 4629 | } |
| 4630 | |
| 4631 | fn (mut e Eval) maybe_call_os_builtin(fn_name string, args []Value) !MaybeValue { |
| 4632 | match fn_name { |
| 4633 | 'execute' { |
| 4634 | cmd := e.expect_string_arg(args, 0)! |
| 4635 | return MaybeValue{ |
| 4636 | found: true |
| 4637 | value: os_result_value(os.execute(cmd)) |
| 4638 | } |
| 4639 | } |
| 4640 | 'user_os' { |
| 4641 | return MaybeValue{ |
| 4642 | found: true |
| 4643 | value: Value(os.user_os()) |
| 4644 | } |
| 4645 | } |
| 4646 | 'getenv' { |
| 4647 | return MaybeValue{ |
| 4648 | found: true |
| 4649 | value: Value(os.getenv(e.expect_string_arg(args, 0)!)) |
| 4650 | } |
| 4651 | } |
| 4652 | 'temp_dir' { |
| 4653 | return MaybeValue{ |
| 4654 | found: true |
| 4655 | value: Value(os.temp_dir()) |
| 4656 | } |
| 4657 | } |
| 4658 | 'getpid' { |
| 4659 | return MaybeValue{ |
| 4660 | found: true |
| 4661 | value: Value(i64(os.getpid())) |
| 4662 | } |
| 4663 | } |
| 4664 | 'join_path' { |
| 4665 | parts := args.map(e.value_string(it)) |
| 4666 | if parts.len == 0 { |
| 4667 | return MaybeValue{ |
| 4668 | found: true |
| 4669 | value: Value('') |
| 4670 | } |
| 4671 | } |
| 4672 | return MaybeValue{ |
| 4673 | found: true |
| 4674 | value: Value(os.join_path(parts[0], ...parts[1..])) |
| 4675 | } |
| 4676 | } |
| 4677 | 'join_path_single' { |
| 4678 | a := e.expect_string_arg(args, 0)! |
| 4679 | b := e.expect_string_arg(args, 1)! |
| 4680 | return MaybeValue{ |
| 4681 | found: true |
| 4682 | value: Value(os.join_path_single(a, b)) |
| 4683 | } |
| 4684 | } |
| 4685 | 'dir' { |
| 4686 | return MaybeValue{ |
| 4687 | found: true |
| 4688 | value: Value(os.dir(e.expect_string_arg(args, 0)!)) |
| 4689 | } |
| 4690 | } |
| 4691 | 'is_dir' { |
| 4692 | return MaybeValue{ |
| 4693 | found: true |
| 4694 | value: Value(os.is_dir(e.expect_string_arg(args, 0)!)) |
| 4695 | } |
| 4696 | } |
| 4697 | 'exists' { |
| 4698 | return MaybeValue{ |
| 4699 | found: true |
| 4700 | value: Value(os.exists(e.expect_string_arg(args, 0)!)) |
| 4701 | } |
| 4702 | } |
| 4703 | 'quoted_path' { |
| 4704 | return MaybeValue{ |
| 4705 | found: true |
| 4706 | value: Value(os.quoted_path(e.expect_string_arg(args, 0)!)) |
| 4707 | } |
| 4708 | } |
| 4709 | else { |
| 4710 | return MaybeValue{} |
| 4711 | } |
| 4712 | } |
| 4713 | } |
| 4714 | |
| 4715 | fn os_result_value(result os.Result) Value { |
| 4716 | return StructValue{ |
| 4717 | type_name: 'os.Result' |
| 4718 | fields: { |
| 4719 | 'exit_code': Value(i64(result.exit_code)) |
| 4720 | 'output': Value(result.output) |
| 4721 | } |
| 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | fn (mut e Eval) call_value_method(receiver Value, receiver_type_name string, method_name string, args []Value) !MethodCallResult { |
| 4726 | static_type_name := e.normalize_type_name(receiver_type_name) |
| 4727 | if value := e.direct_builtin_str_method_value(receiver, static_type_name, method_name, args) { |
| 4728 | return MethodCallResult{ |
| 4729 | value: value |
| 4730 | receiver: receiver |
| 4731 | } |
| 4732 | } |
| 4733 | if static_type_name.len > 0 { |
| 4734 | if target := e.resolve_method_target(static_type_name, method_name) { |
| 4735 | return e.call_method_target(receiver, target, args)! |
| 4736 | } |
| 4737 | } |
| 4738 | if value := e.direct_str_method_value(receiver, method_name, args) { |
| 4739 | return MethodCallResult{ |
| 4740 | value: value |
| 4741 | receiver: receiver |
| 4742 | } |
| 4743 | } |
| 4744 | if receiver is string { |
| 4745 | value := e.call_string_method(receiver, method_name, args)! |
| 4746 | return MethodCallResult{ |
| 4747 | value: value |
| 4748 | receiver: receiver |
| 4749 | } |
| 4750 | } |
| 4751 | if receiver is ArrayValue { |
| 4752 | value := e.call_array_method(receiver, method_name, args)! |
| 4753 | return MethodCallResult{ |
| 4754 | value: value |
| 4755 | receiver: receiver |
| 4756 | } |
| 4757 | } |
| 4758 | if receiver is MapValue { |
| 4759 | value := e.call_map_method(receiver, method_name, args)! |
| 4760 | return MethodCallResult{ |
| 4761 | value: value |
| 4762 | receiver_changed: method_name in ['clear', 'delete'] |
| 4763 | receiver: value |
| 4764 | } |
| 4765 | } |
| 4766 | if receiver is StructValue { |
| 4767 | if target := e.resolve_method_target(receiver.type_name, method_name) { |
| 4768 | return e.call_method_target(receiver, target, args)! |
| 4769 | } |
| 4770 | } |
| 4771 | if receiver is SumValue { |
| 4772 | result := e.call_value_method(receiver.payload, receiver.variant_name, method_name, args)! |
| 4773 | if result.receiver_changed { |
| 4774 | return MethodCallResult{ |
| 4775 | value: result.value |
| 4776 | receiver_changed: true |
| 4777 | receiver: SumValue{ |
| 4778 | type_name: receiver.type_name |
| 4779 | variant_name: receiver.variant_name |
| 4780 | payload: result.receiver |
| 4781 | } |
| 4782 | mutated_args: result.mutated_args |
| 4783 | } |
| 4784 | } |
| 4785 | return result |
| 4786 | } |
| 4787 | return error('v3.eval: unsupported method `${method_name}` on `${e.runtime_type_name(receiver)}`') |
| 4788 | } |
| 4789 | |
| 4790 | fn (mut e Eval) direct_builtin_str_method_value(receiver Value, static_type_name string, method_name string, args []Value) ?Value { |
| 4791 | if !is_builtin_str_receiver_type_name(static_type_name) { |
| 4792 | return none |
| 4793 | } |
| 4794 | return e.direct_str_method_value(receiver, method_name, args) |
| 4795 | } |
| 4796 | |
| 4797 | fn (mut e Eval) direct_str_method_value(receiver Value, method_name string, args []Value) ?Value { |
| 4798 | if method_name != 'str' || args.len != 0 { |
| 4799 | return none |
| 4800 | } |
| 4801 | match receiver { |
| 4802 | bool, i64, f64, string, EnumValue { |
| 4803 | return Value(e.value_string(receiver)) |
| 4804 | } |
| 4805 | StructValue { |
| 4806 | if _ := e.resolve_method_target(receiver.type_name, 'str') { |
| 4807 | return none |
| 4808 | } |
| 4809 | return Value(e.default_struct_string(receiver)) |
| 4810 | } |
| 4811 | else {} |
| 4812 | } |
| 4813 | |
| 4814 | return none |
| 4815 | } |
| 4816 | |
| 4817 | fn is_builtin_str_receiver_type_name(name string) bool { |
| 4818 | return name in ['bool', 'int', 'i8', 'i16', 'i32', 'i64', 'isize', 'u8', 'byte', 'u16', 'u32', |
| 4819 | 'u64', 'usize', 'f32', 'f64', 'rune', 'char', 'string'] |
| 4820 | } |
| 4821 | |
| 4822 | fn (mut e Eval) call_method_target(receiver Value, target FunctionDef, args []Value) !MethodCallResult { |
| 4823 | mut call_args := []Value{} |
| 4824 | call_args << receiver |
| 4825 | call_args << args |
| 4826 | result := e.call_function(target.module_name, target.name, call_args)! |
| 4827 | mut value := Value(void_value()) |
| 4828 | if result.values.len == 1 { |
| 4829 | value = result.values[0] |
| 4830 | } else { |
| 4831 | value = TupleValue{ |
| 4832 | values: result.values |
| 4833 | } |
| 4834 | } |
| 4835 | mut updated_receiver := receiver |
| 4836 | mut receiver_changed := false |
| 4837 | if 0 in result.mutated_args { |
| 4838 | receiver_changed = true |
| 4839 | updated_receiver = result.mutated_args[0] or { receiver } |
| 4840 | } |
| 4841 | mut mutated_args := map[int]Value{} |
| 4842 | for arg_index, mutated_value in result.mutated_args { |
| 4843 | if arg_index > 0 { |
| 4844 | mutated_args[arg_index - 1] = mutated_value |
| 4845 | } |
| 4846 | } |
| 4847 | return MethodCallResult{ |
| 4848 | value: value |
| 4849 | receiver_changed: receiver_changed |
| 4850 | receiver: updated_receiver |
| 4851 | mutated_args: mutated_args |
| 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | fn (e &Eval) resolve_method_target(type_name string, method_name string) ?FunctionDef { |
| 4856 | short_type := type_name.all_after_last('.') |
| 4857 | module_name := if type_name.contains('.') { |
| 4858 | type_name.all_before_last('.') |
| 4859 | } else { |
| 4860 | e.current_module_name() |
| 4861 | } |
| 4862 | names := ['${type_name}.${method_name}', '${short_type}.${method_name}'] |
| 4863 | if module_name in e.functions { |
| 4864 | for name in names { |
| 4865 | if name in e.functions[module_name] { |
| 4866 | return e.functions[module_name][name] |
| 4867 | } |
| 4868 | } |
| 4869 | } |
| 4870 | for mod in e.functions.keys() { |
| 4871 | for name in names { |
| 4872 | if name in e.functions[mod] { |
| 4873 | return e.functions[mod][name] |
| 4874 | } |
| 4875 | } |
| 4876 | } |
| 4877 | return none |
| 4878 | } |
| 4879 | |
| 4880 | fn (e &Eval) infix_operator_call_info(left Value, op flat.Op) ?InfixOperatorCallInfo { |
| 4881 | type_name := e.infix_operator_receiver_type_name(left) |
| 4882 | if type_name.len == 0 { |
| 4883 | return none |
| 4884 | } |
| 4885 | if op_name := infix_operator_symbol(op) { |
| 4886 | if target := e.resolve_method_target(type_name, op_name) { |
| 4887 | return InfixOperatorCallInfo{ |
| 4888 | target: target |
| 4889 | } |
| 4890 | } |
| 4891 | } |
| 4892 | match op { |
| 4893 | .gt { |
| 4894 | if target := e.resolve_method_target(type_name, '<') { |
| 4895 | return InfixOperatorCallInfo{ |
| 4896 | target: target |
| 4897 | reverse: true |
| 4898 | } |
| 4899 | } |
| 4900 | } |
| 4901 | .ge { |
| 4902 | if target := e.resolve_method_target(type_name, '<') { |
| 4903 | return InfixOperatorCallInfo{ |
| 4904 | target: target |
| 4905 | negate: true |
| 4906 | } |
| 4907 | } |
| 4908 | } |
| 4909 | .le { |
| 4910 | if target := e.resolve_method_target(type_name, '<') { |
| 4911 | return InfixOperatorCallInfo{ |
| 4912 | target: target |
| 4913 | reverse: true |
| 4914 | negate: true |
| 4915 | } |
| 4916 | } |
| 4917 | } |
| 4918 | .ne { |
| 4919 | if target := e.resolve_method_target(type_name, '==') { |
| 4920 | return InfixOperatorCallInfo{ |
| 4921 | target: target |
| 4922 | negate: true |
| 4923 | } |
| 4924 | } |
| 4925 | } |
| 4926 | else {} |
| 4927 | } |
| 4928 | |
| 4929 | return none |
| 4930 | } |
| 4931 | |
| 4932 | fn (e &Eval) infix_operator_receiver_type_name(value Value) string { |
| 4933 | return match value { |
| 4934 | StructValue { value.type_name } |
| 4935 | else { '' } |
| 4936 | } |
| 4937 | } |
| 4938 | |
| 4939 | fn infix_operator_symbol(op flat.Op) ?string { |
| 4940 | return match op { |
| 4941 | .plus { '+' } |
| 4942 | .minus { '-' } |
| 4943 | .mul { '*' } |
| 4944 | .div { '/' } |
| 4945 | .mod { '%' } |
| 4946 | .eq { '==' } |
| 4947 | .ne { '!=' } |
| 4948 | .lt { '<' } |
| 4949 | .gt { '>' } |
| 4950 | .le { '<=' } |
| 4951 | .ge { '>=' } |
| 4952 | else { none } |
| 4953 | } |
| 4954 | } |
| 4955 | |
| 4956 | fn (mut e Eval) apply_infix_operator_overload(op flat.Op, left Value, right Value) !MaybeValue { |
| 4957 | if call_info := e.infix_operator_call_info(left, op) { |
| 4958 | mut receiver := left |
| 4959 | mut arg := right |
| 4960 | if call_info.reverse { |
| 4961 | receiver = right |
| 4962 | arg = left |
| 4963 | } |
| 4964 | result := e.call_method_target(receiver, call_info.target, [arg])! |
| 4965 | mut value := result.value |
| 4966 | if call_info.negate { |
| 4967 | value = Value(!e.value_as_bool(value)!) |
| 4968 | } |
| 4969 | return MaybeValue{ |
| 4970 | found: true |
| 4971 | value: value |
| 4972 | } |
| 4973 | } |
| 4974 | return MaybeValue{} |
| 4975 | } |
| 4976 | |
| 4977 | fn (mut e Eval) call_string_method(receiver string, method_name string, args []Value) !Value { |
| 4978 | match method_name { |
| 4979 | 'int' { |
| 4980 | return Value(strconv.parse_int(receiver, 10, 64) or { i64(0) }) |
| 4981 | } |
| 4982 | 'i64' { |
| 4983 | return Value(strconv.parse_int(receiver, 10, 64) or { i64(0) }) |
| 4984 | } |
| 4985 | 'str' { |
| 4986 | return Value(receiver) |
| 4987 | } |
| 4988 | 'contains' { |
| 4989 | return Value(receiver.contains(e.expect_string_arg(args, 0)!)) |
| 4990 | } |
| 4991 | 'starts_with' { |
| 4992 | return Value(receiver.starts_with(e.expect_string_arg(args, 0)!)) |
| 4993 | } |
| 4994 | 'ends_with' { |
| 4995 | return Value(receiver.ends_with(e.expect_string_arg(args, 0)!)) |
| 4996 | } |
| 4997 | 'all_after_last' { |
| 4998 | return Value(receiver.all_after_last(e.expect_string_arg(args, 0)!)) |
| 4999 | } |
| 5000 | 'all_before_last' { |
| 5001 | return Value(receiver.all_before_last(e.expect_string_arg(args, 0)!)) |
| 5002 | } |
| 5003 | 'trim_space' { |
| 5004 | return Value(receiver.trim_space()) |
| 5005 | } |
| 5006 | else { |
| 5007 | return error('v3.eval: unsupported string method `${method_name}`') |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | |
| 5012 | fn (mut e Eval) call_array_method(receiver ArrayValue, method_name string, args []Value) !Value { |
| 5013 | match method_name { |
| 5014 | 'clone', 'move' { |
| 5015 | return ArrayValue{ |
| 5016 | elem_type_name: receiver.elem_type_name |
| 5017 | values: receiver.values.clone() |
| 5018 | } |
| 5019 | } |
| 5020 | 'first' { |
| 5021 | if receiver.values.len == 0 { |
| 5022 | return error('v3.eval: array.first on empty array') |
| 5023 | } |
| 5024 | return receiver.values[0] |
| 5025 | } |
| 5026 | 'last' { |
| 5027 | if receiver.values.len == 0 { |
| 5028 | return error('v3.eval: array.last on empty array') |
| 5029 | } |
| 5030 | return receiver.values[receiver.values.len - 1] |
| 5031 | } |
| 5032 | 'contains' { |
| 5033 | return Value(receiver.values.any(e.value_eq(it, args[0] or { void_value() }))) |
| 5034 | } |
| 5035 | 'index' { |
| 5036 | needle := args[0] or { void_value() } |
| 5037 | for i, item in receiver.values { |
| 5038 | if e.value_eq(item, needle) { |
| 5039 | return Value(i64(i)) |
| 5040 | } |
| 5041 | } |
| 5042 | return Value(i64(-1)) |
| 5043 | } |
| 5044 | 'join' { |
| 5045 | sep := e.expect_string_arg(args, 0) or { '' } |
| 5046 | return Value(receiver.values.map(e.value_string(it)).join(sep)) |
| 5047 | } |
| 5048 | else { |
| 5049 | return error('v3.eval: unsupported array method `${method_name}`') |
| 5050 | } |
| 5051 | } |
| 5052 | } |
| 5053 | |
| 5054 | fn (mut e Eval) call_map_method(receiver MapValue, method_name string, args []Value) !Value { |
| 5055 | match method_name { |
| 5056 | 'keys' { |
| 5057 | return e.map_keys(receiver) |
| 5058 | } |
| 5059 | 'values' { |
| 5060 | return e.map_values(receiver) |
| 5061 | } |
| 5062 | 'clone', 'move' { |
| 5063 | return e.map_clone(receiver) |
| 5064 | } |
| 5065 | 'clear' { |
| 5066 | return MapValue{ |
| 5067 | key_type_name: receiver.key_type_name |
| 5068 | value_type_name: receiver.value_type_name |
| 5069 | default_value: receiver.default_value |
| 5070 | } |
| 5071 | } |
| 5072 | 'delete' { |
| 5073 | if args.len == 0 { |
| 5074 | return receiver |
| 5075 | } |
| 5076 | return e.map_delete_value(receiver, args[0]) |
| 5077 | } |
| 5078 | else { |
| 5079 | return error('v3.eval: unsupported map method `${method_name}`') |
| 5080 | } |
| 5081 | } |
| 5082 | } |
| 5083 | |
| 5084 | fn (mut e Eval) apply_infix(op flat.Op, left Value, right Value) !Value { |
| 5085 | overloaded := e.apply_infix_operator_overload(op, left, right)! |
| 5086 | if overloaded.found { |
| 5087 | return overloaded.value |
| 5088 | } |
| 5089 | match op { |
| 5090 | .plus { |
| 5091 | if left is string || right is string { |
| 5092 | return Value(e.value_string(left) + e.value_string(right)) |
| 5093 | } |
| 5094 | if left is f64 || right is f64 { |
| 5095 | return Value(e.value_as_f64(left)! + e.value_as_f64(right)!) |
| 5096 | } |
| 5097 | return Value(e.value_as_int(left)! + e.value_as_int(right)!) |
| 5098 | } |
| 5099 | .minus { |
| 5100 | if left is f64 || right is f64 { |
| 5101 | return Value(e.value_as_f64(left)! - e.value_as_f64(right)!) |
| 5102 | } |
| 5103 | return Value(e.value_as_int(left)! - e.value_as_int(right)!) |
| 5104 | } |
| 5105 | .mul { |
| 5106 | if left is f64 || right is f64 { |
| 5107 | return Value(e.value_as_f64(left)! * e.value_as_f64(right)!) |
| 5108 | } |
| 5109 | return Value(e.value_as_int(left)! * e.value_as_int(right)!) |
| 5110 | } |
| 5111 | .div { |
| 5112 | if left is f64 || right is f64 { |
| 5113 | return Value(e.value_as_f64(left)! / e.value_as_f64(right)!) |
| 5114 | } |
| 5115 | return Value(e.value_as_int(left)! / e.value_as_int(right)!) |
| 5116 | } |
| 5117 | .mod { |
| 5118 | return Value(e.value_as_int(left)! % e.value_as_int(right)!) |
| 5119 | } |
| 5120 | .eq { |
| 5121 | return Value(e.value_eq(left, right)) |
| 5122 | } |
| 5123 | .ne { |
| 5124 | return Value(!e.value_eq(left, right)) |
| 5125 | } |
| 5126 | .lt { |
| 5127 | return Value(e.compare_values(left, right) < 0) |
| 5128 | } |
| 5129 | .gt { |
| 5130 | return Value(e.compare_values(left, right) > 0) |
| 5131 | } |
| 5132 | .le { |
| 5133 | return Value(e.compare_values(left, right) <= 0) |
| 5134 | } |
| 5135 | .ge { |
| 5136 | return Value(e.compare_values(left, right) >= 0) |
| 5137 | } |
| 5138 | .amp { |
| 5139 | return Value(e.value_as_int(left)! & e.value_as_int(right)!) |
| 5140 | } |
| 5141 | .pipe { |
| 5142 | return Value(e.value_as_int(left)! | e.value_as_int(right)!) |
| 5143 | } |
| 5144 | .xor { |
| 5145 | return Value(e.value_as_int(left)! ^ e.value_as_int(right)!) |
| 5146 | } |
| 5147 | .left_shift { |
| 5148 | mut result := e.value_as_int(left)! |
| 5149 | for _ in 0 .. int(e.value_as_int(right)!) { |
| 5150 | result *= 2 |
| 5151 | } |
| 5152 | return Value(result) |
| 5153 | } |
| 5154 | .right_shift { |
| 5155 | return Value(e.value_as_int(left)! >> e.value_as_int(right)!) |
| 5156 | } |
| 5157 | .right_shift_unsigned { |
| 5158 | return Value(i64(u64(e.value_as_int(left)!) >>> e.value_as_int(right)!)) |
| 5159 | } |
| 5160 | else { |
| 5161 | return error('v3.eval: unsupported infix operator `${op}`') |
| 5162 | } |
| 5163 | } |
| 5164 | } |
| 5165 | |
| 5166 | fn (e &Eval) value_in(left Value, right Value) bool { |
| 5167 | match right { |
| 5168 | ArrayValue { |
| 5169 | return right.values.any(e.value_eq(it, left)) |
| 5170 | } |
| 5171 | MapValue { |
| 5172 | return e.map_contains_key(right, left) |
| 5173 | } |
| 5174 | string { |
| 5175 | return right.contains(e.value_string(left)) |
| 5176 | } |
| 5177 | RangeValue { |
| 5178 | v := e.value_as_int(left) or { return false } |
| 5179 | if right.inclusive { |
| 5180 | return v >= right.start && v <= right.end |
| 5181 | } |
| 5182 | return v >= right.start && v < right.end |
| 5183 | } |
| 5184 | else { |
| 5185 | return false |
| 5186 | } |
| 5187 | } |
| 5188 | } |
| 5189 | |
| 5190 | fn (e &Eval) compare_values(left Value, right Value) int { |
| 5191 | if left is string && right is string { |
| 5192 | return left.compare(right) |
| 5193 | } |
| 5194 | lf := e.value_as_f64(left) or { 0.0 } |
| 5195 | rf := e.value_as_f64(right) or { 0.0 } |
| 5196 | if lf < rf { |
| 5197 | return -1 |
| 5198 | } |
| 5199 | if lf > rf { |
| 5200 | return 1 |
| 5201 | } |
| 5202 | return 0 |
| 5203 | } |
| 5204 | |
| 5205 | fn (e &Eval) value_eq(left Value, right Value) bool { |
| 5206 | match left { |
| 5207 | bool { |
| 5208 | return right is bool && left == right |
| 5209 | } |
| 5210 | i64 { |
| 5211 | if right is f64 { |
| 5212 | return f64(left) == right |
| 5213 | } |
| 5214 | return e.value_as_int(right) or { return false } == left |
| 5215 | } |
| 5216 | EnumValue { |
| 5217 | if right is EnumValue { |
| 5218 | return e.type_name_matches(left.type_name, right.type_name) |
| 5219 | && left.value == right.value |
| 5220 | } |
| 5221 | return e.value_as_int(right) or { return false } == left.value |
| 5222 | } |
| 5223 | f64 { |
| 5224 | return e.value_as_f64(right) or { return false } == left |
| 5225 | } |
| 5226 | string { |
| 5227 | return right is string && left == right |
| 5228 | } |
| 5229 | VoidValue { |
| 5230 | return right is VoidValue |
| 5231 | } |
| 5232 | TypeValue { |
| 5233 | return right is TypeValue && left.name == right.name |
| 5234 | } |
| 5235 | ModuleValue { |
| 5236 | return right is ModuleValue && left.name == right.name |
| 5237 | } |
| 5238 | ArrayValue { |
| 5239 | if right !is ArrayValue { |
| 5240 | return false |
| 5241 | } |
| 5242 | right_arr := right as ArrayValue |
| 5243 | if left.values.len != right_arr.values.len { |
| 5244 | return false |
| 5245 | } |
| 5246 | for i, item in left.values { |
| 5247 | if !e.value_eq(item, right_arr.values[i]) { |
| 5248 | return false |
| 5249 | } |
| 5250 | } |
| 5251 | return true |
| 5252 | } |
| 5253 | StructValue { |
| 5254 | if right !is StructValue { |
| 5255 | return false |
| 5256 | } |
| 5257 | right_struct := right as StructValue |
| 5258 | if left.type_name != right_struct.type_name |
| 5259 | || left.fields.len != right_struct.fields.len { |
| 5260 | return false |
| 5261 | } |
| 5262 | for name, value in left.fields { |
| 5263 | right_value := right_struct.fields[name] or { return false } |
| 5264 | if !e.value_eq(value, right_value) { |
| 5265 | return false |
| 5266 | } |
| 5267 | } |
| 5268 | return true |
| 5269 | } |
| 5270 | SumValue { |
| 5271 | return right is SumValue && left.type_name == right.type_name |
| 5272 | && left.variant_name == right.variant_name |
| 5273 | && e.value_eq(left.payload, right.payload) |
| 5274 | } |
| 5275 | else { |
| 5276 | return false |
| 5277 | } |
| 5278 | } |
| 5279 | } |
| 5280 | |
| 5281 | fn (e &Eval) map_lookup(receiver MapValue, key Value) (Value, bool) { |
| 5282 | typed_key := e.adapt_value_to_type_name(key, receiver.key_type_name) |
| 5283 | for entry in receiver.entries { |
| 5284 | if e.value_eq(entry.key, typed_key) { |
| 5285 | return entry.value, true |
| 5286 | } |
| 5287 | } |
| 5288 | return receiver.default_value, false |
| 5289 | } |
| 5290 | |
| 5291 | fn (e &Eval) map_contains_key(receiver MapValue, key Value) bool { |
| 5292 | _, ok := e.map_lookup(receiver, key) |
| 5293 | return ok |
| 5294 | } |
| 5295 | |
| 5296 | fn (e &Eval) map_set_value(receiver MapValue, key Value, value Value) MapValue { |
| 5297 | entry_key := e.adapt_value_to_type_name(key, receiver.key_type_name) |
| 5298 | entry_value := e.adapt_value_to_type_name(value, receiver.value_type_name) |
| 5299 | mut m := MapValue{ |
| 5300 | key_type_name: receiver.key_type_name |
| 5301 | value_type_name: receiver.value_type_name |
| 5302 | default_value: receiver.default_value |
| 5303 | entries: receiver.entries.clone() |
| 5304 | } |
| 5305 | for i, entry in m.entries { |
| 5306 | if e.value_eq(entry.key, entry_key) { |
| 5307 | m.entries[i].value = entry_value |
| 5308 | return m |
| 5309 | } |
| 5310 | } |
| 5311 | m.entries << MapEntry{ |
| 5312 | key: entry_key |
| 5313 | value: entry_value |
| 5314 | } |
| 5315 | return m |
| 5316 | } |
| 5317 | |
| 5318 | fn (e &Eval) map_delete_value(receiver MapValue, key Value) MapValue { |
| 5319 | mut m := receiver |
| 5320 | typed_key := e.adapt_value_to_type_name(key, receiver.key_type_name) |
| 5321 | for i, entry in m.entries { |
| 5322 | if e.value_eq(entry.key, typed_key) { |
| 5323 | m.entries.delete(i) |
| 5324 | break |
| 5325 | } |
| 5326 | } |
| 5327 | return m |
| 5328 | } |
| 5329 | |
| 5330 | fn (e &Eval) map_keys(receiver MapValue) ArrayValue { |
| 5331 | return ArrayValue{ |
| 5332 | elem_type_name: receiver.key_type_name |
| 5333 | values: receiver.entries.map(it.key) |
| 5334 | } |
| 5335 | } |
| 5336 | |
| 5337 | fn (e &Eval) map_values(receiver MapValue) ArrayValue { |
| 5338 | return ArrayValue{ |
| 5339 | elem_type_name: receiver.value_type_name |
| 5340 | values: receiver.entries.map(it.value) |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | fn (e &Eval) map_clone(receiver MapValue) MapValue { |
| 5345 | return MapValue{ |
| 5346 | key_type_name: receiver.key_type_name |
| 5347 | value_type_name: receiver.value_type_name |
| 5348 | default_value: receiver.default_value |
| 5349 | entries: receiver.entries.clone() |
| 5350 | } |
| 5351 | } |
| 5352 | |
| 5353 | fn (mut e Eval) zero_value_like(value Value) Value { |
| 5354 | match value { |
| 5355 | bool { |
| 5356 | return Value(false) |
| 5357 | } |
| 5358 | i64 { |
| 5359 | return Value(i64(0)) |
| 5360 | } |
| 5361 | f64 { |
| 5362 | return Value(0.0) |
| 5363 | } |
| 5364 | EnumValue { |
| 5365 | return EnumValue{ |
| 5366 | type_name: value.type_name |
| 5367 | } |
| 5368 | } |
| 5369 | string { |
| 5370 | return Value('') |
| 5371 | } |
| 5372 | ArrayValue { |
| 5373 | return ArrayValue{ |
| 5374 | elem_type_name: value.elem_type_name |
| 5375 | } |
| 5376 | } |
| 5377 | MapValue { |
| 5378 | return MapValue{ |
| 5379 | key_type_name: value.key_type_name |
| 5380 | value_type_name: value.value_type_name |
| 5381 | default_value: value.default_value |
| 5382 | } |
| 5383 | } |
| 5384 | StructValue { |
| 5385 | return e.zero_struct_value(value.type_name) |
| 5386 | } |
| 5387 | SumValue { |
| 5388 | return SumValue{ |
| 5389 | type_name: value.type_name |
| 5390 | variant_name: value.variant_name |
| 5391 | payload: e.zero_value_like(value.payload) |
| 5392 | } |
| 5393 | } |
| 5394 | else { |
| 5395 | return void_value() |
| 5396 | } |
| 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | fn (mut e Eval) zero_value_for_type_name(type_name string) Value { |
| 5401 | return e.zero_value_for_type_name_in_module(type_name, e.current_module_name()) |
| 5402 | } |
| 5403 | |
| 5404 | fn (mut e Eval) zero_value_for_type_name_in_module(type_name string, module_name string) Value { |
| 5405 | name := type_name.trim_left('&') |
| 5406 | if name == '' || name == 'void' { |
| 5407 | return void_value() |
| 5408 | } |
| 5409 | if alias := e.type_alias_info_in_module(name, module_name) { |
| 5410 | return e.zero_value_for_type_name_in_module(alias.target, alias.module_name) |
| 5411 | } |
| 5412 | if name == 'bool' { |
| 5413 | return Value(false) |
| 5414 | } |
| 5415 | if name in ['f32', 'f64'] { |
| 5416 | return Value(0.0) |
| 5417 | } |
| 5418 | if name == 'string' { |
| 5419 | return Value('') |
| 5420 | } |
| 5421 | if name in ['int', 'i8', 'i16', 'i32', 'i64', 'u8', 'byte', 'u16', 'u32', 'u64', 'isize', 'usize', |
| 5422 | 'rune', 'char'] { |
| 5423 | return Value(i64(0)) |
| 5424 | } |
| 5425 | if name.starts_with('[]') { |
| 5426 | elem_type := e.qualify_nested_type_name(module_name, name[2..]) |
| 5427 | return ArrayValue{ |
| 5428 | elem_type_name: elem_type |
| 5429 | } |
| 5430 | } |
| 5431 | if is_fixed_array_type_name(name) { |
| 5432 | elem_type := e.qualify_nested_type_name(module_name, fixed_array_elem_type_name(name)) |
| 5433 | len := e.fixed_array_len_in_module(name, module_name) or { 0 } |
| 5434 | return ArrayValue{ |
| 5435 | elem_type_name: elem_type |
| 5436 | values: []Value{len: len, cap: len, init: e.zero_value_for_type_name_in_module(elem_type, |
| 5437 | module_name)} |
| 5438 | } |
| 5439 | } |
| 5440 | if name.starts_with('map[') { |
| 5441 | key_type, value_type := split_map_type(name) |
| 5442 | qualified_key_type := e.qualify_nested_type_name(module_name, key_type) |
| 5443 | qualified_value_type := e.qualify_nested_type_name(module_name, value_type) |
| 5444 | return MapValue{ |
| 5445 | key_type_name: qualified_key_type |
| 5446 | value_type_name: qualified_value_type |
| 5447 | default_value: e.zero_value_for_type_name_in_module(qualified_value_type, module_name) |
| 5448 | } |
| 5449 | } |
| 5450 | enum_name := e.qualify_type_name(module_name, name) |
| 5451 | if fields := e.enum_fields[enum_name] { |
| 5452 | if fields.len > 0 { |
| 5453 | if value := e.lookup_enum_value(enum_name, fields[0]) { |
| 5454 | return value |
| 5455 | } |
| 5456 | } |
| 5457 | return EnumValue{ |
| 5458 | type_name: enum_name |
| 5459 | } |
| 5460 | } |
| 5461 | sum_name := e.qualify_type_name(module_name, name) |
| 5462 | if sum_name in e.sum_types { |
| 5463 | return SumValue{ |
| 5464 | type_name: sum_name |
| 5465 | payload: void_value() |
| 5466 | } |
| 5467 | } |
| 5468 | if name in e.sum_types { |
| 5469 | return SumValue{ |
| 5470 | type_name: name |
| 5471 | payload: void_value() |
| 5472 | } |
| 5473 | } |
| 5474 | struct_name := e.resolve_struct_type_name_in_module(name, module_name) |
| 5475 | if struct_name in e.structs { |
| 5476 | return e.zero_struct_value(struct_name) |
| 5477 | } |
| 5478 | return void_value() |
| 5479 | } |
| 5480 | |
| 5481 | fn (e &Eval) qualify_nested_type_name(module_name string, type_name string) string { |
| 5482 | name := type_name.trim_space() |
| 5483 | if name == '' { |
| 5484 | return '' |
| 5485 | } |
| 5486 | if name.starts_with('[]') { |
| 5487 | return '[]${e.qualify_nested_type_name(module_name, name[2..])}' |
| 5488 | } |
| 5489 | if name.starts_with('map[') { |
| 5490 | key_type, value_type := split_map_type(name) |
| 5491 | return 'map[${e.qualify_nested_type_name(module_name, key_type)}]${e.qualify_nested_type_name(module_name, |
| 5492 | value_type)}' |
| 5493 | } |
| 5494 | if name.starts_with('?') || name.starts_with('!') { |
| 5495 | return '${name[..1]}${e.qualify_nested_type_name(module_name, name[1..])}' |
| 5496 | } |
| 5497 | return e.qualify_type_name(module_name, name) |
| 5498 | } |
| 5499 | |
| 5500 | fn (mut e Eval) zero_struct_value(type_name string) StructValue { |
| 5501 | name := e.resolve_struct_type_name(type_name) |
| 5502 | mut fields := map[string]Value{} |
| 5503 | info := e.struct_info(name) |
| 5504 | for field in info.fields { |
| 5505 | fields[field.name] = if int(field.default_node) >= 0 { |
| 5506 | e.eval_struct_field_default(field) |
| 5507 | } else { |
| 5508 | e.zero_value_for_type_name_in_module(field.typ, info.module_name) |
| 5509 | } |
| 5510 | } |
| 5511 | return StructValue{ |
| 5512 | type_name: type_name |
| 5513 | fields: fields |
| 5514 | } |
| 5515 | } |
| 5516 | |
| 5517 | fn (mut e Eval) eval_struct_field_default(field FieldInfo) Value { |
| 5518 | e.call_stack << CallFrame{ |
| 5519 | module_name: field.module_name |
| 5520 | file_name: field.file_name |
| 5521 | fn_name: '<field ${field.name}>' |
| 5522 | } |
| 5523 | value := e.eval_expr_expected(field.default_node, field.typ) or { |
| 5524 | e.call_stack.delete(e.call_stack.len - 1) |
| 5525 | return e.zero_value_for_type_name_in_module(field.typ, field.module_name) |
| 5526 | } |
| 5527 | adapted := e.adapt_value_to_type_name(value, field.typ) |
| 5528 | e.call_stack.delete(e.call_stack.len - 1) |
| 5529 | return adapted |
| 5530 | } |
| 5531 | |
| 5532 | fn (e &Eval) struct_fields(type_name string) []FieldInfo { |
| 5533 | return e.struct_info(type_name).fields |
| 5534 | } |
| 5535 | |
| 5536 | fn (e &Eval) struct_field_type_name(value StructValue, field_name string) string { |
| 5537 | return e.struct_field_type_name_by_type(value.type_name, field_name) |
| 5538 | } |
| 5539 | |
| 5540 | fn (e &Eval) struct_field_type_name_by_type(type_name string, field_name string) string { |
| 5541 | info := e.struct_info(type_name) |
| 5542 | for field in info.fields { |
| 5543 | if field.name == field_name { |
| 5544 | return e.qualify_nested_type_name(field.module_name, field.typ) |
| 5545 | } |
| 5546 | } |
| 5547 | return '' |
| 5548 | } |
| 5549 | |
| 5550 | fn (e &Eval) struct_info(type_name string) StructInfo { |
| 5551 | name := e.resolve_struct_type_name(type_name) |
| 5552 | if name in e.structs { |
| 5553 | return e.structs[name] |
| 5554 | } |
| 5555 | return StructInfo{} |
| 5556 | } |
| 5557 | |
| 5558 | fn (e &Eval) resolve_struct_type_name(type_name string) string { |
| 5559 | return e.resolve_struct_type_name_in_module(type_name, e.current_module_name()) |
| 5560 | } |
| 5561 | |
| 5562 | fn (e &Eval) resolve_struct_type_name_in_module(type_name string, module_name string) string { |
| 5563 | if type_name.contains('.') { |
| 5564 | if type_name in e.structs { |
| 5565 | return type_name |
| 5566 | } |
| 5567 | return type_name |
| 5568 | } |
| 5569 | qualified := e.qualify_type_name(module_name, type_name) |
| 5570 | if qualified in e.structs { |
| 5571 | return qualified |
| 5572 | } |
| 5573 | if type_name in e.structs { |
| 5574 | return type_name |
| 5575 | } |
| 5576 | short := type_name.all_after_last('.') |
| 5577 | if short in e.structs { |
| 5578 | return short |
| 5579 | } |
| 5580 | return type_name |
| 5581 | } |
| 5582 | |
| 5583 | fn (e &Eval) adapt_value_to_type_name(value Value, type_name string) Value { |
| 5584 | if alias := e.type_alias_info_in_module(type_name, e.current_module_name()) { |
| 5585 | return e.adapt_value_to_type_name(value, e.qualify_type_name(alias.module_name, |
| 5586 | alias.target)) |
| 5587 | } |
| 5588 | if type_name.starts_with('?') { |
| 5589 | if value is VoidValue || e.is_option_like_value(value) { |
| 5590 | return value |
| 5591 | } |
| 5592 | inner_type := type_name[1..] |
| 5593 | data := if inner_type.len > 0 { |
| 5594 | e.adapt_value_to_type_name(value, inner_type) |
| 5595 | } else { |
| 5596 | value |
| 5597 | } |
| 5598 | return StructValue{ |
| 5599 | type_name: 'Option' |
| 5600 | fields: { |
| 5601 | 'state': Value(i64(0)) |
| 5602 | 'err': Value('') |
| 5603 | 'data': data |
| 5604 | } |
| 5605 | } |
| 5606 | } |
| 5607 | if type_name.starts_with('!') { |
| 5608 | if e.is_option_like_value(value) { |
| 5609 | return value |
| 5610 | } |
| 5611 | inner_type := type_name[1..] |
| 5612 | data := if inner_type.len > 0 { |
| 5613 | e.adapt_value_to_type_name(value, inner_type) |
| 5614 | } else { |
| 5615 | value |
| 5616 | } |
| 5617 | return StructValue{ |
| 5618 | type_name: 'Result' |
| 5619 | fields: { |
| 5620 | 'is_error': Value(false) |
| 5621 | 'err': Value('') |
| 5622 | 'data': data |
| 5623 | } |
| 5624 | } |
| 5625 | } |
| 5626 | if type_name.starts_with('[]') && value is ArrayValue { |
| 5627 | elem_type_name := e.qualify_nested_type_name(e.current_module_name(), type_name[2..]) |
| 5628 | mut arr := value |
| 5629 | arr.elem_type_name = elem_type_name |
| 5630 | mut values := []Value{cap: arr.values.cap} |
| 5631 | for item in arr.values { |
| 5632 | values << e.adapt_value_to_type_name(item, elem_type_name) |
| 5633 | } |
| 5634 | arr.values = values |
| 5635 | return arr |
| 5636 | } |
| 5637 | if type_name.starts_with('map[') && value is MapValue { |
| 5638 | key_type_name, value_type_name := split_map_type(type_name) |
| 5639 | qualified_key_type_name := e.qualify_nested_type_name(e.current_module_name(), |
| 5640 | key_type_name) |
| 5641 | qualified_value_type_name := e.qualify_nested_type_name(e.current_module_name(), |
| 5642 | value_type_name) |
| 5643 | mut m := MapValue{ |
| 5644 | key_type_name: qualified_key_type_name |
| 5645 | value_type_name: qualified_value_type_name |
| 5646 | default_value: e.adapt_value_to_type_name(value.default_value, |
| 5647 | qualified_value_type_name) |
| 5648 | } |
| 5649 | for entry in value.entries { |
| 5650 | m = e.map_set_value(m, entry.key, entry.value) |
| 5651 | } |
| 5652 | return m |
| 5653 | } |
| 5654 | target_type_name := e.normalize_type_name(type_name) |
| 5655 | if target_type_name in e.enum_fields { |
| 5656 | return e.enum_value(target_type_name, value) |
| 5657 | } |
| 5658 | if type_name in e.enum_fields { |
| 5659 | return e.enum_value(type_name, value) |
| 5660 | } |
| 5661 | if target_type_name in e.sum_types && !e.value_matches_type_name(value, target_type_name) { |
| 5662 | return e.wrap_sum_value(target_type_name, value) |
| 5663 | } |
| 5664 | if type_name in e.sum_types && !e.value_matches_type_name(value, type_name) { |
| 5665 | return e.wrap_sum_value(type_name, value) |
| 5666 | } |
| 5667 | return value |
| 5668 | } |
| 5669 | |
| 5670 | fn (e &Eval) is_option_like_value(value Value) bool { |
| 5671 | if value is StructValue { |
| 5672 | return 'data' in value.fields && ('state' in value.fields || 'is_error' in value.fields) |
| 5673 | } |
| 5674 | return false |
| 5675 | } |
| 5676 | |
| 5677 | fn (e &Eval) wrap_sum_value(type_name string, value Value) Value { |
| 5678 | return SumValue{ |
| 5679 | type_name: type_name |
| 5680 | variant_name: e.runtime_type_name(value) |
| 5681 | payload: value |
| 5682 | } |
| 5683 | } |
| 5684 | |
| 5685 | fn (e &Eval) unwrap_sum_cast_value(value Value, type_name string) Value { |
| 5686 | if value is SumValue { |
| 5687 | if !e.type_name_matches(value.type_name, type_name) |
| 5688 | && (e.type_name_matches(value.variant_name, type_name) |
| 5689 | || e.value_matches_type_name(value.payload, type_name)) { |
| 5690 | return value.payload |
| 5691 | } |
| 5692 | } |
| 5693 | return value |
| 5694 | } |
| 5695 | |
| 5696 | fn (e &Eval) cast_value(value Value, type_name string) !Value { |
| 5697 | return e.cast_value_in_module(value, type_name, e.current_module_name()) |
| 5698 | } |
| 5699 | |
| 5700 | fn (e &Eval) cast_value_in_module(value Value, type_name string, module_name string) !Value { |
| 5701 | name := type_name.trim_left('&') |
| 5702 | if name == '' { |
| 5703 | return value |
| 5704 | } |
| 5705 | if alias := e.type_alias_info_in_module(name, module_name) { |
| 5706 | return e.cast_value_in_module(value, alias.target, alias.module_name) |
| 5707 | } |
| 5708 | target_name := e.qualify_type_name(module_name, name) |
| 5709 | source := e.unwrap_sum_cast_value(value, target_name) |
| 5710 | if name in ['int', 'i8', 'i16', 'i32', 'i64', 'u8', 'byte', 'u16', 'u32', 'u64', 'isize', 'usize', |
| 5711 | 'rune', 'char'] { |
| 5712 | return Value(e.value_as_int(source)!) |
| 5713 | } |
| 5714 | if name in ['f32', 'f64'] { |
| 5715 | return Value(e.value_as_f64(source)!) |
| 5716 | } |
| 5717 | if name == 'bool' { |
| 5718 | return Value(e.value_as_bool(source)!) |
| 5719 | } |
| 5720 | if name == 'string' { |
| 5721 | return Value(e.value_string(source)) |
| 5722 | } |
| 5723 | if target_name in e.enum_fields { |
| 5724 | return e.enum_value(target_name, source) |
| 5725 | } |
| 5726 | if name in e.enum_fields { |
| 5727 | return e.enum_value(name, source) |
| 5728 | } |
| 5729 | if name.starts_with('?') { |
| 5730 | inner_type := name[1..] |
| 5731 | data := if inner_type.len > 0 { |
| 5732 | e.adapt_value_to_type_name(source, inner_type) |
| 5733 | } else { |
| 5734 | source |
| 5735 | } |
| 5736 | return StructValue{ |
| 5737 | type_name: 'Option' |
| 5738 | fields: { |
| 5739 | 'state': Value(i64(0)) |
| 5740 | 'err': Value('') |
| 5741 | 'data': data |
| 5742 | } |
| 5743 | } |
| 5744 | } |
| 5745 | if target_name in e.sum_types || name in e.sum_types { |
| 5746 | sum_name := if target_name in e.sum_types { target_name } else { name } |
| 5747 | if e.value_matches_type_name(source, sum_name) { |
| 5748 | return source |
| 5749 | } |
| 5750 | return e.wrap_sum_value(sum_name, source) |
| 5751 | } |
| 5752 | return source |
| 5753 | } |
| 5754 | |
| 5755 | fn (e &Eval) type_alias_info_in_module(type_name string, module_name string) ?TypeAliasInfo { |
| 5756 | name := type_name.trim_left('&') |
| 5757 | if name == '' { |
| 5758 | return none |
| 5759 | } |
| 5760 | qualified := e.qualify_type_name(module_name, name) |
| 5761 | if alias := e.type_aliases[qualified] { |
| 5762 | return alias |
| 5763 | } |
| 5764 | if alias := e.type_aliases[name] { |
| 5765 | return alias |
| 5766 | } |
| 5767 | return none |
| 5768 | } |
| 5769 | |
| 5770 | fn (e &Eval) value_matches_type_name(value Value, target_name string) bool { |
| 5771 | name := e.normalize_type_name(target_name) |
| 5772 | match value { |
| 5773 | bool { |
| 5774 | return name == 'bool' |
| 5775 | } |
| 5776 | i64 { |
| 5777 | return name in ['int', 'i8', 'i16', 'i32', 'i64', 'u8', 'byte', 'u16', 'u32', 'u64', |
| 5778 | 'isize', 'usize', 'rune', 'char'] |
| 5779 | } |
| 5780 | EnumValue { |
| 5781 | return e.type_name_matches(value.type_name, name) |
| 5782 | } |
| 5783 | f64 { |
| 5784 | return name in ['f32', 'f64'] |
| 5785 | } |
| 5786 | string { |
| 5787 | return name == 'string' |
| 5788 | } |
| 5789 | ArrayValue { |
| 5790 | if name == 'array' { |
| 5791 | return true |
| 5792 | } |
| 5793 | if !name.starts_with('[]') { |
| 5794 | return false |
| 5795 | } |
| 5796 | return e.type_name_matches(value.elem_type_name, name[2..]) |
| 5797 | } |
| 5798 | MapValue { |
| 5799 | if !name.starts_with('map[') { |
| 5800 | return false |
| 5801 | } |
| 5802 | key_type, value_type := split_map_type(name) |
| 5803 | return e.type_name_matches(value.key_type_name, key_type) |
| 5804 | && e.type_name_matches(value.value_type_name, value_type) |
| 5805 | } |
| 5806 | StructValue { |
| 5807 | return e.type_name_matches(value.type_name, name) |
| 5808 | } |
| 5809 | SumValue { |
| 5810 | return e.type_name_matches(value.type_name, name) |
| 5811 | || e.type_name_matches(value.variant_name, name) |
| 5812 | || e.value_matches_type_name(value.payload, name) |
| 5813 | } |
| 5814 | TypeValue { |
| 5815 | return name == 'Type' || name == 'types.Type' |
| 5816 | } |
| 5817 | else { |
| 5818 | return false |
| 5819 | } |
| 5820 | } |
| 5821 | } |
| 5822 | |
| 5823 | fn (e &Eval) type_name_matches(actual string, expected string) bool { |
| 5824 | return e.normalize_type_name(actual) == e.normalize_type_name(expected) |
| 5825 | } |
| 5826 | |
| 5827 | fn (e &Eval) type_value_module_name(value TypeValue) string { |
| 5828 | if value.name.contains('.') { |
| 5829 | return value.name.all_before_last('.') |
| 5830 | } |
| 5831 | return e.current_module_name() |
| 5832 | } |
| 5833 | |
| 5834 | fn is_builtin_type_name(name string) bool { |
| 5835 | return name in ['bool', 'int', 'i8', 'i16', 'i32', 'i64', 'isize', 'u8', 'byte', 'u16', 'u32', |
| 5836 | 'u64', 'usize', 'f32', 'f64', 'rune', 'char', 'string', 'void', 'voidptr', 'charptr', |
| 5837 | 'byteptr', 'array'] |
| 5838 | } |
| 5839 | |
| 5840 | fn (e &Eval) qualify_type_name(module_name string, type_name string) string { |
| 5841 | name := e.resolve_import_alias_type_name(type_name) |
| 5842 | if name == '' || name.contains('.') || name.starts_with('[]') || name.starts_with('map[') |
| 5843 | || is_builtin_type_name(name) { |
| 5844 | return name |
| 5845 | } |
| 5846 | if module_name != '' && module_name != 'main' && module_name != 'builtin' { |
| 5847 | return '${module_name}.${name}' |
| 5848 | } |
| 5849 | return name |
| 5850 | } |
| 5851 | |
| 5852 | fn (e &Eval) resolve_import_alias_type_name(type_name string) string { |
| 5853 | if !type_name.contains('.') { |
| 5854 | return type_name |
| 5855 | } |
| 5856 | mod_alias := type_name.all_before('.') |
| 5857 | real_module := e.resolve_module_name(mod_alias) |
| 5858 | if real_module == mod_alias { |
| 5859 | return type_name |
| 5860 | } |
| 5861 | return '${real_module}.${type_name.all_after('.')}' |
| 5862 | } |
| 5863 | |
| 5864 | fn (e &Eval) normalize_type_name(type_name string) string { |
| 5865 | name := type_name.trim_left('&') |
| 5866 | if name == '' { |
| 5867 | return '' |
| 5868 | } |
| 5869 | return e.qualify_type_name(e.current_module_name(), name) |
| 5870 | } |
| 5871 | |
| 5872 | fn (e &Eval) sizeof_type_name(name string) i64 { |
| 5873 | return match name { |
| 5874 | 'bool', 'i8', 'u8', 'byte', 'char' { i64(1) } |
| 5875 | 'i16', 'u16' { i64(2) } |
| 5876 | 'int', 'i32', 'u32', 'rune', 'f32' { i64(4) } |
| 5877 | 'i64', 'u64', 'isize', 'usize', 'f64' { i64(8) } |
| 5878 | else { i64(8) } |
| 5879 | } |
| 5880 | } |
| 5881 | |
| 5882 | fn (e &Eval) expect_string_arg(args []Value, index int) !string { |
| 5883 | if index >= args.len { |
| 5884 | return error('v3.eval: missing argument ${index}') |
| 5885 | } |
| 5886 | return e.value_string(args[index]) |
| 5887 | } |
| 5888 | |
| 5889 | fn (e &Eval) value_as_bool(value Value) !bool { |
| 5890 | match value { |
| 5891 | bool { return value } |
| 5892 | i64 { return value != 0 } |
| 5893 | EnumValue { return value.value != 0 } |
| 5894 | VoidValue { return false } |
| 5895 | else { return error('v3.eval: `${e.runtime_type_name(value)}` can not be used as bool') } |
| 5896 | } |
| 5897 | } |
| 5898 | |
| 5899 | fn (e &Eval) value_as_int(value Value) !i64 { |
| 5900 | match value { |
| 5901 | i64 { |
| 5902 | return value |
| 5903 | } |
| 5904 | EnumValue { |
| 5905 | return value.value |
| 5906 | } |
| 5907 | f64 { |
| 5908 | return i64(value) |
| 5909 | } |
| 5910 | bool { |
| 5911 | return if value { i64(1) } else { i64(0) } |
| 5912 | } |
| 5913 | string { |
| 5914 | return strconv.parse_int(value, 10, 64) or { i64(0) } |
| 5915 | } |
| 5916 | StructValue { |
| 5917 | if 'value' in value.fields { |
| 5918 | return e.value_as_int(value.fields['value'] or { void_value() }) |
| 5919 | } |
| 5920 | } |
| 5921 | else {} |
| 5922 | } |
| 5923 | |
| 5924 | return error('v3.eval: `${e.runtime_type_name(value)}` can not be used as int') |
| 5925 | } |
| 5926 | |
| 5927 | fn (e &Eval) value_as_f64(value Value) !f64 { |
| 5928 | match value { |
| 5929 | f64 { return value } |
| 5930 | i64 { return f64(value) } |
| 5931 | EnumValue { return f64(value.value) } |
| 5932 | bool { return if value { 1.0 } else { 0.0 } } |
| 5933 | string { return strconv.atof64(value) or { 0.0 } } |
| 5934 | else { return error('v3.eval: `${e.runtime_type_name(value)}` can not be used as float') } |
| 5935 | } |
| 5936 | } |
| 5937 | |
| 5938 | fn (e &Eval) value_string(value Value) string { |
| 5939 | match value { |
| 5940 | bool { |
| 5941 | return value.str() |
| 5942 | } |
| 5943 | i64 { |
| 5944 | return value.str() |
| 5945 | } |
| 5946 | EnumValue { |
| 5947 | return e.enum_value_string(value) |
| 5948 | } |
| 5949 | f64 { |
| 5950 | return value.str() |
| 5951 | } |
| 5952 | string { |
| 5953 | return value |
| 5954 | } |
| 5955 | VoidValue { |
| 5956 | return '' |
| 5957 | } |
| 5958 | ArrayValue { |
| 5959 | return '[' + value.values.map(e.value_string(it)).join(', ') + ']' |
| 5960 | } |
| 5961 | MapValue { |
| 5962 | return '{' + |
| 5963 | value.entries.map('${e.value_string(it.key)}: ${e.value_string(it.value)}').join(', ') + |
| 5964 | '}' |
| 5965 | } |
| 5966 | ModuleValue { |
| 5967 | return value.name |
| 5968 | } |
| 5969 | RangeValue { |
| 5970 | return '${value.start}..${value.end}' |
| 5971 | } |
| 5972 | StructValue { |
| 5973 | if value.type_name == 'os.Result' && 'output' in value.fields { |
| 5974 | return e.value_string(value.fields['output'] or { void_value() }) |
| 5975 | } |
| 5976 | return e.default_struct_string(value) |
| 5977 | } |
| 5978 | SumValue { |
| 5979 | return e.value_string(value.payload) |
| 5980 | } |
| 5981 | TupleValue { |
| 5982 | return value.values.map(e.value_string(it)).join(', ') |
| 5983 | } |
| 5984 | TypeValue { |
| 5985 | return value.name |
| 5986 | } |
| 5987 | FnValue { |
| 5988 | return '<fn>' |
| 5989 | } |
| 5990 | } |
| 5991 | } |
| 5992 | |
| 5993 | fn (mut e Eval) display_string(value Value) !string { |
| 5994 | match value { |
| 5995 | ArrayValue { |
| 5996 | mut parts := []string{cap: value.values.len} |
| 5997 | for item in value.values { |
| 5998 | parts << e.display_string(item)! |
| 5999 | } |
| 6000 | return '[' + parts.join(', ') + ']' |
| 6001 | } |
| 6002 | MapValue { |
| 6003 | mut parts := []string{cap: value.entries.len} |
| 6004 | for entry in value.entries { |
| 6005 | key := e.display_string(entry.key)! |
| 6006 | map_value := e.display_string(entry.value)! |
| 6007 | parts << '${key}: ${map_value}' |
| 6008 | } |
| 6009 | return '{' + parts.join(', ') + '}' |
| 6010 | } |
| 6011 | StructValue { |
| 6012 | if value.type_name == 'os.Result' && 'output' in value.fields { |
| 6013 | return e.display_string(value.fields['output'] or { void_value() }) |
| 6014 | } |
| 6015 | if target := e.resolve_method_target(value.type_name, 'str') { |
| 6016 | result := e.call_method_target(value, target, [])! |
| 6017 | return e.value_string(result.value) |
| 6018 | } |
| 6019 | return e.default_struct_string(value) |
| 6020 | } |
| 6021 | SumValue { |
| 6022 | return e.display_string(value.payload) |
| 6023 | } |
| 6024 | TupleValue { |
| 6025 | mut parts := []string{cap: value.values.len} |
| 6026 | for item in value.values { |
| 6027 | parts << e.display_string(item)! |
| 6028 | } |
| 6029 | return parts.join(', ') |
| 6030 | } |
| 6031 | else { |
| 6032 | return e.value_string(value) |
| 6033 | } |
| 6034 | } |
| 6035 | } |
| 6036 | |
| 6037 | fn (e &Eval) default_struct_string(value StructValue) string { |
| 6038 | if value.fields.len == 0 { |
| 6039 | return '${value.type_name}{}' |
| 6040 | } |
| 6041 | mut names := []string{} |
| 6042 | fields := e.struct_fields(value.type_name) |
| 6043 | for field in fields { |
| 6044 | if field.name in value.fields { |
| 6045 | names << field.name |
| 6046 | } |
| 6047 | } |
| 6048 | if names.len == 0 { |
| 6049 | names = value.fields.keys() |
| 6050 | names.sort() |
| 6051 | } |
| 6052 | mut out := '${value.type_name}{\n' |
| 6053 | for name in names { |
| 6054 | field_value := value.fields[name] or { void_value() } |
| 6055 | out += ' ${name}: ${e.struct_field_value_string(field_value)}\n' |
| 6056 | } |
| 6057 | out += '}' |
| 6058 | return out |
| 6059 | } |
| 6060 | |
| 6061 | fn (e &Eval) struct_field_value_string(value Value) string { |
| 6062 | match value { |
| 6063 | string { |
| 6064 | return "'${value}'" |
| 6065 | } |
| 6066 | else { |
| 6067 | return e.value_string(value) |
| 6068 | } |
| 6069 | } |
| 6070 | } |
| 6071 | |
| 6072 | fn (e &Eval) enum_value_string(value EnumValue) string { |
| 6073 | enum_name := e.normalize_type_name(value.type_name) |
| 6074 | if fields := e.enum_fields[enum_name] { |
| 6075 | module_name := if enum_name.contains('.') { |
| 6076 | enum_name.all_before_last('.') |
| 6077 | } else { |
| 6078 | e.current_module_name() |
| 6079 | } |
| 6080 | short_name := enum_name.all_after_last('.') |
| 6081 | if module_name in e.consts { |
| 6082 | for field in fields { |
| 6083 | if entry := e.consts[module_name]['${short_name}.${field}'] { |
| 6084 | if e.value_as_int(entry.value) or { i64(-1) } == value.value { |
| 6085 | return field |
| 6086 | } |
| 6087 | } |
| 6088 | } |
| 6089 | } |
| 6090 | index := int(value.value) |
| 6091 | if index >= 0 && index < fields.len { |
| 6092 | return fields[index] |
| 6093 | } |
| 6094 | } |
| 6095 | return value.value.str() |
| 6096 | } |
| 6097 | |
| 6098 | fn (e &Eval) runtime_type_name(value Value) string { |
| 6099 | match value { |
| 6100 | bool { return 'bool' } |
| 6101 | i64 { return 'int' } |
| 6102 | EnumValue { return value.type_name } |
| 6103 | f64 { return 'f64' } |
| 6104 | string { return 'string' } |
| 6105 | VoidValue { return 'void' } |
| 6106 | ArrayValue { return '[]${value.elem_type_name}' } |
| 6107 | MapValue { return 'map[${value.key_type_name}]${value.value_type_name}' } |
| 6108 | ModuleValue { return 'module' } |
| 6109 | RangeValue { return 'range' } |
| 6110 | StructValue { return value.type_name } |
| 6111 | SumValue { return value.type_name } |
| 6112 | TupleValue { return 'tuple' } |
| 6113 | TypeValue { return value.name } |
| 6114 | FnValue { return 'fn' } |
| 6115 | } |
| 6116 | } |
| 6117 | |
| 6118 | fn (mut e Eval) write_stdout(s string) { |
| 6119 | if e.capture_output { |
| 6120 | e.stdout_data += s |
| 6121 | } else { |
| 6122 | print(s) |
| 6123 | } |
| 6124 | } |
| 6125 | |
| 6126 | fn (mut e Eval) write_stderr(s string) { |
| 6127 | if e.capture_output { |
| 6128 | e.stderr_data += s |
| 6129 | } else { |
| 6130 | eprint(s) |
| 6131 | } |
| 6132 | } |
| 6133 | |