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 diff --git a/05_conditions/if.v b/05_conditions/if.v new file mode 100644 index 0000000..48a15bf --- /dev/null +++ b/05_conditions/if.v @@ -0,0 +1,88 @@ +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) + } + + // ================================================================ // + + 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 +{ + return 42 +} \ No newline at end of file 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 diff --git a/05_conditions/match.v b/05_conditions/match.v new file mode 100644 index 0000000..74feb0f --- /dev/null +++ b/05_conditions/match.v @@ -0,0 +1,73 @@ +pub fn main() +{ + 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