This commit is contained in:
Scott Richmond 2024-10-23 17:27:32 -04:00
parent 54e7597b33
commit bb0aaef060
6 changed files with 31 additions and 8 deletions

Binary file not shown.

View File

@ -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 {

Binary file not shown.

View File

@ -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

View File

@ -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 ")

View File

@ -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)))