builtin: rename `any_*` -> `*_literal` - 2. part (#8032)

pull/8050/head
Uwe Krüger 2021-01-11 22:58:15 +01:00 committed by GitHub
parent 77d2e82309
commit 5f95dd54bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 194 additions and 176 deletions

View File

@ -342,7 +342,7 @@ fn get_src_link(repo_url string, file_name string, line_nr int) string {
fn html_highlight(code string, tb &table.Table) string { fn html_highlight(code string, tb &table.Table) string {
builtin := ['bool', 'string', 'i8', 'i16', 'int', 'i64', 'i128', 'byte', 'u16', 'u32', 'u64', builtin := ['bool', 'string', 'i8', 'i16', 'int', 'i64', 'i128', 'byte', 'u16', 'u32', 'u64',
'u128', 'rune', 'f32', 'f64', 'any_int', 'any_float', 'byteptr', 'voidptr', 'any'] 'u128', 'rune', 'f32', 'f64', 'int_literal', 'float_literal', 'byteptr', 'voidptr', 'any']
highlight_code := fn (tok token.Token, typ HighlightTokenTyp) string { highlight_code := fn (tok token.Token, typ HighlightTokenTyp) string {
lit := if typ in [.unone, .operator, .punctuation] { lit := if typ in [.unone, .operator, .punctuation] {
tok.kind.str() tok.kind.str()

View File

@ -371,8 +371,6 @@ rune // represents a Unicode code point
f32 f64 f32 f64
any_int, any_float // internal intermediate types of number literals
byteptr, voidptr, charptr, size_t // these are mostly used for C interoperability byteptr, voidptr, charptr, size_t // these are mostly used for C interoperability
any // similar to C's void* and Go's interface{} any // similar to C's void* and Go's interface{}
@ -400,6 +398,21 @@ or `i64` but not to `f32` or `u32`. (`f32` would mean precision
loss for large values and `u32` would mean loss of the sign for loss for large values and `u32` would mean loss of the sign for
negative values). negative values).
Literals like `123` or `4.56` are treated in a special way. They do
not lead to type promotions, however they default to `int` and `f64`
respectively, when their type has to be decided:
```v ignore
u := u16(12)
v := 13 + u // v is of type `u16` - no promotion
x := f32(45.6)
y := x + 3.14 // x is of type `f32` - no promotion
a := 75 // a is of type `int` - default for int literal
b := 14.7 // b is of type `f64` - default for float literal
c := u + a // c is of type `int` - automatic promotion of `u`'s value
d := b + x // d is of type `f64` - automatic promotion of `x`'s value
```
### Strings ### Strings
```v ```v

View File

@ -20,9 +20,9 @@ pub fn (x f64) str() string {
} }
} }
// str returns the value of the `any_float` as a `string`. // str returns the value of the `float_literal` as a `string`.
[inline] [inline]
pub fn (d any_float) str() string { pub fn (d float_literal) str() string {
return f64(d).str() return f64(d).str()
} }

View File

@ -162,9 +162,9 @@ pub fn (nn u32) str() string {
// return tos(buf + index, (max-index)) // return tos(buf + index, (max-index))
} }
// str returns the value of the `any_int` as a `string`. // str returns the value of the `int_literal` as a `string`.
[inline] [inline]
pub fn (n any_int) str() string { pub fn (n int_literal) str() string {
return i64(n).str() return i64(n).str()
} }
@ -399,9 +399,9 @@ pub fn (nn i64) hex() string {
return u64(nn).hex() return u64(nn).hex()
} }
// hex returns the value of the `any_int` as a hexadecimal `string`. // hex returns the value of the `int_literal` as a hexadecimal `string`.
// Note that the output is ***not*** zero padded. // Note that the output is ***not*** zero padded.
pub fn (nn any_int) hex() string { pub fn (nn int_literal) hex() string {
return u64(nn).hex() return u64(nn).hex()
} }
@ -434,7 +434,7 @@ pub fn (nn u64) hex_full() string {
/* /*
pub fn (nn i64) hex_full() string { return u64_to_hex(u64(nn), 16) } pub fn (nn i64) hex_full() string { return u64_to_hex(u64(nn), 16) }
pub fn (nn any_int) hex_full() string { return u64_to_hex(nn, 16) } pub fn (nn int_literal) hex_full() string { return u64_to_hex(nn, 16) }
pub fn (nn voidptr) hex_full() string { return u64_to_hex(nn, 16) } pub fn (nn voidptr) hex_full() string { return u64_to_hex(nn, 16) }
pub fn (nn byteptr) hex_full() string { return u64_to_hex(nn, 16) } pub fn (nn byteptr) hex_full() string { return u64_to_hex(nn, 16) }
*/ */

View File

@ -217,9 +217,9 @@ fn (c &Checker) promote_num(left_type table.Type, right_type table.Type) table.T
idx_hi := type_hi.idx() idx_hi := type_hi.idx()
idx_lo := type_lo.idx() idx_lo := type_lo.idx()
// the following comparisons rely on the order of the indices in atypes.v // the following comparisons rely on the order of the indices in atypes.v
if idx_hi == table.any_int_type_idx { if idx_hi == table.int_literal_type_idx {
return type_lo return type_lo
} else if idx_hi == table.any_flt_type_idx { } else if idx_hi == table.float_literal_type_idx {
if idx_lo in table.float_type_idxs { if idx_lo in table.float_type_idxs {
return type_lo return type_lo
} else { } else {
@ -232,7 +232,7 @@ fn (c &Checker) promote_num(left_type table.Type, right_type table.Type) table.T
} else { } else {
return type_hi return type_hi
} }
} else { // f64, any_flt } else { // f64, float_literal
return type_hi return type_hi
} }
} else if idx_lo >= table.byte_type_idx { // both operands are unsigned } else if idx_lo >= table.byte_type_idx { // both operands are unsigned
@ -276,7 +276,7 @@ pub fn (mut c Checker) check_types(got table.Type, expected table.Type) bool {
// allow direct int-literal assignment for pointers for now // allow direct int-literal assignment for pointers for now
// maybe in the future optionals should be used for that // maybe in the future optionals should be used for that
if expected.is_ptr() || expected.is_pointer() { if expected.is_ptr() || expected.is_pointer() {
if got == table.any_int_type { if got == table.int_literal_type {
return true return true
} }
} }
@ -320,13 +320,13 @@ pub fn (mut c Checker) symmetric_check(left table.Type, right table.Type) bool {
// allow direct int-literal assignment for pointers for now // allow direct int-literal assignment for pointers for now
// maybe in the future optionals should be used for that // maybe in the future optionals should be used for that
if right.is_ptr() || right.is_pointer() { if right.is_ptr() || right.is_pointer() {
if left == table.any_int_type { if left == table.int_literal_type {
return true return true
} }
} }
// allow direct int-literal assignment for pointers for now // allow direct int-literal assignment for pointers for now
if left.is_ptr() || left.is_pointer() { if left.is_ptr() || left.is_pointer() {
if right == table.any_int_type { if right == table.int_literal_type {
return true return true
} }
} }
@ -338,7 +338,7 @@ pub fn (c &Checker) get_default_fmt(ftyp table.Type, typ table.Type) byte {
return `s` return `s`
} else if typ.is_float() { } else if typ.is_float() {
return `g` return `g`
} else if typ.is_signed() || typ.is_any_int() { } else if typ.is_signed() || typ.is_int_literal() {
return `d` return `d`
} else if typ.is_unsigned() { } else if typ.is_unsigned() {
return `u` return `u`
@ -399,7 +399,7 @@ pub fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) table.T
} }
if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) || if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) ||
(typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`]) ||
(typ.is_any_int() && fmt !in [`d`, `c`, `x`, `X`, `o`, `u`, `x`, `X`, `o`]) || (typ.is_int_literal() && fmt !in [`d`, `c`, `x`, `X`, `o`, `u`, `x`, `X`, `o`]) ||
(typ.is_float() && fmt !in [`E`, `F`, `G`, `e`, `f`, `g`]) || (typ.is_float() && fmt !in [`E`, `F`, `G`, `e`, `f`, `g`]) ||
(typ.is_pointer() && fmt !in [`p`, `x`, `X`]) || (typ.is_pointer() && fmt !in [`p`, `x`, `X`]) ||
(typ.is_string() && fmt != `s`) || (typ.is_string() && fmt != `s`) ||

View File

@ -319,7 +319,7 @@ pub fn (mut c Checker) type_decl(node ast.TypeDecl) {
c.check_valid_pascal_case(node.name, 'type alias', node.pos) c.check_valid_pascal_case(node.name, 'type alias', node.pos)
} }
typ_sym := c.table.get_type_symbol(node.parent_type) typ_sym := c.table.get_type_symbol(node.parent_type)
if typ_sym.kind in [.placeholder, .any_int, .any_float] { if typ_sym.kind in [.placeholder, .int_literal, .float_literal] {
c.error("type `$typ_sym.name` doesn't exist", node.pos) c.error("type `$typ_sym.name` doesn't exist", node.pos)
} else if typ_sym.kind == .alias { } else if typ_sym.kind == .alias {
orig_sym := c.table.get_type_symbol((typ_sym.info as table.Alias).parent_type) orig_sym := c.table.get_type_symbol((typ_sym.info as table.Alias).parent_type)
@ -356,7 +356,7 @@ pub fn (mut c Checker) type_decl(node ast.TypeDecl) {
if sym.name in names_used { if sym.name in names_used {
c.error('sum type $node.name cannot hold the type `$sym.name` more than once', c.error('sum type $node.name cannot hold the type `$sym.name` more than once',
variant.pos) variant.pos)
} else if sym.kind in [.placeholder, .any_int, .any_float] { } else if sym.kind in [.placeholder, .int_literal, .float_literal] {
c.error("type `$sym.name` doesn't exist", variant.pos) c.error("type `$sym.name` doesn't exist", variant.pos)
} else if sym.kind == .interface_ { } else if sym.kind == .interface_ {
c.error('sum type cannot hold an interface', variant.pos) c.error('sum type cannot hold an interface', variant.pos)
@ -400,10 +400,10 @@ pub fn (mut c Checker) struct_decl(mut decl ast.StructDecl) {
c.error(util.new_suggestion(sym.name, c.table.known_type_names()).say('unknown type `$sym.name`'), c.error(util.new_suggestion(sym.name, c.table.known_type_names()).say('unknown type `$sym.name`'),
field.type_pos) field.type_pos)
} }
// Separate error condition for `any_int` and `any_float` because `util.suggestion` may give different // Separate error condition for `int_literal` and `float_literal` because `util.suggestion` may give different
// suggestions due to f32 comparision issue. // suggestions due to f32 comparision issue.
if sym.kind in [.any_int, .any_float] { if sym.kind in [.int_literal, .float_literal] {
msg := if sym.kind == .any_int { msg := if sym.kind == .int_literal {
'unknown type `$sym.name`.\nDid you mean `int`?' 'unknown type `$sym.name`.\nDid you mean `int`?'
} else { } else {
'unknown type `$sym.name`.\nDid you mean `f64`?' 'unknown type `$sym.name`.\nDid you mean `f64`?'
@ -2174,7 +2174,7 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
pos) pos)
} }
if (exp_type.is_ptr() || exp_type.is_pointer()) && if (exp_type.is_ptr() || exp_type.is_pointer()) &&
(!got_typ.is_ptr() && !got_typ.is_pointer()) && got_typ != table.any_int_type { (!got_typ.is_ptr() && !got_typ.is_pointer()) && got_typ != table.int_literal_type {
pos := return_stmt.exprs[i].position() pos := return_stmt.exprs[i].position()
c.error('fn `$c.cur_fn.name` expects you to return a reference type `${c.table.type_to_str(exp_type)}`, but you are returning `${c.table.type_to_str(got_typ)}` instead', c.error('fn `$c.cur_fn.name` expects you to return a reference type `${c.table.type_to_str(exp_type)}`, but you are returning `${c.table.type_to_str(got_typ)}` instead',
pos) pos)
@ -2608,7 +2608,7 @@ fn scope_register_ab(mut s ast.Scope, pos token.Position, typ table.Type) {
fn (mut c Checker) check_array_init_para_type(para string, expr ast.Expr, pos token.Position) { fn (mut c Checker) check_array_init_para_type(para string, expr ast.Expr, pos token.Position) {
sym := c.table.get_type_symbol(c.expr(expr)) sym := c.table.get_type_symbol(c.expr(expr))
if sym.kind !in [.int, .any_int] { if sym.kind !in [.int, .int_literal] {
c.error('array $para needs to be an int', pos) c.error('array $para needs to be an int', pos)
} }
} }
@ -3207,8 +3207,8 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
return c.chan_init(mut node) return c.chan_init(mut node)
} }
ast.CharLiteral { ast.CharLiteral {
// return any_int, not rune, so that we can do "bytes << `A`" without a cast etc // return int_literal, not rune, so that we can do "bytes << `A`" without a cast etc
// return table.any_int_type // return table.int_literal_type
return table.rune_type return table.rune_type
// return table.byte_type // return table.byte_type
} }
@ -3264,7 +3264,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
return c.enum_val(mut node) return c.enum_val(mut node)
} }
ast.FloatLiteral { ast.FloatLiteral {
return table.any_flt_type return table.float_literal_type
} }
ast.Ident { ast.Ident {
// c.checked_ident = node.name // c.checked_ident = node.name
@ -3289,7 +3289,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
return c.infix_expr(mut node) return c.infix_expr(mut node)
} }
ast.IntegerLiteral { ast.IntegerLiteral {
return table.any_int_type return table.int_literal_type
} }
ast.LockExpr { ast.LockExpr {
return c.lock_expr(mut node) return c.lock_expr(mut node)
@ -3390,8 +3390,8 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type {
node.pos) node.pos)
} }
if to_type_sym.kind == .sum_type { if to_type_sym.kind == .sum_type {
if node.expr_type in [table.any_int_type, table.any_flt_type] { if node.expr_type in [table.int_literal_type, table.float_literal_type] {
node.expr_type = c.promote_num(node.expr_type, if node.expr_type == table.any_int_type { node.expr_type = c.promote_num(node.expr_type, if node.expr_type == table.int_literal_type {
table.int_type table.int_type
} else { } else {
table.f64_type table.f64_type
@ -3407,7 +3407,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type {
node.pos) node.pos)
} }
} else if node.typ == table.string_type && } else if node.typ == table.string_type &&
(from_type_sym.kind in [.any_int, .int, .byte, .byteptr, .bool] || (from_type_sym.kind in [.int_literal, .int, .byte, .byteptr, .bool] ||
(from_type_sym.kind == .array && from_type_sym.name == 'array_byte')) { (from_type_sym.kind == .array && from_type_sym.name == 'array_byte')) {
type_name := c.table.type_to_str(node.expr_type) type_name := c.table.type_to_str(node.expr_type)
c.error('cannot cast type `$type_name` to string, use `x.str()` instead', node.pos) c.error('cannot cast type `$type_name` to string, use `x.str()` instead', node.pos)
@ -4318,25 +4318,25 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
node.is_expr = true node.is_expr = true
node.typ = last_expr.typ node.typ = last_expr.typ
continue continue
} else if node.typ in [table.any_flt_type, table.any_int_type] { } else if node.typ in [table.float_literal_type, table.int_literal_type] {
if node.typ == table.any_int_type { if node.typ == table.int_literal_type {
if last_expr.typ.is_int() || last_expr.typ.is_float() { if last_expr.typ.is_int() || last_expr.typ.is_float() {
node.typ = last_expr.typ node.typ = last_expr.typ
continue continue
} }
} else { // node.typ == any_float } else { // node.typ == float_literal
if last_expr.typ.is_float() { if last_expr.typ.is_float() {
node.typ = last_expr.typ node.typ = last_expr.typ
continue continue
} }
} }
} }
if last_expr.typ in [table.any_flt_type, table.any_int_type] { if last_expr.typ in [table.float_literal_type, table.int_literal_type] {
if last_expr.typ == table.any_int_type { if last_expr.typ == table.int_literal_type {
if node.typ.is_int() || node.typ.is_float() { if node.typ.is_int() || node.typ.is_float() {
continue continue
} }
} else { // expr_type == any_float } else { // expr_type == float_literal
if node.typ.is_float() { if node.typ.is_float() {
continue continue
} }
@ -4378,9 +4378,9 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
} }
} }
// if only untyped literals were given default to int/f64 // if only untyped literals were given default to int/f64
if node.typ == table.any_int_type { if node.typ == table.int_literal_type {
node.typ = table.int_type node.typ = table.int_type
} else if node.typ == table.any_flt_type { } else if node.typ == table.float_literal_type {
node.typ = table.f64_type node.typ = table.f64_type
} }
if expr_required && !node.has_else { if expr_required && !node.has_else {
@ -5096,14 +5096,14 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
for arg in node.params { for arg in node.params {
sym := c.table.get_type_symbol(arg.typ) sym := c.table.get_type_symbol(arg.typ)
if sym.kind == .placeholder || if sym.kind == .placeholder ||
(sym.kind in [table.Kind.any_int, .any_float] && !c.is_builtin_mod) { (sym.kind in [table.Kind.int_literal, .float_literal] && !c.is_builtin_mod) {
c.error('unknown type `$sym.name`', node.pos) c.error('unknown type `$sym.name`', node.pos)
} }
} }
} }
return_sym := c.table.get_type_symbol(node.return_type) return_sym := c.table.get_type_symbol(node.return_type)
if node.language == .v && if node.language == .v &&
return_sym.kind in [.placeholder, .any_int, .any_float] && return_sym.language == .v { return_sym.kind in [.placeholder, .int_literal, .float_literal] && return_sym.language == .v {
c.error('unknown type `$return_sym.name`', node.pos) c.error('unknown type `$return_sym.name`', node.pos)
} }
if node.language == .v && node.is_method && node.name == 'str' { if node.language == .v && node.is_method && node.name == 'str' {

View File

@ -1,47 +1,47 @@
vlib/v/checker/tests/any_int_float_ban_err.vv:1:12: error: type `any_int` doesn't exist vlib/v/checker/tests/any_int_float_ban_err.vv:1:12: error: type `int_literal` doesn't exist
1 | type Foo = any_int | any_float 1 | type Foo = int_literal | float_literal
| ~~~~~~~ | ~~~~~~~~~~~
2 | type Fo2 = any_int 2 | type Fo2 = int_literal
3 | 3 |
vlib/v/checker/tests/any_int_float_ban_err.vv:2:1: error: type `any_int` doesn't exist vlib/v/checker/tests/any_int_float_ban_err.vv:2:1: error: type `int_literal` doesn't exist
1 | type Foo = any_int | any_float 1 | type Foo = int_literal | float_literal
2 | type Fo2 = any_int 2 | type Fo2 = int_literal
| ~~~~~~~~ | ~~~~~~~~
3 | 3 |
4 | struct Int { 4 | struct Int {
vlib/v/checker/tests/any_int_float_ban_err.vv:5:7: error: unknown type `any_int`. vlib/v/checker/tests/any_int_float_ban_err.vv:5:7: error: unknown type `int_literal`.
Did you mean `any_float`? Did you mean `float_literal`?
3 | 3 |
4 | struct Int { 4 | struct Int {
5 | i any_int 5 | i int_literal
| ~~~~~~~ | ~~~~~~~~~~~
6 | f any_float 6 | f float_literal
7 | } 7 | }
vlib/v/checker/tests/any_int_float_ban_err.vv:6:7: error: unknown type `any_float`. vlib/v/checker/tests/any_int_float_ban_err.vv:6:7: error: unknown type `float_literal`.
Did you mean `any_int`? Did you mean `int_literal`?
4 | struct Int { 4 | struct Int {
5 | i any_int 5 | i int_literal
6 | f any_float 6 | f float_literal
| ~~~~~~~~~ | ~~~~~~~~~~~~~
7 | } 7 | }
8 | 8 |
vlib/v/checker/tests/any_int_float_ban_err.vv:9:1: error: unknown type `any_int` vlib/v/checker/tests/any_int_float_ban_err.vv:9:1: error: unknown type `int_literal`
7 | } 7 | }
8 | 8 |
9 | fn foo(i any_int) any_int { 9 | fn foo(i int_literal) int_literal {
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 | return i 10 | return i
11 | } 11 | }
vlib/v/checker/tests/any_int_float_ban_err.vv:13:1: error: unknown type `any_int` vlib/v/checker/tests/any_int_float_ban_err.vv:13:1: error: unknown type `int_literal`
11 | } 11 | }
12 | 12 |
13 | fn foo2() any_int { 13 | fn foo2() int_literal {
| ~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~
14 | return 1 14 | return 1
15 | } 15 | }
vlib/v/checker/tests/any_int_float_ban_err.vv:14:12: error: cannot use `int literal` as type `any_int` in return argument vlib/v/checker/tests/any_int_float_ban_err.vv:14:12: error: cannot use `int literal` as type `int_literal` in return argument
12 | 12 |
13 | fn foo2() any_int { 13 | fn foo2() int_literal {
14 | return 1 14 | return 1
| ^ | ^
15 | } 15 | }

View File

@ -1,15 +1,15 @@
type Foo = any_int | any_float type Foo = int_literal | float_literal
type Fo2 = any_int type Fo2 = int_literal
struct Int { struct Int {
i any_int i int_literal
f any_float f float_literal
} }
fn foo(i any_int) any_int { fn foo(i int_literal) int_literal {
return i return i
} }
fn foo2() any_int { fn foo2() int_literal {
return 1 return 1
} }

View File

@ -23,7 +23,7 @@ fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) tabl
} }
if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) || if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) ||
(typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`]) ||
(typ.is_any_int() && fmt !in [`d`, `c`, `x`, `X`, `o`, `u`, `x`, `X`, `o`]) || (typ.is_int_literal() && fmt !in [`d`, `c`, `x`, `X`, `o`, `u`, `x`, `X`, `o`]) ||
(typ.is_float() && fmt !in [`E`, `F`, `G`, `e`, `f`, `g`]) || (typ.is_float() && fmt !in [`E`, `F`, `G`, `e`, `f`, `g`]) ||
(typ.is_pointer() && fmt !in [`p`, `x`, `X`]) || (typ.is_pointer() && fmt !in [`p`, `x`, `X`]) ||
(typ.is_string() && fmt != `s`) || (typ.is_string() && fmt != `s`) ||

View File

@ -25,7 +25,7 @@ fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) tabl
c.error('plus prefix only allowd for numbers', node.fmt_poss[i]) c.error('plus prefix only allowd for numbers', node.fmt_poss[i])
} }
if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) || (typ.is_signed() && if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) || (typ.is_signed() &&
fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_any_int() fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_int_literal()
&& fmt !in [`d`, `c`, `x`, `X`, `o`, && fmt !in [`d`, `c`, `x`, `X`, `o`,
`u`, `x`, `X`, `o`]) || (typ.is_float() && fmt !in [`E`, `F`, `u`, `x`, `X`, `o`]) || (typ.is_float() && fmt !in [`E`, `F`,
`G`, `e`, `f`, `g`]) || (typ.is_pointer() && `G`, `e`, `f`, `g`]) || (typ.is_pointer() &&

View File

@ -3235,7 +3235,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
d := !b && d := !b &&
g.typ((left_sym.info as table.Alias).parent_type).split('__').last()[0].is_capital() g.typ((left_sym.info as table.Alias).parent_type).split('__').last()[0].is_capital()
// Do not generate operator overloading with these `right_sym.kind`. // Do not generate operator overloading with these `right_sym.kind`.
e := right_sym.kind !in [.voidptr, .any_int, .int] e := right_sym.kind !in [.voidptr, .int_literal, .int]
if node.op in [.plus, .minus, .mul, .div, .mod, .lt, .gt, .eq, .ne, .le, .ge] && if node.op in [.plus, .minus, .mul, .div, .mod, .lt, .gt, .eq, .ne, .le, .ge] &&
((a && b && e) || c || d) { ((a && b && e) || c || d) {
// Overloaded operators // Overloaded operators

View File

@ -439,8 +439,8 @@ typedef uint8_t byte;
typedef uint32_t rune; typedef uint32_t rune;
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
typedef int64_t any_int; typedef int64_t int_literal;
typedef double any_float; typedef double float_literal;
typedef unsigned char* byteptr; typedef unsigned char* byteptr;
typedef void* voidptr; typedef void* voidptr;
typedef char* charptr; typedef char* charptr;

View File

@ -18,7 +18,7 @@ fn (mut g JsGen) to_js_typ_val(t table.Type) string {
mut styp := '' mut styp := ''
mut prefix := if g.file.mod.name == 'builtin' { 'new ' } else { '' } mut prefix := if g.file.mod.name == 'builtin' { 'new ' } else { '' }
match sym.kind { match sym.kind {
.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64, .any_int, .any_float, .size_t { .i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64, .int_literal, .float_literal, .size_t {
styp = '${prefix}${g.sym_to_js_typ(sym)}(0)' styp = '${prefix}${g.sym_to_js_typ(sym)}(0)'
} }
.bool { .bool {
@ -57,8 +57,8 @@ fn (mut g JsGen) sym_to_js_typ(sym table.TypeSymbol) string {
.u64 { styp = 'u64' } .u64 { styp = 'u64' }
.f32 { styp = 'f32' } .f32 { styp = 'f32' }
.f64 { styp = 'f64' } .f64 { styp = 'f64' }
.any_int { styp = 'any_int' } .int_literal { styp = 'int_literal' }
.any_float { styp = 'any_float' } .float_literal { styp = 'float_literal' }
.size_t { styp = 'size_t' } .size_t { styp = 'size_t' }
.bool { styp = 'bool' } .bool { styp = 'bool' }
.string { styp = 'string' } .string { styp = 'string' }
@ -90,7 +90,7 @@ pub fn (mut g JsGen) typ(t table.Type) string {
.byteptr, .charptr { .byteptr, .charptr {
styp = '${g.sym_to_js_typ(sym)}' styp = '${g.sym_to_js_typ(sym)}'
} }
.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64, .any_int, .any_float, .size_t { .i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64, .int_literal, .float_literal, .size_t {
styp = '${g.sym_to_js_typ(sym)}' styp = '${g.sym_to_js_typ(sym)}'
} }
.bool { .bool {
@ -234,11 +234,11 @@ fn (mut g JsGen) gen_builtin_type_defs() {
for typ_name in v_types { for typ_name in v_types {
// TODO: JsDoc // TODO: JsDoc
match typ_name { match typ_name {
'i8', 'i16', 'int', 'i64', 'byte', 'u16', 'u32', 'u64', 'any_int', 'size_t' { 'i8', 'i16', 'int', 'i64', 'byte', 'u16', 'u32', 'u64', 'int_literal', 'size_t' {
// TODO: Bounds checking // TODO: Bounds checking
g.gen_builtin_prototype(typ_name, 'val', 'new Number(0)', 'this.val = val | 0;', 'this.val | 0', '(this.val | 0).toString()', '') g.gen_builtin_prototype(typ_name, 'val', 'new Number(0)', 'this.val = val | 0;', 'this.val | 0', '(this.val | 0).toString()', '')
} }
'f32', 'f64', 'any_float' { 'f32', 'f64', 'float_literal' {
g.gen_builtin_prototype(typ_name, 'val', 'new Number(0)', 'this.val = val;', 'this.val', 'this.val.toString()', '') g.gen_builtin_prototype(typ_name, 'val', 'new Number(0)', 'this.val = val;', 'this.val', 'this.val.toString()', '')
} }
'bool' { 'bool' {
@ -257,4 +257,4 @@ fn (mut g JsGen) gen_builtin_type_defs() {
} }
} }
g.dec_indent() g.dec_indent()
} }

View File

@ -16,8 +16,8 @@ const (
'public', 'return', 'static', 'super', 'switch', 'this', 'throw', 'try', 'typeof', 'var', 'void', 'public', 'return', 'static', 'super', 'switch', 'this', 'throw', 'try', 'typeof', 'var', 'void',
'while', 'with', 'yield', 'Number', 'String', 'Boolean', 'Array', 'Map'] 'while', 'with', 'yield', 'Number', 'String', 'Boolean', 'Array', 'Map']
// used to generate type structs // used to generate type structs
v_types = ['i8', 'i16', 'int', 'i64', 'byte', 'u16', 'u32', 'u64', 'f32', 'f64', 'any_int', v_types = ['i8', 'i16', 'int', 'i64', 'byte', 'u16', 'u32', 'u64', 'f32', 'f64', 'int_literal',
'any_float', 'size_t', 'bool', 'string', 'map', 'array'] 'float_literal', 'size_t', 'bool', 'string', 'map', 'array']
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t', tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t',
'\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t'] '\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t']
) )
@ -1416,7 +1416,7 @@ fn (mut g JsGen) greater_typ(left table.Type, right table.Type) table.Type {
if table.f32_type_idx in lr { if table.f32_type_idx in lr {
return table.Type(table.f32_type_idx) return table.Type(table.f32_type_idx)
} }
return table.Type(table.any_flt_type) return table.Type(table.float_literal_type)
} }
should_int := (l in table.integer_type_idxs && r in table.integer_type_idxs) should_int := (l in table.integer_type_idxs && r in table.integer_type_idxs)
if should_int { if should_int {
@ -1443,7 +1443,7 @@ fn (mut g JsGen) greater_typ(left table.Type, right table.Type) table.Type {
if table.i8_type_idx in lr { if table.i8_type_idx in lr {
return table.Type(table.i8_type_idx) return table.Type(table.i8_type_idx)
} }
return table.Type(table.any_int_type_idx) return table.Type(table.int_literal_type_idx)
} }
return table.Type(l) return table.Type(l)
} }

View File

@ -336,11 +336,11 @@ pub fn (mut p Parser) parse_any_type(language table.Language, is_ptr bool, check
'bool' { 'bool' {
return table.bool_type return table.bool_type
} }
'any_float', 'float_literal' { 'float_literal' {
return table.any_flt_type return table.float_literal_type
} }
'any_int', 'int_literal' { 'int_literal' {
return table.any_int_type return table.int_literal_type
} }
else { else {
if name.len == 1 && name[0].is_capital() { if name.len == 1 && name[0].is_capital() {

View File

@ -680,8 +680,8 @@ pub fn (t &Table) value_type(typ Type) Type {
[inline] [inline]
pub fn (t &Table) mktyp(typ Type) Type { pub fn (t &Table) mktyp(typ Type) Type {
match typ { match typ {
any_flt_type { return f64_type } float_literal_type { return f64_type }
any_int_type { return int_type } int_literal_type { return int_type }
else { return typ } else { return typ }
} }
} }
@ -732,8 +732,8 @@ pub fn (table &Table) sumtype_has_variant(parent Type, variant Type) bool {
pub fn (table &Table) known_type_names() []string { pub fn (table &Table) known_type_names() []string {
mut res := []string{} mut res := []string{}
for _, idx in table.type_idxs { for _, idx in table.type_idxs {
// Skip `any_int_type_idx` and `any_flt_type_idx` because they shouldn't be visible to the User. // Skip `int_literal_type_idx` and `float_literal_type_idx` because they shouldn't be visible to the User.
if idx in [0, any_int_type_idx, any_flt_type_idx] { if idx in [0, int_literal_type_idx, float_literal_type_idx] {
continue continue
} }
res << table.type_to_str(idx) res << table.type_to_str(idx)

View File

@ -264,8 +264,8 @@ pub fn (typ Type) is_unsigned() bool {
} }
[inline] [inline]
pub fn (typ Type) is_any_int() bool { pub fn (typ Type) is_int_literal() bool {
return typ.idx() == any_int_type_idx return typ.idx() == int_literal_type_idx
} }
[inline] [inline]
@ -279,43 +279,43 @@ pub fn (typ Type) is_string() bool {
} }
pub const ( pub const (
void_type_idx = 1 void_type_idx = 1
voidptr_type_idx = 2 voidptr_type_idx = 2
byteptr_type_idx = 3 byteptr_type_idx = 3
charptr_type_idx = 4 charptr_type_idx = 4
i8_type_idx = 5 i8_type_idx = 5
i16_type_idx = 6 i16_type_idx = 6
int_type_idx = 7 int_type_idx = 7
i64_type_idx = 8 i64_type_idx = 8
byte_type_idx = 9 byte_type_idx = 9
u16_type_idx = 10 u16_type_idx = 10
u32_type_idx = 11 u32_type_idx = 11
u64_type_idx = 12 u64_type_idx = 12
f32_type_idx = 13 f32_type_idx = 13
f64_type_idx = 14 f64_type_idx = 14
char_type_idx = 15 char_type_idx = 15
bool_type_idx = 16 bool_type_idx = 16
none_type_idx = 17 none_type_idx = 17
string_type_idx = 18 string_type_idx = 18
ustring_type_idx = 19 ustring_type_idx = 19
rune_type_idx = 20 rune_type_idx = 20
array_type_idx = 21 array_type_idx = 21
map_type_idx = 22 map_type_idx = 22
chan_type_idx = 23 chan_type_idx = 23
sizet_type_idx = 24 sizet_type_idx = 24
any_type_idx = 25 any_type_idx = 25
any_flt_type_idx = 26 float_literal_type_idx = 26
any_int_type_idx = 27 int_literal_type_idx = 27
) )
pub const ( pub const (
integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx, integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx,
u16_type_idx, u32_type_idx, u64_type_idx, any_int_type_idx, rune_type_idx] u16_type_idx, u32_type_idx, u64_type_idx, int_literal_type_idx, rune_type_idx]
signed_integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx] signed_integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx]
unsigned_integer_type_idxs = [byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx] unsigned_integer_type_idxs = [byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx]
float_type_idxs = [f32_type_idx, f64_type_idx, any_flt_type_idx] float_type_idxs = [f32_type_idx, f64_type_idx, float_literal_type_idx]
number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx, number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx,
u16_type_idx, u32_type_idx, u64_type_idx, f32_type_idx, f64_type_idx, any_int_type_idx, any_flt_type_idx, u16_type_idx, u32_type_idx, u64_type_idx, f32_type_idx, f64_type_idx, int_literal_type_idx, float_literal_type_idx,
rune_type_idx, rune_type_idx,
] ]
pointer_type_idxs = [voidptr_type_idx, byteptr_type_idx, charptr_type_idx] pointer_type_idxs = [voidptr_type_idx, byteptr_type_idx, charptr_type_idx]
@ -323,39 +323,39 @@ pub const (
) )
pub const ( pub const (
void_type = new_type(void_type_idx) void_type = new_type(void_type_idx)
ovoid_type = new_type(void_type_idx).set_flag(.optional) // the return type of `fn () ?` ovoid_type = new_type(void_type_idx).set_flag(.optional) // the return type of `fn () ?`
voidptr_type = new_type(voidptr_type_idx) voidptr_type = new_type(voidptr_type_idx)
byteptr_type = new_type(byteptr_type_idx) byteptr_type = new_type(byteptr_type_idx)
charptr_type = new_type(charptr_type_idx) charptr_type = new_type(charptr_type_idx)
i8_type = new_type(i8_type_idx) i8_type = new_type(i8_type_idx)
int_type = new_type(int_type_idx) int_type = new_type(int_type_idx)
i16_type = new_type(i16_type_idx) i16_type = new_type(i16_type_idx)
i64_type = new_type(i64_type_idx) i64_type = new_type(i64_type_idx)
byte_type = new_type(byte_type_idx) byte_type = new_type(byte_type_idx)
u16_type = new_type(u16_type_idx) u16_type = new_type(u16_type_idx)
u32_type = new_type(u32_type_idx) u32_type = new_type(u32_type_idx)
u64_type = new_type(u64_type_idx) u64_type = new_type(u64_type_idx)
f32_type = new_type(f32_type_idx) f32_type = new_type(f32_type_idx)
f64_type = new_type(f64_type_idx) f64_type = new_type(f64_type_idx)
char_type = new_type(char_type_idx) char_type = new_type(char_type_idx)
bool_type = new_type(bool_type_idx) bool_type = new_type(bool_type_idx)
none_type = new_type(none_type_idx) none_type = new_type(none_type_idx)
string_type = new_type(string_type_idx) string_type = new_type(string_type_idx)
ustring_type = new_type(ustring_type_idx) ustring_type = new_type(ustring_type_idx)
rune_type = new_type(rune_type_idx) rune_type = new_type(rune_type_idx)
array_type = new_type(array_type_idx) array_type = new_type(array_type_idx)
map_type = new_type(map_type_idx) map_type = new_type(map_type_idx)
chan_type = new_type(chan_type_idx) chan_type = new_type(chan_type_idx)
any_type = new_type(any_type_idx) any_type = new_type(any_type_idx)
any_flt_type = new_type(any_flt_type_idx) float_literal_type = new_type(float_literal_type_idx)
any_int_type = new_type(any_int_type_idx) int_literal_type = new_type(int_literal_type_idx)
) )
pub const ( pub const (
builtin_type_names = ['void', 'voidptr', 'charptr', 'byteptr', 'i8', 'i16', 'int', 'i64', 'u16', builtin_type_names = ['void', 'voidptr', 'charptr', 'byteptr', 'i8', 'i16', 'int', 'i64', 'u16',
'u32', 'u64', 'any_int', 'f32', 'f64', 'any_float', 'string', 'ustring', 'char', 'byte', 'bool', 'u32', 'u64', 'int_literal', 'f32', 'f64', 'float_literal', 'string', 'ustring', 'char', 'byte',
'none', 'array', 'array_fixed', 'map', 'chan', 'any', 'struct', 'mapnode', 'size_t', 'rune'] 'bool', 'none', 'array', 'array_fixed', 'map', 'chan', 'any', 'struct', 'mapnode', 'size_t', 'rune']
) )
pub struct MultiReturn { pub struct MultiReturn {
@ -414,8 +414,8 @@ pub enum Kind {
enum_ enum_
function function
interface_ interface_
any_float float_literal
any_int int_literal
aggregate aggregate
} }
@ -518,12 +518,17 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_type_symbol(kind: .size_t, name: 'size_t', cname: 'size_t', mod: 'builtin') t.register_type_symbol(kind: .size_t, name: 'size_t', cname: 'size_t', mod: 'builtin')
t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin') t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin')
t.register_type_symbol( t.register_type_symbol(
kind: .any_float kind: .float_literal
name: 'float literal' name: 'float literal'
cname: 'any_float' cname: 'float_literal'
mod: 'builtin'
)
t.register_type_symbol(
kind: .int_literal
name: 'int literal'
cname: 'int_literal'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol(kind: .any_int, name: 'int literal', cname: 'any_int', mod: 'builtin')
} }
[inline] [inline]
@ -533,12 +538,12 @@ pub fn (t &TypeSymbol) is_pointer() bool {
[inline] [inline]
pub fn (t &TypeSymbol) is_int() bool { pub fn (t &TypeSymbol) is_int() bool {
return t.kind in [.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .any_int] return t.kind in [.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .int_literal]
} }
[inline] [inline]
pub fn (t &TypeSymbol) is_float() bool { pub fn (t &TypeSymbol) is_float() bool {
return t.kind in [.f32, .f64, .any_float] return t.kind in [.f32, .f64, .float_literal]
} }
[inline] [inline]
@ -563,10 +568,10 @@ pub fn (k Kind) str() string {
.u16 { 'u16' } .u16 { 'u16' }
.u32 { 'u32' } .u32 { 'u32' }
.u64 { 'u64' } .u64 { 'u64' }
.any_int { 'any_int' } .int_literal { 'int_literal' }
.f32 { 'f32' } .f32 { 'f32' }
.f64 { 'f64' } .f64 { 'f64' }
.any_float { 'any_float' } .float_literal { 'float_literal' }
.string { 'string' } .string { 'string' }
.char { 'char' } .char { 'char' }
.bool { 'bool' } .bool { 'bool' }
@ -714,7 +719,7 @@ pub fn (table &Table) type_to_str(t Type) string {
// type name in code (for builtin) // type name in code (for builtin)
pub fn (table &Table) type_to_code(t Type) string { pub fn (table &Table) type_to_code(t Type) string {
match t { match t {
any_int_type, any_flt_type { return table.get_type_symbol(t).kind.str() } int_literal_type, float_literal_type { return table.get_type_symbol(t).kind.str() }
else { return table.type_to_str_using_aliases(t, map[string]string{}) } else { return table.type_to_str_using_aliases(t, map[string]string{}) }
} }
} }
@ -724,7 +729,7 @@ pub fn (table &Table) type_to_str_using_aliases(t Type, import_aliases map[strin
sym := table.get_type_symbol(t) sym := table.get_type_symbol(t)
mut res := sym.name mut res := sym.name
match sym.kind { match sym.kind {
.any_int, .any_float { .int_literal, .float_literal {
res = sym.name res = sym.name
} }
.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64, .char, .rune, .string, .bool, .none_, .byteptr, .voidptr, .charptr { .i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64, .char, .rune, .string, .bool, .none_, .byteptr, .voidptr, .charptr {

View File

@ -20,7 +20,7 @@ fn test_typeof_for_builtin_int_types() {
assert typeof(i8(1)) == 'i8' assert typeof(i8(1)) == 'i8'
assert typeof(i16(1)) == 'i16' assert typeof(i16(1)) == 'i16'
assert typeof(int(1)) == 'int' assert typeof(int(1)) == 'int'
// assert typeof(1) == 'any_int' // assert typeof(1) == 'int_literal'
assert typeof(i64(1)) == 'i64' assert typeof(i64(1)) == 'i64'
assert typeof(byte(1)) == 'byte' assert typeof(byte(1)) == 'byte'
assert typeof(u16(1)) == 'u16' assert typeof(u16(1)) == 'u16'
@ -31,7 +31,7 @@ fn test_typeof_for_builtin_int_types() {
fn test_typeof_for_builtin_float_types() { fn test_typeof_for_builtin_float_types() {
assert typeof(f32(1.0)) == 'f32' assert typeof(f32(1.0)) == 'f32'
assert typeof(f64(1.0)) == 'f64' assert typeof(f64(1.0)) == 'f64'
// assert typeof(1.0) == 'any_float' // assert typeof(1.0) == 'float_literal'
} }
fn test_typeof_for_builtin_string_type() { fn test_typeof_for_builtin_string_type() {

View File

@ -8,11 +8,11 @@ fn test_typeof_on_simple_expressions() {
assert typeof(a) == 'int' assert typeof(a) == 'int'
assert typeof(a).name == 'int' assert typeof(a).name == 'int'
// a2 := 123 // a2 := 123
// assert typeof(a2) == 'any_int' // assert typeof(a2) == 'int_literal'
// assert typeof(42) == 'any_int' // assert typeof(42) == 'int_literal'
// assert typeof(3.14) == 'any_float' // assert typeof(3.14) == 'float_literal'
// assert typeof(2+2*10) == 'any_int' // assert typeof(2+2*10) == 'int_literal'
// assert typeof(1.0 * 12.2) == 'any_float' // assert typeof(1.0 * 12.2) == 'float_literal'
} }
fn test_arrays() { fn test_arrays() {