summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2019-11-21 23:23:38 +0100
committerTom Smeding <tom.smeding@gmail.com>2019-11-21 23:23:38 +0100
commite99298d8719c71ff23921fdb9a79197880ac5bfc (patch)
tree05cd9bba78dd00a7da29284a0d9e95739f4d0537
parent206689c943ba93ad48ce2b41166ca1902ed920a9 (diff)
Rotate arguments to substr and allow negative length
-rw-r--r--VM.hs5
-rw-r--r--tests/stringtest.lisp2
-rw-r--r--tests/stringtest.out2
3 files changed, 5 insertions, 4 deletions
diff --git a/VM.hs b/VM.hs
index 64674fb..ea94597 100644
--- a/VM.hs
+++ b/VM.hs
@@ -152,8 +152,9 @@ vmRunBuiltin state "sys-stdin" [] = return (RVNum (-1), state)
vmRunBuiltin state "sys-stdout" [] = return (RVNum (-2), state)
vmRunBuiltin state "sys-stderr" [] = return (RVNum (-3), state)
vmRunBuiltin state "length" [RVString str] = return (RVNum (length str), state)
-vmRunBuiltin state "substr" [RVString str, RVNum idx, RVNum len] =
- return (RVString (take len (drop idx str)), state)
+vmRunBuiltin state "substr" [RVNum idx, RVNum len, RVString str] =
+ let s = (if len >= 0 then take len else id) (drop idx str)
+ in return (RVString s, state)
vmRunBuiltin state "ord" [RVString str] = return (RVNum (case str of { "" -> 0; c:_ -> ord c }), state)
vmRunBuiltin state "chr" [RVNum num] = return (RVString [chr num], state)
vmRunBuiltin state "concat" values
diff --git a/tests/stringtest.lisp b/tests/stringtest.lisp
index aa4898c..57a7ec4 100644
--- a/tests/stringtest.lisp
+++ b/tests/stringtest.lisp
@@ -4,6 +4,6 @@
(for 0 (length str) (lambda (i)
(if (= (mod i 60) 0)
- (sys-put-string stdout (concat "<" (substr str i 10) ">"))
+ (sys-put-string stdout (concat "<" (substr i 10 str) ">"))
'())))
(print "")
diff --git a/tests/stringtest.out b/tests/stringtest.out
index a0c457b..5011462 100644
--- a/tests/stringtest.out
+++ b/tests/stringtest.out
@@ -1 +1 @@
-<#include "><test.lisp"><d i 60) 0)><r str i 10>
+<#include "><test.lisp"><d i 60) 0)><r i 10 str>