Wire up resolver to interpreter
This commit is contained in:
parent
5168890ea0
commit
6e88a41b72
@ -64,6 +64,20 @@ public class Env
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Env Ancestor(int distance)
|
||||
{
|
||||
Env? curr = this;
|
||||
for (int i = 0; i < distance; i++)
|
||||
{
|
||||
curr = curr.enclosing;
|
||||
if (curr is null)
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
}
|
||||
return curr;
|
||||
}
|
||||
}
|
||||
|
||||
public interface Callable
|
||||
@ -302,14 +316,16 @@ public class Interpreter : AST.IExprVisitor<Env, object>
|
||||
binding.Params[i].accept((arguments[i], env), new PatternBinder());
|
||||
}
|
||||
return interpreter.evaluate(env, binding.Value);
|
||||
// throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
protected internal readonly Env Globals = new Env();
|
||||
private readonly Resolver Resolver;
|
||||
|
||||
public Interpreter()
|
||||
public Interpreter(Resolver resolver)
|
||||
{
|
||||
Resolver = resolver;
|
||||
|
||||
Globals["clock"] = new NativeFunction((args) =>
|
||||
{
|
||||
return (double)DateTimeOffset.Now.ToUnixTimeSeconds();
|
||||
@ -413,14 +429,14 @@ public class Interpreter : AST.IExprVisitor<Env, object>
|
||||
case TokenType.BangEqual:
|
||||
return Variant.FromBool((left, right) switch
|
||||
{
|
||||
(String l, String r) => l != r,
|
||||
(string l, string r) => l != r,
|
||||
(double l, double r) => l != r,
|
||||
_ => throw new ArgumentException(),
|
||||
});
|
||||
case TokenType.DoubleEqual:
|
||||
return Variant.FromBool((left, right) switch
|
||||
{
|
||||
(String l, String r) => l == r,
|
||||
(string l, string r) => l == r,
|
||||
(double l, double r) => l == r,
|
||||
_ => throw new ArgumentException(),
|
||||
});
|
||||
@ -453,7 +469,15 @@ public class Interpreter : AST.IExprVisitor<Env, object>
|
||||
|
||||
public object visitVariableExpr(Env env, AST.Variable expr)
|
||||
{
|
||||
return env[expr.Value];
|
||||
var name = (string)expr.Value.Literal!;
|
||||
if (Resolver.TryResolve(expr, out var distance))
|
||||
{
|
||||
return env.Ancestor(distance)[expr.Value];
|
||||
}
|
||||
else
|
||||
{
|
||||
return Globals[expr.Value];
|
||||
}
|
||||
}
|
||||
|
||||
public object visitIfExpr(Env env, AST.If expr)
|
||||
@ -499,9 +523,11 @@ public class Interpreter : AST.IExprVisitor<Env, object>
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var start = e is PatternMismatchException ?
|
||||
((PatternMismatchException)e).Start :
|
||||
binding.Start;
|
||||
var start = e switch
|
||||
{
|
||||
PatternMismatchException pme => pme.Start,
|
||||
_ => binding.Start,
|
||||
};
|
||||
throw new RuntimeError(start, e.Message);
|
||||
}
|
||||
break;
|
||||
|
18
Program.cs
18
Program.cs
@ -7,7 +7,8 @@ namespace Finn;
|
||||
|
||||
class Program
|
||||
{
|
||||
private static readonly Interpreter interpreter = new Interpreter();
|
||||
private static readonly Resolver resolver = new Resolver();
|
||||
private static readonly Interpreter interpreter = new Interpreter(resolver);
|
||||
static bool hadError = false;
|
||||
static bool hadRuntimeError = true;
|
||||
|
||||
@ -41,16 +42,8 @@ class Program
|
||||
{
|
||||
var scanner = new Scanner(src);
|
||||
List<Token> tokens = scanner.scanTokens();
|
||||
// Console.WriteLine("TOKENS\n=======");
|
||||
// foreach (var token in tokens)
|
||||
// {
|
||||
// Console.WriteLine(token);
|
||||
// }
|
||||
Parser parser = new Parser(tokens);
|
||||
Expr? expression = parser.parse();
|
||||
// Console.WriteLine("\nAST\n=======");
|
||||
// Console.WriteLine(expression);
|
||||
// Console.WriteLine();
|
||||
|
||||
if (hadError)
|
||||
{
|
||||
@ -58,6 +51,13 @@ class Program
|
||||
return;
|
||||
}
|
||||
|
||||
resolver.Add(expression!);
|
||||
if (hadError)
|
||||
{
|
||||
hadError = false;
|
||||
return;
|
||||
}
|
||||
|
||||
interpreter.Interpret(expression!);
|
||||
}
|
||||
|
||||
|
27
Resolver.cs
27
Resolver.cs
@ -8,8 +8,24 @@ using ScopeStack = System.Collections.Immutable.ImmutableStack<System.Collection
|
||||
using Resolutions = System.Collections.Immutable.ImmutableDictionary<Finn.AST.Expr, int>;
|
||||
using System.Collections.Generic;
|
||||
|
||||
class Resolver : IExprVisitor<ScopeStack, Resolutions>
|
||||
public class Resolver
|
||||
{
|
||||
private static readonly Resolutions EmptyResolutions = Resolutions.Empty.WithComparers(ReferenceEqualityComparer.Instance);
|
||||
|
||||
private Resolutions Resolutions = EmptyResolutions;
|
||||
public bool TryResolve(Expr expr, out int distance)
|
||||
{
|
||||
return Resolutions.TryGetValue(expr, out distance);
|
||||
}
|
||||
|
||||
public void Add(Expr expr)
|
||||
{
|
||||
var newResolutions = ResolverBuilder.Instance.Resolve(ScopeStack.Empty, expr);
|
||||
Resolutions = Resolutions.AddRange(newResolutions);
|
||||
}
|
||||
|
||||
private class ResolverBuilder : IExprVisitor<ScopeStack, Resolutions>
|
||||
{
|
||||
private class NameCollection
|
||||
{
|
||||
public ImmutableHashSet<string> Names { get; private set; } = ImmutableHashSet<string>.Empty;
|
||||
@ -74,14 +90,13 @@ class Resolver : IExprVisitor<ScopeStack, Resolutions>
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Resolutions EmptyResolutions = Resolutions.Empty.WithComparers(ReferenceEqualityComparer.Instance);
|
||||
public static readonly Resolver Instance = new Resolver();
|
||||
public static readonly ResolverBuilder Instance = new ResolverBuilder();
|
||||
|
||||
private Resolver() { }
|
||||
private ResolverBuilder() { }
|
||||
|
||||
private static Resolutions AddRange(Resolutions r1, Resolutions r2) => r1.AddRange(r2);
|
||||
|
||||
private Resolutions Resolve(ScopeStack scopes, Expr expr)
|
||||
public Resolutions Resolve(ScopeStack scopes, Expr expr)
|
||||
{
|
||||
return expr.accept(scopes, this);
|
||||
}
|
||||
@ -254,4 +269,6 @@ class Resolver : IExprVisitor<ScopeStack, Resolutions>
|
||||
return Resolve(scopes, names.Names, _case.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user