Skip to content

Commit 3e26d33

Browse files
committed
Add new BasicFormatter
1 parent c1d8aab commit 3e26d33

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Text;
2+
3+
namespace BooleanExpressionParser.Formatter;
4+
5+
6+
class BasicFormatter : IFormatter
7+
{
8+
public string FormatTokens(IEnumerable<Token> tokens)
9+
{
10+
var sb = new StringBuilder();
11+
12+
foreach (var token in tokens)
13+
{
14+
string s = token.ToString()!;
15+
if (token is not VariableToken && s.Length > 1) s = $"[{s}]";
16+
sb.Append(s);
17+
}
18+
19+
return sb.ToString();
20+
}
21+
22+
public string FormatTruthTable(Ast ast, List<bool[]> table, string label = "Result")
23+
{
24+
// Return a string representation of the truth table, that is simplistic and machine readable
25+
// Format: <var1>,<var2>,...<label>;<row1,0>,<row1,1>,...<row1,result>;...
26+
27+
var sb = new StringBuilder();
28+
29+
// Header
30+
sb.AppendJoin(',', ast.Variables);
31+
sb.Append(',');
32+
sb.Append(label);
33+
sb.Append(';');
34+
35+
// Rows
36+
for (int i = 0; i < table.Count; i++)
37+
{
38+
sb.AppendJoin(',', table[i].Select(b => b ? "1" : "0"));
39+
sb.Append(';');
40+
}
41+
42+
return sb.ToString();
43+
}
44+
45+
public string JoinTruthTables(params string[] tables)
46+
{
47+
throw new NotImplementedException();
48+
}
49+
}

0 commit comments

Comments
 (0)