Merge pull request #3 from viniciusfdasilva/dev

Commits of day 23-12-2023
This commit is contained in:
Vinicius Silva 2023-12-23 23:47:20 -03:00 committed by GitHub
commit 999ea8de8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 256 additions and 3 deletions

49
04_conditions/if.v Normal file
View File

@ -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
}

View File

@ -1,3 +0,0 @@
pub fn main() {
println('oi')
}

4
04_conditions/match.v Normal file
View File

@ -0,0 +1,4 @@
pub fn main()
{
println('oi')
}

88
05_conditions/if.v Normal file
View File

@ -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
}

42
05_conditions/in.v Normal file
View File

@ -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
}

73
05_conditions/match.v Normal file
View File

@ -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
}