| 542 | // ── Const expression ────────────────────────────────────────────────────────── |
| 543 | |
| 544 | ConstInstr ParseContext::parse_constexpr() { |
| 545 | bool parenthesized = (tok_.peek().type == TokenType::LParen); |
| 546 | if (parenthesized) tok_.consume(); // '(' |
| 547 | Token kw = tok_.expect(TokenType::Keyword, "const expression"); |
| 548 | ConstInstr result; |
| 549 | if (kw.text == "i32.const") result = ConstInstr{Instr::I32_const(parse_i32())}; |
| 550 | else if (kw.text == "i64.const") result = ConstInstr{Instr::I64_const(parse_i64())}; |
| 551 | else if (kw.text == "f32.const") result = ConstInstr{Instr::F32_const(parse_f32())}; |
| 552 | else if (kw.text == "f64.const") result = ConstInstr{Instr::F64_const(parse_f64())}; |
| 553 | else if (kw.text == "ref.null") { |
| 554 | Token ref = tok_.expect(TokenType::Keyword, "func/extern"); |
| 555 | result = ConstInstr{Instr::Ref_null(ref.text == "func" ? RefType::funcref : RefType::externref)}; |
| 556 | } |
| 557 | else if (kw.text == "ref.func") result = ConstInstr{Instr::Ref_func(parse_funcidx())}; |
| 558 | else if (kw.text == "global.get") result = ConstInstr{Instr::Global_get(parse_globalidx())}; |
| 559 | else throw Exception::Parse("unknown const expression '" + kw.text + "'", {kw.line, kw.column}); |
| 560 | if (parenthesized) tok_.expect(TokenType::RParen); |
| 561 | return result; |
| 562 | } |
| 563 | |
| 564 | ConstInstr ParseContext::parse_elemexpr() { |
| 565 | tok_.expect(TokenType::LParen); |