Make sure changetype works

This commit is contained in:
dcodeIO 2018-01-02 05:02:05 +01:00
parent dc78fc8d20
commit 2b182b505e
7 changed files with 43 additions and 12 deletions

View File

@ -10,7 +10,7 @@ import {
Range
} from "./tokenizer";
import{
import {
CharCode
} from "./util/charcode";

View File

@ -694,11 +694,11 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
return module.createUnreachable();
arg0 = compiler.compileExpression(operands[0], Type.void, ConversionKind.NONE);
if ((compiler.currentType == usizeType && (<Type[]>typeArguments)[1].classType) || (compiler.currentType.classType && (<Type[]>typeArguments)[1] == usizeType)) {
compiler.currentType = (<Type[]>typeArguments)[1];
if ((compiler.currentType == usizeType && (<Type[]>typeArguments)[0].classType) || (compiler.currentType.classType && (<Type[]>typeArguments)[0] == usizeType)) {
compiler.currentType = (<Type[]>typeArguments)[0];
return arg0;
}
compiler.error(DiagnosticCode.Type_0_cannot_be_changed_to_type_1, reportNode.range, (<Type[]>typeArguments)[0].toString(), (<Type[]>typeArguments)[1].toString());
compiler.error(DiagnosticCode.Type_0_cannot_be_changed_to_type_1, reportNode.range, compiler.currentType.toString(), (<Type[]>typeArguments)[0].toString());
return module.createUnreachable();
case "assert": // assert(isTrue: bool) -> void

View File

@ -1506,7 +1506,17 @@ export class Parser extends DiagnosticEmitter {
return Node.createFloatLiteral(tn.readFloat(), tn.range(startPos, tn.pos));
// RegexpLiteralExpression
case Token.REGEXPLITERAL:
/*
case Token.SLASH:
var regexpLit = Node.createRegexpLiteral(tn.readRegexp(), tn.range(startPos, tn.pos));
if (!tn.skip(Token.SLASH)) {
this.error(DiagnosticCode._0_expected, tn.range(), "/");
return null;
}
// TODO: modifiers, may be move to tokenizer
return regexpLit;
*/
case Token.REGEXPLITERAL: // not yet supported
return Node.createRegexpLiteral(tn.readRegexp(), tn.range(startPos, tn.pos));
default:

View File

@ -3,14 +3,14 @@
(type $fff (func (param f32 f32) (result f32)))
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $ifff (func (param i32 f32 f32) (result f32)))
(type $iv (func (param i32)))
(type $ii (func (param i32) (result i32)))
(type $v (func))
(global $class/Animal.ONE (mut i32) (i32.const 1))
(memory $0 1)
(export "test" (func $class/test))
(export "memory" (memory $0))
(start $start)
(func $class/test (; 0 ;) (type $iv) (param $0 i32)
(func $class/test (; 0 ;) (type $ii) (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
@ -63,6 +63,7 @@
)
)
)
(get_local $0)
)
(func $start (; 1 ;) (type $v)
(local $0 i32)

View File

@ -3,7 +3,7 @@
(type $fff (func (param f32 f32) (result f32)))
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $ifff (func (param i32 f32 f32) (result f32)))
(type $iv (func (param i32)))
(type $ii (func (param i32) (result i32)))
(type $v (func))
(global $class/Animal.ONE (mut i32) (i32.const 1))
(memory $0 1)
@ -50,7 +50,7 @@
)
)
)
(func $class/test (; 4 ;) (type $iv) (param $0 i32)
(func $class/test (; 4 ;) (type $ii) (param $0 i32) (result i32)
(drop
(call $class/Animal#instanceAdd
(get_local $0)
@ -65,6 +65,7 @@
(f32.const 2)
)
)
(get_local $0)
)
(func $start (; 5 ;) (type $v)
(drop

View File

@ -16,7 +16,11 @@ Animal.ONE;
Animal.add(1,2);
Animal.sub<f32>(1, 2);
export function test(animal: Animal<f64>): void {
export function test(animal: Animal<f64>): Animal<f64> {
animal.instanceAdd(1, 2);
animal.instanceSub<f32>(1, 2);
var ptr = changetype<usize>(animal);
var cls = changetype<Animal<f64>>(ptr);
return cls;
}

View File

@ -3,7 +3,7 @@
(type $fff (func (param f32 f32) (result f32)))
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $ifff (func (param i32 f32 f32) (result f32)))
(type $iv (func (param i32)))
(type $ii (func (param i32) (result i32)))
(type $v (func))
(global $class/Animal.ONE (mut i32) (i32.const 1))
(global $HEAP_BASE i32 (i32.const 4))
@ -59,7 +59,9 @@
)
)
)
(func $class/test (; 4 ;) (type $iv) (param $0 i32)
(func $class/test (; 4 ;) (type $ii) (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
(drop
(call $class/Animal#instanceAdd
(get_local $0)
@ -74,6 +76,19 @@
(f32.const 2)
)
)
(block
(set_local $1
(get_local $0)
)
)
(block
(set_local $2
(get_local $1)
)
)
(return
(get_local $2)
)
)
(func $start (; 5 ;) (type $v)
(if