Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/CharExpressions.cs @ 17189

Last change on this file since 17189 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 5.5 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.ProgramSynthesis {
6
7  /// <summary>
8  /// Pushes TRUE onto the BOOLEAN stack if the top char represents a whitespace char, otherwise FALSE.
9  /// </summary>
10  [PushExpression(
11    StackTypes.Char,
12    "CHAR.ISWHITESPACE",
13    "Pushes TRUE onto the BOOLEAN stack if the top char represents a whitespace char, otherwise FALSE.",
14    StackTypes.Boolean)]
15  [StorableClass]
16  public class CharIsWhitespaceExpression : StatelessExpression {
17    public CharIsWhitespaceExpression() { }
18    [StorableConstructor]
19    public CharIsWhitespaceExpression(bool deserializing) : base(deserializing) { }
20
21    public override bool IsNoop(IInternalPushInterpreter interpreter) {
22      return interpreter.CharStack.IsEmpty;
23    }
24
25    public override void Eval(IInternalPushInterpreter interpreter) {
26      var c = interpreter.CharStack.Pop();
27      interpreter.BooleanStack.Push(char.IsWhiteSpace(c));
28    }
29  }
30
31  /// <summary>
32  /// Pushes TRUE onto the CHAR stack if the top char represents a letter, otherwise FALSE.
33  /// </summary>
34  [PushExpression(
35    StackTypes.Char,
36    "CHAR.ISLETTER",
37    "Pushes TRUE onto the CHAR stack if the top char represents a letter, otherwise FALSE.",
38    StackTypes.Boolean)]
39  [StorableClass]
40  public class CharIsLetterExpression : StatelessExpression {
41    public CharIsLetterExpression() { }
42    [StorableConstructor]
43    public CharIsLetterExpression(bool deserializing) : base(deserializing) { }
44
45    public override bool IsNoop(IInternalPushInterpreter interpreter) {
46      return interpreter.CharStack.IsEmpty;
47    }
48
49    public override void Eval(IInternalPushInterpreter interpreter) {
50      var c = interpreter.CharStack.Pop();
51      interpreter.BooleanStack.Push(char.IsLetter(c));
52    }
53  }
54
55  /// <summary>
56  /// Pushes TRUE onto the CHAR stack if the top char represents a digit, otherwise FALSE.
57  /// </summary>
58  [PushExpression(
59    StackTypes.Char,
60    "CHAR.ISDIGIT",
61    "Pushes TRUE onto the CHAR stack if the top char represents a digit, otherwise FALSE.",
62    StackTypes.Boolean)]
63  [StorableClass]
64  public class CharIsDigitExpression : StatelessExpression {
65    public CharIsDigitExpression() { }
66    [StorableConstructor]
67    public CharIsDigitExpression(bool deserializing) : base(deserializing) { }
68
69    public override bool IsNoop(IInternalPushInterpreter interpreter) {
70      return interpreter.CharStack.IsEmpty;
71    }
72
73    public override void Eval(IInternalPushInterpreter interpreter) {
74      var c = interpreter.CharStack.Pop();
75      interpreter.BooleanStack.Push(char.IsDigit(c));
76    }
77  }
78
79  /// <summary>
80  /// Takes the top integer, converts it to an char using ASCII encoding and pushes this char onto the CHAR stack.
81  /// </summary>
82  [PushExpression(
83    StackTypes.Char,
84    "CHAR.FROMINTEGER",
85    "Takes the top integer, converts it to an char using ASCII encoding and pushes this char onto the CHAR stack.",
86    StackTypes.Integer)]
87  [StorableClass]
88  public class CharFromIntegerExpression : StatelessExpression {
89    public CharFromIntegerExpression() { }
90    [StorableConstructor]
91    public CharFromIntegerExpression(bool deserializing) : base(deserializing) { }
92
93    public override bool IsNoop(IInternalPushInterpreter interpreter) {
94      return interpreter.IntegerStack.IsEmpty;
95    }
96
97    public override void Eval(IInternalPushInterpreter interpreter) {
98      var value = (int)Math.Abs(interpreter.IntegerStack.Pop() % 128);
99      var c = Convert.ToChar(value);
100
101      interpreter.CharStack.Push(c);
102    }
103  }
104
105  /// <summary>
106  /// Takes the top float, casts it to an integer, converts it to an char using ASCII encoding and pushes this char onto the CHAR stack.
107  /// </summary>
108  [PushExpression(
109    StackTypes.Char,
110    "CHAR.FROMFLOAT",
111    "Takes the top float, casts it to an integer, converts it to an char using ASCII encoding and pushes this char onto the CHAR stack.",
112    StackTypes.Float)]
113  [StorableClass]
114  public class CharFromFloatExpression : StatelessExpression {
115    public CharFromFloatExpression() { }
116    [StorableConstructor]
117    public CharFromFloatExpression(bool deserializing) : base(deserializing) { }
118
119    public override bool IsNoop(IInternalPushInterpreter interpreter) {
120      return interpreter.FloatStack.IsEmpty;
121    }
122
123    public override void Eval(IInternalPushInterpreter interpreter) {
124      var value = (int)Math.Abs(interpreter.FloatStack.Pop() % 128);
125      var c = Convert.ToChar(value);
126
127      interpreter.CharStack.Push(c);
128    }
129  }
130
131  /// <summary>
132  /// Takes the top string and pushes the chars of this string onto the CHAR stack in reversed order.
133  /// </summary>
134  [PushExpression(
135    StackTypes.Char,
136    "CHAR.ALLFROMSTRING",
137    "Takes the top string and pushes the chars of this string onto the CHAR stack in reversed order.",
138    StackTypes.String)]
139  [StorableClass]
140  public class CharAllFromStringExpression : StatelessExpression {
141    public CharAllFromStringExpression() { }
142    [StorableConstructor]
143    public CharAllFromStringExpression(bool deserializing) : base(deserializing) { }
144
145    public override bool IsNoop(IInternalPushInterpreter interpreter) {
146      return interpreter.StringStack.IsEmpty;
147    }
148
149    public override void Eval(IInternalPushInterpreter interpreter) {
150      var chars = interpreter.StringStack.Pop().Reverse().ToList();
151      interpreter.CharStack.Push(chars);
152    }
153  }
154
155}
Note: See TracBrowser for help on using the repository browser.