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

View File

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

View File

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

View File

@ -5,51 +5,53 @@ import gx
import automaton
const (
screen_width = 800
screen_width = 800
screen_height = 600
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()
filled_color = gx.blue
)
[live]
fn print_automaton(a &automaton.Automaton){
gg.clear(gx.white)
fn print_automaton(app &App) {
square_size := 18
for y := 1; y<a.field.maxy; y++ {
for x := 1; x<a.field.maxx; x++ {
cell := a.field.get(x,y)
for y := 1; y < app.a.field.maxy; y++ {
for x := 1; x < app.a.field.maxx; x++ {
cell := app.a.field.get(x, y)
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)
}
}
}
}
fn main() {
mut a := automaton.gun()
for {
if graphics.window.should_close() { graphics.window.destroy() break }
gg.post_empty_event() // needed so the animation does not stop
///////////////////////////////////////////////
a.update()
print_automaton(a)
graphics.render()
}
struct App {
mut:
gg &gg.Context
a automaton.Automaton
}
fn frame(mut app App) {
app.gg.begin()
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
/////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////
pub struct A2D {
pub mut:
maxx int
maxy 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 {
mut e := &int(0)
e = a.data + y*a.maxx + x
e = a.data + y * a.maxx + x
*e = newval
}
}
[inline] pub fn (a &A2D) get(x,y int) int {
[inline]
pub fn (a &A2D) get(x int, y int) int {
unsafe {
mut e := &int(0)
e = a.data + y*a.maxx + x
e = a.data + y * a.maxx + x
return *e
}
}
[inline] pub fn (a &A2D) clear() {
for y := 0; y<a.maxy; y++ {
for x := 0; x<a.maxx; x++ {
a.set(x,y,0)
[inline]
pub fn (a &A2D) clear() {
for y := 0; y < a.maxy; y++ {
for x := 0; x < a.maxx; x++ {
a.set(x, y, 0)
}
}
}
/////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////
pub struct Automaton {
pub mut:
field &A2D
field &A2D
new_field &A2D
}
fn new_automaton(f [][]int) Automaton {
maxy := f.len
mut maxx := 0
for y := 0; y<f.len; y++ {
for y := 0; y < f.len; y++ {
if maxx < f[y].len {
maxx = f[y].len
}
}
size := (maxx * maxy)
field := &A2D{ maxx: maxx maxy: maxy data: &int( vcalloc( 4 * (size) ) ) }
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] )
field := &A2D{
maxx: maxx
maxy: maxy
data: &int(vcalloc(4 * (size)))
}
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() {
aa.new_field.clear()
for y := 1; y<aa.field.maxy; y++ {
for x := 1; x<aa.field.maxx; x++ {
moore_sum := ( 0 +
aa.field.get(x-1,y-1) + aa.field.get(x,y-1) + aa.field.get(x+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,y+1) + aa.field.get(x+1,y+1)
)
cell := aa.field.get(x,y)
v := if cell == 1 {
moore_sum in [2, 3]
for y := 1; y < aa.field.maxy; y++ {
for x := 1; x < aa.field.maxx; x++ {
moore_sum := (0 + aa.field.get(x - 1, y - 1) + aa.field.get(x, y - 1) + aa.field.get(x +
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, 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 }
aa.new_field.set(x, y, if v {
1
} else {
moore_sum == 3
}
aa.new_field.set(x,y, if v { 1 } else { 0 })
0
})
}
}
tmp := aa.field
@ -82,35 +95,64 @@ pub fn (mut aa Automaton) update() {
pub fn gun() Automaton {
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, 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, 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, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0,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 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, 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, 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],
]
return new_automaton(field)
}

View File

@ -4,21 +4,21 @@ const (
)
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\...')
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 {
move(1,a,c)
move(1, a, c)
} else {
hanoi(n-1, a, c, b)
move(n,a,c)
hanoi(n-1, b, a, c)
hanoi(n - 1, a, c, b)
move(n, a, c)
hanoi(n - 1, b, a, c)
}
return 0
}

View File

@ -1,13 +1,13 @@
import os
fn regex_match(src, pat string) bool {
fn regex_match(src string, pat string) bool {
src_size := src.len + 1
pat_size := pat.len + 1
mut memo := [][]int{len: src_size, init: []int{len: pat_size, init: -1}}
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 {
return memo[src_pos][pat_pos] == 1
}
@ -121,6 +121,6 @@ fn main() {
if pat == 'exit' {
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)
}
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.v2f(x, y)
sgl.v2f(x + w, y)
@ -65,7 +65,7 @@ fn draw_hollow_rect(x, y, w, h f32) {
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.v2f(x, y)
sgl.v2f(x + w, y)

View File

@ -10,7 +10,7 @@ mut:
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
for frame := 0; frame < num_frames; frame++ {
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() {
// first, reset and setup ortho projection
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.bsample(x + 700), 255))
}

View File

@ -8,11 +8,11 @@ const (
)
[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))
}
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
unsafe {
mut soundbuffer := buffer

View File

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

View File

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

View File

@ -2,28 +2,34 @@ import term
fn main() {
term.erase_clear()
sleeping_line(5,5,5,'*')
standing_line(5,5,5,'*')
sleeping_line(5,10,5,'*')
standing_line(9,5,5,'*')
sleeping_line(5, 5, 5, '*')
standing_line(5, 5, 5, '*')
sleeping_line(5, 10, 5, '*')
standing_line(9, 5, 5, '*')
term.cursor_down(5)
print('\n')
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
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)))
i++
}
}
fn standing_line(x,y,size int, ch string) {
fn standing_line(x int, y int, size int, ch string) {
mut i := 0
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)))
i++
}

View File

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