Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
feat: a basic visual programming linter
Browse files Browse the repository at this point in the history
  • Loading branch information
prescientmoon committed Jul 9, 2020
1 parent cf58c65 commit 3574486
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Data/Dataflow/Expression.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ module Lunarbox.Data.Dataflow.Expression
, wrappers
, everywhereOnExpressionM
, everywhereOnExpression
, foldExpression
) where

import Prelude
import Control.Monad.Writer (execWriter, tell)
import Data.Identity (Identity(..))
import Data.List (List(..), foldr, (:))
import Data.Map as Map
Expand Down Expand Up @@ -89,6 +91,10 @@ everywhereOnExpressionM f = go'
everywhereOnExpression :: forall l. (Expression l -> Expression l) -> Expression l -> Expression l
everywhereOnExpression mapper = unwrap <<< everywhereOnExpressionM (Identity <<< mapper)

-- | Accumulate a value running trough all the layers of an expression
foldExpression :: forall l m. Monoid m => (Expression l -> m) -> Expression l -> m
foldExpression f = execWriter <<< everywhereOnExpressionM (\expr -> tell (f expr) $> expr)

-- Takes a list of argument names and a body and creates the body of a function
functionDeclaration :: forall l. l -> Expression l -> List VarName -> Expression l
functionDeclaration = foldr <<< Lambda
Expand Down
28 changes: 28 additions & 0 deletions src/Data/Dataflow/Expression/Lint.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Lunarbox.Data.Dataflow.Expression.Lint where

import Prelude
import Data.Array as Array
import Lunarbox.Data.Dataflow.Expression (Expression(..), VarName, foldExpression)

data LintError l
= UnusedDeclaration VarName l

-- | Collect linting errors inside an expression
lint :: forall l. Expression l -> Array (LintError l)
lint = foldExpression go
where
go (Let location name _ body)
| Array.null (references name body) =
[ UnusedDeclaration name location
]

go a = []

-- | Get all the places a variable is referenced in
references :: forall l. VarName -> Expression l -> Array l
references target = foldExpression go
where
go (Variable location name)
| target == name = [ location ]

go _ = []

0 comments on commit 3574486

Please sign in to comment.