mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: c2v fixes
This commit is contained in:
parent
0692164333
commit
c65dbe51d6
@ -841,9 +841,10 @@ pub fn (s string) capitalize() string {
|
||||
if s.len == 0 {
|
||||
return ''
|
||||
}
|
||||
sl := s.to_lower()
|
||||
cap := sl[0].str().to_upper() + sl.right(1)
|
||||
return cap
|
||||
return s[0].str().to_upper() + s[1..]
|
||||
//sl := s.to_lower()
|
||||
//cap := sl[0].str().to_upper() + sl.right(1)
|
||||
//return cap
|
||||
}
|
||||
|
||||
pub fn (s string) is_capital() bool {
|
||||
|
@ -575,10 +575,11 @@ fn test_capitalize() {
|
||||
assert s.capitalize() == ''
|
||||
s = 'TEST IT'
|
||||
assert !s.is_capital()
|
||||
assert s.capitalize() == 'Test it'
|
||||
assert s.capitalize() == 'TEST IT'
|
||||
s = 'Test it'
|
||||
assert s.is_capital()
|
||||
assert s.capitalize() == 'Test it'
|
||||
assert 'GameMission_t'.capitalize() == 'GameMission_t'
|
||||
}
|
||||
|
||||
fn test_title() {
|
||||
|
@ -105,3 +105,14 @@ pub fn (c Color) eq(c2 Color) bool {
|
||||
pub fn (c Color) str() string {
|
||||
return 'Color{$c.r, $c.g, $c.b, $c.a}'
|
||||
}
|
||||
|
||||
const (
|
||||
string_colors = {
|
||||
'black': black
|
||||
'red': red
|
||||
}
|
||||
)
|
||||
|
||||
pub fn color_from_string(s string) Color {
|
||||
return string_colors[s]
|
||||
}
|
||||
|
@ -236,7 +236,7 @@ fn (mut c Checker) check_valid_snake_case(name, identifier string, pos token.Pos
|
||||
if !c.pref.is_vweb && (name[0] == `_` || name.contains('._')) {
|
||||
c.error('$identifier `$name` cannot start with `_`', pos)
|
||||
}
|
||||
if util.contains_capital(name) {
|
||||
if !c.pref.experimental && !c.pref.translated && util.contains_capital(name) {
|
||||
c.error('$identifier `$name` cannot contain uppercase letters, use snake_case instead',
|
||||
pos)
|
||||
}
|
||||
@ -251,7 +251,7 @@ fn stripped_name(name string) string {
|
||||
|
||||
fn (mut c Checker) check_valid_pascal_case(name, identifier string, pos token.Position) {
|
||||
sname := stripped_name(name)
|
||||
if !sname[0].is_capital() {
|
||||
if !sname[0].is_capital() && !c.pref.translated {
|
||||
c.error('$identifier `$name` must begin with capital letter', pos)
|
||||
}
|
||||
}
|
||||
@ -465,7 +465,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type {
|
||||
if field.has_default_expr || field.name in inited_fields {
|
||||
continue
|
||||
}
|
||||
if field.typ.is_ptr() {
|
||||
if field.typ.is_ptr() && !c.pref.translated {
|
||||
c.warn('reference field `${type_sym.name}.$field.name` must be initialized',
|
||||
struct_init.pos)
|
||||
}
|
||||
@ -694,7 +694,7 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
|
||||
ast.Ident {
|
||||
if expr.obj is ast.Var {
|
||||
mut v := expr.obj as ast.Var
|
||||
if !v.is_mut {
|
||||
if !v.is_mut && !c.pref.translated {
|
||||
c.error('`$expr.name` is immutable, declare it with `mut` to make it mutable',
|
||||
expr.pos)
|
||||
}
|
||||
@ -737,7 +737,7 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
|
||||
c.error('unknown field `${type_str}.$expr.field_name`', expr.pos)
|
||||
return '', pos
|
||||
}
|
||||
if !field_info.is_mut {
|
||||
if !field_info.is_mut && !c.pref.translated {
|
||||
type_str := c.table.type_to_str(expr.expr_type)
|
||||
c.error('field `$expr.field_name` of struct `$type_str` is immutable',
|
||||
expr.pos)
|
||||
@ -1537,7 +1537,8 @@ pub fn (mut c Checker) enum_decl(decl ast.EnumDecl) {
|
||||
c.check_valid_pascal_case(decl.name, 'enum name', decl.pos)
|
||||
mut seen := []int{}
|
||||
for i, field in decl.fields {
|
||||
if util.contains_capital(field.name) {
|
||||
if !c.pref.experimental && util.contains_capital(field.name) {
|
||||
// TODO C2V uses hundreds of enums with capitals, remove -experimental check once it's handled
|
||||
c.error('field name `$field.name` cannot contain uppercase letters, use snake_case instead',
|
||||
field.pos)
|
||||
}
|
||||
@ -2094,7 +2095,7 @@ fn (mut c Checker) stmt(node ast.Stmt) {
|
||||
ast.ForStmt {
|
||||
c.in_for_count++
|
||||
typ := c.expr(node.cond)
|
||||
if !node.is_inf && typ.idx() != table.bool_type_idx {
|
||||
if !node.is_inf && typ.idx() != table.bool_type_idx && !c.pref.translated {
|
||||
c.error('non-bool used as for condition', node.pos)
|
||||
}
|
||||
// TODO: update loop var type
|
||||
@ -2383,10 +2384,10 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
|
||||
if node.op == .mul && right_type.is_ptr() {
|
||||
return right_type.deref()
|
||||
}
|
||||
if node.op == .bit_not && !right_type.is_int() {
|
||||
if node.op == .bit_not && !right_type.is_int() && !c.pref.translated {
|
||||
c.error('operator ~ only defined on int types', node.pos)
|
||||
}
|
||||
if node.op == .not && right_type != table.bool_type_idx {
|
||||
if node.op == .not && right_type != table.bool_type_idx && !c.pref.translated {
|
||||
c.error('! operator can only be used with bool types', node.pos)
|
||||
}
|
||||
return right_type
|
||||
@ -2885,7 +2886,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
|
||||
if !node.has_else || i < node.branches.len - 1 {
|
||||
// check condition type is boolean
|
||||
cond_typ := c.expr(branch.cond)
|
||||
if cond_typ.idx() !in [table.bool_type_idx, table.void_type_idx] {
|
||||
if cond_typ.idx() !in [table.bool_type_idx, table.void_type_idx] && !c.pref.translated {
|
||||
// void types are skipped, because they mean the var was initialized incorrectly
|
||||
// (via missing function etc)
|
||||
typ_sym := c.table.get_type_symbol(cond_typ)
|
||||
@ -3059,7 +3060,7 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) table.Type {
|
||||
is_ok = v.is_mut && v.is_arg && !typ.deref().is_ptr()
|
||||
}
|
||||
}
|
||||
if !is_ok {
|
||||
if !is_ok && !c.pref.translated {
|
||||
c.warn('pointer indexing is only allowed in `unsafe` blocks', node.pos)
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,8 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||
if language == .v && no_body {
|
||||
p.error('`$p.tok.lit` lacks body')
|
||||
}
|
||||
if language == .v && p.mod != 'builtin' && name.len > 0 && !name[0].is_capital() {
|
||||
if language == .v &&
|
||||
p.mod != 'builtin' && name.len > 0 && !name[0].is_capital() && !p.pref.translated {
|
||||
p.error_with_pos('struct name `$name` must begin with capital letter', name_pos)
|
||||
}
|
||||
if name.len == 1 {
|
||||
|
Loading…
Reference in New Issue
Block a user