Skip to content

Commit

Permalink
Merge pull request #47 from furesoft/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
furesoft authored Sep 27, 2024
2 parents f1d0dea + 887d10e commit 8b8c13f
Show file tree
Hide file tree
Showing 46 changed files with 549 additions and 83 deletions.
22 changes: 22 additions & 0 deletions Source/Samples/Sample.Brainfuck/BrainfuckParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Sample.Brainfuck.Parselets;
using Silverfly;
using Silverfly.Lexing.IgnoreMatcher.Comments;

namespace Sample.Brainfuck;

public class BrainfuckParser : Parser
{
protected override void InitLexer(LexerConfig lexer)
{
lexer.IgnoreWhitespace();
lexer.Ignore(new SingleLineCommentIgnoreMatcher("#"));
}

protected override void InitParser(ParserDefinition def)
{
def.Register(new OperationParselet(), "+", "-", "<", ">", ".", ",");
def.Register("[", new LoopParselet());

def.Block(PredefinedSymbols.SOF, PredefinedSymbols.EOF);
}
}
70 changes: 70 additions & 0 deletions Source/Samples/Sample.Brainfuck/EvalVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Sample.Brainfuck.Nodes;
using Silverfly;
using Silverfly.Generator;
using Silverfly.Nodes;

namespace Sample.Brainfuck;

[Visitor]
public partial class EvalVisitor : NodeVisitor
{
private int _pointer = 0;
readonly char[] _cells = new char[100];

[VisitorCondition("_.Token == '.'")]
void Print(OperationNode node)
{
Console.Write(_cells[_pointer]);
}

[VisitorCondition("_.Token == ','")]
void Read(OperationNode node)
{
_cells[_pointer] = Console.ReadKey().KeyChar;
}

[VisitorCondition("_.Token == '<'")]
void Decrement(OperationNode node)
{
_pointer--;
}

[VisitorCondition("_.Token == '>'")]
void Increment(OperationNode node)
{
_pointer++;
}

[VisitorCondition("_.Token == '+'")]
void IncrementCell(OperationNode node)
{
_cells[_pointer]++;
}

[VisitorCondition("_.Token == '-'")]
void DecrementCell(OperationNode node)
{
_cells[_pointer]--;
}

[VisitorCondition("_.Tag == 'loop'")]
void Loop(BlockNode node)
{
while (_cells[_pointer] != '\0')
{
foreach (var child in node.Children)
{
Visit(child);
}
}
}

[VisitorCondition("_.Tag == null")]
void Block(BlockNode node)
{
foreach (var child in node.Children)
{
Visit(child);
}
}
}
8 changes: 8 additions & 0 deletions Source/Samples/Sample.Brainfuck/Nodes/OperationNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Silverfly;
using Silverfly.Nodes;

namespace Sample.Brainfuck.Nodes;

public record OperationNode(Token Token) : AstNode
{
}
18 changes: 18 additions & 0 deletions Source/Samples/Sample.Brainfuck/Parselets/LoopParselet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Silverfly;
using Silverfly.Nodes;
using Silverfly.Parselets;

namespace Sample.Brainfuck.Parselets;

public class LoopParselet : IPrefixParselet
{
public AstNode Parse(Parser parser, Token token)
{
var instructions = parser.ParseList(terminators: "]");

return new BlockNode(null, "]")
.WithChildren(instructions)
.WithTag("loop")
.WithRange(token, parser.LookAhead(0));
}
}
14 changes: 14 additions & 0 deletions Source/Samples/Sample.Brainfuck/Parselets/OperationParselet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Sample.Brainfuck.Nodes;
using Silverfly;
using Silverfly.Nodes;
using Silverfly.Parselets;

namespace Sample.Brainfuck.Parselets;

public class OperationParselet : IPrefixParselet
{
public AstNode Parse(Parser parser, Token token)
{
return new OperationNode(token).WithRange(token);
}
}
9 changes: 9 additions & 0 deletions Source/Samples/Sample.Brainfuck/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Sample.Brainfuck;

public static class Program
{
public static async Task Main(string[] args)
{
new Repl().Run();
}
}
32 changes: 32 additions & 0 deletions Source/Samples/Sample.Brainfuck/Repl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Silverfly.Repl;

namespace Sample.Brainfuck;

public class Repl : ReplInstance<BrainfuckParser>
{
protected override void Evaluate(string input)
{
var helloWorld = """
++++++++++
[
>+++++++>++++++++++>+++>+<<<<-
]
>++. #'H'
>+. #'e'
+++++++. #'l'
. #'l'
+++. #'o'
>++. #Space
<<+++++++++++++++. #'W'
>. #'o'
+++. #'r'
------. #'l'
--------. #'d'
>+. #'!'
>.
+++.
""";
var parsed = Parser.Parse(helloWorld);
parsed.Tree.Accept(new EvalVisitor());
}
}
17 changes: 17 additions & 0 deletions Source/Samples/Sample.Brainfuck/Sample.Brainfuck.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../Silverfly.Generator/Silverfly.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\Silverfly.Repl\Silverfly.Repl.csproj" />
<ProjectReference Include="..\..\Silverfly\Silverfly.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.68</Version>
<Version>1.0.69</Version>
<OutputType>Exe</OutputType>
<LangVersion>preview</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
2 changes: 1 addition & 1 deletion Source/Samples/Sample.JSON/Sample.JSON.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.69</Version>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -14,7 +15,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.68</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
21 changes: 14 additions & 7 deletions Source/Samples/Sample.Rockstar/Evaluation/EvaluationVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Silverfly;
using Silverfly.Generator;
using Silverfly.Generator;
using Silverfly.Nodes;
using Silverfly.Nodes.Operators;
using Silverfly.Sample.Rockstar.Evaluation;
using Silverfly.Text;

namespace Sample.Rockstar.Evaluation;
namespace Silverfly.Sample.Rockstar.Evaluation;

[Visitor]
public partial class EvaluationVisitor : TaggedNodeVisitor<object, Scope>
Expand All @@ -14,16 +13,24 @@ private object VisitAssignment(BinaryOperatorNode node, Scope scope)
{
if (node.LeftExpr is not NameNode name) return null;

scope.Define(name.Token.Text.ToString(), Visit(node.RightExpr, scope));
scope.Define(name.Token.Text.Trim().ToString(), Visit(node.RightExpr, scope));

return null;
}

private object VisitCall(CallNode call, Scope scope)
{
if (call.FunctionExpr is NameNode name && name.Token == "say")
if (call.FunctionExpr is NameNode name && name.Token == "print")
{
Console.WriteLine(Visit(call.Arguments[0], scope));
var arg = Visit(call.Arguments[0], scope);

if (arg is null && call.Arguments[0] is NameNode n)
{
call.Arguments[0].AddMessage(MessageSeverity.Error, $"Variable '{n.Token.Text}' is not defined");
return null;
}

Console.WriteLine(arg);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Silverfly;
using Silverfly.Lexing;
using Silverfly.Lexing;

namespace Sample.Rockstar.Matchers;
namespace Silverfly.Sample.Rockstar.Matchers;

public class AliasedBooleanMatcher : IMatcher
{
Expand Down
5 changes: 2 additions & 3 deletions Source/Samples/Sample.Rockstar/Matchers/MappingMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Silverfly;
using Silverfly.Lexing;
using Silverfly.Lexing;

namespace Sample.Rockstar.Matchers;
namespace Silverfly.Sample.Rockstar.Matchers;

public class MappingMatcher(Symbol type, string[] aliases) : IMatcher
{
Expand Down
7 changes: 7 additions & 0 deletions Source/Samples/Sample.Rockstar/Nodes/IfNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Collections.Immutable;
using Silverfly.Nodes;

namespace Silverfly.Sample.Rockstar.Nodes;

public record IfNode(AstNode Condition, ImmutableList<AstNode> TruePart, ImmutableList<AstNode> FalsePart)
: StatementNode;
6 changes: 6 additions & 0 deletions Source/Samples/Sample.Rockstar/Nodes/LoopNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Collections.Immutable;
using Silverfly.Nodes;

namespace Silverfly.Sample.Rockstar.Nodes;

public record LoopNode(AstNode Condition, ImmutableList<AstNode> Body) : StatementNode;
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Sample.Rockstar.Matchers;
using Silverfly;
using Silverfly.Nodes;
using Silverfly.Nodes;
using Silverfly.Parselets;
using Silverfly.Sample.Rockstar.Matchers;

namespace Sample.Rockstar.Parselets;
namespace Silverfly.Sample.Rockstar.Parselets;

public class AliasedBooleanParselet : IPrefixParselet
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Silverfly;
using Silverfly.Nodes;
using Silverfly.Nodes;
using Silverfly.Nodes.Operators;
using Silverfly.Parselets;

namespace Sample.Rockstar.Parselets;
namespace Silverfly.Sample.Rockstar.Parselets;

public class AssignmentParselet : IPrefixParselet
{
Expand Down
16 changes: 16 additions & 0 deletions Source/Samples/Sample.Rockstar/Parselets/IfParselet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Silverfly.Nodes;
using Silverfly.Parselets;
using Silverfly.Sample.Rockstar.Nodes;

namespace Silverfly.Sample.Rockstar.Parselets;

public class IfParselet : IStatementParselet
{
public AstNode Parse(Parser parser, Token token)
{
var condition = parser.ParseExpression();
var body = parser.ParseList(PredefinedSymbols.EOF, Environment.NewLine + Environment.NewLine);

return new IfNode(condition, body, []);
}
}
22 changes: 22 additions & 0 deletions Source/Samples/Sample.Rockstar/Parselets/LoopParselet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Silverfly.Nodes;
using Silverfly.Parselets;
using Silverfly.Sample.Rockstar.Nodes;

namespace Silverfly.Sample.Rockstar.Parselets;

public class LoopParselet : IStatementParselet
{
public AstNode Parse(Parser parser, Token token)
{
var condition = parser.ParseExpression();

if (parser.IsMatch(",") || parser.IsMatch("\n"))
{
parser.Consume();
}

var body = parser.ParseList(PredefinedSymbols.EOF, "\n\n", ".");

return new LoopNode(condition, body);
}
}
5 changes: 2 additions & 3 deletions Source/Samples/Sample.Rockstar/Parselets/MappingParselet.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Silverfly;
using Silverfly.Nodes;
using Silverfly.Nodes;
using Silverfly.Parselets;

namespace Sample.Rockstar.Parselets;
namespace Silverfly.Sample.Rockstar.Parselets;

public class MappingParselet(object Value) : IPrefixParselet
{
Expand Down
Loading

0 comments on commit 8b8c13f

Please sign in to comment.