From c32419508ef8157e54c207b433091325b0de4f7c Mon Sep 17 00:00:00 2001 From: mark Date: Tue, 23 May 2023 23:56:45 +0200 Subject: [PATCH] fixed examples --- examples/custom_type.mers | 2 +- examples/get_ref.mers | 13 +++++-------- examples/iterators.mers | 4 ++-- examples/thread.mers | 7 +++---- mers/src/lang/builtins.rs | 40 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/examples/custom_type.mers b/examples/custom_type.mers index f505cb5..47f708e 100644 --- a/examples/custom_type.mers +++ b/examples/custom_type.mers @@ -10,7 +10,7 @@ fn print_linked_list(start elem) { println("[END]") true // break } - elem start = elem // continue + elem &start = elem // continue } } [] diff --git a/examples/get_ref.mers b/examples/get_ref.mers index 03d1fe1..6e33329 100644 --- a/examples/get_ref.mers +++ b/examples/get_ref.mers @@ -1,13 +1,10 @@ list = [1 2 3 4 5 6 7 8 9 ...] -// second = &list.get_ref(2).assume1() -// second.debug() -// *second = 24 -// second.debug() - -&list.get_ref(2).assume1() = 24 -should_not_be_changeable = &list.get(3).assume1() -should_not_be_changeable = 24 +// calling get on an &list will get a reference +&list.get(2).assume1() = 24 +// calling get on a list will get a value +should_not_be_changeable = list.get(3).assume1() +&should_not_be_changeable = 24 if list.get(2) != [24] println("[!!] list.get(2) != 24 (was {0})".format(list.get(2).to_string())) if list.get(3) == [24] println("[!!] list.get(3) == 24") diff --git a/examples/iterators.mers b/examples/iterators.mers index ff4d2af..ad45e98 100644 --- a/examples/iterators.mers +++ b/examples/iterators.mers @@ -27,8 +27,8 @@ fn square_numbers() { i = 0 val = 0 () { - val = val + { 2 * i } + 1 - i = i + 1 + &val = val + { 2 * i } + 1 + &i = i + 1 [val] } } diff --git a/examples/thread.mers b/examples/thread.mers index dbcadde..f542746 100644 --- a/examples/thread.mers +++ b/examples/thread.mers @@ -12,11 +12,10 @@ calculator = (max int) { sum = 0 for i max { i = i + 1 - // println("i: {0}".format(i.to_string())) - println("i: {0} s: {1}".format(i.to_string() sum.to_string())) - sum = sum + i + 1 + // println("i: {0} s: {1}".format(i.to_string() sum.to_string())) + &sum = sum + i if fake_delay sleep(1) - v = i * 100 / max + &progress = i * 100 / max } "the sum of all numbers from 0 to {0} is {1}!".format(max.to_string() sum.to_string()) } diff --git a/mers/src/lang/builtins.rs b/mers/src/lang/builtins.rs index 8d046d7..ae9fdd5 100755 --- a/mers/src/lang/builtins.rs +++ b/mers/src/lang/builtins.rs @@ -587,7 +587,43 @@ impl BuiltinFunction { unreachable!("await called without args") } } - Self::Pop | Self::Remove | Self::Get => { + Self::Pop => { + if let Some(v) = input.first() { + if let Some(v) = v.dereference() { + VType { + types: vec![ + VSingleType::Tuple(vec![]), + VSingleType::Tuple(vec![v + .get_any(info) + .expect("cannot use get on this type")]), + ], + } + } else { + unreachable!("pop called on a non-reference"); + } + } else { + unreachable!("pop called without args"); + } + } + Self::Remove => { + if input[1].fits_in(&VSingleType::Int.to(), info).is_empty() { + if let Some(v) = input[0].dereference() { + VType { + types: vec![ + VSingleType::Tuple(vec![]), + VSingleType::Tuple(vec![v + .get_any(info) + .expect("cannot use get on this type")]), + ], + } + } else { + unreachable!("remove called on a non-reference"); + } + } else { + unreachable!("remove called, but second arg not an int"); + } + } + Self::Get => { if let Some(v) = input.first() { VType { types: vec![ @@ -598,7 +634,7 @@ impl BuiltinFunction { ], } } else { - unreachable!("get, pop or remove called without args") + unreachable!("get called without args") } } Self::Exit => VType { types: vec![] }, // doesn't return