Skip to content

Commit d968ed4

Browse files
committed
feat: Add Walrus Operator (:=) assignment expression
1 parent 3120235 commit d968ed4

5 files changed

Lines changed: 57 additions & 0 deletions

File tree

FEATURES.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CodingYok adalah bahasa pemrograman Indonesia dengan fitur modern. Dokumen ini m
1010

1111
| Feature | Description |
1212
|---------|-------------|
13+
| Walrus Operator | `(x := value)` assignment expression |
1314
| Ternary Expression | `x jika kondisi kalau_tidak y` inline conditional |
1415
| Slicing | `arr[start:stop:step]` untuk list dan string |
1516
| Tuple Unpacking | `a, b = [1, 2]` dan `untuk x, y dalam items` |
@@ -196,6 +197,27 @@ hasil = ["genap" jika n % 2 == 0 kalau_tidak "ganjil" untuk n dalam angka]
196197

197198
---
198199

200+
## 🦭 Walrus Operator
201+
202+
```codingyok
203+
# Assignment expression - assign and return value
204+
tulis((x := 10)) # prints 10, x is now 10
205+
206+
# In conditions
207+
jika (n := panjang(data)) > 3:
208+
tulis(f"List punya {n} elemen")
209+
210+
# In while loops
211+
selama (line := baca_baris()):
212+
tulis(line)
213+
214+
# With calculations
215+
tulis(f"Total: {(total := a + b)}")
216+
tulis(f"Variable: {total}")
217+
```
218+
219+
---
220+
199221
## 🎯 Pattern Matching
200222

201223
```codingyok

src/codingyok/ast_nodes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ def accept(self, visitor):
7171
return visitor.visit_ternary(self)
7272

7373

74+
@dataclass
75+
class WalrusExpression(Expression):
76+
"""Walrus/assignment expression (name := value)"""
77+
78+
name: str
79+
value: Expression
80+
81+
def accept(self, visitor):
82+
return visitor.visit_walrus(self)
83+
84+
7485
@dataclass
7586
class UnaryExpression(Expression):
7687
"""Unary operations (-a, bukan a, etc.)"""

src/codingyok/interpreter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,12 @@ def visit_ternary(self, expr) -> Any:
764764
else:
765765
return self.evaluate(expr.false_value)
766766

767+
def visit_walrus(self, expr) -> Any:
768+
"""Visit walrus expression (name := value)"""
769+
value = self.evaluate(expr.value)
770+
self.environment.define(expr.name, value)
771+
return value
772+
767773
def visit_unary(self, expr: UnaryExpression) -> Any:
768774
"""Visit unary expression"""
769775
operand = self.evaluate(expr.operand)

src/codingyok/parser.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,22 @@ def primary(self) -> Expression:
477477
return IdentifierExpression(self.previous().value)
478478

479479
if self.match(TokenType.LEFT_PAREN):
480+
# Check for walrus operator (name := value)
481+
if self.check(TokenType.IDENTIFIER):
482+
# Save position to backtrack if not walrus
483+
saved_pos = self.current
484+
name_token = self.advance()
485+
if self.match(TokenType.WALRUS):
486+
value = self.expression()
487+
self.consume(
488+
TokenType.RIGHT_PAREN,
489+
"Diharapkan ')' setelah walrus expression"
490+
)
491+
return WalrusExpression(name_token.value, value)
492+
else:
493+
# Not walrus, backtrack
494+
self.current = saved_pos
495+
480496
expr = self.expression()
481497
self.consume(TokenType.RIGHT_PAREN, "Diharapkan ')' setelah ekspresi")
482498
return expr

src/codingyok/tokens.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class TokenType(Enum):
7878

7979
# Assignment
8080
ASSIGN = auto() # =
81+
WALRUS = auto() # :=
8182
PLUS_ASSIGN = auto() # +=
8283
MINUS_ASSIGN = auto() # -=
8384
MULTIPLY_ASSIGN = auto() # *=
@@ -177,6 +178,7 @@ def __repr__(self) -> str:
177178
">": TokenType.GREATER_THAN,
178179
">=": TokenType.GREATER_EQUAL,
179180
"=": TokenType.ASSIGN,
181+
":=": TokenType.WALRUS,
180182
"+=": TokenType.PLUS_ASSIGN,
181183
"-=": TokenType.MINUS_ASSIGN,
182184
"*=": TokenType.MULTIPLY_ASSIGN,

0 commit comments

Comments
 (0)