Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,072 changes: 536 additions & 536 deletions hscript/Async.hx

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions hscript/Bytes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Bytes {
doEncode(e);
doEncodeString(f);
case EBinop(op,e1,e2):
doEncodeString(op);
doEncodeString(op.toString());
doEncode(e1);
doEncode(e2);
case EUnop(op,prefix,e):
Expand Down Expand Up @@ -278,7 +278,7 @@ class Bytes {
var e = doDecode();
EField(e,doDecodeString());
case 6:
var op = doDecodeString();
var op = Binop.fromString(doDecodeString());
var e1 = doDecode();
EBinop(op,e1,doDecode());
case 7:
Expand Down
42 changes: 28 additions & 14 deletions hscript/Checker.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1113,11 +1113,11 @@ class Checker {
return TVoid;
case EBinop(op, e1, e2):
switch( op ) {
case "&", "|", "^", ">>", ">>>", "<<":
case OpAnd, OpOr, OpXor, OpShr, OpUshr, OpShl:
typeExprWith(e1,TInt);
typeExprWith(e2,TInt);
return TInt;
case "=":
case OpAssign:
if( allowDefine ) {
switch( edef(e1) ) {
case EIdent(i) if( !locals.exists(i) && !globals.exists(i) ):
Expand All @@ -1133,7 +1133,7 @@ class Checker {
}
typeExprWith(e2,vt);
return vt;
case "+":
case OpAdd:
var t1 = typeExpr(e1,WithType(TInt));
var t2 = typeExpr(e2,WithType(t1));
tryUnify(t1,t2);
Expand All @@ -1150,36 +1150,36 @@ class Checker {
unify(t1, TFloat, e1);
unify(t2, TFloat, e2);
}
case "-", "*", "/", "%":
case OpSub, OpMult, OpDiv, OpMod:
var t1 = typeExpr(e1,WithType(TInt));
var t2 = typeExpr(e2,WithType(t1));
if( !tryUnify(t1,t2) )
unify(t2,t1,e2);
switch( [follow(t1), follow(t2)]) {
case [TInt, TInt]:
if( op == "/" ) return TFloat;
if( op == OpDiv ) return TFloat;
return TInt;
case [TFloat|TDynamic, TInt|TDynamic], [TInt|TDynamic, TFloat|TDynamic], [TFloat, TFloat]:
return TFloat;
default:
unify(t1, TFloat, e1);
unify(t2, TFloat, e2);
}
case "&&", "||":
case OpBoolAnd, OpBoolOr:
typeExprWith(e1,TBool);
typeExprWith(e2,TBool);
return TBool;
case "...":
case OpInterval:
typeExprWith(e1,TInt);
typeExprWith(e2,TInt);
return makeIterator(TInt);
case "==", "!=":
case OpEq, OpNeq:
var t1 = typeExpr(e1,Value);
var t2 = typeExpr(e2,WithType(t1));
if( !tryUnify(t1,t2) )
unify(t2,t1,e2);
return TBool;
case ">", "<", ">=", "<=":
case OpGt, OpLt, OpGte, OpLte:
var t1 = typeExpr(e1,Value);
var t2 = typeExpr(e2,WithType(t1));
if( !tryUnify(t1,t2) )
Expand All @@ -1190,12 +1190,26 @@ class Checker {
error("Cannot compare "+typeStr(t1), expr);
}
return TBool;
case OpAddAssign, OpSubAssign, OpMultAssign, OpDivAssign, OpModAssign, OpAndAssign, OpOrAssign, OpXorAssign, OpShlAssign, OpShrAssign, OpUshrAssign, OpNcoalAssign:
var baseOp = switch(op) {
case OpAddAssign: OpAdd;
case OpSubAssign: OpSub;
case OpMultAssign: OpMult;
case OpDivAssign: OpDiv;
case OpModAssign: OpMod;
case OpAndAssign: OpAnd;
case OpOrAssign: OpOr;
case OpXorAssign: OpXor;
case OpShlAssign: OpShl;
case OpShrAssign: OpShr;
case OpUshrAssign: OpUshr;
case OpNcoalAssign: OpNcoal;
default: op;
};
var t = typeExpr(mk(EBinop(baseOp,e1,e2),expr),withType);
return typeExpr(mk(EBinop(OpAssign,e1,e2),expr), withType);
default:
if( op.charCodeAt(op.length-1) == "=".code ) {
var t = typeExpr(mk(EBinop(op.substr(0,op.length-1),e1,e2),expr),withType);
return typeExpr(mk(EBinop("=",e1,e2),expr), withType);
}
error("Unsupported operation "+op, expr);
error("Unsupported operation "+op.toString(), expr);
}
case ETry(etry, v, et, ecatch):
var vt = typeExpr(etry, withType);
Expand Down
128 changes: 127 additions & 1 deletion hscript/Expr.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,132 @@ typedef Int64 = #if cpp cpp.Int64 #elseif java java.Int64 #elseif cs cs.Int64 #e

typedef UInt8 = #if cpp cpp.UInt8 #elseif cs cs.UInt8 #else Int #end;
typedef UInt16 = #if cpp cpp.UInt16 #elseif cs cs.UInt16 #else Int #end;

enum abstract Binop(Int) from Int to Int {
var OpAdd = 0;
var OpSub = 1;
var OpMult = 2;
var OpDiv = 3;
var OpMod = 4;
var OpAnd = 5;
var OpOr = 6;
var OpXor = 7;
var OpShl = 8;
var OpShr = 9;
var OpUshr = 10;
var OpEq = 11;
var OpNeq = 12;
var OpGte = 13;
var OpLte = 14;
var OpGt = 15;
var OpLt = 16;
var OpBoolOr = 17;
var OpBoolAnd = 18;
var OpIs = 19;
var OpAssign = 20;
var OpNcoal = 21;
var OpInterval = 22;
var OpArrow = 23;
var OpAddAssign = 24;
var OpSubAssign = 25;
var OpMultAssign = 26;
var OpDivAssign = 27;
var OpModAssign = 28;
var OpAndAssign = 29;
var OpOrAssign = 30;
var OpXorAssign = 31;
var OpShlAssign = 32;
var OpShrAssign = 33;
var OpUshrAssign = 34;
var OpNcoalAssign = 35;
var OpArrowFn = 36;

public static inline function fromString(s:String):Binop {
return switch(s) {
case "+": OpAdd;
case "-": OpSub;
case "*": OpMult;
case "/": OpDiv;
case "%": OpMod;
case "&": OpAnd;
case "|": OpOr;
case "^": OpXor;
case "<<": OpShl;
case ">>": OpShr;
case ">>>": OpUshr;
case "==": OpEq;
case "!=": OpNeq;
case ">=": OpGte;
case "<=": OpLte;
case ">": OpGt;
case "<": OpLt;
case "||": OpBoolOr;
case "&&": OpBoolAnd;
case "is": OpIs;
case "=": OpAssign;
case "??": OpNcoal;
case "...": OpInterval;
case "->": OpArrow;
case "=>": OpArrowFn;
case "+=": OpAddAssign;
case "-=": OpSubAssign;
case "*=": OpMultAssign;
case "/=": OpDivAssign;
case "%=": OpModAssign;
case "&=": OpAndAssign;
case "|=": OpOrAssign;
case "^=": OpXorAssign;
case "<<=": OpShlAssign;
case ">>=": OpShrAssign;
case ">>>=": OpUshrAssign;
case "??=": OpNcoalAssign;
default: -1;
}
}

public inline function toString():String {
return switch(this) {
case OpAdd: "+";
case OpSub: "-";
case OpMult: "*";
case OpDiv: "/";
case OpMod: "%";
case OpAnd: "&";
case OpOr: "|";
case OpXor: "^";
case OpShl: "<<";
case OpShr: ">>";
case OpUshr: ">>>";
case OpEq: "==";
case OpNeq: "!=";
case OpGte: ">=";
case OpLte: "<=";
case OpGt: ">";
case OpLt: "<";
case OpBoolOr: "||";
case OpBoolAnd: "&&";
case OpIs: "is";
case OpAssign: "=";
case OpNcoal: "??";
case OpInterval: "...";
case OpArrow: "->";
case OpArrowFn: "=>";
case OpAddAssign: "+=";
case OpSubAssign: "-=";
case OpMultAssign: "*=";
case OpDivAssign: "/=";
case OpModAssign: "%=";
case OpAndAssign: "&=";
case OpOrAssign: "|=";
case OpXorAssign: "^=";
case OpShlAssign: "<<=";
case OpShrAssign: ">>=";
case OpUshrAssign: ">>>=";
case OpNcoalAssign: "??=";
default: "?";
}
}
}
typedef UInt32 = #if cpp cpp.UInt32 #else Int #end;
typedef UInt64 = #if cpp cpp.UInt64 #else Int #end;

Expand Down Expand Up @@ -57,7 +183,7 @@ enum Expr {
EParent( e : Expr );
EBlock( e : Array<Expr> );
EField( e : Expr, f : String , ?safe : Bool );
EBinop( op : String, e1 : Expr, e2 : Expr );
EBinop( op : Binop, e1 : Expr, e2 : Expr );
EUnop( op : String, prefix : Bool, e : Expr );
ECall( e : Expr, params : Array<Expr> );
EIf( cond : Expr, e1 : Expr, ?e2 : Expr );
Expand Down
Loading