From c381a758a844648048614860cda04a2acc19fe60 Mon Sep 17 00:00:00 2001 From: Nicolas Cannasse Date: Thu, 8 Jan 2009 10:41:18 +0000 Subject: [PATCH] added int32 support --- Test.hx | 2 ++ hscript.hxml | 2 +- hscript.hxp | 2 +- hscript/Bytes.hx | 12 ++++++++++++ hscript/Expr.hx | 1 + hscript/Interp.hx | 1 + hscript/Parser.hx | 14 ++++++++++---- 7 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Test.hx b/Test.hx index 36a12c0..fae6704 100644 --- a/Test.hx +++ b/Test.hx @@ -16,6 +16,8 @@ class Test { static function main() { test("0",0); test("0xFF",255); + test("n(0xBFFFFFFF)",0xBFFFFFFF,{ n : haxe.Int32.toNativeInt }); + test("n(0x7FFFFFFF)",0x7FFFFFFF,{ n : haxe.Int32.toNativeInt }); test("-123",-123); test("- 123",-123); test("1.546",1.546); diff --git a/hscript.hxml b/hscript.hxml index 33bc917..49c996a 100644 --- a/hscript.hxml +++ b/hscript.hxml @@ -1,5 +1,5 @@ # HScript --swf9 hscript.swf +-neko hscript.n -main Test -debug diff --git a/hscript.hxp b/hscript.hxp index d20eb74..aa8d369 100644 --- a/hscript.hxp +++ b/hscript.hxp @@ -1,5 +1,5 @@  - + diff --git a/hscript/Bytes.hx b/hscript/Bytes.hx index 756cab3..b68bc21 100644 --- a/hscript/Bytes.hx +++ b/hscript/Bytes.hx @@ -87,6 +87,13 @@ class Bytes { bout.addByte((v >> 16) & 0xFF); bout.addByte(v >>> 24); } + case CInt32(v): + bout.addByte(4); + var mid = haxe.Int32.toInt(haxe.Int32.and(v,haxe.Int32.ofInt(0xFFFFFF))); + bout.addByte(mid & 0xFF); + bout.addByte((mid >> 8) & 0xFF); + bout.addByte(mid >> 16); + bout.addByte(haxe.Int32.toInt(haxe.Int32.ushr(v,24))); case CFloat(f): bout.addByte(2); doEncodeString(Std.string(f)); @@ -108,6 +115,11 @@ class Bytes { CFloat( Std.parseFloat(doDecodeString()) ); case 3: CString( doDecodeString() ); + case 4: + var i = bin.get(pin) | (bin.get(pin+1) << 8) | (bin.get(pin+2) << 16); + var j = bin.get(pin+3); + pin += 4; + CInt32(haxe.Int32.or(haxe.Int32.ofInt(i),haxe.Int32.shl(haxe.Int32.ofInt(j),24))); default: throw "Invalid code "+bin.get(pin-1); } diff --git a/hscript/Expr.hx b/hscript/Expr.hx index 164d2e6..7684070 100644 --- a/hscript/Expr.hx +++ b/hscript/Expr.hx @@ -28,6 +28,7 @@ enum Const { CInt( v : Int ); CFloat( f : Float ); CString( s : String ); + CInt32( v : haxe.Int32 ); } enum Expr { diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 611fe7e..4d589ba 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -206,6 +206,7 @@ class Interp { case EConst(c): switch( c ) { case CInt(v): return v; + case CInt32(v): return v; case CFloat(f): return f; case CString(s): return s; } diff --git a/hscript/Parser.hx b/hscript/Parser.hx index 314bc76..26e763b 100644 --- a/hscript/Parser.hx +++ b/hscript/Parser.hx @@ -466,18 +466,23 @@ class Parser { if( n > 0 || exp > 0 ) throw Error.EInvalidChar(char); // read hexa + var n = haxe.Int32.ofInt(0); while( true ) { char = readChar(s); switch( char ) { case 48,49,50,51,52,53,54,55,56,57: // 0-9 - n = (n << 4) + (char - 48); + n = haxe.Int32.add(haxe.Int32.shl(n,4), cast (char - 48)); case 65,66,67,68,69,70: // A-F - n = (n << 4) + (char - 55); + n = haxe.Int32.add(haxe.Int32.shl(n,4), cast (char - 55)); case 97,98,99,100,101,102: // a-f - n = (n << 4) + (char - 87); + n = haxe.Int32.add(haxe.Int32.shl(n,4), cast (char - 87)); default: this.char = char; - return TConst( CInt(n) ); + // we allow to parse hexadecimal Int32 in Neko, but when the value will be + // evaluated by Interpreter, a failure will occur if no Int32 operation is + // performed + var v = try CInt(haxe.Int32.toInt(n)) catch( e : Dynamic ) CInt32(n); + return TConst(v); } } default: @@ -591,6 +596,7 @@ class Parser { function constString( c ) { return switch(c) { case CInt(v): Std.string(v); + case CInt32(v): Std.string(v); case CFloat(f): Std.string(f); case CString(s): s; // TODO : escape + quote }