Skip to content

Commit 43e360b

Browse files
committed
Application changes
1 parent f7a4057 commit 43e360b

File tree

19 files changed

+369
-21
lines changed

19 files changed

+369
-21
lines changed

.idea/.idea.SimpleLangGui/.idea/.gitignore

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.SimpleLangGui/.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.SimpleLangGui/.idea/indexLayout.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.SimpleLangGui/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SL.Application/App.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="SL.Application.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:SL.Application"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>

SL.Application/App.xaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace SL.Application
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : System.Windows.Application
15+
{
16+
}
17+
}

SL.Application/AssemblyInfo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly: ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]

SL.Application/MainWindow.xaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Window x:Class="SL.Application.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:SL.Application"
7+
mc:Ignorable="d"
8+
Title="Simple Lang" Height="1000" Width="1900" WindowStartupLocation="CenterScreen">
9+
<Grid ShowGridLines="True">
10+
<Grid.ColumnDefinitions>
11+
<ColumnDefinition Width="25*"></ColumnDefinition>
12+
<ColumnDefinition Width="25*"></ColumnDefinition>
13+
<ColumnDefinition Width="25*"></ColumnDefinition>
14+
<ColumnDefinition Width="25*"></ColumnDefinition>
15+
</Grid.ColumnDefinitions>
16+
<GridSplitter Grid.Column="1" Width="5" Grid.RowSpan ="3"
17+
VerticalAlignment="Stretch" ResizeBehavior="PreviousAndNext">
18+
</GridSplitter>
19+
<RichTextBox x:Name="Input" Grid.Column="0" FontSize="25" Block.LineHeight="2"/>
20+
<RichTextBox x:Name="Output_token" Grid.Column="1" FontSize="25"/>
21+
<RichTextBox x:Name="Output_tree" Grid.Column="2" FontSize="25"/>
22+
<RichTextBox x:Name="Output_program" Grid.Column="3" FontSize="25"/>
23+
</Grid>
24+
</Window>

SL.Application/MainWindow.xaml.cs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Documents;
9+
using System.Windows.Input;
10+
using SL.Parser;
11+
using SL.Parser.Api;
12+
using SL.Parser.Common;
13+
using SL.Interpreter;
14+
15+
namespace SL.Application
16+
{
17+
/// <summary>
18+
/// Interaction logic for MainWindow.xaml
19+
/// </summary>
20+
public partial class MainWindow : Window
21+
{
22+
private readonly bool ignoreEvent = false;
23+
private CancellationTokenSource _source;
24+
private bool autoCompiler = true;
25+
26+
public MainWindow()
27+
{
28+
InitializeComponent();
29+
Input.TextChanged += onTextChangedNew;
30+
// Input.Background = new SolidColorBrush(Colors.Gray);
31+
Input.Margin = new Thickness(0);
32+
Output_token.FontSize = 25;
33+
Output_tree.FontSize = 12;
34+
_source = new CancellationTokenSource();
35+
36+
InputBindings.Add(new KeyBinding( new SaveFileCommand(() =>
37+
{
38+
RunProgram();
39+
}), new KeyGesture(Key.S, ModifierKeys.Control)));
40+
41+
InputBindings.Add(new KeyBinding( new SaveFileCommand(() =>
42+
{
43+
autoCompiler = !autoCompiler;
44+
}), new KeyGesture(Key.D, ModifierKeys.Control)));
45+
46+
}
47+
48+
public void onTextChangedNew(object a, TextChangedEventArgs args)
49+
{
50+
if (ignoreEvent)
51+
{
52+
return;
53+
}
54+
55+
if (!autoCompiler)
56+
{
57+
return;
58+
}
59+
60+
RunProgram();
61+
}
62+
63+
public void RunProgram()
64+
{
65+
_source.Cancel();
66+
_source = new CancellationTokenSource();
67+
68+
69+
70+
var input = StringFromRichTextBox(Input);
71+
Task.Run(async () =>
72+
{
73+
try
74+
{
75+
InvokeAction(() => Clear(Output_program));
76+
77+
var lexer = LexerFactory.CreateLexer(input);
78+
var parser = ParserFactory.CreateParser();
79+
var evaluator = EvaluatorFactory.CreateEvaluator(_source.Token);
80+
81+
82+
83+
var tokenIterator = await lexer.LexAllToInterator(_source.Token);
84+
InvokeAction(() => DisplayTokens(tokenIterator.ToList()));
85+
86+
var programTree = parser.Parse(tokenIterator);
87+
InvokeAction(() => SetText(Output_tree, programTree.ToJson()));
88+
89+
90+
evaluator.OnConsoleUpdate(list =>
91+
{
92+
InvokeAction(() => DisplayConsole(list));
93+
});
94+
await evaluator.ExecuteProgram(programTree);
95+
96+
}
97+
catch (Exception e)
98+
{
99+
InvokeAction(() => SetText(Output_program, e.Message));
100+
}
101+
}, _source.Token);
102+
}
103+
104+
private void DisplayConsole(List<string> tokens)
105+
{
106+
var builder = new StringBuilder();
107+
foreach (var token in tokens)
108+
{
109+
builder.AppendLine(token);
110+
}
111+
112+
SetText(Output_program, builder.ToString());
113+
}
114+
115+
private void DisplayTokens(List<Token> tokens)
116+
{
117+
var builder = new StringBuilder();
118+
foreach (var token in tokens)
119+
{
120+
builder.AppendLine(token.ToString());
121+
}
122+
123+
SetText(Output_token, builder.ToString());
124+
}
125+
126+
private void InvokeAction(Action action)
127+
{
128+
System.Windows.Application.Current.Dispatcher.Invoke(action.Invoke);
129+
}
130+
131+
private string StringFromRichTextBox(RichTextBox rtb)
132+
{
133+
TextRange textRange = new TextRange(
134+
rtb.Document.ContentStart,
135+
rtb.Document.ContentEnd
136+
);
137+
return textRange.Text;
138+
}
139+
140+
private void Clear(RichTextBox rtb)
141+
{
142+
rtb.Document.Blocks.Clear();
143+
}
144+
145+
private void SetText(RichTextBox rtb, String text)
146+
{
147+
rtb.Document.Blocks.Clear();
148+
rtb.Document.Blocks.Add(new Paragraph(new Run(text)));
149+
}
150+
151+
private void AddText(RichTextBox rtb, String text)
152+
{
153+
rtb.Document.Blocks.Add(new Paragraph(new Run(text)));
154+
}
155+
}
156+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net6.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<UseWPF>true</UseWPF>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\SL.Parser\SL.Parser.csproj" />
12+
<ProjectReference Include="..\SL.Interpreter\SL.Interpreter.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)