🐐 Array concat
This commit is contained in:
@@ -59,7 +59,7 @@ def rep_join(l):
|
||||
OP_STR = {
|
||||
"SYMBOL_PLUS": "+", "SYMBOL_MINUS": "-",
|
||||
"SYMBOL_TIMES": "*", "SYMBOL_DIVIDE": "/",
|
||||
"SYMBOL_AMPERSAND": "&",
|
||||
"SYMBOL_AMPERSAND": "&", "SYMBOL_AT": "@",
|
||||
"KEYWORD_RELIQVVM": "RELIQVVM",
|
||||
"KEYWORD_EST": "EST", "KEYWORD_DISPAR": "DISPAR",
|
||||
"KEYWORD_MINVS": "MINVS",
|
||||
@@ -833,6 +833,10 @@ class BinOp(Node):
|
||||
return vtable, ValNul()
|
||||
result = (lv or 0) + (rv or 0)
|
||||
return vtable, ValFrac(result) if isinstance(result, Fraction) else ValInt(result)
|
||||
case "SYMBOL_AT":
|
||||
if not isinstance(left, ValList) or not isinstance(right, ValList):
|
||||
raise CentvrionError("@ requires two arrays")
|
||||
return vtable, ValList(list(lv) + list(rv))
|
||||
case "SYMBOL_AMPERSAND":
|
||||
magnvm = "MAGNVM" in vtable["#modules"]
|
||||
svbnvlla = "SVBNVLLA" in vtable["#modules"]
|
||||
|
||||
@@ -13,6 +13,7 @@ _BINOP_FN = {
|
||||
"SYMBOL_TIMES": "cent_mul",
|
||||
"SYMBOL_DIVIDE": "cent_div",
|
||||
"SYMBOL_AMPERSAND": "cent_concat",
|
||||
"SYMBOL_AT": "cent_array_concat",
|
||||
"KEYWORD_RELIQVVM": "cent_mod",
|
||||
"KEYWORD_EST": "cent_eq",
|
||||
"KEYWORD_DISPAR": "cent_neq",
|
||||
|
||||
@@ -409,6 +409,18 @@ CentValue cent_add(CentValue a, CentValue b) {
|
||||
return cent_null();
|
||||
}
|
||||
|
||||
CentValue cent_array_concat(CentValue a, CentValue b) {
|
||||
if (a.type != CENT_LIST || b.type != CENT_LIST)
|
||||
cent_type_error("'@' requires two arrays");
|
||||
int total = a.lval.len + b.lval.len;
|
||||
CentValue result = cent_list_new(total);
|
||||
for (int i = 0; i < a.lval.len; i++)
|
||||
cent_list_push(&result, a.lval.items[i]);
|
||||
for (int i = 0; i < b.lval.len; i++)
|
||||
cent_list_push(&result, b.lval.items[i]);
|
||||
return result;
|
||||
}
|
||||
|
||||
CentValue cent_concat(CentValue a, CentValue b) {
|
||||
const char *sa = (a.type == CENT_NULL) ? "" : cent_make_string(a);
|
||||
const char *sb = (b.type == CENT_NULL) ? "" : cent_make_string(b);
|
||||
|
||||
@@ -198,6 +198,7 @@ char *cent_make_string(CentValue v);
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
CentValue cent_add(CentValue a, CentValue b); /* INT+INT or FRAC+FRAC/INT */
|
||||
CentValue cent_array_concat(CentValue a, CentValue b); /* @ operator: concatenate two arrays */
|
||||
CentValue cent_concat(CentValue a, CentValue b); /* & operator: coerce all types to str */
|
||||
CentValue cent_sub(CentValue a, CentValue b); /* INT-INT or FRAC-FRAC/INT */
|
||||
CentValue cent_mul(CentValue a, CentValue b); /* INT*INT or FRAC*FRAC/INT */
|
||||
|
||||
@@ -94,6 +94,7 @@ symbol_tokens = [
|
||||
("SYMBOL_TIMES", r"\*"),
|
||||
("SYMBOL_DIVIDE", r"\/"),
|
||||
("SYMBOL_AMPERSAND", r"&"),
|
||||
("SYMBOL_AT", r"@"),
|
||||
("SYMBOL_COMMA", r",")
|
||||
]
|
||||
|
||||
|
||||
+2
-1
@@ -116,7 +116,7 @@ class Parser():
|
||||
('left', ["KEYWORD_AVT"]),
|
||||
('left', ["KEYWORD_ET"]),
|
||||
('left', ["KEYWORD_PLVS", "KEYWORD_MINVS", "KEYWORD_EST", "KEYWORD_DISPAR"]),
|
||||
('left', ["SYMBOL_AMPERSAND", "SYMBOL_PLUS", "SYMBOL_MINUS"]),
|
||||
('left', ["SYMBOL_AMPERSAND", "SYMBOL_AT", "SYMBOL_PLUS", "SYMBOL_MINUS"]),
|
||||
('left', ["SYMBOL_TIMES", "SYMBOL_DIVIDE", "KEYWORD_RELIQVVM"]),
|
||||
('right', ["UMINUS", "UNOT"]),
|
||||
('left', ["SYMBOL_LBRACKET", "INDEX"]),
|
||||
@@ -311,6 +311,7 @@ class Parser():
|
||||
def expression_nullus(_):
|
||||
return ast_nodes.Nullus()
|
||||
|
||||
@self.pg.production('expression : expression SYMBOL_AT expression')
|
||||
@self.pg.production('expression : expression SYMBOL_AMPERSAND expression')
|
||||
@self.pg.production('expression : expression SYMBOL_MINUS expression')
|
||||
@self.pg.production('expression : expression SYMBOL_PLUS expression')
|
||||
|
||||
Reference in New Issue
Block a user