checker: AsCast, CharLiteral, fix integer index check

pull/3915/head
Alexander Medvednikov 2020-03-02 19:00:33 +01:00
parent 156e36c082
commit 22ffe336cb
6 changed files with 29 additions and 16 deletions

View File

@ -38,7 +38,7 @@ Installing V: [github.com/vlang/v#installing-v-from-source](https://github.com/v
- Built-in ORM
- C and JavaScript backends
A stable 0.2 release is planned for February 2020. Right now V is in an alpha stage.
A stable 0.2 release is planned for March 2020. Right now V is in an alpha stage.
## Installing V from source

View File

@ -12,7 +12,7 @@ pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLitera
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr |
ConcatExpr | TypeName
ConcatExpr | TypeName | AsCast
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
@ -408,6 +408,11 @@ pub:
pos token.Position
}
pub struct AsCast {
pub:
typ table.Type
}
// e.g. `[unsafe_fn]`
pub struct Attr {
pub:

View File

@ -480,6 +480,9 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
ast.ArrayInit {
return c.array_init(mut it)
}
ast.AsCast {
return it.typ
}
ast.AssignExpr {
c.check_assign_expr(it)
}
@ -769,7 +772,7 @@ pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
index_type_sym := c.table.get_type_symbol(index_type)
// println('index expr left=$typ_sym.name $node.pos.line_nr')
if typ_sym.kind == .array && (!(table.type_idx(index_type) in table.number_idxs) && index_type_sym.kind != .enum_) {
c.error('non-integer index (type `$typ_sym.name`)', node.pos)
c.error('non-integer index `$index_type_sym.name` (array type `$typ_sym.name`)', node.pos)
}
else if typ_sym.kind == .map && table.type_idx(index_type) != table.string_type_idx {
c.error('non-string map index (type `$typ_sym.name`)', node.pos)

View File

@ -262,6 +262,9 @@ fn (g mut Gen) expr(node ast.Expr) {
ast.BoolLiteral {
g.write(it.val.str())
}
ast.CharLiteral {
g.write("'$it.val'")
}
ast.EnumVal {
g.write('${it.enum_name}_$it.val')
}
@ -404,7 +407,7 @@ fn (g mut Gen) expr(node ast.Expr) {
}
}
else {
println(term.red('cgen.expr(): bad node'))
verror(term.red('cgen.expr(): bad node ' + typeof(node)))
}
}
}

View File

@ -58,7 +58,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{}
scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0}
}
p.init_parse_fns()
p.read_first_token()
@ -82,7 +82,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
parent: 0
}
// comments_mode: comments_mode
}
p.read_first_token()
// p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0}
@ -359,7 +359,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
return ast.ExprStmt{
expr: expr
// typ: typ
}
}
}
@ -663,7 +663,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
p.expr_mod = ''
return ast.EnumVal{
enum_name: enum_name // lp.prepend_mod(enum_name)
val: val
pos: p.tok.position()
}
@ -825,6 +825,9 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
else if p.tok.kind == .key_as {
p.next()
typ = p.parse_type()
node = ast.AsCast {
typ: typ
}
}
else if p.tok.kind.is_infix() {
node,typ = p.infix_expr(node)
@ -1220,11 +1223,11 @@ fn (p mut Parser) if_expr() ast.Expr {
stmts: stmts
else_stmts: else_stmts
// typ: typ
pos: pos
has_else: has_else
// left: left
}
return node
}
@ -1650,12 +1653,12 @@ fn (p mut Parser) var_decl_and_assign_stmt() ast.Stmt {
return ast.VarDecl{
name: ident.name
// name2: name2
expr: expr // p.expr(token.lowest_prec)
is_mut: info0.is_mut
// typ: typ
pos: p.tok.position()
}
// return p.var_decl(ident[0], exprs[0])
@ -1798,7 +1801,7 @@ fn (p mut Parser) match_expr() ast.Expr {
blocks: blocks
match_exprs: match_exprs
// typ: typ
cond: cond
}
return node

View File

@ -118,9 +118,8 @@ pub fn new_type_ptr(idx int, nr_muls int) Type {
}
pub const (
number_idxs = [int_type_idx, byte_type_idx, u32_type_idx, u64_type_idx]
number_idxs = [int_type_idx, byte_type_idx, u16_type_idx, i16_type_idx, i64_type_idx, u32_type_idx, u64_type_idx]
)
/*
pub fn is_number(typ Type) bool {
typ_sym := c.table.get_type_symbol(typ)