diff --git a/prelude.ld b/prelude.ld index fefbf93..b15e3d5 100644 --- a/prelude.ld +++ b/prelude.ld @@ -44,6 +44,7 @@ fn ordered? { "Returns true if a value is an indexed collection: list or tuple." (coll as :list) -> true (coll as :tuple) -> true + (coll as :string) -> true (_) -> false } @@ -138,6 +139,7 @@ fn rest { (()) -> () (xs as :list) -> base :rest (xs) (xs as :tuple) -> base :rest (xs) + (xs as :string) -> base :str_slice (xs, 1) } fn inc { @@ -629,10 +631,13 @@ fn at { "Returns the element at index n of a list or tuple, or the byte at index n of a string. Zero-indexed: the first element is at index 0. Returns nil if nothing is found in a list or tuple; returns an empty string if nothing is found in a string." (xs as :list, n as :number) -> base :nth (n, xs) (xs as :tuple, n as :number) -> base :nth (n, xs) - (str as :string, n as :number) -> when { - neg? (n) -> "" - gte? (n, count (str)) -> "" - true -> base :slice (str, n, inc (n)) + (str as :string, n as :number) -> { + let raw = base :nth (n, str) + when { + nil? (raw) -> nil + gte? (raw, 128) -> panic! "not an ASCII char" + true -> raw + } } (_) -> nil } @@ -1283,7 +1288,9 @@ fn apply_command { (:home) -> do state > assoc (_, :position, (0, 0)) > assoc (_, :heading, 0) - (:clear) -> assoc (state, :position, (0, 0)) + (:clear) -> do state > + assoc (state, :position, (0, 0)) > + assoc (_, :heading, 0) (:right, turns) -> update (state, :heading, add (_, turns)) (:left, turns) -> update (state, :heading, sub (_, turns)) (:forward, steps) -> { diff --git a/src/interpreter.janet b/src/interpreter.janet index a7e4a25..5858706 100644 --- a/src/interpreter.janet +++ b/src/interpreter.janet @@ -640,7 +640,7 @@ # # (when (has-errors? validated) (break (validated :errors))) # # (def cleaned (get-in parsed [:ast :data 1])) # # # (pp cleaned) -# (interpret (parsed :ast) @{:^parent b/ctx}) +# (interpret (parsed :ast) @{:^parent b/lett}) # # (try (interpret (parsed :ast) @{:^parent b/ctx}) # # ([e] (if (struct? e) (error (e :msg)) (error e)))) # ) diff --git a/src/ludus.janet b/src/ludus.janet index da43994..b438c35 100644 --- a/src/ludus.janet +++ b/src/ludus.janet @@ -51,18 +51,18 @@ (comment # (do -(def source ` -contains? (:d, [:a, :b]) -`) -(def out (-> source - ludus - j/decode - )) -(setdyn :out stdout) -(pp out) -(def console (out "console")) -(print console) -(def result (out "result")) -(print result) + (def source ` + add (1, 2) + `) + (def out (-> source + ludus + j/decode + )) + (setdyn :out stdout) + # (pp out) + (def console (out "console")) + (print console) + (def result (out "result")) + (print result) ) diff --git a/src/parser.janet b/src/parser.janet index 0286fac..bdad83c 100644 --- a/src/parser.janet +++ b/src/parser.janet @@ -1117,10 +1117,10 @@ # (do (comment (def source ` -"{f} a {f}" +let foo = :bar `) (def scanned (s/scan source)) # (print "\n***NEW PARSE***\n") (def a-parser (new-parser scanned)) -(def parsed (interpolated a-parser)) +(def parsed (lett a-parser)) ) diff --git a/src/scanner.janet b/src/scanner.janet index 903a03a..6cbbe78 100644 --- a/src/scanner.janet +++ b/src/scanner.janet @@ -350,6 +350,6 @@ (comment # (do - (def source "/iii") + (def source "add 1 2 () four") (scan source) )