have rust make words

Former-commit-id: a75a5b8881
This commit is contained in:
Scott Richmond 2025-07-06 16:39:29 -04:00
parent 866a511fab
commit 2543ac1dac
9 changed files with 119 additions and 64 deletions

View File

@ -431,7 +431,7 @@ fn condense {
fn words { fn words {
"Takes a string and returns a list of the words in the string. Strips all whitespace." "Takes a string and returns a list of the words in the string. Strips all whitespace."
(str as :string) -> do str > condense > strip > split (_, " ") (str as :string) -> base :words (str)
} }
fn sentence { fn sentence {

4
pkg/rudus.d.ts vendored
View File

@ -14,8 +14,8 @@ export interface InitOutput {
readonly __wbindgen_malloc: (a: number, b: number) => number; readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_export_6: WebAssembly.Table; readonly __wbindgen_export_6: WebAssembly.Table;
readonly closure352_externref_shim: (a: number, b: number, c: any) => void; readonly closure359_externref_shim: (a: number, b: number, c: any) => void;
readonly closure365_externref_shim: (a: number, b: number, c: any, d: any) => void; readonly closure382_externref_shim: (a: number, b: number, c: any, d: any) => void;
readonly __wbindgen_start: () => void; readonly __wbindgen_start: () => void;
} }

View File

@ -17,6 +17,22 @@ function handleError(f, args) {
} }
} }
function logError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
let error = (function () {
try {
return e instanceof Error ? `${e.message}\n\nStack:\n${e.stack}` : e.toString();
} catch(_) {
return "<failed to stringify thrown value>";
}
}());
console.error("wasm-bindgen: imported JS function that was not marked as `catch` threw an error:", error);
throw e;
}
}
const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );
if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };
@ -54,6 +70,8 @@ const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
function passStringToWasm0(arg, malloc, realloc) { function passStringToWasm0(arg, malloc, realloc) {
if (typeof(arg) !== 'string') throw new Error(`expected a string argument, found ${typeof(arg)}`);
if (realloc === undefined) { if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg); const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0; const ptr = malloc(buf.length, 1) >>> 0;
@ -82,7 +100,7 @@ function passStringToWasm0(arg, malloc, realloc) {
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view); const ret = encodeString(arg, view);
if (ret.read !== arg.length) throw new Error('failed to pass whole string');
offset += ret.written; offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0; ptr = realloc(ptr, len, offset, 1) >>> 0;
} }
@ -104,6 +122,12 @@ function isLikeNone(x) {
return x === undefined || x === null; return x === undefined || x === null;
} }
function _assertBoolean(n) {
if (typeof(n) !== 'boolean') {
throw new Error(`expected a boolean argument, found ${typeof(n)}`);
}
}
const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined') const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} } ? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(state => { : new FinalizationRegistry(state => {
@ -210,12 +234,19 @@ export function ludus(src) {
return ret; return ret;
} }
function _assertNum(n) {
if (typeof(n) !== 'number') throw new Error(`expected a number argument, found ${typeof(n)}`);
}
function __wbg_adapter_20(arg0, arg1, arg2) { function __wbg_adapter_20(arg0, arg1, arg2) {
wasm.closure352_externref_shim(arg0, arg1, arg2); _assertNum(arg0);
_assertNum(arg1);
wasm.closure359_externref_shim(arg0, arg1, arg2);
} }
function __wbg_adapter_46(arg0, arg1, arg2, arg3) { function __wbg_adapter_46(arg0, arg1, arg2, arg3) {
wasm.closure365_externref_shim(arg0, arg1, arg2, arg3); _assertNum(arg0);
_assertNum(arg1);
wasm.closure382_externref_shim(arg0, arg1, arg2, arg3);
} }
async function __wbg_load(module, imports) { async function __wbg_load(module, imports) {
@ -260,7 +291,7 @@ function __wbg_get_imports() {
const ret = arg0.call(arg1, arg2); const ret = arg0.call(arg1, arg2);
return ret; return ret;
}, arguments) }; }, arguments) };
imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function(arg0, arg1) { imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function() { return logError(function (arg0, arg1) {
let deferred0_0; let deferred0_0;
let deferred0_1; let deferred0_1;
try { try {
@ -270,7 +301,7 @@ function __wbg_get_imports() {
} finally { } finally {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1); wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
} }
}; }, arguments) };
imports.wbg.__wbg_io_5a3c8ea72d8c6ea3 = function() { return handleError(function (arg0, arg1) { imports.wbg.__wbg_io_5a3c8ea72d8c6ea3 = function() { return handleError(function (arg0, arg1) {
let deferred0_0; let deferred0_0;
let deferred0_1; let deferred0_1;
@ -283,10 +314,10 @@ function __wbg_get_imports() {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1); wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
} }
}, arguments) }; }, arguments) };
imports.wbg.__wbg_log_11652c6a56eeddfb = function(arg0, arg1) { imports.wbg.__wbg_log_11652c6a56eeddfb = function() { return logError(function (arg0, arg1) {
console.log(getStringFromWasm0(arg0, arg1)); console.log(getStringFromWasm0(arg0, arg1));
}; }, arguments) };
imports.wbg.__wbg_new_23a2665fac83c611 = function(arg0, arg1) { imports.wbg.__wbg_new_23a2665fac83c611 = function() { return logError(function (arg0, arg1) {
try { try {
var state0 = {a: arg0, b: arg1}; var state0 = {a: arg0, b: arg1};
var cb0 = (arg0, arg1) => { var cb0 = (arg0, arg1) => {
@ -303,65 +334,65 @@ function __wbg_get_imports() {
} finally { } finally {
state0.a = state0.b = 0; state0.a = state0.b = 0;
} }
}; }, arguments) };
imports.wbg.__wbg_new_8a6f238a6ece86ea = function() { imports.wbg.__wbg_new_8a6f238a6ece86ea = function() { return logError(function () {
const ret = new Error(); const ret = new Error();
return ret; return ret;
}; }, arguments) };
imports.wbg.__wbg_newnoargs_105ed471475aaf50 = function(arg0, arg1) { imports.wbg.__wbg_newnoargs_105ed471475aaf50 = function() { return logError(function (arg0, arg1) {
const ret = new Function(getStringFromWasm0(arg0, arg1)); const ret = new Function(getStringFromWasm0(arg0, arg1));
return ret; return ret;
}; }, arguments) };
imports.wbg.__wbg_now_8dddb61fa4928554 = function() { imports.wbg.__wbg_now_8dddb61fa4928554 = function() { return logError(function () {
const ret = Date.now(); const ret = Date.now();
return ret; return ret;
}; }, arguments) };
imports.wbg.__wbg_queueMicrotask_97d92b4fcc8a61c5 = function(arg0) { imports.wbg.__wbg_queueMicrotask_97d92b4fcc8a61c5 = function() { return logError(function (arg0) {
queueMicrotask(arg0); queueMicrotask(arg0);
}; }, arguments) };
imports.wbg.__wbg_queueMicrotask_d3219def82552485 = function(arg0) { imports.wbg.__wbg_queueMicrotask_d3219def82552485 = function() { return logError(function (arg0) {
const ret = arg0.queueMicrotask; const ret = arg0.queueMicrotask;
return ret; return ret;
}; }, arguments) };
imports.wbg.__wbg_random_57c118f142535bb6 = function() { imports.wbg.__wbg_random_57c118f142535bb6 = function() { return logError(function () {
const ret = Math.random(); const ret = Math.random();
return ret; return ret;
}; }, arguments) };
imports.wbg.__wbg_resolve_4851785c9c5f573d = function(arg0) { imports.wbg.__wbg_resolve_4851785c9c5f573d = function() { return logError(function (arg0) {
const ret = Promise.resolve(arg0); const ret = Promise.resolve(arg0);
return ret; return ret;
}; }, arguments) };
imports.wbg.__wbg_stack_0ed75d68575b0f3c = function(arg0, arg1) { imports.wbg.__wbg_stack_0ed75d68575b0f3c = function() { return logError(function (arg0, arg1) {
const ret = arg1.stack; const ret = arg1.stack;
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN; const len1 = WASM_VECTOR_LEN;
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
}; }, arguments) };
imports.wbg.__wbg_static_accessor_GLOBAL_88a902d13a557d07 = function() { imports.wbg.__wbg_static_accessor_GLOBAL_88a902d13a557d07 = function() { return logError(function () {
const ret = typeof global === 'undefined' ? null : global; const ret = typeof global === 'undefined' ? null : global;
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
}; }, arguments) };
imports.wbg.__wbg_static_accessor_GLOBAL_THIS_56578be7e9f832b0 = function() { imports.wbg.__wbg_static_accessor_GLOBAL_THIS_56578be7e9f832b0 = function() { return logError(function () {
const ret = typeof globalThis === 'undefined' ? null : globalThis; const ret = typeof globalThis === 'undefined' ? null : globalThis;
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
}; }, arguments) };
imports.wbg.__wbg_static_accessor_SELF_37c5d418e4bf5819 = function() { imports.wbg.__wbg_static_accessor_SELF_37c5d418e4bf5819 = function() { return logError(function () {
const ret = typeof self === 'undefined' ? null : self; const ret = typeof self === 'undefined' ? null : self;
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
}; }, arguments) };
imports.wbg.__wbg_static_accessor_WINDOW_5de37043a91a9c40 = function() { imports.wbg.__wbg_static_accessor_WINDOW_5de37043a91a9c40 = function() { return logError(function () {
const ret = typeof window === 'undefined' ? null : window; const ret = typeof window === 'undefined' ? null : window;
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
}; }, arguments) };
imports.wbg.__wbg_then_44b73946d2fb3e7d = function(arg0, arg1) { imports.wbg.__wbg_then_44b73946d2fb3e7d = function() { return logError(function (arg0, arg1) {
const ret = arg0.then(arg1); const ret = arg0.then(arg1);
return ret; return ret;
}; }, arguments) };
imports.wbg.__wbg_then_48b406749878a531 = function(arg0, arg1, arg2) { imports.wbg.__wbg_then_48b406749878a531 = function() { return logError(function (arg0, arg1, arg2) {
const ret = arg0.then(arg1, arg2); const ret = arg0.then(arg1, arg2);
return ret; return ret;
}; }, arguments) };
imports.wbg.__wbindgen_cb_drop = function(arg0) { imports.wbg.__wbindgen_cb_drop = function(arg0) {
const obj = arg0.original; const obj = arg0.original;
if (obj.cnt-- == 1) { if (obj.cnt-- == 1) {
@ -369,12 +400,13 @@ function __wbg_get_imports() {
return true; return true;
} }
const ret = false; const ret = false;
_assertBoolean(ret);
return ret; return ret;
}; };
imports.wbg.__wbindgen_closure_wrapper1073 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper8162 = function() { return logError(function (arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 353, __wbg_adapter_20); const ret = makeMutClosure(arg0, arg1, 360, __wbg_adapter_20);
return ret; return ret;
}; }, arguments) };
imports.wbg.__wbindgen_debug_string = function(arg0, arg1) { imports.wbg.__wbindgen_debug_string = function(arg0, arg1) {
const ret = debugString(arg1); const ret = debugString(arg1);
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@ -394,10 +426,12 @@ function __wbg_get_imports() {
}; };
imports.wbg.__wbindgen_is_function = function(arg0) { imports.wbg.__wbindgen_is_function = function(arg0) {
const ret = typeof(arg0) === 'function'; const ret = typeof(arg0) === 'function';
_assertBoolean(ret);
return ret; return ret;
}; };
imports.wbg.__wbindgen_is_undefined = function(arg0) { imports.wbg.__wbindgen_is_undefined = function(arg0) {
const ret = arg0 === undefined; const ret = arg0 === undefined;
_assertBoolean(ret);
return ret; return ret;
}; };
imports.wbg.__wbindgen_string_get = function(arg0, arg1) { imports.wbg.__wbindgen_string_get = function(arg0, arg1) {

BIN
pkg/rudus_bg.wasm (Stored with Git LFS)

Binary file not shown.

View File

@ -9,6 +9,6 @@ export const __wbindgen_free: (a: number, b: number, c: number) => void;
export const __wbindgen_malloc: (a: number, b: number) => number; export const __wbindgen_malloc: (a: number, b: number) => number;
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
export const __wbindgen_export_6: WebAssembly.Table; export const __wbindgen_export_6: WebAssembly.Table;
export const closure352_externref_shim: (a: number, b: number, c: any) => void; export const closure359_externref_shim: (a: number, b: number, c: any) => void;
export const closure365_externref_shim: (a: number, b: number, c: any, d: any) => void; export const closure382_externref_shim: (a: number, b: number, c: any, d: any) => void;
export const __wbindgen_start: () => void; export const __wbindgen_start: () => void;

View File

@ -85,7 +85,7 @@ pub fn chars(x: &Value) -> Value {
Value::Interned(s) => { Value::Interned(s) => {
let mut charlist = vector![]; let mut charlist = vector![];
for char in s.chars() { for char in s.chars() {
charlist.push_back(Value::string(char.to_string())) charlist.push_back(Value::from_string(char.to_string()))
} }
Value::Tuple(Rc::new(vec![ Value::Tuple(Rc::new(vec![
Value::Keyword("ok"), Value::Keyword("ok"),
@ -95,7 +95,7 @@ pub fn chars(x: &Value) -> Value {
Value::String(s) => { Value::String(s) => {
let mut charlist = vector![]; let mut charlist = vector![];
for char in s.chars() { for char in s.chars() {
charlist.push_back(Value::string(char.to_string())) charlist.push_back(Value::from_string(char.to_string()))
} }
Value::Tuple(Rc::new(vec![ Value::Tuple(Rc::new(vec![
Value::Keyword("ok"), Value::Keyword("ok"),
@ -106,6 +106,23 @@ pub fn chars(x: &Value) -> Value {
} }
} }
pub fn words(str: &Value) -> Value {
console_log!("wordsing words");
let str = Value::as_string(str);
let mut words = Vector::new();
let mut word = String::new();
for char in str.chars() {
if char.is_alphanumeric() {
word.push(char)
} else if !word.is_empty() {
words.push_back(Value::from_string(word));
word = String::new()
}
}
console_log!("words gathered into vector; returning to ludus");
Value::list(words)
}
// TODO: figure out how to get to opportunistic mutation here // TODO: figure out how to get to opportunistic mutation here
pub fn concat(x: &Value, y: &Value) -> Value { pub fn concat(x: &Value, y: &Value) -> Value {
match (x, y) { match (x, y) {
@ -193,11 +210,11 @@ pub fn first(ordered: &Value) -> Value {
None => Value::Nil, None => Value::Nil,
}, },
Value::String(s) => match s.chars().next() { Value::String(s) => match s.chars().next() {
Some(char) => Value::string(char.to_string()), Some(char) => Value::from_string(char.to_string()),
None => Value::Nil, None => Value::Nil,
}, },
Value::Interned(s) => match s.chars().next() { Value::Interned(s) => match s.chars().next() {
Some(char) => Value::string(char.to_string()), Some(char) => Value::from_string(char.to_string()),
None => Value::Nil, None => Value::Nil,
}, },
_ => unreachable!("internal Ludus error"), _ => unreachable!("internal Ludus error"),
@ -225,14 +242,14 @@ pub fn at(ordered: &Value, i: &Value) -> Value {
(Value::String(s), Value::Number(n)) => { (Value::String(s), Value::Number(n)) => {
let i = f64::from(*n) as usize; let i = f64::from(*n) as usize;
match s.chars().nth(i) { match s.chars().nth(i) {
Some(n) => Value::string(n.to_string()), Some(n) => Value::from_string(n.to_string()),
None => Value::Nil, None => Value::Nil,
} }
} }
(Value::Interned(s), Value::Number(n)) => { (Value::Interned(s), Value::Number(n)) => {
let i = f64::from(*n) as usize; let i = f64::from(*n) as usize;
match s.chars().nth(i) { match s.chars().nth(i) {
Some(n) => Value::string(n.to_string()), Some(n) => Value::from_string(n.to_string()),
None => Value::Nil, None => Value::Nil,
} }
} }
@ -269,11 +286,11 @@ pub fn last(ordered: &Value) -> Value {
None => Value::Nil, None => Value::Nil,
}, },
Value::String(s) => match s.chars().last() { Value::String(s) => match s.chars().last() {
Some(char) => Value::string(char.to_string()), Some(char) => Value::from_string(char.to_string()),
None => Value::Nil, None => Value::Nil,
}, },
Value::Interned(s) => match s.chars().last() { Value::Interned(s) => match s.chars().last() {
Some(char) => Value::string(char.to_string()), Some(char) => Value::from_string(char.to_string()),
None => Value::Nil, None => Value::Nil,
}, },
_ => unreachable!("internal Ludus error"), _ => unreachable!("internal Ludus error"),
@ -717,6 +734,10 @@ pub fn make_base() -> Value {
"upcase", "upcase",
Value::BaseFn(Box::new(BaseFn::Unary("upcase", upcase))), Value::BaseFn(Box::new(BaseFn::Unary("upcase", upcase))),
), ),
(
"words",
Value::BaseFn(Box::new(BaseFn::Unary("words", words))),
),
]; ];
let members = members let members = members
.iter() .iter()

View File

@ -51,11 +51,11 @@ impl std::fmt::Display for MsgIn {
impl MsgIn { impl MsgIn {
pub fn into_value(self) -> Value { pub fn into_value(self) -> Value {
match self { match self {
MsgIn::Input(str) => Value::string(str), MsgIn::Input(str) => Value::from_string(str),
MsgIn::Fetch(url, status_f64, string) => { MsgIn::Fetch(url, status_f64, string) => {
let url = Value::string(url); let url = Value::from_string(url);
let status = Value::from_f64(status_f64); let status = Value::from_f64(status_f64);
let text = Value::string(string); let text = Value::from_string(string);
let result_tuple = if status_f64 == 200.0 { let result_tuple = if status_f64 == 200.0 {
Value::tuple(vec![OK, text]) Value::tuple(vec![OK, text])
} else { } else {

View File

@ -455,7 +455,7 @@ impl Value {
} }
} }
pub fn string(str: String) -> Value { pub fn from_string(str: String) -> Value {
Value::String(Rc::new(str)) Value::String(Rc::new(str))
} }

View File

@ -390,7 +390,7 @@ impl World {
if zoo_msgs.is_empty() { if zoo_msgs.is_empty() {
None None
} else { } else {
let inner = zoo_msgs.into_iter().map(Value::string).collect::<imbl::Vector<_>>(); let inner = zoo_msgs.into_iter().map(Value::from_string).collect::<imbl::Vector<_>>();
Some(MsgOut::Console(Value::list(inner))) Some(MsgOut::Console(Value::list(inner)))
} }
} }
@ -437,9 +437,9 @@ impl World {
let result = self.active_result().clone().unwrap(); let result = self.active_result().clone().unwrap();
self.result = Some(result.clone()); self.result = Some(result.clone());
match result { match result {
Ok(value) => outbox.push(MsgOut::Complete(Value::string(value.show()))), Ok(value) => outbox.push(MsgOut::Complete(Value::from_string(value.show()))),
Err(p) => { Err(p) => {
outbox.push(MsgOut::Console(Value::list(imbl::vector!(Value::string("Ludus panicked!".to_string()))))); outbox.push(MsgOut::Console(Value::list(imbl::vector!(Value::from_string("Ludus panicked!".to_string())))));
outbox.push(MsgOut::Error(panic(p))) outbox.push(MsgOut::Error(panic(p)))
} }
} }
@ -460,7 +460,7 @@ impl World {
} }
fn fill_input(&mut self, str: String) { fn fill_input(&mut self, str: String) {
let value = Value::string(str); let value = Value::from_string(str);
let working = RefCell::new(value); let working = RefCell::new(value);
let input = self.buffers.input(); let input = self.buffers.input();
input.swap(&working); input.swap(&working);
@ -497,7 +497,7 @@ impl World {
let console = self.buffers.console(); let console = self.buffers.console();
let mut console = console.as_ref().borrow_mut(); let mut console = console.as_ref().borrow_mut();
let Value::List(ref mut console) = *console else {unreachable!("expect console to be a list")}; let Value::List(ref mut console) = *console else {unreachable!("expect console to be a list")};
console.push_back(Value::string(msg)); console.push_back(Value::from_string(msg));
} }
fn report_process_end(&mut self) { fn report_process_end(&mut self) {