diff dmd/FileExp.d @ 163:fe932c1a9563

*.interpret functions implemenation
author korDen
date Thu, 23 Sep 2010 13:55:20 +0400
parents 438eaa11eed4
children e3afd1303184
line wrap: on
line diff
--- a/dmd/FileExp.d	Tue Sep 21 14:59:56 2010 +0400
+++ b/dmd/FileExp.d	Thu Sep 23 13:55:20 2010 +0400
@@ -2,13 +2,20 @@
 
 import dmd.common;
 import dmd.Expression;
+import dmd.File;
 import dmd.UnaExp;
+import dmd.StringExp;
+import dmd.WANT;
+import dmd.Global;
+import dmd.FileName;
 import dmd.OutBuffer;
 import dmd.Loc;
 import dmd.Scope;
 import dmd.HdrGenState;
 import dmd.TOK;
 
+import core.stdc.stdio;
+
 class FileExp : UnaExp
 {
 	this(Loc loc, Expression e)
@@ -18,7 +25,65 @@
 
 	override Expression semantic(Scope sc)
 	{
-		assert(false);
+		StringExp se;
+
+	version (LOGSEMANTIC) {
+		printf("FileExp.semantic('%.*s')\n", toChars());
+	}
+		UnaExp.semantic(sc);
+		e1 = resolveProperties(sc, e1);
+		e1 = e1.optimize(WANTvalue);
+		if (e1.op != TOKstring)
+		{	
+			error("file name argument must be a string, not (%s)", e1.toChars());
+			goto Lerror;
+		}
+		se = cast(StringExp)e1;
+		se = se.toUTF8(sc);
+
+		string name = (cast(immutable(char)*)se.string_)[0..se.len];
+
+		if (!global.params.fileImppath)
+		{	
+			error("need -Jpath switch to import text file %s", name);
+			goto Lerror;
+		}
+
+		if (name != FileName.name(name))
+		{	
+			error("use -Jpath switch to provide path for filename %s", name);
+			goto Lerror;
+		}
+
+		name = FileName.searchPath(global.filePath, name, 0);
+		if (!name)
+		{	
+			error("file %s cannot be found, check -Jpath", se.toChars());
+			goto Lerror;
+		}
+
+		if (global.params.verbose)
+			printf("file      %s\t(%s)\n", cast(char*)se.string_, name);
+
+		{	
+			scope File f = new File(name);
+			if (f.read())
+			{   
+				error("cannot read file %s", f.toChars());
+				goto Lerror;
+			}
+			else
+			{
+				f.ref_ = 1;
+				se = new StringExp(loc, (cast(immutable(char)*)f.buffer)[0..f.len]);
+			}
+		}
+	  Lret:
+		return se.semantic(sc);
+
+	  Lerror:
+		se = new StringExp(loc, "");
+		goto Lret;
 	}
 
 	override void toCBuffer(OutBuffer buf, HdrGenState* hgs)