Skip to content

Commit 9455b74

Browse files
committed
Feature: inside array for syntax
1 parent ae36010 commit 9455b74

File tree

7 files changed

+774
-1
lines changed

7 files changed

+774
-1
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ exclude =
1717
ply
1818
nml/actions/action2var_variables.py
1919
nml/actions/action3_callbacks.py
20+
.venv

nml/ast/for_.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
__license__ = """
2+
NML is free software; you can redistribute it and/or modify
3+
it under the terms of the GNU General Public License as published by
4+
the Free Software Foundation; either version 2 of the License, or
5+
(at your option) any later version.
6+
7+
NML is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License along
13+
with NML; if not, write to the Free Software Foundation, Inc.,
14+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""
15+
16+
from nml.ast import base_statement
17+
from nml.expression.array import Array
18+
19+
20+
class InArrayFor(base_statement.BaseStatement):
21+
def __init__(self, array, param, expressions, pos=None):
22+
base_statement.BaseStatement.__init__(self, "for", pos, False, False)
23+
self.array = array
24+
self.param = param
25+
self.expressions = expressions
26+
27+
def __str__(self):
28+
expressions_string = ""
29+
for expression in self.expressions:
30+
expressions_string += str(expression) + ", "
31+
expressions_string = expressions_string[:-2]
32+
return "[{} for {} in {}]".format(
33+
expressions_string,
34+
self.param,
35+
self.array,
36+
)
37+
38+
def reduce(self, id_dicts=None, unknown_id_fatal=True):
39+
self.array = self.array.reduce(id_dicts, unknown_id_fatal)
40+
out_list = []
41+
for value in self.array.values:
42+
param_tuple = ({self.param.value: value}, lambda name, x, pos : x)
43+
id_dicts.append(param_tuple)
44+
for expression in self.expressions:
45+
out_list.append(expression.reduce(id_dicts, unknown_id_fatal))
46+
id_dicts.remove(param_tuple)
47+
return Array(out_list, self.pos)

nml/parser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
disable_item,
3030
error,
3131
font,
32+
for_,
3233
general,
3334
grf,
3435
item,
@@ -835,3 +836,7 @@ def p_tilelayout_item_prop(self, t):
835836
def p_constant(self, t):
836837
"constant : CONST expression EQ expression SEMICOLON"
837838
t[0] = constant.Constant(t[2], t[4])
839+
840+
def p_in_array_for(self, t):
841+
"""expression : LBRACKET non_empty_expression_list FOR ID IN expression RBRACKET"""
842+
t[0] = for_.InArrayFor(t[6], t[4], t[2], t.lineno(1))

nml/tokens.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
"recolour_sprite": "RECOLOUR_SPRITE",
6262
"engine_override": "ENGINE_OVERRIDE",
6363
"sort": "SORT_VEHICLES",
64-
"const": "CONST"
64+
"const": "CONST",
65+
"for": "FOR",
66+
"in": "IN",
6567
}
6668
# fmt: on
6769

regression/042_for.nml

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//Add action8, so we can test the vehicle in-game
2+
grf {
3+
grfid: "NML\42";
4+
name: string(STR_REGRESSION_NAME);
5+
desc: string(STR_REGRESSION_DESC);
6+
version: 0;
7+
min_compatible_version: 0;
8+
}
9+
10+
cargotable {
11+
COAL, LVST
12+
}
13+
14+
spriteset(station_spriteset, "station.png") {
15+
[ 1, 1, 5, 5, -2, -2]
16+
[ 7, 1, 5, 5, -2, -2]
17+
[ 2, 2, 3, 3, -1, -1]
18+
[ 8, 2, 3, 3, -1, -1]
19+
}
20+
21+
spritelayout station_sprite_layout_X(a) {
22+
ground {
23+
sprite: GROUNDSPRITE_RAIL_X;
24+
}
25+
building {
26+
sprite: 0x42E;
27+
xoffset: 0;
28+
yoffset: 0;
29+
zoffset: 0;
30+
xextent: 16;
31+
yextent: 5;
32+
zextent: 2;
33+
recolour_mode: RECOLOUR_REMAP;
34+
palette: CUSTOM(0);
35+
}
36+
childsprite {
37+
sprite: DEFAULT(a);
38+
xoffset: 17;
39+
yoffset: 11;
40+
recolour_mode: RECOLOUR_REMAP;
41+
palette: CUSTOM(1);
42+
}
43+
}
44+
45+
spritelayout station_sprite_layout_Y(a) {
46+
ground {
47+
sprite: GROUNDSPRITE_RAIL_Y;
48+
}
49+
building {
50+
sprite: 0x42F;
51+
xoffset: 0;
52+
yoffset: 0;
53+
zoffset: 0;
54+
xextent: 5;
55+
yextent: 16;
56+
zextent: 2;
57+
recolour_mode: RECOLOUR_REMAP;
58+
palette: CUSTOM(2);
59+
}
60+
childsprite {
61+
sprite: station_spriteset(a);
62+
xoffset: 20;
63+
yoffset: 11;
64+
recolour_mode: RECOLOUR_REMAP;
65+
palette: CUSTOM(3);
66+
}
67+
}
68+
69+
spriteset (palette_1) {
70+
recolour_sprite {
71+
0xC6:0xB3;0xC7:0xB4;0xC8:0xB5;0xC9:0xB6;0xCA:0xB7;0xCB:0xA4;0xCC:0xA5;0xCD:0xA6;
72+
}
73+
}
74+
spriteset (palette_2) {
75+
recolour_sprite {
76+
0xC6:0x60;0xC7:0x61;0xC8:0x62;0xC9:0x63;0xCA:0x64;0xCB:0x65;0xCC:0x66;0xCD:0x67;
77+
}
78+
}
79+
spriteset (palette_3) {
80+
recolour_sprite {
81+
0xC6:0xC6;0xC7:0xC7;0xC8:0xC8;0xC9:0xC9;0xCA:0xCA;0xCB:0xCB;0xCC:0xCC;0xCD:0xCD;
82+
}
83+
}
84+
spriteset (palette_4) {
85+
recolour_sprite {
86+
0xC6:0x58;0xC7:0x59;0xC8:0x5A;0xC9:0x5B;0xCA:0x5C;0xCB:0x5D;0xCC:0x5E;0xCD:0x5F;
87+
}
88+
}
89+
90+
switch (FEAT_STATIONS, SELF, palette_switch, a, company_colour2-a) {
91+
0 : palette_1;
92+
1 : palette_2;
93+
2 : palette_3;
94+
palette_4;
95+
}
96+
97+
item (FEAT_STATIONS, basic_station, 255) {
98+
property {
99+
class : "TEST";
100+
classname: string(STR_STATION_TEST_CLASS);
101+
name : string(STR_STATION_BASIC);
102+
general_flags: bitmask(STAT_FLAG_EXTENDED_FOUNDATIONS);
103+
cargo_random_triggers: [LVST];
104+
disabled_platforms: bitmask(5, 6, 7, 8);
105+
tile_flags: [a | bitmask(a) for a in [0, 1, 2, 3]];
106+
station_layouts: [[[2]],[[a, a] for a in [4, 6]]];
107+
}
108+
graphics {
109+
foundations: param[param[2]];
110+
prepare_layout: [STORE_TEMP(0,nearby_tile_station_id(-1,2)), STORE_TEMP(1,1)];
111+
purchase_prepare_layout: STORE_TEMP(3,3);
112+
sprite_layouts: [
113+
station_sprite_layout_X(a),
114+
station_sprite_layout_Y(a) for a in [0, 1] + [2, 3]
115+
];
116+
custom_spritesets: [palette_switch(a) for a in [param[param[1]],5,6,10]];
117+
anim_speed: company_colour1 + company_colour2;
118+
LVST: station_spriteset;
119+
COAL: station_spriteset;
120+
station_spriteset;
121+
}
122+
}

regression/expected/042_for.grf

3.58 KB
Binary file not shown.

0 commit comments

Comments
 (0)