diff --git a/README.md b/README.md index 1a74e07e7b..9e89f66b2d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 0140dadc10..c9ef539eb8 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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: diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6683423dfc..e08ea8491f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index b9d5cab290..5221083087 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -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))) } } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 4f7f909998..f3b19b95dd 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index c706e4bb97..6433904503 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -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)