comparison src/dil/semantic/Analysis.d @ 806:bcb74c9b895c

Moved out files in the trunk folder to the root.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sun, 09 Mar 2008 00:12:19 +0100
parents trunk/src/dil/semantic/Analysis.d@78be32e3e157
children fde064aca673
comparison
equal deleted inserted replaced
805:a3fab8b74a7d 806:bcb74c9b895c
1 /++
2 Author: Aziz Köksal
3 License: GPL3
4 +/
5 module dil.semantic.Analysis;
6
7 import dil.ast.Node;
8 import dil.ast.Expressions;
9 import dil.semantic.Scope;
10 import dil.lexer.IdTable;
11 import dil.Compilation;
12 import common;
13
14 /// Common semantics for pragma declarations and statements.
15 void pragmaSemantic(Scope scop, Token* pragmaLoc,
16 Identifier* ident,
17 Expression[] args)
18 {
19 if (ident is Ident.msg)
20 pragma_msg(scop, pragmaLoc, args);
21 else if (ident is Ident.lib)
22 pragma_lib(scop, pragmaLoc, args);
23 // else
24 // scop.error(begin, "unrecognized pragma");
25 }
26
27 /// Evaluates a msg pragma.
28 void pragma_msg(Scope scop, Token* pragmaLoc, Expression[] args)
29 {
30 if (args.length == 0)
31 return /*scop.error(pragmaLoc, "expected expression arguments to pragma")*/;
32
33 foreach (arg; args)
34 {
35 auto e = arg/+.evaluate()+/;
36 if (e is null)
37 {
38 // scop.error(e.begin, "expression is not evaluatable at compile time");
39 }
40 else if (auto stringExpr = e.Is!(StringExpression))
41 // Print string to standard output.
42 Stdout(stringExpr.getString());
43 else
44 {
45 // scop.error(e.begin, "expression must evaluate to a string");
46 }
47 }
48 // Print a newline at the end.
49 Stdout('\n');
50 }
51
52 /// Evaluates a lib pragma.
53 void pragma_lib(Scope scop, Token* pragmaLoc, Expression[] args)
54 {
55 if (args.length != 1)
56 return /*scop.error(pragmaLoc, "expected one expression argument to pragma")*/;
57
58 auto e = args[0]/+.evaluate()+/;
59 if (e is null)
60 {
61 // scop.error(e.begin, "expression is not evaluatable at compile time");
62 }
63 else if (auto stringExpr = e.Is!(StringExpression))
64 {
65 // TODO: collect library paths in Module?
66 // scop.modul.addLibrary(stringExpr.getString());
67 }
68 else
69 {
70 // scop.error(e.begin, "expression must evaluate to a string");
71 }
72 }
73
74 /// Returns true if the first branch (of a debug declaration/statement) or
75 /// false if the else-branch should be compiled in.
76 bool debugBranchChoice(Token* cond, CompilationContext context)
77 {
78 if (cond)
79 {
80 if (cond.kind == TOK.Identifier)
81 {
82 if (context.findDebugId(cond.ident.str))
83 return true;
84 }
85 else if (cond.uint_ <= context.debugLevel)
86 return true;
87 }
88 else if (1 <= context.debugLevel)
89 return true;
90 return false;
91 }
92
93 /// Returns true if the first branch (of a version declaration/statement) or
94 /// false if the else-branch should be compiled in.
95 bool versionBranchChoice(Token* cond, CompilationContext context)
96 {
97 assert(cond);
98 if (cond.kind == TOK.Identifier)
99 {
100 if (context.findVersionId(cond.ident.str))
101 return true;
102 }
103 else if (cond.uint_ >= context.versionLevel)
104 return true;
105 return false;
106 }