changeset 559:c4bb948e5cc1

Added semantic code for pragmas.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Mon, 24 Dec 2007 20:39:14 +0100
parents 32f55cac5c46
children 709e223a8eb9
files trunk/src/dil/Declarations.d trunk/src/dil/Expressions.d trunk/src/dil/Semantics.d trunk/src/dil/Statements.d
diffstat 4 files changed, 99 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/Declarations.d	Mon Dec 24 20:23:45 2007 +0100
+++ b/trunk/src/dil/Declarations.d	Mon Dec 24 20:39:14 2007 +0100
@@ -10,7 +10,8 @@
 import dil.Token;
 import dil.Enums;
 import dil.Scope;
-import dil.Identifier;
+import dil.IdTable;
+import dil.Semantics;
 
 abstract class Declaration : Node
 {
@@ -654,6 +655,12 @@
     this.ident = ident;
     this.args = args;
   }
+
+  void semantic(Scope scop)
+  {
+    pragmaSemantic(scop, begin, ident, args);
+    decls.semantic(scop);
+  }
 }
 
 class MixinDeclaration : Declaration
--- a/trunk/src/dil/Expressions.d	Mon Dec 24 20:23:45 2007 +0100
+++ b/trunk/src/dil/Expressions.d	Mon Dec 24 20:39:14 2007 +0100
@@ -31,6 +31,11 @@
     return this;
   }
 
+  Expression evaluate()
+  {
+    return null;
+  }
+
   import dil.Messages;
   void error(Scope scop, MID mid)
   {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trunk/src/dil/Semantics.d	Mon Dec 24 20:39:14 2007 +0100
@@ -0,0 +1,72 @@
+/++
+  Author: Aziz Köksal
+  License: GPL3
++/
+module dil.Semantics;
+
+import dil.Expressions;
+import dil.Token;
+import dil.Scope;
+import dil.SyntaxTree;
+import dil.IdTable;
+import common;
+
+/// Common semantics for pragma declarations and statements.
+void pragmaSemantic(Scope scop, Token* pragmaLoc,
+                    Identifier* ident,
+                    Expression[] args)
+{
+  if (ident is Ident.msg)
+    pragma_msg(scop, pragmaLoc, args);
+  else if (ident is Ident.lib)
+    pragma_lib(scop, pragmaLoc, args);
+  // else
+  //   scop.error(begin, "unrecognized pragma");
+}
+
+/// Evaluates a msg pragma.
+void pragma_msg(Scope scop, Token* pragmaLoc, Expression[] args)
+{
+  if (args.length == 0)
+    return /*scop.error(pragmaLoc, "expected expression arguments to pragma")*/;
+
+  foreach (arg; args)
+  {
+    auto e = arg.evaluate();
+    if (e is null)
+    {
+      // scop.error(e.begin, "expression is not evaluatable at compile time");
+    }
+    else if (auto stringExpr = TryCast!(StringExpression)(e))
+      // Print string to standard output.
+      Stdout(stringExpr.getString());
+    else
+    {
+      // scop.error(e.begin, "expression must evaluate to a string");
+    }
+  }
+  // Print a newline at the end.
+  Stdout('\n');
+}
+
+/// Evaluates a lib pragma.
+void pragma_lib(Scope scop, Token* pragmaLoc, Expression[] args)
+{
+  if (args.length != 1)
+    return /*scop.error(pragmaLoc, "expected one expression argument to pragma")*/;
+
+  auto e = args[0].evaluate();
+  if (e is null)
+  {
+    // scop.error(e.begin, "expression is not evaluatable at compile time");
+  }
+  else if (auto stringExpr = TryCast!(StringExpression)(e))
+  {
+    // TODO: collect library paths in Module?
+    // scop.modul.addLibrary(stringExpr.getString());
+  }
+  else
+  {
+    // scop.error(e.begin, "expression must evaluate to a string");
+  }
+}
--- a/trunk/src/dil/Statements.d	Mon Dec 24 20:23:45 2007 +0100
+++ b/trunk/src/dil/Statements.d	Mon Dec 24 20:39:14 2007 +0100
@@ -8,7 +8,9 @@
 import dil.Declarations;
 import dil.Types;
 import dil.Token;
-import dil.Identifier;
+import dil.IdTable;
+import dil.Scope;
+import dil.Semantics;
 
 abstract class Statement : Node
 {
@@ -16,6 +18,11 @@
   {
     super(NodeCategory.Statement);
   }
+
+  void semantic(Scope scop)
+  {
+
+  }
 }
 
 class Statements : Statement
@@ -480,6 +487,12 @@
     this.args = args;
     this.pragmaBody = pragmaBody;
   }
+
+  void semantic(Scope scop)
+  {
+    pragmaSemantic(scop, begin, ident, args);
+    pragmaBody.semantic(scop);
+  }
 }
 
 class MixinStatement : Statement