瀏覽代碼

added cast operation / opcode.

Nicolas Cannasse 19 年之前
父節點
當前提交
a2364fdea5
共有 5 個文件被更改,包括 13 次插入3 次删除
  1. 4 1
      ast.ml
  2. 2 1
      lexer.mll
  3. 1 0
      parser.ml
  4. 1 1
      type.ml
  5. 5 0
      typer.ml

+ 4 - 1
ast.ml

@@ -1,4 +1,4 @@
-(*
+7(*
  *  Haxe Compiler
  *  Copyright (c)2005 Nicolas Cannasse
  *
@@ -54,6 +54,7 @@ type keyword =
 	| In
 	| Interface
 	| Untyped
+	| Cast
 	
 type binop =
 	| OpAdd
@@ -167,6 +168,7 @@ and expr_def =
 	| EContinue
 	| EUntyped of expr
 	| EThrow of expr
+	| ECast of expr * type_path
 
 and expr = expr_def * pos
 
@@ -276,6 +278,7 @@ let s_keyword = function
 	| In -> "in"
 	| Interface -> "interface"
 	| Untyped -> "untyped"
+	| Cast -> "cast"
 
 let rec s_binop = function
 	| OpAdd -> "+"

+ 2 - 1
lexer.mll

@@ -50,7 +50,8 @@ let keywords =
 		[Function;Class;Static;Var;If;Else;While;Do;For;
 		Break;Return;Continue;Extends;Implements;Import;
 		Switch;Case;Default;Public;Private;Try;Untyped;
-		Catch;New;This;Throw;Extern;Enum;In;Interface];
+		Catch;New;This;Throw;Extern;Enum;In;Interface;
+		Cast];
 	h
 
 let init file =

+ 1 - 0
parser.ml

@@ -293,6 +293,7 @@ and expr = parser
 	| [< '(BrOpen,p1); e = block1; '(BrClose,p2) >] -> (e,punion p1 p2)
 	| [< '(Const c,p); s >] -> expr_next (EConst c,p) s
 	| [< '(Kwd This,p); s >] -> expr_next (EConst (Ident "this"),p) s
+	| [< '(Kwd Cast,p1); '(POpen,_); e = expr; '(Comma,_); t = parse_type_path; '(PClose,p2); s >] -> expr_next (ECast (e,t),punion p1 p2) s
 	| [< '(Kwd Throw,p); e = expr >] -> (EThrow e,p)
 	| [< '(Kwd New,p1); t = parse_type_path_normal; '(POpen,_); al = psep Comma expr; '(PClose,p2); s >] -> expr_next (ENew (t,al),punion p1 p2) s
 	| [< '(POpen,p1); e = expr; '(PClose,p2); s >] -> expr_next (EParenthesis e, punion p1 p2) s

+ 1 - 1
type.ml

@@ -409,7 +409,7 @@ let rec unify a b =
 			| None -> ()
 			| Some (c,tl) -> loop c tl
 		in
-		if c.cl_locked then error [cannot_unify a b]
+		if c.cl_locked then error [cannot_unify a b];
 		(try
 			loop c tl;
 		with

+ 5 - 0
typer.ml

@@ -1187,6 +1187,11 @@ and type_expr ctx ?(need_val=true) (e,p) =
 			etype = mk_mono();
 			epos = e.epos;
 		}
+	| ECast (e,t) ->
+		type_expr ctx (ETry ((EThrow e,p),[
+			("e",t,(EConst (Ident "e"),p));
+			("e",TPNormal { tpackage = []; tname = "Dynamic"; tparams = [] },(EThrow (EConst (String "Class cast error"),p),p))
+		]),p)
 
 and type_function ctx t static constr f p =
 	let locals = save_locals ctx in