diff --git a/build/ludus.jimage b/build/ludus.jimage index eed067a..85c35fc 100644 Binary files a/build/ludus.jimage and b/build/ludus.jimage differ diff --git a/build/out.mjs b/build/out.mjs index d8162e0..c00f957 100644 --- a/build/out.mjs +++ b/build/out.mjs @@ -6489,7 +6489,7 @@ var __emscripten_stack_alloc = (a0) => (__emscripten_stack_alloc = wasmExports[' var _emscripten_stack_get_current = () => (_emscripten_stack_get_current = wasmExports['emscripten_stack_get_current'])(); var ___cxa_is_pointer_type = createExportWrapper('__cxa_is_pointer_type', 1); var dynCall_jiji = Module['dynCall_jiji'] = createExportWrapper('dynCall_jiji', 5); -var ___emscripten_embedded_file_data = Module['___emscripten_embedded_file_data'] = 1816104; +var ___emscripten_embedded_file_data = Module['___emscripten_embedded_file_data'] = 1819008; function invoke_i(index) { var sp = stackSave(); try { diff --git a/build/out.wasm b/build/out.wasm index 7f9e40f..436d5d6 100755 Binary files a/build/out.wasm and b/build/out.wasm differ diff --git a/prelude.ld b/prelude.ld index a64a5fe..fd4e1e2 100644 --- a/prelude.ld +++ b/prelude.ld @@ -279,9 +279,10 @@ fn concat { } fn set { - "Takes an ordered collection--list or tuple--and turns it into a set." + "Takes an ordered collection--list or tuple--and turns it into a set. Returns sets unharmed." (xs as :list) -> fold (append, xs, ${}) (xs as :tuple) -> do xs > list > set + (xs as :set) -> xs } fn set? { @@ -434,6 +435,11 @@ fn sentence { (strs as :list) -> join (strs, " ") } +fn to_number { + "Takes a string that presumably contains a representation of a number, and tries to give you back the number represented. Returns a result tuple." + (num as :string) -> base :to_number (num) +} + &&& boxes: mutable state and state changes fn box? { @@ -1434,6 +1440,7 @@ pkg Prelude { sum_of_squares & math tan & math tau & math + to_number &strings numbers trim & strings tuple? & tuples turn/deg & math diff --git a/src/base.janet b/src/base.janet index 7c5ffe4..6ce3977 100644 --- a/src/base.janet +++ b/src/base.janet @@ -1,6 +1,8 @@ # A base library for Ludus # Only loaded in the prelude +(import /src/scanner :as s) + (defn bool [x] (if (= :^nil x) nil x)) (defn ludus/and [& args] (every? (map bool args))) @@ -248,6 +250,20 @@ ([e] (break [:err e]))) [:ok out]) +(defn to_number [str] + (when (string/find "&" str) + (break [:err (string "Could not parse `" str "` as a number")])) + (def scanned (s/scan (string/trim str))) + (when (< 0 (length (scanned :errors))) + (break [:err (string "Could not parse `" str "` as a number")])) + (def tokens (scanned :tokens)) + (when (< 3 (length tokens)) + (break [:err (string "Could not parse `" str "` as a number")])) + (def fst (first tokens)) + (when (not= :number (fst :type)) + (break [:err (string "Could not parse `" str "` as a number")])) + [:ok (fst :literal)]) + (def ctx { "add" + "and" ludus/and @@ -304,6 +320,7 @@ "sub" - "tan" math/tan "to_list" to_list + "to_number" to_number "trim" string/trim "triml" string/triml "trimr" string/trimr @@ -317,4 +334,4 @@ (set (b (keyword k)) v)) b)) - +(to_number " 123 a ") diff --git a/src/scanner.janet b/src/scanner.janet index b7f89d1..e593728 100644 --- a/src/scanner.janet +++ b/src/scanner.janet @@ -349,8 +349,7 @@ (recur (-> scanner (scan-token) (next-token))))) (recur (new-scanner source input))) -(comment -# (do - (def source "add 1 2 () four") - (scan source) -) +# (comment +(do + (def source " -123 ") + (length ((scan source) :tokens)))