Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/CharExpressions.cs @ 15692

Last change on this file since 15692 was 15189, checked in by pkimmesw, 7 years ago

#2665 Fixed small issues, testet benchmark suite, added INX Expressions

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