From 62e0326bc9646768429eef28605134e12b858584 Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Fri, 29 Dec 2023 01:03:03 -0300 Subject: [PATCH 1/4] 10_goto directory created --- 10_goto/main.v | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 10_goto/main.v diff --git a/10_goto/main.v b/10_goto/main.v new file mode 100644 index 0000000..07e4004 --- /dev/null +++ b/10_goto/main.v @@ -0,0 +1,15 @@ +pub fn main(){ + + x := true + y := false + + if x { + if !y { + unsafe { goto my_label } + } + return + } + + my_label: + println('oi') +} \ No newline at end of file From e7787fca522edb15614ae21596188f58dc0f5676 Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Fri, 29 Dec 2023 10:53:21 -0300 Subject: [PATCH 2/4] 11_structs directory created --- 11_structs/default_structs.v | 11 +++++++++++ 11_structs/heap_structs.v | 11 +++++++++++ 11_structs/heap_structs2.v | 23 +++++++++++++++++++++++ 11_structs/literal_struct.v | 17 +++++++++++++++++ 11_structs/required_structs.v | 12 ++++++++++++ 11_structs/structs.v | 15 +++++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 11_structs/default_structs.v create mode 100644 11_structs/heap_structs.v create mode 100644 11_structs/heap_structs2.v create mode 100644 11_structs/literal_struct.v create mode 100644 11_structs/required_structs.v create mode 100644 11_structs/structs.v diff --git a/11_structs/default_structs.v b/11_structs/default_structs.v new file mode 100644 index 0000000..61d2ae0 --- /dev/null +++ b/11_structs/default_structs.v @@ -0,0 +1,11 @@ +struct Foo{ + n int // Default is 0 + s string // Default is '' + a []int // Default is []int{} + pos int = -1 // Default is -1 +} + +pub fn main(){ + mut f := Foo{1,'Test', [12,3,5,3,6], -2} + println(f) +} \ No newline at end of file diff --git a/11_structs/heap_structs.v b/11_structs/heap_structs.v new file mode 100644 index 0000000..fb8a74e --- /dev/null +++ b/11_structs/heap_structs.v @@ -0,0 +1,11 @@ +struct Point{ + x int + y int +} + + + +pub fn main(){ + p := &Point{10,10} + println(p.x) +} \ No newline at end of file diff --git a/11_structs/heap_structs2.v b/11_structs/heap_structs2.v new file mode 100644 index 0000000..130c1dc --- /dev/null +++ b/11_structs/heap_structs2.v @@ -0,0 +1,23 @@ +struct Foo{ + mut: + x int +} + +pub fn main(){ + fa := Foo{1} + mut a := fa + a.x = 2 + assert fa.x == 1 + assert a.x == 2 + + mut fc := Foo{1} + mut c := &fc + + c.x = 2 + assert fc.x == 2 + assert c.x == 2 + + println(fc) + println(c) + +} \ No newline at end of file diff --git a/11_structs/literal_struct.v b/11_structs/literal_struct.v new file mode 100644 index 0000000..e8dcf35 --- /dev/null +++ b/11_structs/literal_struct.v @@ -0,0 +1,17 @@ + +struct Point{ + x int + y int +} + +pub fn main(){ + mut p := Point{x: 10, y: 20} + + assert p.x == 10 + + mut p1 := Point{x: 30, y: 4} + + assert p1.y == 4 + point := [Point{x: 10, y: 20},Point{x: 20, y: 30},Point{x: 30, y: 40}] + println(point) +} \ No newline at end of file diff --git a/11_structs/required_structs.v b/11_structs/required_structs.v new file mode 100644 index 0000000..0bdc485 --- /dev/null +++ b/11_structs/required_structs.v @@ -0,0 +1,12 @@ +struct Foo{ + m int @[required] + n string +} + +pub fn main(){ + mut a := Foo{1, ''} + assert a.m == 1 + + _ := Foo{1,'Test'} + +} \ No newline at end of file diff --git a/11_structs/structs.v b/11_structs/structs.v new file mode 100644 index 0000000..85583e3 --- /dev/null +++ b/11_structs/structs.v @@ -0,0 +1,15 @@ + +struct Point{ + x int + y int +} + +pub fn main(){ + mut p := Point{x: 10, y: 20} + println('${p.x} | ${p.y}') + + + p1 := Point{10, 20} + assert p1.x == 10 +} + From c84fde67724b123c0279e36db38503066c475e84 Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Fri, 29 Dec 2023 14:05:24 -0300 Subject: [PATCH 3/4] code implemented on 11_struct directory --- 11_structs/access_structs.v | 16 ++++++++++++++++ 11_structs/anonymous.v | 17 +++++++++++++++++ 11_structs/static_types.v | 11 +++++++++++ 11_structs/trailing_structs.v | 26 ++++++++++++++++++++++++++ 11_structs/update_struct.v | 22 ++++++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 11_structs/access_structs.v create mode 100644 11_structs/anonymous.v create mode 100644 11_structs/static_types.v create mode 100644 11_structs/trailing_structs.v create mode 100644 11_structs/update_struct.v diff --git a/11_structs/access_structs.v b/11_structs/access_structs.v new file mode 100644 index 0000000..2a47912 --- /dev/null +++ b/11_structs/access_structs.v @@ -0,0 +1,16 @@ +struct Foo{ + a int + mut: + b int + c int + pub: + d int + pub mut: + e int + __global: + f int +} + +pub fn main(){ + println("OI") +} \ No newline at end of file diff --git a/11_structs/anonymous.v b/11_structs/anonymous.v new file mode 100644 index 0000000..153b9d9 --- /dev/null +++ b/11_structs/anonymous.v @@ -0,0 +1,17 @@ +struct Book{ + author struct { + name string + age int + } + + title string +} + + +pub fn main(){ + book := Book{author: struct{name: 'Samantha Black', age: 23}, title: 'Livro'} + + assert book.author.name == 'Samantha Black' + assert book.author.age == 23 + assert book.title == 'Livro' +} \ No newline at end of file diff --git a/11_structs/static_types.v b/11_structs/static_types.v new file mode 100644 index 0000000..bb4c1c2 --- /dev/null +++ b/11_structs/static_types.v @@ -0,0 +1,11 @@ + +struct User{} + +fn User.new() User{ + return User{} +} + +pub fn main(){ + user := User.new() + println(user) +} \ No newline at end of file diff --git a/11_structs/trailing_structs.v b/11_structs/trailing_structs.v new file mode 100644 index 0000000..3650246 --- /dev/null +++ b/11_structs/trailing_structs.v @@ -0,0 +1,26 @@ +@[params] +struct ButtonConfig{ + text string = 'oi' + is_disabled bool + width int = 70 + height int //= 20 +} + +struct Button{ + text string + width int + height int +} + +fn new_button(c ButtonConfig) &Button{ + return &Button{ + width: c.width + height: c.height + text: c.text + } +} + +pub fn main(){ + button := new_button() + assert button.height == 0 +} \ No newline at end of file diff --git a/11_structs/update_struct.v b/11_structs/update_struct.v new file mode 100644 index 0000000..88383de --- /dev/null +++ b/11_structs/update_struct.v @@ -0,0 +1,22 @@ +struct User{ + name string + age int + is_registered bool +} + +fn register(u User) User{ + return User{ + ...u + is_registered: true + } +} + +pub fn main(){ + mut user := User{ + name: 'abc' + age: 23 + } + + user = register(user) + println(user) +} \ No newline at end of file From c93bd34a2fe5ce1ef8eb2405781fec01dd5a3c60 Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Fri, 29 Dec 2023 17:17:32 -0300 Subject: [PATCH 4/4] Structs code file implemented --- 11_structs/embedded_structs.v | 33 ++++++++++++++++++++++++++++++ 11_structs/methods_structs.v | 18 ++++++++++++++++ 11_structs/sample/noinit_structs.v | 8 ++++++++ 11_structs/sample/sample.v | 18 ++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 11_structs/embedded_structs.v create mode 100644 11_structs/methods_structs.v create mode 100644 11_structs/sample/noinit_structs.v create mode 100644 11_structs/sample/sample.v diff --git a/11_structs/embedded_structs.v b/11_structs/embedded_structs.v new file mode 100644 index 0000000..6bfc3fd --- /dev/null +++ b/11_structs/embedded_structs.v @@ -0,0 +1,33 @@ +struct Size{ +mut: + width int + height int +} + +fn (s &Size) area() int{ + return s.width * s.height +} + +struct Button{ + Size + title string +} + +pub fn main(){ + mut button := Button{title: 'Click me', height: 2} + button.width = 3 + + assert button.area() == 6 + assert button.Size.area() == 6 + println(button) + + mut button1 := Button{Size: Size{ + width : 34 + height: 10 + }} + + println(button1) + + button1.Size = Size{width: 2, height: 9} + println(button1) +} \ No newline at end of file diff --git a/11_structs/methods_structs.v b/11_structs/methods_structs.v new file mode 100644 index 0000000..e66c745 --- /dev/null +++ b/11_structs/methods_structs.v @@ -0,0 +1,18 @@ +struct User{ + age int +} + +fn (u User) can_register() bool{ + return u.age > 16 +} + +pub fn main(){ + + user1 := User{age: 10} + + println(user1.can_register()) + + user2 := User{age: 20} + + println(user2.can_register()) +} \ No newline at end of file diff --git a/11_structs/sample/noinit_structs.v b/11_structs/sample/noinit_structs.v new file mode 100644 index 0000000..dccda3e --- /dev/null +++ b/11_structs/sample/noinit_structs.v @@ -0,0 +1,8 @@ +import sample + +fn main(){ + //mut into1 := sample.Information{data: 'Sample'} + //println(into1) + info := sample.new_information('Sample information')! + println(info) +} \ No newline at end of file diff --git a/11_structs/sample/sample.v b/11_structs/sample/sample.v new file mode 100644 index 0000000..0c75d09 --- /dev/null +++ b/11_structs/sample/sample.v @@ -0,0 +1,18 @@ +module sample + +@[noinit] +pub struct Information{ + pub: + data string +} + +pub fn new_information(data string) !Information{ + + if data.len == 0 || data.len > 100{ + return error('data must be between 1 and 100 characters') + }else{ + return Information{ + data: data + } + } +} \ No newline at end of file