mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
Compare commits
6 commits
f6b60e4d9f
...
dbd5b5f56c
Author | SHA1 | Date | |
---|---|---|---|
![]() |
dbd5b5f56c | ||
![]() |
e15d8fcf49 | ||
![]() |
f17e0fd52b | ||
![]() |
dabc08b6ee | ||
![]() |
8868696017 | ||
![]() |
682db66852 |
10 changed files with 72 additions and 13 deletions
|
@ -441,7 +441,7 @@ fn run_repl(workdir string, vrepl_prefix string) int {
|
|||
prompt = '... '
|
||||
}
|
||||
oline := r.get_one_line(prompt) or { break }
|
||||
line := oline.trim_space()
|
||||
line := oline.all_before('//').trim_space()
|
||||
if line == '' {
|
||||
continue
|
||||
}
|
||||
|
|
|
@ -127,6 +127,7 @@ pub:
|
|||
attrs []string // the attributes of the field f
|
||||
is_pub bool // f is in a `pub:` section
|
||||
is_mut bool // f is in a `mut:` section
|
||||
is_embed bool // f is a embedded struct
|
||||
|
||||
is_shared bool // `f shared Abc`
|
||||
is_atomic bool // `f atomic int` , TODO
|
||||
|
|
|
@ -1398,7 +1398,7 @@ fn (mut c Checker) comptime_if_cond(mut cond ast.Expr, mut sb strings.Builder) (
|
|||
}
|
||||
ast.SelectorExpr {
|
||||
if c.comptime.comptime_for_field_var != '' && cond.expr is ast.Ident {
|
||||
if (cond.expr as ast.Ident).name == c.comptime.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_option', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias', 'is_enum'] {
|
||||
if (cond.expr as ast.Ident).name == c.comptime.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_embed', 'is_shared', 'is_atomic', 'is_option', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias', 'is_enum'] {
|
||||
is_true = c.type_resolver.get_comptime_selector_bool_field(cond.field_name)
|
||||
sb.write_string('${is_true}')
|
||||
return is_true, true
|
||||
|
|
|
@ -221,11 +221,14 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !node.is_comptime || (node.is_comptime && comptime_match_branch_result) {
|
||||
if node.is_expr {
|
||||
c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type)
|
||||
} else {
|
||||
c.stmts(mut branch.stmts)
|
||||
}
|
||||
}
|
||||
c.smartcast_mut_pos = token.Pos{}
|
||||
c.smartcast_cond_pos = token.Pos{}
|
||||
if node.is_expr {
|
||||
|
|
|
@ -740,6 +740,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) {
|
|||
g.writeln('\t${node.val_var}.unaliased_typ = ${int(unaliased_styp.idx())};\t// ${g.table.type_to_str(unaliased_styp)}')
|
||||
g.writeln('\t${node.val_var}.is_pub = ${field.is_pub};')
|
||||
g.writeln('\t${node.val_var}.is_mut = ${field.is_mut};')
|
||||
g.writeln('\t${node.val_var}.is_embed = ${field.is_embed};')
|
||||
|
||||
g.writeln('\t${node.val_var}.is_shared = ${field.typ.has_flag(.shared_f)};')
|
||||
g.writeln('\t${node.val_var}.is_atomic = ${field.typ.has_flag(.atomic_f)};')
|
||||
|
@ -779,9 +780,14 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) {
|
|||
g.write('\t${node.val_var}.value = ')
|
||||
if g.pref.translated && node.typ.is_number() {
|
||||
g.writeln('_const_main__${val};')
|
||||
} else {
|
||||
node_sym := g.table.sym(node.typ)
|
||||
if node_sym.info is ast.Alias {
|
||||
g.writeln('${g.styp(node_sym.info.parent_type)}__${val};')
|
||||
} else {
|
||||
g.writeln('${g.styp(node.typ)}__${val};')
|
||||
}
|
||||
}
|
||||
enum_attrs := sym.info.attrs[val]
|
||||
if enum_attrs.len == 0 {
|
||||
g.writeln('\t${node.val_var}.attrs = __new_array_with_default(0, 0, sizeof(string), 0);')
|
||||
|
|
5
vlib/v/slow_tests/repl/line_comment.repl
Normal file
5
vlib/v/slow_tests/repl/line_comment.repl
Normal file
|
@ -0,0 +1,5 @@
|
|||
math.pi
|
||||
math.pi // some comment
|
||||
===output===
|
||||
3.141592653589793
|
||||
3.141592653589793
|
|
@ -5,6 +5,8 @@ enum CharacterGroup {
|
|||
special
|
||||
}
|
||||
|
||||
type AnotherCharGroup = CharacterGroup
|
||||
|
||||
fn (self CharacterGroup) value() string {
|
||||
return match self {
|
||||
.chars { 'first' }
|
||||
|
@ -33,3 +35,21 @@ fn test_main() {
|
|||
assert values == [CharacterGroup.chars, CharacterGroup.alphanumerics, CharacterGroup.numeric,
|
||||
CharacterGroup.special]
|
||||
}
|
||||
|
||||
fn test_alias_enum() {
|
||||
mut values := []EnumData{}
|
||||
$for entry in AnotherCharGroup.values {
|
||||
values << entry
|
||||
}
|
||||
assert values[0].value == int(CharacterGroup.chars)
|
||||
assert values[0].name == CharacterGroup.chars.str()
|
||||
|
||||
assert values[1].value == int(CharacterGroup.alphanumerics)
|
||||
assert values[1].name == CharacterGroup.alphanumerics.str()
|
||||
|
||||
assert values[2].value == int(CharacterGroup.numeric)
|
||||
assert values[2].name == CharacterGroup.numeric.str()
|
||||
|
||||
assert values[3].value == int(CharacterGroup.special)
|
||||
assert values[3].name == CharacterGroup.special.str()
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
struct App {
|
||||
Inner
|
||||
a string
|
||||
b string
|
||||
mut:
|
||||
|
@ -12,6 +13,8 @@ pub mut:
|
|||
h u8
|
||||
}
|
||||
|
||||
struct Inner {}
|
||||
|
||||
@['foo/bar/three']
|
||||
fn (mut app App) run() {
|
||||
}
|
||||
|
@ -85,13 +88,16 @@ fn test_comptime_for_fields() {
|
|||
assert field.name in ['d', 'e']
|
||||
}
|
||||
if field.is_mut {
|
||||
assert field.name in ['c', 'd', 'g', 'h']
|
||||
assert field.name in ['c', 'd', 'g', 'h', 'Inner']
|
||||
}
|
||||
if field.is_pub {
|
||||
assert field.name in ['e', 'f', 'g', 'h']
|
||||
assert field.name in ['e', 'f', 'g', 'h', 'Inner']
|
||||
}
|
||||
if field.is_pub && field.is_mut {
|
||||
assert field.name in ['g', 'h']
|
||||
assert field.name in ['g', 'h', 'Inner']
|
||||
}
|
||||
if field.is_embed {
|
||||
assert field.name == 'Inner'
|
||||
}
|
||||
if field.name == 'f' {
|
||||
assert sizeof(field) == 8
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
module main
|
||||
|
||||
fn func[T]() bool {
|
||||
$match T {
|
||||
u8, u16 {
|
||||
return true
|
||||
}
|
||||
$else {
|
||||
// return false
|
||||
$compile_error('fail')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_comptime_match_eval_only_true_branch() {
|
||||
assert func[u8]()
|
||||
}
|
|
@ -224,6 +224,7 @@ pub fn (mut t TypeResolver) get_comptime_selector_bool_field(field_name string)
|
|||
match field_name {
|
||||
'is_pub' { return field.is_pub }
|
||||
'is_mut' { return field.is_mut }
|
||||
'is_embed' { return field.is_embed }
|
||||
'is_shared' { return field_typ.has_flag(.shared_f) }
|
||||
'is_atomic' { return field_typ.has_flag(.atomic_f) }
|
||||
'is_option' { return field.typ.has_flag(.option) }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue