-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from furesoft/develop
Develop
- Loading branch information
Showing
46 changed files
with
549 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
Source/Samples/Sample.Brainfuck/Parselets/OperationParselet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
Source/Samples/Sample.Rockstar/Matchers/AliasedBooleanMatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
7 changes: 3 additions & 4 deletions
7
Source/Samples/Sample.Rockstar/Parselets/AliasedBooleanParselet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
Source/Samples/Sample.Rockstar/Parselets/AssignmentParselet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.