This commit is contained in:
Alexander Luzgarev 2018-12-08 10:46:49 +01:00
parent a55a5bd575
commit e38cdd8b36
5 changed files with 92 additions and 47 deletions

View File

@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Parser\Parser.csproj" />
<ProjectReference Include="..\Repl\Repl.csproj" />
<ProjectReference Include="..\Semantics\Semantics.csproj" />
</ItemGroup>
</Project>

View File

@ -1,52 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Parser;
using Semantics;
using System;
using System.IO;
using System.Linq;
using Parser;
using ProjectConsole;
using Semantics;
using Repl;
namespace ConsoleDemo
{
internal class TreeRenderer
{
private static void RenderToken(SyntaxToken token, string indent, bool isLast)
{
Console.Write(indent + (isLast ? "└── " : "├── "));
Console.Write($"<{token.Kind}>");
Console.Write($" {token.Text}");
Console.WriteLine();
}
private static void RenderNode(SyntaxNode node, string indent, bool isLast)
{
Console.Write(indent);
Console.Write(isLast ? "└── " : "├── ");
Console.Write($"<{node.Kind}>");
Console.WriteLine();
var children = node.GetChildNodesAndTokens();
var last = children.Count - 1;
indent += isLast ? " " : "│ ";
for (var index = 0; index <= last; index++)
{
var child = children[index];
if (child.IsNode)
{
RenderNode(child.AsNode(), indent, index == last);
} else if (child.IsToken)
{
RenderToken(child.AsToken(), indent, index == last);
}
}
}
public static void RenderTree(SyntaxTree tree)
{
RenderNode(tree.Root, "", true);
}
}
class Program
{
private static readonly string BaseDirectory;
@ -156,9 +116,16 @@ namespace ConsoleDemo
printer.Visit(tree.Root);
}
public static void ReplDemo()
{
var repl = new MRepl();
repl.Run();
}
public static void Main(string[] args)
{
ParserDemo();
ReplDemo();
//ParserDemo();
//SemanticsDemo();
//ContextDemo();
//DumbPrinterDemo();

60
Repl/MRepl.cs Normal file
View File

@ -0,0 +1,60 @@
using System;
using Parser;
namespace Repl
{
public class MRepl
{
public void Run()
{
while (true)
{
Console.Write("> ");
var line = Console.ReadLine();
var window = new TextWindowWithNull(line);
var parser = new MParser(window);
var tree = parser.Parse();
TreeRenderer.RenderTree(tree);
}
}
}
public class TreeRenderer
{
private static void RenderToken(SyntaxToken token, string indent, bool isLast)
{
Console.Write(indent + (isLast ? "└── " : "├── "));
Console.Write($"<{token.Kind}>");
Console.Write($" {token.Text}");
Console.WriteLine();
}
private static void RenderNode(SyntaxNode node, string indent, bool isLast)
{
Console.Write(indent);
Console.Write(isLast ? "└── " : "├── ");
Console.Write($"<{node.Kind}>");
Console.WriteLine();
var children = node.GetChildNodesAndTokens();
var last = children.Count - 1;
indent += isLast ? " " : "│ ";
for (var index = 0; index <= last; index++)
{
var child = children[index];
if (child.IsNode)
{
RenderNode(child.AsNode(), indent, index == last);
}
else if (child.IsToken)
{
RenderToken(child.AsToken(), indent, index == last);
}
}
}
public static void RenderTree(SyntaxTree tree)
{
RenderNode(tree.Root, "", true);
}
}
}

11
Repl/Repl.csproj Normal file
View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Parser\Parser.csproj" />
</ItemGroup>
</Project>

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2026
# Visual Studio Version 16
VisualStudioVersion = 16.0.28315.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleDemo", "ConsoleDemo\ConsoleDemo.csproj", "{5025FD8F-0F1A-43E5-A996-7753BC703D62}"
EndProject
@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repl", "Repl\Repl.csproj", "{8FEDFE5D-3320-418F-88A6-09C1B51C4441}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -45,6 +47,10 @@ Global
{4595633B-7F9A-4771-B348-F12BB9DD7ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4595633B-7F9A-4771-B348-F12BB9DD7ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4595633B-7F9A-4771-B348-F12BB9DD7ABC}.Release|Any CPU.Build.0 = Release|Any CPU
{8FEDFE5D-3320-418F-88A6-09C1B51C4441}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8FEDFE5D-3320-418F-88A6-09C1B51C4441}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FEDFE5D-3320-418F-88A6-09C1B51C4441}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FEDFE5D-3320-418F-88A6-09C1B51C4441}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE