checker: AsCast, CharLiteral, fix integer index check
parent
156e36c082
commit
22ffe336cb
|
@ -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
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue