Skip to content

Commit

Permalink
Add is operator (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
gamerbross authored Jun 14, 2024
1 parent b92fcae commit 59b3316
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions TestHScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,38 @@ class TestHScript extends TestCase {
assertScript("pt2?.pt?.x", 10, vars);
}

function testIsOperator():Void {
var vars = {
String: String,
Bool: Bool,
Int: Int,
Float: Float,
Dynamic: Dynamic
}
assertScript("10 is Int", true, vars);
assertScript("10.0 is Int", true, vars);
assertScript("10.1 is Int", false, vars);
assertScript("10 is Float", true, vars);
assertScript("10.0 is Float", true, vars);
assertScript("10.1 is Float", true, vars);
assertScript("10 is String", false, vars);
assertScript('"hscript" is String', true, vars);
assertScript('"" is String', true, vars);
assertScript('true is Bool', true, vars);
assertScript('false is Bool', true, vars);
assertScript('0 is Bool', false, vars);
assertScript('1 is Bool', false, vars);
assertScript('1 is Bool', false, vars);
assertScript("10 is Dynamic", true, vars);
assertScript("10.1 is Dynamic", true, vars);
assertScript('"hscript" is Dynamic', true, vars);
assertScript('null is Int', false, vars);
assertScript('null is Float', false, vars);
assertScript('null is String', false, vars);
assertScript('null is Bool', false, vars);
assertScript('null is Dynamic', false, vars);
}

function testMap():Void {
var objKey = { ok:true };
var vars = {
Expand Down
1 change: 1 addition & 0 deletions hscript/Interp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Interp {
binops.set("&&",function(e1,e2) return me.expr(e1) == true && me.expr(e2) == true);
binops.set("=",assign);
binops.set("...",function(e1,e2) return new #if (haxe_211 || haxe3) IntIterator #else IntIter #end(me.expr(e1),me.expr(e2)));
binops.set("is",function(e1,e2) return #if (haxe_ver >= 4.2) Std.isOfType #else Std.is #end (me.expr(e1), me.expr(e2)));
assignOp("+=",function(v1:Dynamic,v2:Dynamic) return v1 + v2);
assignOp("-=",function(v1:Float,v2:Float) return v1 - v2);
assignOp("*=",function(v1:Float,v2:Float) return v1 * v2);
Expand Down
2 changes: 2 additions & 0 deletions hscript/Parser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,8 @@ class Parser {
return parseExprNext(mk(EUnop(op,false,e1),pmin(e1)));
}
return makeBinop(op,e1,parseExpr());
case TId(op) if ( op == 'is' ):
return makeBinop(op,e1,parseExpr());
case TDot:
var field = getIdent();
return parseExprNext(mk(EField(e1,field),pmin(e1)));
Expand Down

0 comments on commit 59b3316

Please sign in to comment.