examples: fix warnings when doing `./v -W -progress -check-syntax build-examples`

pull/6688/head
Delyan Angelov 2020-10-26 13:14:21 +02:00
parent a7e3092165
commit 9772eb7c96
14 changed files with 203 additions and 157 deletions

View File

@ -202,7 +202,7 @@ enum Direction {
// Utility functions // Utility functions
[inline] [inline]
fn min(a, b int) int { fn min(a int, b int) int {
if a < b { if a < b {
return a return a
} else { } else {
@ -211,7 +211,7 @@ fn min(a, b int) int {
} }
[inline] [inline]
fn max(a, b int) int { fn max(a int, b int) int {
if a > b { if a > b {
return a return a
} else { } else {
@ -229,7 +229,7 @@ fn abs(a int) int {
} }
[inline] [inline]
fn avg(a, b int) int { fn avg(a int, b int) int {
return (a + b) / 2 return (a + b) / 2
} }
@ -676,7 +676,7 @@ fn (app &App) draw_tiles() {
} }
match app.tile_format { match app.tile_format {
.normal { .normal {
app.gg.draw_text(xpos, ypos, '${1<<tidx}', fmt) app.gg.draw_text(xpos, ypos, '${1 << tidx}', fmt)
} }
.log { .log {
app.gg.draw_text(xpos, ypos, '$tidx', fmt) app.gg.draw_text(xpos, ypos, '$tidx', fmt)
@ -693,7 +693,7 @@ fn (app &App) draw_tiles() {
} }
.shifts { .shifts {
fs2 := int(f32(fmt.size) * 0.6) fs2 := int(f32(fmt.size) * 0.6)
app.gg.draw_text(xpos, ypos, '2<<${tidx-1}', { app.gg.draw_text(xpos, ypos, '2<<${tidx - 1}', {
fmt | fmt |
size: fs2 size: fs2
}) })

View File

@ -2,10 +2,10 @@ import sync
import time import time
// Simulate expensive computing using sleep function // Simulate expensive computing using sleep function
fn expensive_computing(id, duration int, mut wg sync.WaitGroup) { fn expensive_computing(id int, duration int, mut wg sync.WaitGroup) {
println('Executing expensive computing task (${id})...') println('Executing expensive computing task ($id)...')
time.sleep_ms(duration) time.sleep_ms(duration)
println('Finish task ${id} on ${duration} ms') println('Finish task $id on $duration ms')
wg.done() wg.done()
} }

View File

@ -1,12 +1,13 @@
module main module main
import time import time
import automaton import automaton
fn print_automaton(a &automaton.Automaton){ fn print_automaton(a &automaton.Automaton) {
for y := 1; y<a.field.maxy; y++ { for y := 1; y < a.field.maxy; y++ {
mut s := ' ' mut s := ' '
for x := 1; x<a.field.maxx; x++ { for x := 1; x < a.field.maxx; x++ {
cell := a.field.get(x,y) cell := a.field.get(x, y)
s += if cell == 1 { '@' } else { '.' } s += if cell == 1 { '@' } else { '.' }
} }
println(s) println(s)
@ -22,4 +23,3 @@ fn main() {
time.sleep_ms(100) time.sleep_ms(100)
} }
} }

View File

@ -5,51 +5,53 @@ import gx
import automaton import automaton
const ( const (
screen_width = 800 screen_width = 800
screen_height = 600 screen_height = 600
filled_color = gx.blue filled_color = gx.blue
)
fn new_graphics() &gg.Context {
glfw.init_glfw()
return gg.new_context(gg.Cfg{
width: screen_width
height: screen_height
use_ortho: true
create_window: true
resizable: false
window_title: 'v life (with gg, glfw, gx)'
window_user_ptr: 0
})
}
const (
graphics = new_graphics()
) )
[live] [live]
fn print_automaton(a &automaton.Automaton){ fn print_automaton(app &App) {
gg.clear(gx.white)
square_size := 18 square_size := 18
for y := 1; y<a.field.maxy; y++ { for y := 1; y < app.a.field.maxy; y++ {
for x := 1; x<a.field.maxx; x++ { for x := 1; x < app.a.field.maxx; x++ {
cell := a.field.get(x,y) cell := app.a.field.get(x, y)
if cell == 1 { if cell == 1 {
graphics.draw_rect(f32(square_size*x), f32(square_size*y), f32(square_size), app.gg.draw_rect(f32(square_size * x), f32(square_size * y), f32(square_size),
f32(square_size), filled_color) f32(square_size), filled_color)
} }
} }
} }
} }
fn main() { struct App {
mut a := automaton.gun() mut:
for { gg &gg.Context
if graphics.window.should_close() { graphics.window.destroy() break } a automaton.Automaton
gg.post_empty_event() // needed so the animation does not stop }
///////////////////////////////////////////////
a.update() fn frame(mut app App) {
print_automaton(a) app.gg.begin()
graphics.render() app.a.update()
} print_automaton(app)
app.gg.end()
}
fn main() {
mut app := App{
gg: 0
a: automaton.gun()
}
app.gg = gg.new_context({
bg_color: gx.white
frame_fn: frame
user_data: &app
width: screen_width
height: screen_height
use_ortho: true
create_window: true
resizable: false
window_title: 'v life (with gg, gx)'
})
app.gg.run()
} }

View File

@ -1,78 +1,91 @@
module automaton module automaton
///////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////
pub struct A2D { pub struct A2D {
pub mut: pub mut:
maxx int maxx int
maxy int maxy int
data &int data &int
} }
[inline] pub fn (a &A2D) set(x,y int, newval int) {
[inline]
pub fn (a &A2D) set(x int, y int, newval int) {
unsafe { unsafe {
mut e := &int(0) mut e := &int(0)
e = a.data + y*a.maxx + x e = a.data + y * a.maxx + x
*e = newval *e = newval
} }
} }
[inline] pub fn (a &A2D) get(x,y int) int {
[inline]
pub fn (a &A2D) get(x int, y int) int {
unsafe { unsafe {
mut e := &int(0) mut e := &int(0)
e = a.data + y*a.maxx + x e = a.data + y * a.maxx + x
return *e return *e
} }
} }
[inline] pub fn (a &A2D) clear() {
for y := 0; y<a.maxy; y++ { [inline]
for x := 0; x<a.maxx; x++ { pub fn (a &A2D) clear() {
a.set(x,y,0) for y := 0; y < a.maxy; y++ {
for x := 0; x < a.maxx; x++ {
a.set(x, y, 0)
} }
} }
} }
///////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////
pub struct Automaton { pub struct Automaton {
pub mut: pub mut:
field &A2D field &A2D
new_field &A2D new_field &A2D
} }
fn new_automaton(f [][]int) Automaton { fn new_automaton(f [][]int) Automaton {
maxy := f.len maxy := f.len
mut maxx := 0 mut maxx := 0
for y := 0; y<f.len; y++ { for y := 0; y < f.len; y++ {
if maxx < f[y].len { if maxx < f[y].len {
maxx = f[y].len maxx = f[y].len
} }
} }
size := (maxx * maxy) size := (maxx * maxy)
field := &A2D{ maxx: maxx maxy: maxy data: &int( vcalloc( 4 * (size) ) ) } field := &A2D{
new_field := &A2D{ maxx: maxx maxy: maxy data: &int( vcalloc( 4 * (size)) ) } maxx: maxx
for y in 0..field.maxy { maxy: maxy
for x in 0..field.maxx { data: &int(vcalloc(4 * (size)))
field.set( x, y, f[y][x] ) }
new_field := &A2D{
maxx: maxx
maxy: maxy
data: &int(vcalloc(4 * (size)))
}
for y in 0 .. field.maxy {
for x in 0 .. field.maxx {
field.set(x, y, f[y][x])
} }
} }
return Automaton{ field: field new_field: new_field } return Automaton{
field: field
new_field: new_field
}
} }
pub fn (mut aa Automaton) update() { pub fn (mut aa Automaton) update() {
aa.new_field.clear() aa.new_field.clear()
for y := 1; y<aa.field.maxy; y++ { for y := 1; y < aa.field.maxy; y++ {
for x := 1; x<aa.field.maxx; x++ { for x := 1; x < aa.field.maxx; x++ {
moore_sum := ( 0 + moore_sum := (0 + aa.field.get(x - 1, y - 1) + aa.field.get(x, y - 1) + aa.field.get(x +
aa.field.get(x-1,y-1) + aa.field.get(x,y-1) + aa.field.get(x+1,y-1) + 1, y - 1) + aa.field.get(x - 1, y) + 0 + aa.field.get(x + 1, y) + aa.field.get(x - 1, y + 1) +
aa.field.get(x-1,y ) + 0 + aa.field.get(x+1,y ) + aa.field.get(x, y + 1) + aa.field.get(x + 1, y + 1))
aa.field.get(x-1,y+1) + aa.field.get(x,y+1) + aa.field.get(x+1,y+1) cell := aa.field.get(x, y)
) v := if cell == 1 { moore_sum in [2, 3] } else { moore_sum == 3 }
cell := aa.field.get(x,y) aa.new_field.set(x, y, if v {
v := if cell == 1 { 1
moore_sum in [2, 3]
} else { } else {
moore_sum == 3 0
} })
aa.new_field.set(x,y, if v { 1 } else { 0 })
} }
} }
tmp := aa.field tmp := aa.field
@ -82,35 +95,64 @@ pub fn (mut aa Automaton) update() {
pub fn gun() Automaton { pub fn gun() Automaton {
mut field := [ mut field := [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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ton(field) return new_automaton(field)
} }

View File

@ -4,21 +4,21 @@ const (
) )
fn main() { fn main() {
hanoi(num, 'A','B','C') hanoi(num, 'A', 'B', 'C')
} }
fn move(n int, a, b string) int { fn move(n int, a string, b string) int {
println('Disc $n from $a to $b\...') println('Disc $n from $a to $b\...')
return 0 return 0
} }
fn hanoi(n int, a, b, c string) int { fn hanoi(n int, a string, b string, c string) int {
if n == 1 { if n == 1 {
move(1,a,c) move(1, a, c)
} else { } else {
hanoi(n-1, a, c, b) hanoi(n - 1, a, c, b)
move(n,a,c) move(n, a, c)
hanoi(n-1, b, a, c) hanoi(n - 1, b, a, c)
} }
return 0 return 0
} }

View File

@ -1,13 +1,13 @@
import os import os
fn regex_match(src, pat string) bool { fn regex_match(src string, pat string) bool {
src_size := src.len + 1 src_size := src.len + 1
pat_size := pat.len + 1 pat_size := pat.len + 1
mut memo := [][]int{len: src_size, init: []int{len: pat_size, init: -1}} mut memo := [][]int{len: src_size, init: []int{len: pat_size, init: -1}}
return regex_match_core(src, pat, 0, 0, mut memo) return regex_match_core(src, pat, 0, 0, mut memo)
} }
fn regex_match_core(src, pat string, src_pos, pat_pos int, mut memo [][]int) bool { fn regex_match_core(src string, pat string, src_pos int, pat_pos int, mut memo [][]int) bool {
if memo[src_pos][pat_pos] != -1 { if memo[src_pos][pat_pos] != -1 {
return memo[src_pos][pat_pos] == 1 return memo[src_pos][pat_pos] == 1
} }
@ -121,6 +121,6 @@ fn main() {
if pat == 'exit' { if pat == 'exit' {
break break
} }
println('[$cnt] whether `$src` matches `$pat`: ${regex_match(src,pat)}') println('[$cnt] whether `$src` matches `$pat`: ${regex_match(src, pat)}')
} }
} }

View File

@ -55,7 +55,7 @@ fn draw() {
// line(0, 0, 500, 500) // line(0, 0, 500, 500)
} }
fn draw_hollow_rect(x, y, w, h f32) { fn draw_hollow_rect(x f32, y f32, w f32, h f32) {
sgl.begin_line_strip() sgl.begin_line_strip()
sgl.v2f(x, y) sgl.v2f(x, y)
sgl.v2f(x + w, y) sgl.v2f(x + w, y)
@ -65,7 +65,7 @@ fn draw_hollow_rect(x, y, w, h f32) {
sgl.end() sgl.end()
} }
fn draw_filled_rect(x, y, w, h f32) { fn draw_filled_rect(x f32, y f32, w f32, h f32) {
sgl.begin_quads() sgl.begin_quads()
sgl.v2f(x, y) sgl.v2f(x, y)
sgl.v2f(x + w, y) sgl.v2f(x + w, y)

View File

@ -10,7 +10,7 @@ mut:
gg &gg.Context // used for drawing gg &gg.Context // used for drawing
} }
fn my_audio_stream_callback(buffer &f32, num_frames, num_channels int, mut acontext AppState) { fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int, mut acontext AppState) {
mut soundbuffer := buffer mut soundbuffer := buffer
for frame := 0; frame < num_frames; frame++ { for frame := 0; frame < num_frames; frame++ {
t := int(f32(acontext.frame_0 + frame) * 0.245) t := int(f32(acontext.frame_0 + frame) * 0.245)
@ -68,7 +68,7 @@ fn (mut state AppState) bsample(idx int) byte {
fn (mut state AppState) draw() { fn (mut state AppState) draw() {
// first, reset and setup ortho projection // first, reset and setup ortho projection
for x in 0 .. 1024 { for x in 0 .. 1024 {
mut y := 100 * (state.frames[2*x] + state.frames[2*x+1]) mut y := 100 * (state.frames[2 * x] + state.frames[2 * x + 1])
state.gg.draw_line(x, 200, x, 200 + y, gx.rgba(state.bsample(x), state.bsample(x + 300), state.gg.draw_line(x, 200, x, 200 + y, gx.rgba(state.bsample(x), state.bsample(x + 300),
state.bsample(x + 700), 255)) state.bsample(x + 700), 255))
} }

View File

@ -8,11 +8,11 @@ const (
) )
[inline] [inline]
fn sintone(periods, frame, num_frames int) f32 { fn sintone(periods int, frame int, num_frames int) f32 {
return math.sinf(f32(periods) * (2 * math.pi) * f32(frame) / f32(num_frames)) return math.sinf(f32(periods) * (2 * math.pi) * f32(frame) / f32(num_frames))
} }
fn my_audio_stream_callback(buffer &f32, num_frames, num_channels int) { fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int) {
ms := sw.elapsed().milliseconds() - sw_start_ms ms := sw.elapsed().milliseconds() - sw_start_ms
unsafe { unsafe {
mut soundbuffer := buffer mut soundbuffer := buffer

View File

@ -12,10 +12,10 @@ mut:
fn main() { fn main() {
if os.args.len < 2 { if os.args.len < 2 {
eprintln('Usage: play_wav file1.wav file2.wav ...') eprintln('Usage: play_wav file1.wav file2.wav ...')
play_sounds([os.resource_abs_path('uhoh.wav')])? play_sounds([os.resource_abs_path('uhoh.wav')]) ?
exit(1) exit(1)
} }
play_sounds(os.args[1..])? play_sounds(os.args[1..]) ?
} }
fn play_sounds(files []string) ? { fn play_sounds(files []string) ? {
@ -31,13 +31,13 @@ fn play_sounds(files []string) ? {
eprintln('skipping "$f" (not a .wav file)') eprintln('skipping "$f" (not a .wav file)')
continue continue
} }
player.play_wav_file(f)? player.play_wav_file(f) ?
} }
player.stop() player.stop()
} }
// //
fn audio_player_callback(buffer &f32, num_frames, num_channels int, mut p Player) { fn audio_player_callback(buffer &f32, num_frames int, num_channels int, mut p Player) {
if p.finished { if p.finished {
return return
} }
@ -48,9 +48,7 @@ fn audio_player_callback(buffer &f32, num_frames, num_channels int, mut p Player
p.finished = true p.finished = true
return return
} }
unsafe { unsafe {C.memcpy(buffer, &p.samples[p.pos], nsamples * int(sizeof(f32)))}
C.memcpy(buffer, &p.samples[p.pos], nsamples * int(sizeof(f32)))
}
p.pos += nsamples p.pos += nsamples
} }
@ -69,7 +67,7 @@ fn (mut p Player) stop() {
fn (mut p Player) play_wav_file(fpath string) ? { fn (mut p Player) play_wav_file(fpath string) ? {
println('> play_wav_file: $fpath') println('> play_wav_file: $fpath')
samples := read_wav_file_samples(fpath)? samples := read_wav_file_samples(fpath) ?
p.finished = true p.finished = true
p.samples << samples p.samples << samples
p.finished = false p.finished = false
@ -121,7 +119,7 @@ struct RIFFFormat {
fn read_wav_file_samples(fpath string) ?[]f32 { fn read_wav_file_samples(fpath string) ?[]f32 {
mut res := []f32{} mut res := []f32{}
// eprintln('> read_wav_file_samples: $fpath -------------------------------------------------') // eprintln('> read_wav_file_samples: $fpath -------------------------------------------------')
mut bytes := os.read_bytes(fpath)? mut bytes := os.read_bytes(fpath) ?
mut pbytes := byteptr(bytes.data) mut pbytes := byteptr(bytes.data)
mut offset := u32(0) mut offset := u32(0)
rh := &RIFFHeader(pbytes) rh := &RIFFHeader(pbytes)

View File

@ -14,14 +14,14 @@ import math
import os import os
import strconv import strconv
fn evala(i, j int) int { fn evala(i int, j int) int {
return ((i + j) * (i + j + 1) / 2 + i + 1) return ((i + j) * (i + j + 1) / 2 + i + 1)
} }
fn times(mut v []f64, u []f64) { fn times(mut v []f64, u []f64) {
for i in 0..v.len { for i in 0 .. v.len {
mut a := f64(0) mut a := f64(0)
for j in 0..u.len { for j in 0 .. u.len {
a += u[j] / f64(evala(i, j)) a += u[j] / f64(evala(i, j))
} }
v[i] = a v[i] = a
@ -29,9 +29,9 @@ fn times(mut v []f64, u []f64) {
} }
fn times_trans(mut v []f64, u []f64) { fn times_trans(mut v []f64, u []f64) {
for i in 0..v.len { for i in 0 .. v.len {
mut a := f64(0) mut a := f64(0)
for j in 0..u.len { for j in 0 .. u.len {
a += u[j] / f64(evala(j, i)) a += u[j] / f64(evala(j, i))
} }
v[i] = a v[i] = a
@ -39,10 +39,9 @@ fn times_trans(mut v []f64, u []f64) {
} }
fn a_times_transp(mut v []f64, u []f64) { fn a_times_transp(mut v []f64, u []f64) {
mut x := []f64{len:u.len, init:0} mut x := []f64{len: u.len, init: 0}
times(mut x, u) times(mut x, u)
times_trans(mut v, x) times_trans(mut v, x)
} }
fn main() { fn main() {
@ -50,19 +49,18 @@ fn main() {
mut n := 0 mut n := 0
if args.len == 2 { if args.len == 2 {
n = strconv.atoi(args[1]) n = strconv.atoi(args[1])
} } else {
else {
n = 0 n = 0
} }
mut u := []f64{len:n, init:1} mut u := []f64{len: n, init: 1}
mut v := []f64{len:n, init:1} mut v := []f64{len: n, init: 1}
for _ in 0..10 { for _ in 0 .. 10 {
a_times_transp(mut v, u) a_times_transp(mut v, u)
a_times_transp(mut u, v) a_times_transp(mut u, v)
} }
mut vbv := f64(0) mut vbv := f64(0)
mut vv := f64(0) mut vv := f64(0)
for i in 0..n { for i in 0 .. n {
vbv += u[i] * v[i] vbv += u[i] * v[i]
vv += v[i] * v[i] vv += v[i] * v[i]
} }

View File

@ -2,28 +2,34 @@ import term
fn main() { fn main() {
term.erase_clear() term.erase_clear()
sleeping_line(5,5,5,'*') sleeping_line(5, 5, 5, '*')
standing_line(5,5,5,'*') standing_line(5, 5, 5, '*')
sleeping_line(5,10,5,'*') sleeping_line(5, 10, 5, '*')
standing_line(9,5,5,'*') standing_line(9, 5, 5, '*')
term.cursor_down(5) term.cursor_down(5)
print('\n') print('\n')
println(term.bold(term.red('It Worked!'))) println(term.bold(term.red('It Worked!')))
} }
fn sleeping_line(x,y,size int, ch string) { fn sleeping_line(x int, y int, size int, ch string) {
mut i := 0 mut i := 0
for i < size { for i < size {
term.set_cursor_position(x: x+i, y: y) term.set_cursor_position({
x: x + i
y: y
})
print(term.bold(term.yellow(ch))) print(term.bold(term.yellow(ch)))
i++ i++
} }
} }
fn standing_line(x,y,size int, ch string) { fn standing_line(x int, y int, size int, ch string) {
mut i := 0 mut i := 0
for i < size { for i < size {
term.set_cursor_position(x: x, y: y+i) term.set_cursor_position({
x: x
y: y + i
})
print(term.bold(term.yellow(ch))) print(term.bold(term.yellow(ch)))
i++ i++
} }

View File

@ -89,7 +89,7 @@ fn get_bet(money int) int {
return bet return bet
} }
fn run_wheel(bet_nbr, _bet int) int { fn run_wheel(bet_nbr int, _bet int) int {
mut bet := _bet mut bet := _bet
winning_nbr := rand.intn(50) winning_nbr := rand.intn(50)
print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ') print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ')