Skip to content

Commit

Permalink
feat(sample): Add import
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft authored Jul 8, 2024
1 parent 91491d6 commit cb76d15
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Source/Samples/Sample.FuncLanguage/EvaluationVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ public EvaluationVisitor()
For<NameNode>(Visit);
For<CallNode>(Visit);
For<TupleNode>(Visit);
For<ImportNode>(Visit);
}

Value Visit(ImportNode node, Scope scope)
{
var file = new FileInfo($"{node.Path}.f");

var content = File.ReadAllText(file.FullName);
var parsed = Parser.Parse<ExpressionGrammar>(content, file.FullName);
var rewritten = parsed.Tree.Accept(new RewriterVisitor());

rewritten.Accept(new EvaluationVisitor(), scope);

return UnitValue.Shared;
}

Value Visit(BinaryOperatorNode binNode, Scope scope)
Expand Down
1 change: 1 addition & 0 deletions Source/Samples/Sample.FuncLanguage/ExpressionGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected override void InitParselets()
Register("->", new LambdaParselet());

Register("if", new IfParselet());
Register("import", new ImportParselet());

Block(PredefinedSymbols.SOF, PredefinedSymbols.EOF,
seperator: PredefinedSymbols.Semicolon);
Expand Down
5 changes: 5 additions & 0 deletions Source/Samples/Sample.FuncLanguage/Nodes/ImportNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Silverfly.Nodes;

namespace Sample.FuncLanguage.Nodes;

public record ImportNode(string Path) : AstNode;
21 changes: 21 additions & 0 deletions Source/Samples/Sample.FuncLanguage/Parselets/ImportParselet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Sample.FuncLanguage.Nodes;
using Silverfly;
using Silverfly.Nodes;
using Silverfly.Parselets;

namespace Sample.FuncLanguage.Parselets;

public class ImportParselet : IPrefixParselet
{
public AstNode Parse(Parser parser, Token token)
{
var arg = parser.ParseExpression();

if (arg is LiteralNode { Value: string path })
{
return new ImportNode(path);
}

return new InvalidNode(token);
}
}
2 changes: 2 additions & 0 deletions Source/Samples/Sample.FuncLanguage/RewriterVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public RewriterVisitor()
For<TupleNode>(Rewrite);
}

protected override AstNode VisitUnknown(AstNode node) => node;

private AstNode Rewrite(TupleNode node)
{
return node with
Expand Down
4 changes: 4 additions & 0 deletions Source/Samples/Sample.FuncLanguage/Sample.FuncLanguage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Include="std.f" CopyToOutputDirectory="PreserveNewest"/>
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions Source/Samples/Sample.FuncLanguage/ToDo.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[-] implement lambda functions as values
lambdas with more than one parameter has to be implemented
[ ] implement module definition
[-] implement module import from file (read file, parse and evaluate with the current rootscope)
[ ] implement to scope to import clr classes/functions
5 changes: 5 additions & 0 deletions Source/Samples/Sample.FuncLanguage/Values/ModuleValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public ModuleValue(Scope scope)
}

public override bool IsTruthy() => true;

public override string ToString()
{
return $"Module with {Members.Bindings.Count} bindings";
}
}
1 change: 1 addition & 0 deletions Source/Samples/Sample.FuncLanguage/std.f
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let add x y = x + y

0 comments on commit cb76d15

Please sign in to comment.