2
0
Nicolas Cannasse 11 жил өмнө
parent
commit
aebdb8a9e0
4 өөрчлөгдсөн 40 нэмэгдсэн , 7 устгасан
  1. 5 1
      hxsl/Ast.hx
  2. 25 6
      hxsl/Checker.hx
  3. 2 0
      hxsl/MacroParser.hx
  4. 8 0
      hxsl/Printer.hx

+ 5 - 1
hxsl/Ast.hx

@@ -115,6 +115,7 @@ enum ExprDef {
 	EBreak;
 	EContinue;
 	EArray( e : Expr, eindex : Expr );
+	EArrayDecl( el : Array<Expr> );
 }
 
 typedef TVar = {
@@ -218,6 +219,7 @@ enum TExprDef {
 	TContinue;
 	TBreak;
 	TArray( e : TExpr, index : TExpr );
+	TArrayDecl( el : Array<TExpr> );
 }
 
 typedef TExpr = { e : TExprDef, t : Type, p : Position }
@@ -333,6 +335,7 @@ class Tools {
 		case TReturn(e): if( e != null ) f(e);
 		case TFor(_, it, loop): f(it); f(loop);
 		case TArray(e, index): f(e); f(index);
+		case TArrayDecl(el): for( e in el ) f(e);
 		case TConst(_),TVar(_),TGlobal(_), TDiscard, TContinue, TBreak:
 		}
 	}
@@ -349,7 +352,8 @@ class Tools {
 		case TIf(econd, eif, eelse): TIf(f(econd),f(eif),if( eelse != null ) f(eelse) else null);
 		case TReturn(e): TReturn(if( e != null ) f(e) else null);
 		case TFor(v, it, loop): TFor(v, f(it), f(loop));
-		case TArray(e, index): TArray(f(e),f(index));
+		case TArray(e, index): TArray(f(e), f(index));
+		case TArrayDecl(el): TArrayDecl([for( e in el ) f(e)]);
 		case TConst(_), TVar(_), TGlobal(_), TDiscard, TContinue, TBreak: e.e;
 		}
 		return { e : ed, t : e.t, p : e.p };

+ 25 - 6
hxsl/Checker.hx

@@ -376,9 +376,9 @@ class Checker {
 				error("This function should return " + curFun.ret.toString(), e.pos);
 			var e = e == null ? null : typeWith(e, curFun.ret);
 			TReturn(e);
-		case EFor(v, loop, block):
-			var loop = typeExpr(loop, Value);
-			switch( loop.t ) {
+		case EFor(v, it, block):
+			var it = typeExpr(it, Value);
+			switch( it.t ) {
 			case TArray(t, _):
 				var v : TVar = {
 					id : 0,
@@ -390,9 +390,9 @@ class Checker {
 				vars.set(v.name, v);
 				var block = typeExpr(block, NoValue);
 				if( old == null ) vars.remove(v.name) else vars.set(v.name, old);
-				TFor(v, loop, block);
+				TFor(v, it, block);
 			default:
-				error("Cannot iterate on " + loop.t.toString(), loop.p);
+				error("Cannot iterate on " + it.t.toString(), it.p);
 			}
 		case EContinue:
 			if( !inLoop ) error("Continue outside loop", e.pos);
@@ -417,6 +417,14 @@ class Checker {
 			default:
 				error("Cannot index " + e1.t.toString() + " : should be an array", e.pos);
 			}
+		case EArrayDecl(el):
+			if( el.length == 0 ) error("Empty array not supported", e.pos);
+			var el = [for( e in el ) typeExpr(e, Value)];
+			var t = el[0].t;
+			for( i in 1...el.length )
+				unifyExpr(el[i], t);
+			type = TArray(t, SConst(el.length));
+			TArrayDecl(el);
 		}
 		return { e : ed, t : type, p : e.pos };
 	}
@@ -513,6 +521,13 @@ class Checker {
 			}
 			return parent.type;
 		case TArray(t, size):
+			switch( t ) {
+			case TArray(_):
+				error("Multidimentional arrays are not allowed", pos);
+			case TStruct(_):
+				error("Array of structures are not allowed", pos);
+			default:
+			}
 			var s = switch( size ) {
 			case SConst(_): size;
 			case SVar(v):
@@ -810,8 +825,12 @@ class Checker {
 			unifyExpr(e1, TBool);
 			unifyExpr(e2, TBool);
 			TBool;
+		case OpInterval:
+			unifyExpr(e1, TInt);
+			unifyExpr(e2, TInt);
+			TArray(TInt, SConst(0));
 		default:
-			error("TODO", pos);
+			error("Unsupported operator " + op, pos);
 		}
 	}
 }

+ 2 - 0
hxsl/MacroParser.hx

@@ -192,6 +192,8 @@ class MacroParser {
 			EContinue;
 		case EArray(e1, e2):
 			EArray(parseExpr(e1), parseExpr(e2));
+		case EArrayDecl(el):
+			EArrayDecl([for( e in el ) parseExpr(e)]);
 		default:
 			null;
 		};

+ 8 - 0
hxsl/Printer.hx

@@ -191,6 +191,14 @@ class Printer {
 			case CFloat(f): f;
 			case CString(s): '"' + s + '"';
 			});
+		case TArrayDecl(el):
+			add("[");
+			var first = true;
+			for( e in el ) {
+				if( first ) first = false else add(", ");
+				addExpr(e,tabs);
+			}
+			add("]");
 		}
 	}