diff --git a/src/lexer.rs b/src/lexer.rs index 87a0bbe..52d7206 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -145,9 +145,10 @@ fn read_block(str_words: &[String], isfn: bool) -> Result<(Option, Words, u types.push(str_words[i].to_owned()); i += 1; } - let blk = read_block(&str_words[i..], false)?; - i += 2 + blk.2; - let ctch = read_block(&str_words[i..], false)?; + let blk = read_block(&str_words[i + 1..], false)?; + i += 1 + blk.2; + let ctch = read_block(&str_words[i + 1..], false)?; + i += 1 + ctch.2; words.push(Word::Key(Keyword::Catch(types, blk.1, ctch.1))) } "with" => { diff --git a/src/std_fns.rs b/src/std_fns.rs index 91076ca..0d2a4a0 100644 --- a/src/std_fns.rs +++ b/src/std_fns.rs @@ -18,6 +18,12 @@ macro_rules! type_err { }; } +macro_rules! array { + ($stack:expr, $i:expr) => { + || $stack.error(ErrorKind::PropertyNotFound("array".to_owned(), $i.to_string())) + }; +} + pub fn print(stack: &mut Stack) -> OError { let Value::Str(s) = stack.pop().lock_ro().native.clone() else { return stack.err(ErrorKind::InvalidCall("print".to_owned())) @@ -113,7 +119,7 @@ pub fn array_get(stack: &mut Stack) -> OError { let Value::Mega(i) = stack.pop().lock_ro().native.clone() else { return stack.err(ErrorKind::InvalidCall("array-get".to_owned())) }; - stack.push(a[i as usize].clone()); + stack.push(a.get(i as usize).ok_or_else(array!(stack, i))?.clone()); Ok(()) } @@ -126,8 +132,8 @@ pub fn array_set(stack: &mut Stack) -> OError { return stack.err(ErrorKind::InvalidCall("array-set".to_owned())) }; let o = stack.pop(); - stack.push(a[i as usize].clone()); - a[i as usize] = o; + stack.push(a.get(i as usize).ok_or_else(array!(stack, i))?.clone()); + *a.get_mut(i as usize).ok_or_else(array!(stack, i))? = o; Ok(()) } diff --git a/test.spl b/test.spl index 332db24..9f225d5 100644 --- a/test.spl +++ b/test.spl @@ -112,11 +112,23 @@ func main { int | with args ; { | println } (" " "hello how are you" _:split):foreach "" println - use net:http:Request - "testing http" println - def req "tudbut.de" 81 "GET" "/" Request:new =req - req:send:body _str println + catch { + use net:http:Request + "testing http" println + def req "tudbut.de" 81 "GET" "/" Request:new =req + req:send:body _str println + } + with { with e ; + e:message println + "it seems the internet is not available" println + } "" println + catch { + "heya" throw + } with { with e ; + e:object println + } + 100 }