Skip to content

Commit

Permalink
move Visitor.fs to Escalier.Data
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbarabash committed Aug 27, 2024
1 parent af2fede commit 89ef0ca
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 74 deletions.
14 changes: 7 additions & 7 deletions src/Escalier.Codegen/Codegen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ open Escalier.Data
open Escalier.Data.Common
open Escalier.Data.Syntax
open Escalier.Data.Type
open Escalier.Data.Visitor
open Escalier.TypeChecker
open Escalier.TypeChecker.Prune
open Escalier.TypeChecker.Env
open Escalier.TypeChecker.ExprVisitor
open Escalier.TypeChecker

module rec Codegen =
module TS = TypeScript
Expand Down Expand Up @@ -1043,8 +1043,8 @@ module rec Codegen =
map <- Map.add name $"__arg{i}__" map)

// TODO: keep track of shadowed variables and avoid renaming them
let visitor: SyntaxVisitor<Set<string>> =
{ VisitExpr =
let visitor: ExprVisitor.SyntaxVisitor<Set<string>> =
{ ExprVisitor.VisitExpr =
fun (expr, state) ->
match expr.Kind with
| ExprKind.Identifier ident ->
Expand Down Expand Up @@ -1088,7 +1088,7 @@ module rec Codegen =
let checks =
decl.Sig.ParamList
|> List.map (fun param ->
walkPattern visitor Set.empty param.Pattern
ExprVisitor.walkPattern visitor Set.empty param.Pattern
checkExprFromParam param)

let check =
Expand All @@ -1103,10 +1103,10 @@ module rec Codegen =
let body =
match decl.Body with
| Some(BlockOrExpr.Block block) ->
List.iter (walkStmt visitor Set.empty) block.Stmts
List.iter (ExprVisitor.walkStmt visitor Set.empty) block.Stmts
buildBlock ctx block Finalizer.Empty
| Some(BlockOrExpr.Expr expr) ->
walkExpr visitor Set.empty expr
ExprVisitor.walkExpr visitor Set.empty expr
let expr, stmts = buildExpr ctx expr

let body =
Expand Down
4 changes: 2 additions & 2 deletions src/Escalier.Compiler/Prelude.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ open System.IO
open Escalier.Data
open Escalier.Data.Common
open Escalier.Data.Type
open Escalier.Data.Visitor
open Escalier.TypeChecker
open Escalier.TypeChecker.Error
open Escalier.TypeChecker.ExprVisitor
open Escalier.Interop

open Env
Expand Down Expand Up @@ -140,7 +140,7 @@ module Prelude =
ExprVisitor.VisitTypeAnn = fun (_, state) -> (false, state)
ExprVisitor.VisitTypeAnnObjElem = fun (_, state) -> (false, state) }

walkPattern visitor () p
ExprVisitor.walkPattern visitor () p

List.rev names

Expand Down
1 change: 1 addition & 0 deletions src/Escalier.Data/Escalier.Data.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Library.fs"/>
<Compile Include="Visitor.fs"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="FParsec" Version="1.1.1"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
namespace Escalier.TypeChecker
module Escalier.Data.Visitor

open Escalier.Data.Syntax

// TODO: move to Escalier.Data.Visitor
module rec ExprVisitor =
open Escalier.Data.Syntax

type SyntaxVisitor<'S> =
{ VisitExpr: Expr * 'S -> bool * 'S
Expand Down Expand Up @@ -387,8 +385,6 @@ module rec ExprVisitor =
module rec TypeVisitor =
open Escalier.Data.Type

open Prune

let walkFunction (walk: Type -> unit) (f: Function) : unit =
List.iter (fun (param: FuncParam) -> walk param.Type) f.ParamList
walk f.Return
Expand Down
24 changes: 12 additions & 12 deletions src/Escalier.TypeChecker/BuildGraph.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module rec Escalier.TypeChecker.BuildGraph

open Escalier.Data.Type
open Escalier.TypeChecker.Env

open Escalier.Data.Syntax
open Escalier.Data.Visitor

open Escalier.TypeChecker.Env
open Escalier.TypeChecker.QualifiedGraph
open ExprVisitor

let start = FParsec.Position("", 0, 1, 1)
let stop = FParsec.Position("", 0, 1, 1)
Expand Down Expand Up @@ -269,14 +268,14 @@ let findDepsForValueIdent
| _ -> (true, state)
ExprVisitor.VisitTypeAnnObjElem = fun (_, state) -> (true, state) }

walkExpr visitor () expr
ExprVisitor.walkExpr visitor () expr

postProcessDeps env.Namespace locals localsTree ident (Set.toList idents)

let findInferTypeAnns (typeAnn: TypeAnn) : list<QDeclIdent> =
let mutable idents: list<QDeclIdent> = []

let visitor: SyntaxVisitor<unit> =
let visitor: ExprVisitor.SyntaxVisitor<unit> =
{ ExprVisitor.VisitExpr = fun (_, state) -> (false, state)
ExprVisitor.VisitJsxElement = fun (_, state) -> (false, state)
ExprVisitor.VisitJsxFragment = fun (_, state) -> (false, state)
Expand All @@ -292,7 +291,7 @@ let findInferTypeAnns (typeAnn: TypeAnn) : list<QDeclIdent> =
| _ -> (true, state)
ExprVisitor.VisitTypeAnnObjElem = fun (_, state) -> (true, state) }

walkTypeAnn visitor () typeAnn
ExprVisitor.walkTypeAnn visitor () typeAnn
List.rev idents

let findDepsForTypeIdent
Expand All @@ -306,7 +305,7 @@ let findDepsForTypeIdent

let mutable typeRefIdents: list<QDeclIdent> = []

let visitor: SyntaxVisitor<list<QualifiedIdent>> =
let visitor: ExprVisitor.SyntaxVisitor<list<QualifiedIdent>> =
{ ExprVisitor.VisitExpr = fun (_, state) -> (true, state)
ExprVisitor.VisitJsxElement = fun (_, state) -> (true, state)
ExprVisitor.VisitJsxFragment = fun (_, state) -> (true, state)
Expand Down Expand Up @@ -375,7 +374,8 @@ let findDepsForTypeIdent
(true, typeParams @ newTypeParams) }

match syntaxNode with
| SyntaxNode.TypeAnn typeAnn -> walkTypeAnn visitor typeParams typeAnn
| SyntaxNode.TypeAnn typeAnn ->
ExprVisitor.walkTypeAnn visitor typeParams typeAnn
| SyntaxNode.TypeRef typeRef ->
let ident = QualifiedIdent.FromCommonQualifiedIdent typeRef.Ident

Expand All @@ -384,9 +384,9 @@ let findDepsForTypeIdent
match typeRef.TypeArgs with
| Some typeArgs ->
for typeAnn in typeArgs do
walkTypeAnn visitor typeParams typeAnn
ExprVisitor.walkTypeAnn visitor typeParams typeAnn
| None -> ()
| SyntaxNode.Expr expr -> walkExpr visitor typeParams expr
| SyntaxNode.Expr expr -> ExprVisitor.walkExpr visitor typeParams expr

postProcessDeps
env.Namespace
Expand Down Expand Up @@ -542,8 +542,8 @@ let rec findCaptures

match f.Body with
| BlockOrExpr.Block block ->
List.iter (walkStmt visitor localNames) block.Stmts
| BlockOrExpr.Expr expr -> walkExpr visitor localNames expr
List.iter (ExprVisitor.walkStmt visitor localNames) block.Stmts
| BlockOrExpr.Expr expr -> ExprVisitor.walkExpr visitor localNames expr

captures

Expand Down
51 changes: 25 additions & 26 deletions src/Escalier.TypeChecker/Escalier.TypeChecker.fsproj
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="Error.fs" />
<Compile Include="Prune.fs" />
<Compile Include="Visitor.fs" />
<Compile Include="Folder.fs" />
<Compile Include="Env.fs" />
<Compile Include="Mutability.fs" />
<Compile Include="Poly.fs" />
<Compile Include="Unify.fs" />
<Compile Include="Helpers.fs" />
<Compile Include="QualifiedGraph.fs" />
<Compile Include="BuildGraph.fs" />
<Compile Include="Infer.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Escalier.Data\Escalier.Data.fsproj" />
<ProjectReference Include="..\Escalier.Parser\Escalier.Parser.fsproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharpPlus" Version="1.5.0" />
<PackageReference Include="FsToolkit.ErrorHandling" Version="4.10.0" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="Error.fs"/>
<Compile Include="Prune.fs"/>
<Compile Include="Folder.fs"/>
<Compile Include="Env.fs"/>
<Compile Include="Mutability.fs"/>
<Compile Include="Poly.fs"/>
<Compile Include="Unify.fs"/>
<Compile Include="Helpers.fs"/>
<Compile Include="QualifiedGraph.fs"/>
<Compile Include="BuildGraph.fs"/>
<Compile Include="Infer.fs"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Escalier.Data\Escalier.Data.fsproj"/>
<ProjectReference Include="..\Escalier.Parser\Escalier.Parser.fsproj"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharpPlus" Version="1.5.0"/>
<PackageReference Include="FsToolkit.ErrorHandling" Version="4.10.0"/>
</ItemGroup>
</Project>
20 changes: 11 additions & 9 deletions src/Escalier.TypeChecker/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ open Escalier.Data
open Escalier.Data.Syntax
open Escalier.Data.Common
open Escalier.Data.Type
open Escalier.Data.Visitor

open Error
open Env
open ExprVisitor
open Poly

// TODO: update this function to use the type visitor/folder
Expand Down Expand Up @@ -106,7 +106,7 @@ let findBindingNames (p: Syntax.Pattern) : Set<string> =
ExprVisitor.VisitTypeAnn = fun (_, state) -> (false, state)
ExprVisitor.VisitTypeAnnObjElem = fun (_, state) -> (false, state) }

walkPattern visitor () p
ExprVisitor.walkPattern visitor () p

Set.ofList names

Expand Down Expand Up @@ -137,9 +137,10 @@ let findReturns (body: BlockOrExpr) : list<Expr> =
ExprVisitor.VisitTypeAnnObjElem = fun (_, state) -> (false, state) }

match body with
| BlockOrExpr.Block block -> List.iter (walkStmt visitor ()) block.Stmts
| BlockOrExpr.Block block ->
List.iter (ExprVisitor.walkStmt visitor ()) block.Stmts
| BlockOrExpr.Expr expr ->
walkExpr visitor () expr // There might be early returns in match expression
ExprVisitor.walkExpr visitor () expr // There might be early returns in match expression
returns <- expr :: returns // We treat the expression as a return in this case

returns
Expand All @@ -159,7 +160,7 @@ let maybeWrapInPromise (t: Type) (e: Type) : Type =
let findThrows (body: BlockOrExpr) : list<Type> =
let mutable throws: list<Type> = []

let visitor: SyntaxVisitor<unit> =
let visitor: ExprVisitor.SyntaxVisitor<unit> =
{ ExprVisitor.VisitExpr =
fun (expr, state) ->
match expr.Kind with
Expand Down Expand Up @@ -201,16 +202,17 @@ let findThrows (body: BlockOrExpr) : list<Type> =
ExprVisitor.VisitTypeAnnObjElem = fun (_, state) -> (false, state) }

match body with
| BlockOrExpr.Block block -> List.iter (walkStmt visitor ()) block.Stmts
| BlockOrExpr.Expr expr -> walkExpr visitor () expr
| BlockOrExpr.Block block ->
List.iter (ExprVisitor.walkStmt visitor ()) block.Stmts
| BlockOrExpr.Expr expr -> ExprVisitor.walkExpr visitor () expr

throws

// TODO: dedupe with findThrows
let findThrowsInBlock (block: Block) : list<Type> =
let mutable throws: list<Type> = []

let visitor: SyntaxVisitor<unit> =
let visitor: ExprVisitor.SyntaxVisitor<unit> =
{ ExprVisitor.VisitExpr =
fun (expr, state) ->
match expr.Kind with
Expand Down Expand Up @@ -251,7 +253,7 @@ let findThrowsInBlock (block: Block) : list<Type> =
ExprVisitor.VisitTypeAnn = fun (_, state) -> (false, state)
ExprVisitor.VisitTypeAnnObjElem = fun (_, state) -> (false, state) }

List.iter (walkStmt visitor ()) block.Stmts
List.iter (ExprVisitor.walkStmt visitor ()) block.Stmts

throws

Expand Down
19 changes: 10 additions & 9 deletions src/Escalier.TypeChecker/Infer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open Escalier.Data
open Escalier.Data.Common
open Escalier.Data.Syntax
open Escalier.Data.Type
open Escalier.Data.Visitor

open Error
open Prune
Expand All @@ -16,7 +17,6 @@ open Poly
open Unify
open Helpers
open BuildGraph
open ExprVisitor

module rec Infer =
let rec patternToPattern (pat: Syntax.Pattern) : Pattern =
Expand Down Expand Up @@ -412,8 +412,8 @@ module rec Infer =
let mutable assignedProps: list<string> = []
let mutable methodsCalled: list<string> = []

let visitor: SyntaxVisitor<unit> =
{ VisitExpr =
let visitor: ExprVisitor.SyntaxVisitor<unit> =
{ ExprVisitor.VisitExpr =
fun (expr: Expr, state) ->
match expr.Kind with
| ExprKind.Assign { Left = left } ->
Expand All @@ -439,14 +439,15 @@ module rec Infer =
ExprVisitor.VisitJsxElement = fun (_, state) -> (true, state)
ExprVisitor.VisitJsxFragment = fun (_, state) -> (true, state)
ExprVisitor.VisitJsxText = fun (_, state) -> (false, state)
VisitStmt = fun (_, state) -> (true, state)
VisitPattern = fun (_, state) -> (false, state)
VisitTypeAnn = fun (_, state) -> (false, state)
VisitTypeAnnObjElem = fun (_, state) -> (false, state) }
ExprVisitor.VisitStmt = fun (_, state) -> (true, state)
ExprVisitor.VisitPattern = fun (_, state) -> (false, state)
ExprVisitor.VisitTypeAnn = fun (_, state) -> (false, state)
ExprVisitor.VisitTypeAnnObjElem =
fun (_, state) -> (false, state) }

match body with
| BlockOrExpr.Block block ->
List.iter (walkStmt visitor ()) block.Stmts
List.iter (ExprVisitor.walkStmt visitor ()) block.Stmts
| BlockOrExpr.Expr _expr -> failwith "TODO"

if not methodsCalled.IsEmpty then
Expand Down Expand Up @@ -3126,7 +3127,7 @@ module rec Infer =
ExprVisitor.VisitTypeAnn = fun (_, state) -> (false, state)
ExprVisitor.VisitTypeAnnObjElem = fun (_, state) -> (false, state) }

walkPattern visitor () pattern
ExprVisitor.walkPattern visitor () pattern

result

Expand Down
1 change: 1 addition & 0 deletions src/Escalier.TypeChecker/Poly.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open FsToolkit.ErrorHandling

open Escalier.Data.Type
open Escalier.Data.Common
open Escalier.Data.Visitor

open Error
open Folder
Expand Down
6 changes: 3 additions & 3 deletions src/Escalier.TypeChecker/QualifiedGraph.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module Escalier.TypeChecker.QualifiedGraph
open Escalier.Data.Syntax
open FsToolkit.ErrorHandling

open Escalier.Data.Type
open Escalier.Data
open Escalier.Data.Type
open Escalier.Data.Visitor

open Env
open Error
open ExprVisitor

type QualifiedIdent =
{ Parts: list<string> }
Expand Down Expand Up @@ -109,7 +109,7 @@ let findFunctions (expr: Syntax.Expr) : list<Syntax.Function> =
ExprVisitor.VisitTypeAnn = fun (_, state) -> (false, state)
ExprVisitor.VisitTypeAnnObjElem = fun (_, state) -> (false, state) }

walkExpr visitor () expr
ExprVisitor.walkExpr visitor () expr

List.rev fns

Expand Down
Loading

0 comments on commit 89ef0ca

Please sign in to comment.