Skip to content

Commit e161bc4

Browse files
committed
♻️ refactor(lang): organize constants into submodules and improve builtin variable scoping
Reorganize flat constants into `builtins` and `identifiers` submodules for better code organization. Refactor builtin functions (take_while, group_by, fold, unique_by) to use `do...end` blocks for proper mutable variable scoping.
1 parent 7476aba commit e161bc4

File tree

7 files changed

+324
-284
lines changed

7 files changed

+324
-284
lines changed

crates/mq-lang/builtin.mq

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ end
293293

294294
# Skips elements from the beginning of an array while the provided function returns true
295295
def skip_while(arr, f):
296-
let si = if (not(is_array(arr))):
296+
let si = if (!is_array(arr)):
297297
error("first argument must be an array")
298298
elif (is_empty(arr)):
299299
0
@@ -309,15 +309,17 @@ end
309309

310310
# Takes elements from the beginning of an array while the provided function returns true
311311
def take_while(arr, f):
312-
var i = 0
313-
| let ti =
312+
let ti =
314313
if (not(is_array(arr))):
315314
error("first argument must be an array")
316315
elif (is_empty(arr)):
317316
0
318317
else:
319-
while (and(i < len(arr), f(get(arr, i)))):
320-
i += 1 | i
318+
do
319+
var i = 0
320+
| while (and(i < len(arr), f(get(arr, i)))):
321+
i += 1 | i
322+
end
321323
end
322324
| if (is_none(ti)): []
323325
else:
@@ -326,21 +328,23 @@ end
326328

327329
# Groups elements of an array by the result of applying a function to each element
328330
def group_by(arr, f):
329-
var i = 0
330-
| var groups = dict()
331+
var groups = dict()
331332
| if (not(is_array(arr))):
332333
error("first argument must be an array")
333334
elif (is_empty(arr)):
334335
dict()
335336
else:
336-
while (not(is_none(get(arr, i)))):
337-
let v = get(arr, i)
338-
| i += 1
339-
| let key = to_string(f(v))
340-
| let existing = get(groups, key)
341-
| let new_group = if (is_none(existing)): [v] else: existing + v
342-
| groups = set(groups, key, new_group)
343-
| groups
337+
do
338+
var i = 0
339+
| while (not(is_none(get(arr, i)))):
340+
let v = get(arr, i)
341+
| i += 1
342+
| let key = to_string(f(v))
343+
| let existing = get(groups, key)
344+
| let new_group = if (is_none(existing)): [v] else: existing + v
345+
| groups = set(groups, key, new_group)
346+
| groups
347+
end
344348
end
345349
end
346350

@@ -363,38 +367,42 @@ end
363367

364368
# Reduces an array to a single value by applying a function, starting from an initial value.
365369
def fold(arr, init, f):
366-
var acc = init
367-
| var i = 0
368-
| if (not(is_array(arr))):
369-
error("first argument must be an array")
370-
elif (is_empty(arr)):
371-
init
372-
else:
373-
while (i != len(arr)):
374-
acc = f(acc, get(arr, i))
375-
| i += 1
376-
| acc
377-
end
370+
if (not(is_array(arr))):
371+
error("first argument must be an array")
372+
elif (is_empty(arr)):
373+
init
374+
else:
375+
do
376+
var acc = init
377+
| var i = 0
378+
| while (i != len(arr)):
379+
acc = f(acc, get(arr, i))
380+
| i += 1
381+
| acc
382+
end
383+
end
378384
end
379385

380386
# Returns a new array with duplicate elements removed, comparing by the result of the provided function.
381387
def unique_by(arr, f):
382-
var seen = dict()
383-
| var result = []
384-
| var i = 0
385-
| if (not(is_array(arr))):
386-
error("first argument must be an array")
387-
elif (is_empty(arr)): []
388-
else:
389-
while (i != len(arr)):
390-
let item = get(arr, i)
391-
| let key = to_string(f(item))
392-
| let already_seen = get(seen, key)
393-
| result = if (is_none(already_seen)): result + item else: result
394-
| seen = if (is_none(already_seen)): set(seen, key, true) else: seen
395-
| i += 1
396-
| result
397-
end
388+
if (not(is_array(arr))):
389+
error("first argument must be an array")
390+
elif (is_empty(arr)): []
391+
else:
392+
do
393+
var seen = dict()
394+
| var result = []
395+
| var i = 0
396+
| while (i != len(arr)):
397+
let item = get(arr, i)
398+
| let key = to_string(f(item))
399+
| let already_seen = get(seen, key)
400+
| result = if (is_none(already_seen)): result + item else: result
401+
| seen = if (is_none(already_seen)): set(seen, key, true) else: seen
402+
| i += 1
403+
| result
404+
end
405+
end
398406
end
399407

400408
# Returns the input value unchanged.
Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
pub const SELF: &str = "self";
2-
3-
pub const ARRAY: &str = "array";
4-
pub const DICT: &str = "dict";
5-
6-
pub const GET: &str = "get";
7-
pub const SET: &str = "set";
8-
pub const SLICE: &str = "slice";
9-
pub const ATTR: &str = "attr";
10-
pub const SET_ATTR: &str = "set_attr";
11-
pub const LEN: &str = "len";
12-
13-
pub const EQ: &str = "eq";
14-
pub const NE: &str = "ne";
15-
pub const LT: &str = "lt";
16-
pub const LTE: &str = "lte";
17-
pub const GT: &str = "gt";
18-
pub const GTE: &str = "gte";
19-
20-
pub const ADD: &str = "add";
21-
pub const SUB: &str = "sub";
22-
pub const MUL: &str = "mul";
23-
pub const DIV: &str = "div";
24-
pub const MOD: &str = "mod";
25-
pub const FLOOR: &str = "floor";
26-
27-
pub const AND: &str = "and";
28-
pub const OR: &str = "or";
29-
pub const NOT: &str = "not";
30-
pub const NEGATE: &str = "negate";
31-
32-
pub const RANGE: &str = "range";
33-
34-
pub const BREAKPOINT: &str = "breakpoint";
35-
pub const COALESCE: &str = "coalesce";
36-
37-
pub const ASSIGN: &str = "assign";
38-
39-
pub const PATTERN_MATCH_WILDCARD: &str = "_";
1+
pub mod builtins {
2+
pub const ARRAY: &str = "array";
3+
pub const DICT: &str = "dict";
4+
5+
pub const GET: &str = "get";
6+
pub const SET: &str = "set";
7+
pub const SLICE: &str = "slice";
8+
pub const ATTR: &str = "attr";
9+
pub const SET_ATTR: &str = "set_attr";
10+
pub const LEN: &str = "len";
11+
12+
pub const EQ: &str = "eq";
13+
pub const NE: &str = "ne";
14+
pub const LT: &str = "lt";
15+
pub const LTE: &str = "lte";
16+
pub const GT: &str = "gt";
17+
pub const GTE: &str = "gte";
18+
19+
pub const ADD: &str = "add";
20+
pub const SUB: &str = "sub";
21+
pub const MUL: &str = "mul";
22+
pub const DIV: &str = "div";
23+
pub const MOD: &str = "mod";
24+
pub const FLOOR: &str = "floor";
25+
26+
pub const NOT: &str = "not";
27+
pub const NEGATE: &str = "negate";
28+
29+
pub const RANGE: &str = "range";
30+
31+
pub const BREAKPOINT: &str = "breakpoint";
32+
pub const COALESCE: &str = "coalesce";
33+
}
34+
35+
pub mod identifiers {
36+
pub const SELF: &str = "self";
37+
pub const PATTERN_MATCH_WILDCARD: &str = "_";
38+
}

0 commit comments

Comments
 (0)