1 /**
2 *   Copyright: © 2012-2014 Anton Gushcha
3 *   License: Subject to the terms of the MIT license, as written in the included LICENSE file.
4 *   Authors:  NCrashed <ncrashed@gmail.com>,
5 *             LeMarwin <lemarwin42@gmail.com>,
6 *             Nazgull09 <nazgull90@gmail.com>
7 */
8 module devol.std.opif;
9 
10 import std.stdio;
11 
12 import devol.typemng;
13 
14 public
15 {
16 	import devol.individ;
17 	import devol.world;
18 	import devol.operator;	
19 	import devol.std.typepod;
20 }
21 
22 class If : Operator
23 {
24 	TypePod!bool booltype;
25 	TypeVoid voidtype;
26 	
27 	enum description = "Conditional operator that takes three arguments. "
28 	"First one is boolean type that refers to condition. If the argument "
29 	"evaluates to true, second argument is returned, else third one is returned."
30 	"Second and third arguments refer to actions of void type.";
31 	
32 	this()
33 	{
34 		booltype = cast(TypePod!bool)(TypeMng.getSingleton().getType("Typebool"));
35 		assert(booltype, "We need bool type!");
36 		
37 		voidtype = cast(TypeVoid)(TypeMng.getSingleton().getType("TypeVoid"));
38 		
39 		mRetType = voidtype;
40 		super("if", description, ArgsStyle.CONTROL_STYLE);
41 		
42 		ArgInfo a1;
43 		a1.type = booltype;
44 		a1.eval = true;
45 		args ~= a1;
46 		
47 		a1.type = voidtype;
48 		a1.eval = false;
49 		args ~= a1;
50 		args ~= a1;
51 	}
52 	
53 	override Argument apply(IndAbstract ind, Line line, WorldAbstract world)
54 	{
55 		auto cond = cast(ArgPod!bool)(line[0]);
56 		
57 		Line vthen = cast(Line)(line[1]);
58 		Line velse = cast(Line)(line[2]);
59 		
60 		ArgScope sthen = cast(ArgScope)(line[1]);
61 		ArgScope selse = cast(ArgScope)(line[2]);
62 		
63 		if (cond.val)
64 		{
65 			if (vthen !is null)
66 			{
67 				vthen.compile(ind, world);
68 			} else if (sthen !is null)
69 			{
70 				foreach(Line aline; sthen)
71 				{
72 					auto line = cast(Line)aline;
73 					line.compile(ind, world);
74 				}
75 			} else
76 			{
77 				debug writeln("Warning: invalid ThenArg: ", line.tostring);
78 			}//else throw new Exception("If is confused! ThenArg is no line, no scope. " ~ line.tostring);
79 		} else
80 		{
81 			if (velse !is null)
82 			{
83 				velse.compile(ind, world);
84 			} else if (selse !is null)
85 			{
86 				foreach(Line aline; selse)
87 				{
88 					auto line = cast(Line)aline;
89 					line.compile(ind, world);
90 				}				
91 			} else
92 			{
93 				debug writeln("Warning: invalid ElseArg: ", line.tostring);
94 			} //else throw new Exception("If is confused! ElseArg is no line, no scope" ~ line.tostring);			
95 		}
96 		return voidtype.getNewArg();
97 	}	
98 }