Skip to content

Commit 73c904e

Browse files
gh-15, gh-16: Fix a number of indexing issues
1 parent 1bc7a2d commit 73c904e

File tree

6 files changed

+258
-109
lines changed

6 files changed

+258
-109
lines changed

asm-lang.exe

1.32 KB
Binary file not shown.

interpreter.py

Lines changed: 231 additions & 88 deletions
Large diffs are not rendered by default.

lib/numbers.asmln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ FUNC VALUE(STR: num, INT: base):INT{
9494
IF(NOT(KEYIN(digit, digits))){
9595
THROW(JOIN("Digit \"", digit, "\" not valid for base ", STR(base)))
9696
}
97-
ADD(@value, MUL(digits[digit], POW(base, idx)))
97+
ADD(@value, MUL(digits<digit>, POW(base, idx)))
9898
# compute internal binary value as digit*base^(position from right)
9999
}
100100
DEL(digit)
@@ -157,7 +157,7 @@ FUNC CONVERT(STR: num, INT: from, INT: to):STR{
157157
}
158158
WHILE(GT(int_value, 0)){
159159
INT: rem = MOD(int_value, to)
160-
STR: remstr = rev[rem]
160+
STR: remstr = rev<rem>
161161
int_result = JOIN(remstr, int_result)
162162
DIV(@int_value, to)
163163
DEL(remstr)
@@ -188,7 +188,7 @@ FUNC CONVERT(STR: num, INT: from, INT: to):STR{
188188
ADD(@iter, 1)
189189
MUL(@frac_num, to)
190190
INT: d = DIV(frac_num, denom)
191-
STR: dstr = rev[d]
191+
STR: dstr = rev<d>
192192
frac_res = JOIN(frac_res, dstr)
193193
INT: new_num = MOD(frac_num, denom)
194194
frac_num = new_num

lib/prng.asmln

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# LCG-based pseudorandom number generator
22
#
33
# API:
4-
# PRNG_SEED(INT: seed) -> INT
5-
# PRNG_NEXT() -> INT
6-
# PRNG_RANGE(INT: max) -> INT
7-
# PRNG_RANGE_MIN_MAX(INT: min, INT: max) -> INT
4+
# SEED(INT: seed) -> INT
5+
# NEXT() -> INT
6+
# RANGE(INT: max) -> INT
7+
# RANGE_MIN_MAX(INT: min, INT: max) -> INT
88

99
INT: MASK32 = SUB( POW(10,100000), 1 ) # 2^32 - 1
1010

@@ -13,26 +13,26 @@ INT: LCG_A = 000110010110011000001101 # 1664525
1313
INT: LCG_C = 00111100011011101111001101011111 # 1013904223
1414
INT: lcg_state = 1
1515

16-
FUNC PRNG_SEED(INT: seed):INT{
16+
FUNC SEED(INT: seed):INT{
1717
lcg_state = BAND(seed, MASK32)
1818
RETURN(lcg_state)
1919
}
2020

21-
FUNC PRNG_NEXT():INT{
21+
FUNC NEXT():INT{
2222
lcg_state = BAND( ADD( MUL(LCG_A, lcg_state), LCG_C ), MASK32 )
2323
RETURN(lcg_state)
2424
}
2525

26-
FUNC PRNG_RANGE(INT: max):INT{
26+
FUNC RANGE(INT: max):INT{
2727
ASSERT( GT(max, 0) )
28-
RETURN( MOD(PRNG_NEXT(), max) )
28+
RETURN( MOD(NEXT(), max) )
2929
}
3030

31-
FUNC PRNG_RANGE_MIN_MAX(INT: min, INT: max):INT{
31+
FUNC RANGE_MIN_MAX(INT: min, INT: max):INT{
3232
ASSERT( LTE(min, max) )
3333
INT: range = SUB(max, min)
3434
IF( EQ(range, 0) ){ RETURN(min) }
3535
ASSERT( GT(range, 0) )
36-
INT: offset = PRNG_RANGE(range)
36+
INT: offset = RANGE(range)
3737
RETURN( ADD(offset, min) )
3838
}

parser.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ class AsyncExpression(Expression):
180180
class IndexExpression(Expression):
181181
base: Expression
182182
indices: List[Expression]
183+
# True when the indices were written with angle-brackets `<...>` (map-style),
184+
# False when written with square brackets `[...]` (tensor-style).
185+
is_map: bool
183186

184187

185188
@dataclass(slots=True)
@@ -615,7 +618,8 @@ def _parse_index_suffix(self, base: Expression) -> IndexExpression:
615618
if not self._match("COMMA"):
616619
break
617620
self._consume(closing)
618-
return IndexExpression(location=self._location_from_token(lbracket), base=base, indices=indices)
621+
is_map = start_tok.type == "LANGLE"
622+
return IndexExpression(location=self._location_from_token(lbracket), base=base, indices=indices, is_map=is_map)
619623

620624
def _parse_index_expression(self) -> IndexExpression:
621625
expr = self._parse_primary()
@@ -710,9 +714,11 @@ def _looks_like_index_assignment(self) -> bool:
710714
if depth != 0:
711715
return False
712716

713-
# Support additional bracketed suffixes like a[1][2].
714-
while i < len(tokens) and tokens[i].type == "LBRACKET":
715-
depth = 0
717+
# Support additional bracketed or angled suffixes like a[1][2] or a[1]<"k">.
718+
while i < len(tokens) and tokens[i].type in ("LBRACKET", "LANGLE"):
719+
# We advance past the opening bracket/angle and treat that as
720+
# one level of depth already; initialize depth=1 accordingly.
721+
depth = 1
716722
i += 1
717723
while i < len(tokens):
718724
tok = tokens[i]

test.asmln

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,10 @@ FUNC STDLIB_TESTS():INT{
582582
PRINT("Running library tests...")
583583

584584
# PRNG reproducibility (LCG)
585-
prng.PRNG_SEED(1010) # seed = 10
586-
INT: a = prng.PRNG_NEXT()
587-
prng.PRNG_SEED(1010)
588-
INT: b = prng.PRNG_NEXT()
585+
prng.SEED(1010) # seed = 10
586+
INT: a = prng.NEXT()
587+
prng.SEED(1010)
588+
INT: b = prng.NEXT()
589589
ASSERT( EQ(a, b) )
590590
DEL(a)
591591
DEL(b)

0 commit comments

Comments
 (0)