1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: update assoc syntax (#8274)

This commit is contained in:
Daniel Däschle 2021-01-22 23:24:48 +01:00 committed by GitHub
parent 12897d1e2b
commit dbf84520f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 138 additions and 75 deletions

View File

@ -846,7 +846,9 @@ jobs:
- name: Install zzuf
run: sudo apt install -qq zzuf
- name: Build local v
run: make -j4
run: |
make -j4
./v -g cmd/tools/vtest-parser.v
- name: Run test-parser over fuzzed files
run: |
zzuf -R '\x00-\x20\x7f-\xff' -r0.01 < examples/hello_world.v > examples/hello_world_fuzz.v

View File

@ -16,7 +16,7 @@ mut:
board Board
undo []Undo
atickers [4][4]int
state GameState = .play
state GameState = .play
tile_format TileFormat = .normal
moves int
perf &Perf = 0
@ -47,7 +47,7 @@ struct Theme {
}
const (
themes = [
themes = [
&Theme{
bg_color: gx.rgb(250, 248, 239)
padding_color: gx.rgb(143, 130, 119)
@ -55,18 +55,18 @@ const (
game_over_color: gx.rgb(190, 50, 50)
text_color: gx.black
tile_colors: [
gx.rgb(205, 193, 180), // Empty / 0 tile
gx.rgb(238, 228, 218), // 2
gx.rgb(237, 224, 200), // 4
gx.rgb(242, 177, 121), // 8
gx.rgb(245, 149, 99), // 16
gx.rgb(246, 124, 95), // 32
gx.rgb(246, 94, 59), // 64
gx.rgb(237, 207, 114), // 128
gx.rgb(237, 204, 97), // 256
gx.rgb(237, 200, 80), // 512
gx.rgb(237, 197, 63), // 1024
gx.rgb(237, 194, 46), // 2048
gx.rgb(205, 193, 180), /* Empty / 0 tile */
gx.rgb(238, 228, 218), /* 2 */
gx.rgb(237, 224, 200), /* 4 */
gx.rgb(242, 177, 121), /* 8 */
gx.rgb(245, 149, 99), /* 16 */
gx.rgb(246, 124, 95), /* 32 */
gx.rgb(246, 94, 59), /* 64 */
gx.rgb(237, 207, 114), /* 128 */
gx.rgb(237, 204, 97), /* 256 */
gx.rgb(237, 200, 80), /* 512 */
gx.rgb(237, 197, 63), /* 1024 */
gx.rgb(237, 194, 46), /* 2048 */
]
},
&Theme{
@ -149,7 +149,7 @@ struct Undo {
}
struct TileLine {
ypos int
ypos int
mut:
field [5]int
points int
@ -351,7 +351,8 @@ fn (mut b Board) is_game_over() bool {
if (x > 0 && fidx == b.field[y][x - 1]) ||
(x < 4 - 1 && fidx == b.field[y][x + 1]) ||
(y > 0 && fidx == b.field[y - 1][x]) ||
(y < 4 - 1 && fidx == b.field[y + 1][x]) {
(y < 4 - 1 && fidx == b.field[y + 1][x])
{
// there are remaining merges
return false
}
@ -623,8 +624,8 @@ fn (app &App) draw() {
app.gg.draw_text(ww / 2, (m * 4 / 10) + ypad, 'Game Over', app.label_format(.game_over))
f := app.label_format(.tile)
msg := $if android { 'Tap to restart' } $else { 'Press `r` to restart' }
app.gg.draw_text(ww / 2, (m * 6 / 10) + ypad, msg, {
f |
app.gg.draw_text(ww / 2, (m * 6 / 10) + ypad, msg, gx.TextCfg{
...f
color: gx.white
size: f.size * 3 / 4
})
@ -649,7 +650,8 @@ fn (app &App) draw_tiles() {
toffset := app.ui.tile_size + app.ui.padding_size
tiles_size := min(app.ui.window_width, app.ui.window_height) - app.ui.border_size * 2
// Draw the padding around the tiles
app.gg.draw_rounded_rect(xstart, ystart, tiles_size / 2, tiles_size / 2, tiles_size / 24, app.theme.padding_color)
app.gg.draw_rounded_rect(xstart, ystart, tiles_size / 2, tiles_size / 2, tiles_size / 24,
app.theme.padding_color)
// Draw the actual tiles
for y in 0 .. 4 {
for x in 0 .. 4 {
@ -670,8 +672,8 @@ fn (app &App) draw_tiles() {
xpos := xoffset + tw / 2
ypos := yoffset + th / 2
mut fmt := app.label_format(.tile)
fmt = {
fmt |
fmt = gx.TextCfg{
...fmt
size: int(f32(fmt.size - 1) / animation_length * anim_size)
}
match app.tile_format {
@ -685,16 +687,16 @@ fn (app &App) draw_tiles() {
app.gg.draw_text(xpos, ypos, '2', fmt)
fs2 := int(f32(fmt.size) * 0.67)
app.gg.draw_text(xpos + app.ui.tile_size / 10, ypos - app.ui.tile_size / 8,
'$tidx', {
fmt |
'$tidx', gx.TextCfg{
...fmt
size: fs2
align: gx.HorizontalAlign.left
})
}
.shifts {
fs2 := int(f32(fmt.size) * 0.6)
app.gg.draw_text(xpos, ypos, '2<<${tidx - 1}', {
fmt |
app.gg.draw_text(xpos, ypos, '2<<${tidx - 1}', gx.TextCfg{
...fmt
size: fs2
})
}
@ -951,7 +953,7 @@ fn main() {
window_title_ = 'canvas'
}
app.perf = &Perf{}
app.gg = gg.new_context({
app.gg = gg.new_context(
bg_color: app.theme.bg_color
width: default_window_width
height: default_window_height
@ -963,6 +965,6 @@ fn main() {
init_fn: init
user_data: app
font_path: font_path
})
)
app.gg.run()
}

View File

@ -190,17 +190,17 @@ fn event(event &tui.Event, x voidptr) {
match event.code {
.f1, ._1 {
oevent := *event
nevent := { oevent | button: tui.MouseButton.left, x: app.mouse_pos.x , y: app.mouse_pos.y }
nevent := tui.Event{ ...oevent, button: tui.MouseButton.left, x: app.mouse_pos.x , y: app.mouse_pos.y }
app.paint(nevent)
}
.f2, ._2 {
oevent := *event
nevent := { oevent | button: tui.MouseButton.right, x: app.mouse_pos.x , y: app.mouse_pos.y }
nevent := tui.Event{ ...oevent, button: tui.MouseButton.right, x: app.mouse_pos.x , y: app.mouse_pos.y }
app.paint(nevent)
}
.space {
oevent := *event
nevent := { oevent | button: tui.MouseButton.middle, x: app.mouse_pos.x , y: app.mouse_pos.y }
nevent := tui.Event{ ...oevent, button: tui.MouseButton.middle, x: app.mouse_pos.x , y: app.mouse_pos.y }
app.paint(nevent)
}
.j, .down {

View File

@ -88,8 +88,8 @@ pub fn (mut c Client) send(config Mail) ? {
c.send_mailfrom(from) or { return error('Sending mailfrom failed') }
c.send_mailto(config.to) or { return error('Sending mailto failed') }
c.send_data() or { return error('Sending mail data failed') }
c.send_body({
config |
c.send_body(Mail{
...config
from: from
}) or { return error('Sending mail body failed') }
}

View File

@ -44,24 +44,23 @@ fn test_smtp() {
return
}
assert true
// client.send({ send_cfg | body_type: .html, body: '<html><h1>HTML V email!</h1></html>' }) or { assert false return }
client.send({
send_cfg |
client.send(smtp.Mail{
...send_cfg
from: 'alexander@vlang.io'
}) or {
assert false
return
}
client.send({
send_cfg |
client.send(smtp.Mail{
...send_cfg
cc: 'alexander@vlang.io,joe@vlang.io'
bcc: 'spytheman@vlang.io'
}) or {
assert false
return
}
client.send({
send_cfg |
client.send(smtp.Mail{
...send_cfg
date: time.now().add_days(1000)
}) or {
assert false

View File

@ -274,8 +274,8 @@ fn single_char(buf string) &Event {
// special handling for `ctrl + letter`
// TODO: Fix assoc in V and remove this workaround :/
// 1 ... 26 { event = { event | code: KeyCode(96 | ch), modifiers: ctrl } }
// 65 ... 90 { event = { event | code: KeyCode(32 | ch), modifiers: shift } }
// 1 ... 26 { event = Event{ ...event, code: KeyCode(96 | ch), modifiers: ctrl } }
// 65 ... 90 { event = Event{ ...event, code: KeyCode(32 | ch), modifiers: shift } }
// The bit `or`s here are really just `+`'s, just written in this way for a tiny performance improvement
@ -330,7 +330,6 @@ fn escape_sequence(buf_ string) (&Event, int) {
if buf.len == 1 {
c := single_char(buf)
// return { c | modifiers: c.modifiers | alt }, 2
return &Event{
typ: c.typ

View File

@ -158,8 +158,8 @@ pub fn new_time(t Time) Time {
tm_year: t.year - 1900
}
utime := u64(make_unix_time(tt))
return {
t |
return Time{
...t
unix: utime
}
}

View File

@ -3648,9 +3648,9 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) table.Type {
}
if node.is_vweb {
// TODO assoc parser bug
pref := *c.pref
pref2 := {
pref |
pref_ := *c.pref
pref2 := pref.Preferences{
...pref_
is_vweb: true
}
mut c2 := new_checker(c.table, pref2)

View File

@ -40,12 +40,14 @@ fn test_all() {
os.chdir(vroot)
checker_dir := 'vlib/v/checker/tests'
parser_dir := 'vlib/v/parser/tests'
scanner_dir := 'vlib/v/scanner/tests'
module_dir := '$checker_dir/modules'
global_dir := '$checker_dir/globals'
run_dir := '$checker_dir/run'
//
checker_tests := get_tests_in_dir(checker_dir, false)
parser_tests := get_tests_in_dir(parser_dir, false)
scanner_tests := get_tests_in_dir(scanner_dir, false)
global_tests := get_tests_in_dir(global_dir, false)
module_tests := get_tests_in_dir(module_dir, true)
run_tests := get_tests_in_dir(run_dir, false)
@ -53,6 +55,7 @@ fn test_all() {
mut tasks := []TaskDescription{}
tasks.add(vexe, parser_dir, '-prod', '.out', parser_tests, false)
tasks.add(vexe, checker_dir, '-prod', '.out', checker_tests, false)
tasks.add(vexe, scanner_dir, '-prod', '.out', scanner_tests, false)
tasks.add(vexe, checker_dir, '-d mysymbol run', '.mysymbol.run.out', ['custom_comptime_define_error.vv'],
false)
tasks.add(vexe, checker_dir, '-d mydebug run', '.mydebug.run.out', ['custom_comptime_define_if_flag.vv'],

View File

@ -369,13 +369,11 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
p.or_is_handled = true
p.register_auto_import('sync')
}
// mut typ := p.
// println('infix op=$op.str()')
precedence := p.tok.precedence()
mut pos := p.tok.position()
if left.position().line_nr < pos.line_nr {
pos = {
pos |
pos = token.Position{
...pos
line_nr: left.position().line_nr
}
}

View File

@ -212,12 +212,12 @@ fn (mut s Scanner) ident_bin_number() string {
mut first_wrong_digit := `\0`
start_pos := s.pos
s.pos += 2 // skip '0b'
if s.text[s.pos] == num_sep {
if s.pos < s.text.len && s.text[s.pos] == num_sep {
s.error('separator `_` is only valid between digits in a numeric literal')
}
for s.pos < s.text.len {
c := s.text[s.pos]
if c == num_sep && s.text[s.pos + 1] == num_sep {
if c == num_sep && s.text[s.pos - 1] == num_sep {
s.error('cannot use `_` consecutively')
}
if !c.is_bin_digit() && c != num_sep {
@ -232,6 +232,7 @@ fn (mut s Scanner) ident_bin_number() string {
s.pos++
}
if s.text[s.pos - 1] == num_sep {
s.pos--
s.error('cannot use `_` at the end of a numeric literal')
} else if start_pos + 2 == s.pos {
s.pos-- // adjust error position
@ -254,12 +255,12 @@ fn (mut s Scanner) ident_hex_number() string {
return '0x'
}
s.pos += 2 // skip '0x'
if s.text[s.pos] == num_sep {
if s.pos < s.text.len && s.text[s.pos] == num_sep {
s.error('separator `_` is only valid between digits in a numeric literal')
}
for s.pos < s.text.len {
c := s.text[s.pos]
if c == num_sep && s.text[s.pos + 1] == num_sep {
if c == num_sep && s.text[s.pos - 1] == num_sep {
s.error('cannot use `_` consecutively')
}
if !c.is_hex_digit() && c != num_sep {
@ -274,6 +275,7 @@ fn (mut s Scanner) ident_hex_number() string {
s.pos++
}
if s.text[s.pos - 1] == num_sep {
s.pos--
s.error('cannot use `_` at the end of a numeric literal')
} else if start_pos + 2 == s.pos {
s.pos-- // adjust error position
@ -293,12 +295,12 @@ fn (mut s Scanner) ident_oct_number() string {
mut first_wrong_digit := `\0`
start_pos := s.pos
s.pos += 2 // skip '0o'
if s.text[s.pos] == num_sep {
if s.pos < s.text.len && s.text[s.pos] == num_sep {
s.error('separator `_` is only valid between digits in a numeric literal')
}
for s.pos < s.text.len {
c := s.text[s.pos]
if c == num_sep && s.text[s.pos + 1] == num_sep {
if c == num_sep && s.text[s.pos - 1] == num_sep {
s.error('cannot use `_` consecutively')
}
if !c.is_oct_digit() && c != num_sep {
@ -313,6 +315,7 @@ fn (mut s Scanner) ident_oct_number() string {
s.pos++
}
if s.text[s.pos - 1] == num_sep {
s.pos--
s.error('cannot use `_` at the end of a numeric literal')
} else if start_pos + 2 == s.pos {
s.pos-- // adjust error position
@ -334,7 +337,7 @@ fn (mut s Scanner) ident_dec_number() string {
// scan integer part
for s.pos < s.text.len {
c := s.text[s.pos]
if c == num_sep && s.text[s.pos + 1] == num_sep {
if c == num_sep && s.text[s.pos - 1] == num_sep {
s.error('cannot use `_` consecutively')
}
if !c.is_digit() && c != num_sep {
@ -349,6 +352,7 @@ fn (mut s Scanner) ident_dec_number() string {
s.pos++
}
if s.text[s.pos - 1] == num_sep {
s.pos--
s.error('cannot use `_` at the end of a numeric literal')
}
mut call_method := false // true for, e.g., 5.str(), 5.5.str(), 5e5.str()
@ -554,12 +558,12 @@ fn (mut s Scanner) text_scan() token.Token {
} else {
s.is_started = true
}
if s.pos >= s.text.len {
return s.end_of_file()
}
if !s.is_inside_string {
s.skip_whitespace()
}
if s.pos >= s.text.len {
return s.end_of_file()
}
// End of $var, start next string
if s.is_inter_end {
if s.text[s.pos] == s.quote {

View File

@ -0,0 +1,5 @@
vlib/v/scanner/tests/bin_consecutively_separator_err.vv:2:9: error: separator `_` is only valid between digits in a numeric literal
1 | fn main() {
2 | _ := 0b_20
| ^
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
_ := 0b_20
}

View File

@ -0,0 +1,5 @@
vlib/v/scanner/tests/bin_separator_in_front_err.vv:2:9: error: separator `_` is only valid between digits in a numeric literal
1 | fn main() {
2 | _ := 0b_20
| ^
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
_ := 0b_20
}

View File

@ -0,0 +1,5 @@
vlib/v/scanner/tests/dec_consecutively_separator_err.vv:2:7: error: undefined ident: `_20`
1 | fn main() {
2 | _ := _20
| ~~~
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
_ := _20
}

View File

@ -0,0 +1,5 @@
vlib/v/scanner/tests/hex_consecutively_separator_err.vv:2:9: error: separator `_` is only valid between digits in a numeric literal
1 | fn main() {
2 | _ := 0x_20
| ^
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
_ := 0x_20
}

View File

@ -0,0 +1,5 @@
vlib/v/scanner/tests/hex_separator_in_front_err.vv:2:9: error: separator `_` is only valid between digits in a numeric literal
1 | fn main() {
2 | _ := 0o_20
| ^
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
_ := 0o_20
}

View File

@ -0,0 +1,5 @@
vlib/v/scanner/tests/oct_consecutively_separator_err.vv:2:9: error: separator `_` is only valid between digits in a numeric literal
1 | fn main() {
2 | _ := 0o_20
| ^
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
_ := 0o_20
}

View File

@ -0,0 +1,5 @@
vlib/v/scanner/tests/oct_separator_in_front_err.vv:2:9: error: separator `_` is only valid between digits in a numeric literal
1 | fn main() {
2 | _ := 0o_20
| ^
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
_ := 0o_20
}

View File

@ -355,8 +355,8 @@ pub fn (mut t Table) register_type_symbol(typ TypeSymbol) int {
.placeholder {
// override placeholder
// println('overriding type placeholder `$typ.name`')
t.types[existing_idx] = {
typ |
t.types[existing_idx] = TypeSymbol{
...typ
methods: ex_type.methods
}
return existing_idx
@ -368,8 +368,8 @@ pub fn (mut t Table) register_type_symbol(typ TypeSymbol) int {
if existing_idx >= string_type_idx && existing_idx <= map_type_idx {
if existing_idx == string_type_idx {
// existing_type := t.types[existing_idx]
t.types[existing_idx] = {
typ |
t.types[existing_idx] = TypeSymbol{
...typ
kind: ex_type.kind
}
} else {

View File

@ -84,7 +84,7 @@ fn test_assign_multi_expr() {
// test practical complex expressions
val3 := Object { name: 'initial', value: 19 }
mut q, mut r, mut s := if true {
1 + 1, 'awe' + 'some', { val3 | name: 'ok' }
1 + 1, 'awe' + 'some', Object{ ...val3, name: 'ok' }
} else {
0, '0', Object {}
}
@ -97,7 +97,7 @@ fn test_assign_multi_expr() {
q, r, s = if false {
0, '0', Object {}
} else {
5, '55', { val3 | value: 555 }
5, '55', Object{ ...val3, value: 555 }
}
assert q == 5
assert r == '55'

View File

@ -8,8 +8,8 @@ fn new_st() MyStruct {
fn get_st() MyStruct {
r := new_st()
return {
r |
return MyStruct{
...r
s: '6'
}
}

View File

@ -164,8 +164,8 @@ fn test_assoc_with_vars() {
def2 := Def{
a: 12
}
merged := {
def2 |
merged := Def{
...def2
a: 42
}
assert merged.a == 42
@ -181,7 +181,7 @@ const (
fn test_assoc_with_constants() {
println(1)
/*
QTODO
TODO:
merged := { const_def | a: 42 }
assert merged.a == 42
assert merged.b == 7

View File

@ -17,8 +17,8 @@ pub fn (pos Position) str() string {
}
pub fn (pos Position) extend(end Position) Position {
return {
pos |
return Position{
...pos
len: end.pos - pos.pos + end.len
last_line: end.last_line
}