From ff38f7cc61e9a1e9944406eb296140f4f9807d7d Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Mon, 8 Feb 2021 18:17:14 +0900 Subject: [PATCH] Uh, that's not quite what Python is doing, but... --- src/compiler.c | 8 +++++++- src/obj_list.c | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/compiler.c b/src/compiler.c index 81d80df..c602789 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -528,7 +528,13 @@ static void get_(int canAssign) { expression(); emitByte(OP_INVOKE_SETSLICE); } else if (canAssign && matchAssignment()) { - error("operator-assignment not supported on slice"); + /* o s e */ + emitBytes(OP_DUP, 2); /* o s e o */ + emitBytes(OP_DUP, 2); /* o s e o s */ + emitBytes(OP_DUP, 2); /* o s e o s e */ + emitByte(OP_INVOKE_GETSLICE); /* o s e v */ + assignmentValue(); + emitByte(OP_INVOKE_SETSLICE); } else if (inDel && matchEndOfDel()) { emitByte(OP_INVOKE_DELSLICE); inDel = 2; diff --git a/src/obj_list.c b/src/obj_list.c index 91d86ed..4364679 100644 --- a/src/obj_list.c +++ b/src/obj_list.c @@ -355,6 +355,15 @@ KRK_METHOD(list,sort,{ qsort(self->values.values, self->values.count, sizeof(KrkValue), _list_sorter); }) +KRK_METHOD(list,__add__,{ + METHOD_TAKES_EXACTLY(1); + if (!IS_list(argv[1])) return TYPE_ERROR(list,argv[1]); + + KrkValue outList = krk_list_of(self->values.count, self->values.values); /* copy */ + FUNC_NAME(list,extend)(2,(KrkValue[]){outList,argv[1]},0); /* extend */ + return outList; +}) + FUNC_SIG(listiterator,__init__); KRK_METHOD(list,__iter__,{ @@ -437,6 +446,7 @@ void _createAndBind_listClass(void) { BIND_METHOD(list,__setslice__); BIND_METHOD(list,__iter__); BIND_METHOD(list,__mul__); + BIND_METHOD(list,__add__); BIND_METHOD(list,append); BIND_METHOD(list,extend); BIND_METHOD(list,pop);