Skip to content

Project restructure & new web UI #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions BooleanExpressionParser.CLI/BooleanExpressionParser.CLI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>BooleanExpressionParser.CLI</PackageId>
<Version>1.2.0</Version>
<Authors>Tom Chapman</Authors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.45.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BooleanExpressionParser\BooleanExpressionParser.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System.Globalization;
using System.Text;
using BooleanExpressionParser.Formatters;
using BooleanExpressionParser.Tokens;
using Spectre.Console;

namespace BooleanExpressionParser.Formatter;
namespace BooleanExpressionParser.CLI.Formatters;

enum ColourMode
public enum ColourMode
{
None,
Foreground,
Background
}


class DisplayFormatter : IFormatter
public class DisplayFormatter : IFormatter
{
private static int FinalPadding = 2;

Expand Down Expand Up @@ -128,7 +129,7 @@ static string PadBoth(string source, int totalLength, char paddingChar = ' ')
}


public String JoinTruthTables(params string[] tables)
public string JoinTruthTables(params string[] tables)
{
var sb = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.CommandLine;
using System.CommandLine;
using System.Text;
using BooleanExpressionParser.Formatter;
using System;
using Spectre.Console;
using BooleanExpressionParser.CLI.Formatters;
using BooleanExpressionParser.Formatters;

namespace BooleanExpressionParser;
namespace BooleanExpressionParser.CLI;


enum OutputType
Expand Down
12 changes: 12 additions & 0 deletions BooleanExpressionParser.CLI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"BooleanExpressionParser.CLI - table": {
"commandName": "Project",
"commandLineArgs": "table"
},
"BooleanExpressionParser.CLI - convert": {
"commandName": "Project",
"commandLineArgs": "convert"
}
}
}
12 changes: 12 additions & 0 deletions BooleanExpressionParser.Web/Client/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>annotations</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
<PackageId>BooleanExpressionParser.Web.Client</PackageId>
<Version>1.0.0</Version>
<Authors>Tom Chapman</Authors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\BooleanExpressionParser.Web.Shared.csproj" />
<ProjectReference Include="..\..\BooleanExpressionParser\BooleanExpressionParser.csproj" />
</ItemGroup>

<ItemGroup>
<ServiceWorker Include="wwwroot\service-worker.js" PublishedContent="wwwroot\service-worker.published.js" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<input placeholder="Enter expression" @oninput="OnInputChanged" @bind="expression"/>
<button @onclick="OnButtonClicked">@ButtonLabel</button>


@code {
[Parameter]
public string ButtonLabel {get; set;}

public string? expression;

[Parameter]
public EventCallback<string> ButtonClicked { get; set; }

[Parameter]
public EventCallback<string> ExpressionChanged { get; set; }

private Task OnButtonClicked()
{
return ButtonClicked.InvokeAsync(expression);
}

private Task OnInputChanged(ChangeEventArgs args)
{
return ExpressionChanged.InvokeAsync(args.Value.ToString());
}
}
19 changes: 19 additions & 0 deletions BooleanExpressionParser.Web/Client/Components/NavBar.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="container">
<h3>Boolean Expression Parser</h3>
<nav>
<ul>
<li>
<NavLink class="nav-link" href="/" Match="NavLinkMatch.All">Home</NavLink>
</li>
<li>
<NavLink class="nav-link" href="/truth-table" Match="NavLinkMatch.All">Truth Table Generator</NavLink>
</li>
<li>
<NavLink class="nav-link" href="/evaluator" Match="NavLinkMatch.All">Evaluator</NavLink>
</li>
<li>
<NavLink class="nav-link" href="/notation-converter" Match="NavLinkMatch.All">Notation Converter</NavLink>
</li>
</ul>
</nav>
</div>
23 changes: 23 additions & 0 deletions BooleanExpressionParser.Web/Client/Components/NavBar.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.container {
display: flex;
justify-content: space-between;
padding: 2rem;
}

.container ul {
display: flex;
gap: 2rem;
}

.container ul li {
list-style: none;
}

.container ul li ::deep .nav-link {
text-decoration: none;
color: black;
}

.container ul li ::deep .nav-link.active {
font-weight: bold;
}
33 changes: 33 additions & 0 deletions BooleanExpressionParser.Web/Client/Components/ValueInput.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@foreach (var item in Ast.Variables)
{
<span>@item</span>
<input size="3" @bind="inputs[item]" />
}

<button @onclick="OnButtonClicked">Evaluate</button>


@code {
[Parameter]
public Ast Ast { get; set; }

[Parameter]
public EventCallback<Dictionary<string, string?>> EvaluateClicked { get; set; }

private Dictionary<string, string?> inputs = new();

private Task OnButtonClicked()
{
return EvaluateClicked.InvokeAsync(inputs);
}

protected override void OnParametersSet()
{
base.OnParametersSet();

foreach (var item in Ast.Variables)
{
if (!inputs.ContainsKey(item)) inputs.Add(item, null);
}
}
}
62 changes: 62 additions & 0 deletions BooleanExpressionParser.Web/Client/Formatters/HTMLFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using BooleanExpressionParser.Formatters;
using BooleanExpressionParser.Tokens;
using System.Text;

namespace BooleanExpressionParser.Web.Client.Formatters;

public class HTMLFormatter : IFormatter
{
public string FormatTokens(IEnumerable<Token> tokens)
{
var sb = new StringBuilder();

foreach (Token token in tokens)
{
string s = token.ToString()!;
if (token is OperatorToken && s.Length > 1) s = $"[[{s}]]";
sb.Append(s);
}

return sb.ToString();
}

public string FormatTruthTable(Ast ast, List<bool[]> table, string label)
{
var sb = new StringBuilder();

// Generate HTML representation of the truth table
// table
// tr
// th th th
// tr ***
// td td td

sb.Append("<table class=\"truth-table\">");

sb.Append("<tr>");
for (int i = 0; i < ast.Variables.Count; i++)
{
string? item = ast.Variables[i];
sb.Append($"<th>{item}</th>");
}
sb.Append($"<th>{label}</th>");
sb.Append("</tr>");

foreach (bool[] row in table)
{
sb.Append("<tr>");
for (int i = 0; i < row.Length; i++)
{
sb.Append($"<td class=\"{(row[i] ? "true" : "false")}\">{(row[i] ? "1" : "0")}</td>");
}
sb.Append("</tr>");
}

return sb.ToString();
}

public string JoinTruthTables(params string[] tables)
{
throw new NotImplementedException();
}
}
11 changes: 11 additions & 0 deletions BooleanExpressionParser.Web/Client/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@inherits LayoutComponentBase
@using BooleanExpressionParser.Web.Client.Components;


<div id="nav-bar">
<NavBar />
</div>

<main>
@Body
</main>
56 changes: 56 additions & 0 deletions BooleanExpressionParser.Web/Client/Pages/Evaluator.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@page "/evaluator"
@using BooleanExpressionParser.Web.Client.Components

<PageTitle>Expression Evaluator</PageTitle>

<h1>Expression Evaluator</h1>
<p>Produces the output of an expression for given values.</p>

<ExpressionInput ButtonLabel="Load" ButtonClicked="ParseExpression" ExpressionChanged="ExpressionChanged" />

@if (expressionParsed)
{
<ValueInput Ast="ast" EvaluateClicked="Evaluate"/>
}

@if (result is not null)
{
<h3>@result</h3>
}

@code {
private bool expressionParsed = false;
private Ast? ast;
private bool? result;

private void ExpressionChanged(string _)
{
expressionParsed = false;
result = null;
}

private void ParseExpression(string? expression)
{
if (string.IsNullOrWhiteSpace(expression)) return;
var wrapper = new ExpressionWrapper(expression);

var tokeniser = new Tokeniser(wrapper.Expression);
var infixTokens = tokeniser.Tokenise();

var parser = new Parser();
var postfixTokens = parser.InfixToPostfix(infixTokens);

ast = parser.GrowAst(postfixTokens, wrapper.VariableOrder);

expressionParsed = true;
}

private void Evaluate(Dictionary<string, string?> inputs)
{
// Parse inputs
var values = inputs.Select(x => new { Key = x.Key, Value = x.Value == "1" }).ToDictionary(pair => pair.Key, pair => pair.Value);

result = ast.Root.Evaluate(values);
}
}

6 changes: 6 additions & 0 deletions BooleanExpressionParser.Web/Client/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@page "/"

<PageTitle>Index Page</PageTitle>

<h1>BooleanExpressionParser - Web</h1>
<p>This is a web UI for BooleanExpressionParser.</p>
Loading