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 	this()
28 	{
29 		booltype = cast(TypePod!bool)(TypeMng.getSingleton().getType("Typebool"));
30 		assert(booltype, "We need bool type!");
31 		
32 		voidtype = cast(TypeVoid)(TypeMng.getSingleton().getType("TypeVoid"));
33 		
34 		mRetType = voidtype;
35 		super("if","bla bla bla",ArgsStyle.CONTROL_STYLE);
36 		
37 		ArgInfo a1;
38 		a1.type = booltype;
39 		args ~= a1;
40 		
41 		a1.type = voidtype;
42 		args ~= a1;
43 		args ~= a1;
44 	}
45 	
46 	override Argument apply(IndAbstract ind, Line line, WorldAbstract world)
47 	{
48 		auto cond = cast(ArgPod!bool)(line[0]);
49 		
50 		Line vthen = cast(Line)(line[1]);
51 		Line velse = cast(Line)(line[2]);
52 		
53 		ArgScope sthen = cast(ArgScope)(line[1]);
54 		ArgScope selse = cast(ArgScope)(line[2]);
55 		
56 		if (cond.val)
57 		{
58 			if (vthen !is null)
59 			{
60 				vthen.compile(ind, world);
61 			} else if (sthen !is null)
62 			{
63 				foreach(Line aline; sthen)
64 				{
65 					auto line = cast(Line)aline;
66 					line.compile(ind, world);
67 				}
68 			} else
69 			{
70 				debug writeln("Warning: invalid ThenArg: ", line.tostring);
71 			}//else throw new Exception("If is confused! ThenArg is no line, no scope. " ~ line.tostring);
72 		} else
73 		{
74 			if (velse !is null)
75 			{
76 				velse.compile(ind, world);
77 			} else if (selse !is null)
78 			{
79 				foreach(Line aline; selse)
80 				{
81 					auto line = cast(Line)aline;
82 					line.compile(ind, world);
83 				}				
84 			} else
85 			{
86 				debug writeln("Warning: invalid ElseArg: ", line.tostring);
87 			} //else throw new Exception("If is confused! ElseArg is no line, no scope" ~ line.tostring);			
88 		}
89 		return voidtype.getNewArg();
90 	}	
91 }