🐐 Array indexing

This commit is contained in:
2026-03-31 22:10:55 +02:00
parent cdad648f58
commit 58149b5f65
4 changed files with 41 additions and 7 deletions

View File

@@ -5,7 +5,7 @@ from unittest.mock import patch
from parameterized import parameterized
from centvrion.ast_nodes import (
Bool, BinOp, BuiltIn, DataArray, DataRangeArray, Defini,
ArrayIndex, Bool, BinOp, BuiltIn, DataArray, DataRangeArray, Defini,
Designa, DumStatement, Erumpe, ExpressionStatement, ID,
Invoca, ModuleCall, Nullus, Numeral, PerStatement,
Program, Redi, SiStatement, String, UnaryMinus,
@@ -231,6 +231,8 @@ error_tests = [
("DEFINI f (x, y) VT { REDI(x) }\nINVOCA f (I)", TypeError), # too few args
("DEFINI f () VT { REDI(I) }\nINVOCA f (I)", TypeError), # args to zero-param function
("SI NVLLVS TVNC { DESIGNA r VT I }", TypeError), # NVLLVS cannot be used as boolean
("[(I, II)][III]", IndexError), # index too high
("[(I, II)][-I]", IndexError), # negative index
]
class TestErrors(unittest.TestCase):
@@ -604,17 +606,17 @@ class TestEtAvt(unittest.TestCase):
# --- Array indexing ---
# Indexing is 0-based; NVLLVS serves as index 0
# Indexing is 1-based; I is the first element
array_index_tests = [
# basic indexing
("[(I, II, III)][NVLLVS]", None, ValInt(1)), # index 0 → first element
("[(I, II, III)][I]", None, ValInt(2)), # index 1 → second element
("[(I, II, III)][II]", None, ValInt(3)), # index 2 → third element
("[(I, II, III)][I]", None, ValInt(1)), # first element
("[(I, II, III)][II]", None, ValInt(2)), # second element
("[(I, II, III)][III]", None, ValInt(3)), # third element
# index into a variable
("DESIGNA a VT [(X, XX, XXX)]\na[I]", None, ValInt(20)),
("DESIGNA a VT [(X, XX, XXX)]\na[II]", None, ValInt(20)), # second element
# index into range array
("[I VSQVE V][II]", None, ValInt(3)),
("[I VSQVE V][II]", None, ValInt(2)), # second element of [1,2,3,4]
]
class TestArrayIndex(unittest.TestCase):