commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<Application x:Class="InterpreterGui.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources />
|
||||
</Application>
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace InterpreterGui
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
<RootNamespace>InterpreterGui</RootNamespace>
|
||||
<AssemblyName>InterpreterGui</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Interpreter\Interpreter.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,50 @@
|
||||
<Window x:Class="InterpreterGui.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="CMP-7009A Interpreter + Maths Visualiser" Height="650" Width="800"
|
||||
MinHeight="500" MinWidth="650">
|
||||
<Grid Margin="12">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock Grid.Row="0" Text="Enter an expression (e.g. 3 + 4 x 2), a variable assignment (e.g. a = 5), or a line equation (e.g. y = 2x + 1):"
|
||||
TextWrapping="Wrap" Margin="0,0,0,6"/>
|
||||
|
||||
<Grid Grid.Row="1" Margin="0,0,0,6">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBox x:Name="InputBox" Grid.Column="0" Height="28" VerticalContentAlignment="Center"
|
||||
KeyDown="InputBox_KeyDown"/>
|
||||
<Button x:Name="RunButton" Grid.Column="1" Content="Run" Width="80" Margin="6,0,0,0"
|
||||
Click="RunButton_Click"/>
|
||||
<Button x:Name="ClearButton" Grid.Column="2" Content="Clear Canvas" Width="100" Margin="6,0,0,0"
|
||||
Click="ClearButton_Click"/>
|
||||
</Grid>
|
||||
|
||||
<Grid Grid.Row="2" Margin="0,0,0,6">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0" Text="Output: " VerticalAlignment="Center" FontWeight="Bold"/>
|
||||
<TextBox x:Name="OutputBox" Grid.Column="1" Height="28" IsReadOnly="True"
|
||||
VerticalContentAlignment="Center" Background="#F2F2F2"/>
|
||||
</Grid>
|
||||
|
||||
<Border Grid.Row="3" BorderBrush="Gray" BorderThickness="1">
|
||||
<Canvas x:Name="DrawCanvas" Background="White" ClipToBounds="True"
|
||||
SizeChanged="DrawCanvas_SizeChanged"/>
|
||||
</Border>
|
||||
|
||||
<TextBlock Grid.Row="4" x:Name="StatusText" Margin="0,6,0,0" Foreground="Gray"
|
||||
Text="Ready."/>
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,206 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
using Interpreter;
|
||||
using Microsoft.FSharp.Core;
|
||||
|
||||
namespace InterpreterGui
|
||||
{
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
// The F# interpreter engine. Persists variables across evaluations.
|
||||
private readonly Engine _engine = new Engine();
|
||||
|
||||
// Pixels per one unit on the maths axes.
|
||||
private const double Scale = 25.0;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
Loaded += (s, e) => DrawAxes();
|
||||
}
|
||||
|
||||
private void InputBox_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.Key == Key.Enter)
|
||||
{
|
||||
RunInput();
|
||||
}
|
||||
}
|
||||
|
||||
private void RunButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
RunInput();
|
||||
}
|
||||
|
||||
private void ClearButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
DrawCanvas.Children.Clear();
|
||||
DrawAxes();
|
||||
StatusText.Text = "Canvas cleared.";
|
||||
}
|
||||
|
||||
private void DrawCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
|
||||
{
|
||||
// Redraw axes (and nothing else) whenever the window is resized.
|
||||
DrawCanvas.Children.Clear();
|
||||
DrawAxes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decides whether the input is a line equation ("y = ax + b") or a
|
||||
/// plain expression / assignment, and routes it accordingly.
|
||||
/// </summary>
|
||||
private void RunInput()
|
||||
{
|
||||
string text = InputBox.Text;
|
||||
if (text == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
string trimmed = text.Trim();
|
||||
if (trimmed.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Detect "y = ..." form (allowing for whitespace) to route to the
|
||||
// line-drawing parser instead of the general expression evaluator.
|
||||
string noLeadingWs = trimmed.TrimStart();
|
||||
bool looksLikeLine =
|
||||
noLeadingWs.Length > 0 &&
|
||||
(noLeadingWs[0] == 'y' || noLeadingWs[0] == 'Y') &&
|
||||
noLeadingWs.TrimStart('y', 'Y', ' ', '\t').StartsWith("=");
|
||||
|
||||
if (looksLikeLine)
|
||||
{
|
||||
FSharpResult<Tuple<double, double>, string> result = _engine.ParseLine(trimmed);
|
||||
if (result.IsOk)
|
||||
{
|
||||
double a = result.ResultValue.Item1;
|
||||
double b = result.ResultValue.Item2;
|
||||
OutputBox.Text = "Line: y = " + a + "x + " + b;
|
||||
StatusText.Text = "Drew line y = " + a + "x + " + b;
|
||||
DrawLine(a, b);
|
||||
}
|
||||
else
|
||||
{
|
||||
OutputBox.Text = result.ErrorValue;
|
||||
StatusText.Text = "Line parse failed.";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FSharpResult<string, string> result = _engine.Evaluate(trimmed);
|
||||
if (result.IsOk)
|
||||
{
|
||||
OutputBox.Text = result.ResultValue;
|
||||
StatusText.Text = "OK.";
|
||||
}
|
||||
else
|
||||
{
|
||||
OutputBox.Text = result.ErrorValue;
|
||||
StatusText.Text = "Error.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Drawing (uses the WPF Canvas / Shapes API - explicitly permitted)
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private double OriginX => DrawCanvas.ActualWidth / 2.0;
|
||||
private double OriginY => DrawCanvas.ActualHeight / 2.0;
|
||||
|
||||
/// Converts a maths coordinate (x, y) into a canvas pixel point.
|
||||
private Point ToCanvas(double x, double y)
|
||||
{
|
||||
return new Point(OriginX + x * Scale, OriginY - y * Scale);
|
||||
}
|
||||
|
||||
private void DrawAxes()
|
||||
{
|
||||
double w = DrawCanvas.ActualWidth;
|
||||
double h = DrawCanvas.ActualHeight;
|
||||
if (w <= 0 || h <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var xAxis = new Line
|
||||
{
|
||||
X1 = 0, Y1 = OriginY, X2 = w, Y2 = OriginY,
|
||||
Stroke = Brushes.Black, StrokeThickness = 1.5
|
||||
};
|
||||
var yAxis = new Line
|
||||
{
|
||||
X1 = OriginX, Y1 = 0, X2 = OriginX, Y2 = h,
|
||||
Stroke = Brushes.Black, StrokeThickness = 1.5
|
||||
};
|
||||
DrawCanvas.Children.Add(xAxis);
|
||||
DrawCanvas.Children.Add(yAxis);
|
||||
|
||||
// Tick marks every unit, labelled every 5 units to avoid clutter.
|
||||
int maxTicksX = (int)(w / (2 * Scale)) + 1;
|
||||
for (int i = -maxTicksX; i <= maxTicksX; i++)
|
||||
{
|
||||
if (i == 0) continue;
|
||||
double px = OriginX + i * Scale;
|
||||
var tick = new Line { X1 = px, Y1 = OriginY - 4, X2 = px, Y2 = OriginY + 4, Stroke = Brushes.Gray, StrokeThickness = 1 };
|
||||
DrawCanvas.Children.Add(tick);
|
||||
if (i % 5 == 0)
|
||||
{
|
||||
var label = new TextBlock { Text = i.ToString(), FontSize = 10, Foreground = Brushes.Gray };
|
||||
Canvas.SetLeft(label, px - 6);
|
||||
Canvas.SetTop(label, OriginY + 6);
|
||||
DrawCanvas.Children.Add(label);
|
||||
}
|
||||
}
|
||||
|
||||
int maxTicksY = (int)(h / (2 * Scale)) + 1;
|
||||
for (int i = -maxTicksY; i <= maxTicksY; i++)
|
||||
{
|
||||
if (i == 0) continue;
|
||||
double py = OriginY - i * Scale;
|
||||
var tick = new Line { X1 = OriginX - 4, Y1 = py, X2 = OriginX + 4, Y2 = py, Stroke = Brushes.Gray, StrokeThickness = 1 };
|
||||
DrawCanvas.Children.Add(tick);
|
||||
if (i % 5 == 0)
|
||||
{
|
||||
var label = new TextBlock { Text = i.ToString(), FontSize = 10, Foreground = Brushes.Gray };
|
||||
Canvas.SetLeft(label, OriginX + 6);
|
||||
Canvas.SetTop(label, py - 6);
|
||||
DrawCanvas.Children.Add(label);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Draws the line y = a*x + b across the full visible width of the canvas.
|
||||
private void DrawLine(double a, double b)
|
||||
{
|
||||
double w = DrawCanvas.ActualWidth;
|
||||
if (w <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
double xLeftMath = -OriginX / Scale;
|
||||
double xRightMath = (w - OriginX) / Scale;
|
||||
|
||||
double yLeftMath = a * xLeftMath + b;
|
||||
double yRightMath = a * xRightMath + b;
|
||||
|
||||
Point p1 = ToCanvas(xLeftMath, yLeftMath);
|
||||
Point p2 = ToCanvas(xRightMath, yRightMath);
|
||||
|
||||
var line = new Line
|
||||
{
|
||||
X1 = p1.X, Y1 = p1.Y, X2 = p2.X, Y2 = p2.Y,
|
||||
Stroke = Brushes.Blue, StrokeThickness = 2
|
||||
};
|
||||
DrawCanvas.Children.Add(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user