1 module orelang.operator.ConvOperators;
2 import orelang.operator.IOperator,
3        orelang.Engine,
4        orelang.Value;
5 import std.algorithm,
6        std..string,
7        std.array,
8        std.conv;
9 
10 /**
11  * convert number into string
12  */
13 class NumberToStringOperator : IOperator {
14   /**
15    * call
16    */
17   public Value call(Engine engine, Value[] args) {
18     return new Value(engine.eval(args[0]).getNumeric.to!string);
19   }
20 }
21 
22 /**
23  * convert string into number
24  */
25 class StringToNumberOperator : IOperator {
26   /**
27    * call
28    */
29   public Value call(Engine engine, Value[] args) {
30     return new Value(engine.eval(args[0]).getString.to!ulong);
31   }
32 }
33 
34 
35 /**
36  * convert number into char
37  */
38 class NumberToCharOperator : IOperator {
39   /**
40    * call
41    */
42   public Value call(Engine engine, Value[] args) {
43     return new Value(engine.eval(args[0]).getNumeric.to!char.to!string);
44   }
45 }
46 
47 /**
48  * convert char into number 
49  */
50 class CharToNumberOperator : IOperator {
51   /**
52    * call
53    */
54   public Value call(Engine engine, Value[] args) {
55     return new Value(engine.eval(args[0]).getString[0].to!double);
56   }
57 }
58 
59 /**
60  * convert float into integer 
61  */
62 class FloatToIntegerOperator : IOperator {
63   /**
64    * call
65    */
66   public Value call(Engine engine, Value[] args) {
67     return new Value(engine.eval(args[0]).getNumeric.to!long.to!double);
68   }
69 }
70 
71 /**
72  * convert ubytes into string 
73  */
74 class UbytesToStringOperator : IOperator {
75   /**
76    * call
77    */
78   public Value call(Engine engine, Value[] args) {
79     Value[] array = engine.eval(args[0]).getArray;
80     ubyte[] ubytes;
81 
82     foreach (elem; array) {
83       ubytes ~= elem.getUbyte;
84     }
85 
86     return new Value(cast(string)ubytes);
87   }
88 }
89 
90 /**
91  * convert ubytes into string 
92  */
93 class UbytesToIntegersOperator : IOperator {
94   /**
95    * call
96    */
97   public Value call(Engine engine, Value[] args) {
98     Value[] array = engine.eval(args[0]).getArray;
99     Value[] ret;
100 
101 
102     foreach (elem; array) {
103       ret ~= new Value(elem.getUbyte.to!double);
104     }
105 
106     return new Value(ret);
107   }
108 }