navid/haze / commit
7eea2cb1926e
Navid M <navidmomtahen@gmail.com> · 2026-12-02
parents: 454ae72a0f87
Browse files at this commit · Download source
--- a/samples/baseline/10_named_contracts.hz+++ b/samples/baseline/10_named_contracts.hz@@ -8,9 +8,8 @@ fn half(n i32) i32 : Even(n)- : post r => r * 2 == n {- return n / 2-}+ : post r => r * 2 == n+ => n / 2 // FactorialCond from the original sketch. // Note: a static verifier cannot prove the multiplication stays in i32 from@@ -24,9 +23,8 @@ } fn factorial(n i32) i32 : FactorialCond(n) {- if n <= 1 {+ if n <= 1 return 1- } return n * factorial(n - 1) } --- a/samples/baseline_2/main.hz+++ b/samples/baseline_2/main.hz@@ -1,12 +1,4 @@ // Functional chains and modules.-//-// Module rules used here:-// - one file is one module, named after the file (no declaration line)-// - items are private unless marked `pub`-// - `import m` gives qualified access only: m::name-// - `import m::{a, b}` brings names into scope unqualified, so they work-// as plain calls and as dot-calls (UFCS): xs.a(...)-// - `::` is for module paths; `.` stays for fields and UFCS use seq use seq {range, map, filter, fold, take, all}--- a/src/parser.cpp+++ b/src/parser.cpp@@ -50,19 +50,24 @@ } private:+ static constexpr size_t kNoLimit = static_cast<size_t>(-1);+ std::vector<Token> tokens_; size_t i_ = 0;+ size_t limit_ = kNoLimit; std::unordered_map<std::string, TypePtr> named_types_; const Token& cur() const {- return tokens_[i_];+ return i_ < limit_ ? tokens_[i_] : tokens_.back(); } const Token& at(size_t n) const { size_t k = i_ + n;+ if (k >= limit_)+ return tokens_.back(); return k < tokens_.size() ? tokens_[k] : tokens_.back(); } void advance() {- if (i_ + 1 < tokens_.size())+ if (i_ + 1 < tokens_.size() && i_ + 1 <= limit_) i_++; } bool at(Tok k) const {@@ -279,6 +284,63 @@ } expect(Tok::RBrace, "'}'"); return b;+ }++ static bool continues_expression(Tok k) {+ switch (k) {+ case Tok::Plus:+ case Tok::Minus:+ case Tok::Star:+ case Tok::Slash:+ case Tok::Percent:+ case Tok::EqEq:+ case Tok::NotEq:+ case Tok::Le:+ case Tok::Ge:+ case Tok::AndAnd:+ case Tok::OrOr:+ case Tok::Lt:+ case Tok::Gt:+ case Tok::Assign:+ case Tok::PlusEq:+ case Tok::MinusEq:+ case Tok::StarEq:+ case Tok::SlashEq:+ case Tok::PercentEq:+ case Tok::Comma:+ case Tok::Dot:+ case Tok::LParen:+ case Tok::LBracket:+ return true;+ default:+ return false;+ }+ }++ size_t find_indented_body(Pos indent) const {+ for (size_t k = i_; k + 1 < tokens_.size(); k++) {+ const Token& t = tokens_[k];+ if (t.kind == Tok::End || t.kind == Tok::LBrace || t.kind == Tok::RBrace)+ break;+ if (t.pos.line <= indent.line || t.pos.col <= indent.col)+ continue;+ const Token& prev = tokens_[k - 1];+ if (prev.pos.line < t.pos.line && !continues_expression(prev.kind))+ return k;+ }+ return kNoLimit;+ }++ BlockPtr parse_control_body(Pos indent) {+ if (at(Tok::LBrace))+ return parse_block();+ if (cur().pos.line > indent.line && cur().pos.col > indent.col) {+ auto b = std::make_unique<Block>();+ b->pos = cur().pos;+ b->stmts.push_back(parse_stmt());+ return b;+ }+ throw Error(cur().pos, "expected '{' or an indented body"); } StmtPtr parse_stmt() {@@ -497,17 +559,28 @@ ExprPtr parse_if_expr() { Pos p = cur().pos;+ Pos indent = p;+ if (i_ > 0) {+ const Token& prev = tokens_[i_ - 1];+ if (prev.kind == Tok::Ident && prev.text == "else" && prev.pos.line == p.line)+ indent = prev.pos;+ } expect_ident("if"); auto e = std::make_unique<Expr>(); e->kind = Expr::Kind::If; e->pos = p;+ size_t boundary = find_indented_body(indent);+ size_t saved_limit = limit_;+ if (boundary != kNoLimit)+ limit_ = boundary; e->cond = parse_expr();- e->then_e = make_block_expr(parse_block(), p);+ limit_ = saved_limit;+ e->then_e = make_block_expr(parse_control_body(indent), p); if (match_ident("else")) { if (is_ident("if")) { e->else_e = parse_if_expr(); } else {- e->else_e = make_block_expr(parse_block(), p);+ e->else_e = make_block_expr(parse_control_body(indent), p); } } return e;