summaryrefslogtreecommitdiff
path: root/VM.hs
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 /VM.hs
parent206689c943ba93ad48ce2b41166ca1902ed920a9 (diff)
Rotate arguments to substr and allow negative length
Diffstat (limited to 'VM.hs')
-rw-r--r--VM.hs5
1 files changed, 3 insertions, 2 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