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 - Built-in ORM
- C and JavaScript backends - 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 ## 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 | FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr |
ConcatExpr | TypeName ConcatExpr | TypeName | AsCast
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
@ -408,6 +408,11 @@ pub:
pos token.Position pos token.Position
} }
pub struct AsCast {
pub:
typ table.Type
}
// e.g. `[unsafe_fn]` // e.g. `[unsafe_fn]`
pub struct Attr { pub struct Attr {
pub: pub:

View File

@ -480,6 +480,9 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
ast.ArrayInit { ast.ArrayInit {
return c.array_init(mut it) return c.array_init(mut it)
} }
ast.AsCast {
return it.typ
}
ast.AssignExpr { ast.AssignExpr {
c.check_assign_expr(it) 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) index_type_sym := c.table.get_type_symbol(index_type)
// println('index expr left=$typ_sym.name $node.pos.line_nr') // 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_) { 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 { 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) 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 { ast.BoolLiteral {
g.write(it.val.str()) g.write(it.val.str())
} }
ast.CharLiteral {
g.write("'$it.val'")
}
ast.EnumVal { ast.EnumVal {
g.write('${it.enum_name}_$it.val') g.write('${it.enum_name}_$it.val')
} }
@ -404,7 +407,7 @@ fn (g mut Gen) expr(node ast.Expr) {
} }
} }
else { else {
println(term.red('cgen.expr(): bad node')) verror(term.red('cgen.expr(): bad node ' + typeof(node)))
} }
} }
} }

View File

@ -825,6 +825,9 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
else if p.tok.kind == .key_as { else if p.tok.kind == .key_as {
p.next() p.next()
typ = p.parse_type() typ = p.parse_type()
node = ast.AsCast {
typ: typ
}
} }
else if p.tok.kind.is_infix() { else if p.tok.kind.is_infix() {
node,typ = p.infix_expr(node) node,typ = p.infix_expr(node)

View File

@ -118,9 +118,8 @@ pub fn new_type_ptr(idx int, nr_muls int) Type {
} }
pub const ( 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 { pub fn is_number(typ Type) bool {
typ_sym := c.table.get_type_symbol(typ) typ_sym := c.table.get_type_symbol(typ)