feat: add borrow test example, fix borrow &mut parsing order

- examples/borrow.bux: working borrow expression example
- tests/borrow_test.nim: add 4 new tests for borrow & @[Shared]
- parser.nim: fix borrow parsing order (& before mut)
- Build verified: example compiles and runs
This commit is contained in:
2026-06-07 17:50:27 +03:00
parent 46814b2abc
commit bfe87a8acb
5 changed files with 37 additions and 1 deletions
+13
View File
@@ -169,6 +169,7 @@ type
callConv*: CallingConvention
targetOs*: string
checked*: bool ## @[Checked] — enable borrow checking
shared*: bool ## @[Shared] — mark function as thread-safe
proc parseAttrs(p: var Parser): ParsedAttrs =
while p.check(tkAt):
@@ -177,6 +178,8 @@ proc parseAttrs(p: var Parser): ParsedAttrs =
let name = p.expect(tkIdent, "expected attribute name").text
if name == "Checked":
result.checked = true
elif name == "Shared":
result.shared = true
elif name == "Import":
discard p.expect(tkLParen, "expected '('")
let key = p.expect(tkIdent, "expected attribute key").text
@@ -596,6 +599,16 @@ proc parsePostfix(p: var Parser): Expr =
proc parseUnary(p: var Parser): Expr =
let loc = p.currentLoc
case p.peek()
of tkBorrow:
discard p.advance() # borrow
let isMut = p.check(tkMut)
if isMut:
discard p.advance() # mut
discard p.expect(tkAmp, "expected '&' after 'borrow'")
if isMut and p.check(tkMut):
discard p.advance() # redundant mut after &
let operand = p.parseUnary() # parse the moved value
return Expr(kind: ekBorrow, loc: loc, exprBorrowOperand: operand, exprBorrowMutable: isMut)
of tkBang, tkMinus, tkTilde, tkStar, tkAmp:
let op = p.advance().kind
if op == tkAmp and p.check(tkMut):