v2: `as` cast
parent
bb60b3081f
commit
f241945d70
|
@ -367,9 +367,11 @@ pub fn (b []byte) hex() string {
|
|||
mut hex := malloc(b.len * 2 + 1)
|
||||
mut ptr := &hex[0]
|
||||
for i := 0; i < b.len; i++ {
|
||||
// QTODO
|
||||
ptr += C.sprintf(ptr as charptr, '%02x', b[i])
|
||||
}
|
||||
return string(hex)
|
||||
return hex as string
|
||||
//return string(hex)
|
||||
}
|
||||
|
||||
// copy copies the `src` byte array elements to the `dst` byte array.
|
||||
|
|
|
@ -84,6 +84,8 @@ fn (p mut Parser) bool_expression() string {
|
|||
if typ == cast_typ {
|
||||
p.warn('casting `$typ` to `$cast_typ` is not needed')
|
||||
}
|
||||
is_byteptr := typ == 'byte*' || typ == 'byteptr'
|
||||
is_bytearr := typ == 'array_byte'
|
||||
if typ in p.table.sum_types {
|
||||
T := p.table.find_type(cast_typ)
|
||||
if T.parent != typ {
|
||||
|
@ -104,7 +106,28 @@ exit(1);
|
|||
')
|
||||
*/
|
||||
|
||||
} else {
|
||||
} else if cast_typ == 'string' {
|
||||
if is_byteptr || is_bytearr {
|
||||
if p.tok == .comma {
|
||||
p.check(.comma)
|
||||
p.cgen.set_placeholder(start_ph, 'tos((byte *)')
|
||||
if is_bytearr {
|
||||
p.gen('.data')
|
||||
}
|
||||
p.gen(', ')
|
||||
p.check_types(p.expression(), 'int')
|
||||
}
|
||||
else {
|
||||
if is_bytearr {
|
||||
p.gen('.data')
|
||||
}
|
||||
p.cgen.set_placeholder(start_ph, '/*!!!*/tos2((byte *)')
|
||||
p.gen(')')
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
p.cgen.set_placeholder(start_ph, '($cast_typ)(')
|
||||
p.gen(')')
|
||||
}
|
||||
|
|
|
@ -46,6 +46,7 @@ fn (c mut Checker) resolve_types() {
|
|||
}
|
||||
// update any types with unresolved sub types
|
||||
for idx, t in c.table.types {
|
||||
println('Resolve type: $t.name')
|
||||
if t.kind == .array {
|
||||
mut info := t.array_info()
|
||||
if info.elem_type.typ.kind == .unresolved {
|
||||
|
@ -167,6 +168,14 @@ pub fn (c &Checker) check_method_call_expr(method_call_expr ast.MethodCallExpr)
|
|||
if method := typ.typ.find_method(method_call_expr.name) {
|
||||
return method.return_type
|
||||
}
|
||||
if typ.typ.kind == .array {
|
||||
a := c.table.find_type('array') or {
|
||||
exit(1)
|
||||
}
|
||||
if method := a.find_method(method_call_expr.name) {
|
||||
return method.return_type
|
||||
}
|
||||
}
|
||||
c.error('type `$typ.typ.name` has no method `$method_call_expr.name`', method_call_expr.pos)
|
||||
exit(1)
|
||||
}
|
||||
|
|
|
@ -579,6 +579,10 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.TypeRef) {
|
|||
else if p.tok.kind == .lsbr {
|
||||
node = p.index_expr(node) // , typ)
|
||||
}
|
||||
else if p.tok.kind == .key_as {
|
||||
p.next()
|
||||
typ = p.parse_type()
|
||||
}
|
||||
else if p.tok.kind.is_infix() {
|
||||
node,typ = p.infix_expr(node)
|
||||
}
|
||||
|
|
|
@ -393,7 +393,7 @@ pub fn (tok Token) precedence() int {
|
|||
.left_shift_assign, .righ_shift_assign, .mult_assign {
|
||||
return 2
|
||||
}
|
||||
.key_in {
|
||||
.key_in, .key_as {
|
||||
return 1
|
||||
}
|
||||
// /.plus_assign {
|
||||
|
@ -464,7 +464,9 @@ pub fn (tok Kind) is_relational() bool {
|
|||
}
|
||||
|
||||
pub fn (kind Kind) is_infix() bool {
|
||||
return kind in [.plus, .minus, .mod, .mul, .div, .eq, .ne, .gt, .lt, .key_in, .ge, .le, .logical_or,
|
||||
return kind in [.plus, .minus, .mod, .mul, .div, .eq, .ne, .gt, .lt, .key_in,
|
||||
//
|
||||
.key_as, .ge, .le, .logical_or,
|
||||
//
|
||||
.and, .dot, .pipe, .amp, .left_shift, .right_shift]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue