From 24feeebaee647ad17d03a063410e902ab417e1bf Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Sat, 23 Dec 2023 00:24:02 -0300 Subject: [PATCH 1/5] Insert code on 04_conditions directory --- 04_conditions/if.v | 49 +++++++++++++++++++++++++++++++++++++++++++ 04_conditions/main.v | 3 --- 04_conditions/match.v | 4 ++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 04_conditions/if.v delete mode 100644 04_conditions/main.v create mode 100644 04_conditions/match.v diff --git a/04_conditions/if.v b/04_conditions/if.v new file mode 100644 index 0000000..56ffc7c --- /dev/null +++ b/04_conditions/if.v @@ -0,0 +1,49 @@ + + +pub fn main() +{ + mut a := 10 + mut b := 20 + + // Simple if structure + if a < b + { + println("${a} é menor do que ${b}") + }else if(a > b) + { + println("${a} é maior do que ${b}") + }else + { + println("${a} é igual a ${b}") + } + + num := 777 + + s := if num % 2 == 0 { 'even' } else { 'odd' } + println(s) + + + // ================================================================ // + + m := { + 'foor' : 'bar' + } + + if v := m['foo'] + { + println(v) + }else + { + println('not found') + } + + if c := res() + { + println(c) + } +} + +fn res() !int +{ + return 42 +} \ No newline at end of file diff --git a/04_conditions/main.v b/04_conditions/main.v deleted file mode 100644 index bc4e457..0000000 --- a/04_conditions/main.v +++ /dev/null @@ -1,3 +0,0 @@ -pub fn main() { - println('oi') -} \ No newline at end of file diff --git a/04_conditions/match.v b/04_conditions/match.v new file mode 100644 index 0000000..39a5f15 --- /dev/null +++ b/04_conditions/match.v @@ -0,0 +1,4 @@ +pub fn main() +{ + println('oi') +} \ No newline at end of file From 716dbbcdfb4adc06dd5b47843697d7525c029a0f Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Sat, 23 Dec 2023 09:58:30 -0300 Subject: [PATCH 2/5] Moving codes to 05_conditions --- 05_conditions/if.v | 51 +++++++++++++++++++++++++++++++++++++++++++ 05_conditions/match.v | 4 ++++ 2 files changed, 55 insertions(+) create mode 100644 05_conditions/if.v create mode 100644 05_conditions/match.v diff --git a/05_conditions/if.v b/05_conditions/if.v new file mode 100644 index 0000000..84fcc28 --- /dev/null +++ b/05_conditions/if.v @@ -0,0 +1,51 @@ + + +pub fn main() +{ + mut a := 10 + mut b := 20 + + // Simple if structure + if a < b + { + println("${a} é menor do que ${b}") + }else if a > b + { + println("${a} é maior do que ${b}") + }else + { + println("${a} é igual a ${b}") + } + + num := 777 + + s := if num % 2 == 0 { 'even' } else { 'odd' } + println(s) + + + // ================================================================ // + + m := { + 'foo' : 'bar' + } + + if v := m['foo'] + { + println(v) + }else + { + println('not found') + } + + if c := res() + { + println(c) + } + + // ================================================================ // +} + +fn res() !int +{ + return 42 +} \ No newline at end of file diff --git a/05_conditions/match.v b/05_conditions/match.v new file mode 100644 index 0000000..39a5f15 --- /dev/null +++ b/05_conditions/match.v @@ -0,0 +1,4 @@ +pub fn main() +{ + println('oi') +} \ No newline at end of file From a8661b2c60c8727e8bd5548a87e53914334b1a3b Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Sat, 23 Dec 2023 12:13:49 -0300 Subject: [PATCH 3/5] Finalize if.v --- 05_conditions/if.v | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/05_conditions/if.v b/05_conditions/if.v index 84fcc28..48a15bf 100644 --- a/05_conditions/if.v +++ b/05_conditions/if.v @@ -1,5 +1,3 @@ - - pub fn main() { mut a := 10 @@ -43,6 +41,45 @@ pub fn main() } // ================================================================ // + + mut arr := [User{"John"}] + + u_name := if v := arr[0] + { + v.name + }else + { + "Unamed" + } + + println(u_name) + + x := Alphabet(Abc{"Test"}) + + if x is Abc + { + println(x) + }else if x !is Abc + { + println("Not Abc") + } + +} + +type Alphabet = Abc | Xyz + +struct Abc +{ + val string +} + +struct Xyz +{ + foo string +} + +struct User { + name string } fn res() !int From ee1c1a61be78b549b6b64329473528b59fc26670 Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Sat, 23 Dec 2023 14:04:13 -0300 Subject: [PATCH 4/5] Add code in match.v file --- 05_conditions/match.v | 71 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/05_conditions/match.v b/05_conditions/match.v index 39a5f15..74feb0f 100644 --- a/05_conditions/match.v +++ b/05_conditions/match.v @@ -1,4 +1,73 @@ pub fn main() { - println('oi') + os := 'win32' + + match os { + 'darwin' { println('macos') } + 'linux' { println('Linux') } + 'win32' { println('Windows') } + else { println('Unknown operatin system') } + } + + // =========================================================== // + + match true { + 2 == 3 { println('true') } + 2 % 4 == 0 { println('true') } + 2 > 4 { println('true') } + else { println('false') } + } + + match false { + 2 == 3 { println('false') } + 2 % 4 == 0 { println('false') } + 2 > 4 { println('false') } + else { println('false') } + } + + // =========================================================== // + + mut color := Color.red + println(is_red_or_blue(color)) + + // =========================================================== // + + c := `v` + + typ := match c { + `0`...`9` { "digit" } + `A`...`Z` { "uppercase" } + `a`...`z` { "lowercase" } + else { "other" } + } + + println(typ) + + // =========================================================== // + + d := 2 + + num := match d { + start...end { 1000 } + else { 0 } + } + + println(num) +} + +const start := 1 +const end := 10 + +fn is_red_or_blue(c Color) bool +{ + return match c { + .red, .blue { true } + .green { false } + } +} + +enum Color { + blue + red + green } \ No newline at end of file From d7a391d46b5c4795497791f91ddd49f37165dcc2 Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Sat, 23 Dec 2023 23:46:34 -0300 Subject: [PATCH 5/5] Code insert on in.v file --- 05_conditions/in.v | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 05_conditions/in.v diff --git a/05_conditions/in.v b/05_conditions/in.v new file mode 100644 index 0000000..f5dc5c4 --- /dev/null +++ b/05_conditions/in.v @@ -0,0 +1,42 @@ +pub fn main() { + + nums := [2,4,5] + println(2 in nums) // true + println(1 !in nums) // true + println(6 in nums) // false + + // ===================================================== // + + m := { + 'one' : 1 + 'two' : 2 + } + + println('one' in m) // true + println('two' !in m) // false + println('three' in m) // false + + // ===================================================== // + + parse := Parse{} + + if parse.token == .plus || parse.token == .minus || parse.token == .div || parse.token == .mult { + println('yes') + } + + if parse.token in [.plus, .minus, .div, .mult] { + println('yes') + } + +} + +struct Parse { + token Token +} + +enum Token { + plus + minus + div + mult +} \ No newline at end of file