Pārlūkot izejas kodu

allow initialization of local vars outside functions, added array initialization support

Nicolas Cannasse 1 gadu atpakaļ
vecāks
revīzija
5ed3d73a16
2 mainītis faili ar 35 papildinājumiem un 2 dzēšanām
  1. 30 2
      hxsl/Checker.hx
  2. 5 0
      hxsl/Macros.hx

+ 30 - 2
hxsl/Checker.hx

@@ -274,6 +274,32 @@ class Checker {
 		for( i in 0...tfuns.length )
 			typeFun(tfuns[i], funs[i].f.expr);
 
+		var localInits = [];
+		for( i in inits.copy() ) {
+			if( i.v.kind == Local ) {
+				localInits.push({ e : TBinop(OpAssign,{ e : TVar(i.v), p : i.e.p, t : i.v.type },i.e), p : i.e.p, t : i.v.type });
+				inits.remove(i);
+			}
+		}
+		if( localInits.length > 0 ) {
+			var fv : TVar = {
+				id : Tools.allocVarId(),
+				name : "__init__consts__",
+				kind : Function,
+				type : TFun([{ args : [], ret : TVoid }]),
+			};
+			if( vars.exists(fv.name) )
+				error("assert", localInits[0].p);
+			vars.set(fv.name, fv);
+			tfuns.push({
+				kind : Init,
+				ref : fv,
+				args : [],
+				ret : TVoid,
+				expr : { e : TBlock(localInits), p : localInits[0].p, t : TVoid },
+			});
+		}
+
 		var vars = Lambda.array(vars);
 		vars.sort(function(v1, v2) return (v1.id < 0 ? -v1.id : v1.id) - (v2.id < 0 ? -v2.id : v2.id));
 		return {
@@ -706,8 +732,8 @@ class Checker {
 				}
 				var einit = null;
 				if( v.expr != null ) {
-					if( v.kind != Param )
-						error("Cannot initialize variable declaration if not @param", v.expr.pos);
+					if( v.kind != Param && v.kind != Local )
+						error("Cannot initialize variable declaration if not @param or local", v.expr.pos);
 					var e = typeExpr(v.expr, v.type == null ? Value : With(v.type));
 					if( v.type == null )
 						v.type = e.t;
@@ -767,6 +793,8 @@ class Checker {
 		case TParenthesis(e): checkConst(e);
 		case TCall({ e : TGlobal(Vec2 | Vec3 | Vec4 | IVec2 | IVec3 | IVec4) }, args):
 			for( a in args ) checkConst(a);
+		case TArrayDecl(el):
+			for( e in el ) checkConst(e);
 		default:
 			error("This expression should be constant", e.p);
 		}

+ 5 - 0
hxsl/Macros.hx

@@ -102,6 +102,11 @@ class Macros {
 				expr : ENew({ pack : ["hxsl"], name : "Types", sub : (g == Vec4 ? "Vec4" : "Vec") }, [for( a in args ) makeInit(a)]),
 				pos : e.p,
 			}
+		case TArrayDecl(el):
+			{
+				expr : EArrayDecl([for( e in el ) makeInit(e)]),
+				pos : e.p,
+			}
 		default:
 			Error.t("Unsupported init expr", e.p);
 		}