From 78a145a5bb77b73f5fbefb0efa2d188926eb55ef Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 10 Sep 2015 21:42:12 +0200 Subject: Add arrset --- functions.cpp | 13 +++++++++++++ reference.md | 1 + 2 files changed, 14 insertions(+) diff --git a/functions.cpp b/functions.cpp index 792e2cc..f4beeb9 100644 --- a/functions.cpp +++ b/functions.cpp @@ -421,6 +421,19 @@ unordered_map&,unordered_map &S,unordered_map &variables){ + BUILTIN_GUARD_STACKSIZE("arrset",3) + const Stackitem idx=move(S.back()); S.pop_back(); + Stackitem val=move(S.back()); S.pop_back(); + if(idx.type!=SIT_INT) + throw string("Second argument to 'arrset' not a number"); + if(S.back().type!=SIT_ARR) + throw string("Top of stack is not an array in builtin 'arrset'"); + if(idx.intval<0||idx.intval>=S.back().arrval.size()) + throw string("Index argument to 'arrset' out of range"); + S.back().arrval[idx.intval]=move(val); + }}, + //leaves the array on the stack {"arrlen",[](vector &S,unordered_map &variables){ BUILTIN_GUARD_STACKSIZE("arrlen",1) if(S.back().type!=SIT_ARR) diff --git a/reference.md b/reference.md index 1938d5f..13c7b2f 100644 --- a/reference.md +++ b/reference.md @@ -44,6 +44,7 @@ s `ord` | Gets the ascii value of the first character in string `s` (replacing ` `mkarray` | Creates an empty array on the stack [v...] n `mkarrayp` | Takes `n` elements off the stack and creates a new array with these `n` elements a i `arridx` | Gets index `i` from array `a`, leaving `a` on the stack +a v i `arrset` | Sets index `i` in array `a` to value `v`, leaving `a` on the stack a `arrlen` | Gets the length of array `a`, leaving `a` on the stack a v `arrpush` | Pushes the value `v` on the end of array `a` (leaving `a` on the stack, obviously) a v `arrpushf` | Pushes the value `v` at the front of array `a` (leaving `a` on the stack, obviously) -- cgit v1.2.3-70-g09d2