forked from axellang/axel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add quoting and continue macro expansion
- Loading branch information
Joshua Grosso
committed
Jan 9, 2018
1 parent
e7b7a48
commit be28da9
Showing
16 changed files
with
138 additions
and
126 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 |
---|---|---|
|
@@ -20,3 +20,5 @@ cabal.sandbox.config | |
cabal.project.local | ||
.HTF/ | ||
.ghc.environment.* | ||
|
||
resources/autogenerated/* |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
%%%MACRO_DEFINITION%%% | ||
|
||
main :: IO () | ||
main = aUTOGENERATED_MACRO_FUNCTION %%%%ARGUMENTS%%% >>= print | ||
main = %%%MACRO_NAME%%% %%%ARGUMENTS%%% >>= print |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
set -eu | ||
set -o pipefail | ||
|
||
echo "Cleaning autogenerated resources..." | ||
rm -rf resources/autogenerated | ||
echo "Autogenerated resources cleaned!" | ||
|
||
stack clean |
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 |
---|---|---|
@@ -1,57 +1,84 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module Lihsp.Macros where | ||
|
||
import Control.Lens.Operators ((.~)) | ||
import Control.Monad.Except (MonadError) | ||
import Control.Lens.Operators ((^.)) | ||
import Control.Monad.Except (MonadError, throwError) | ||
import Control.Monad.IO.Class (MonadIO, liftIO) | ||
|
||
import Data.Function ((&)) | ||
import Data.Semigroup ((<>)) | ||
import Data.Text (pack, replace, unpack) | ||
|
||
import Lihsp.AST (Expression, MacroDefinition, name, traverseExpression) | ||
import Lihsp.AST (name) | ||
import qualified Lihsp.AST as AST (MacroDefinition) | ||
import Lihsp.Error (Error(MacroError)) | ||
import Lihsp.Eval (evalSource) | ||
import qualified Lihsp.Parse as Parse (Expression) | ||
import Lihsp.Parse (parseProgram) | ||
import qualified Lihsp.Parse as Parse | ||
( Expression(LiteralChar, LiteralInt, SExpression, Symbol) | ||
, parseProgram | ||
) | ||
import Lihsp.Utils.Recursion (Recursive(bottomUpTraverse)) | ||
import Lihsp.Utils.Resources (readDataFile) | ||
import Lihsp.Utils.String (replace) | ||
|
||
generateMacroProgram :: | ||
(MonadIO m) => MacroDefinition -> [Expression] -> m String | ||
generateMacroProgram macroDefinition applicationArguments = do | ||
fileHeader <- liftIO $ readDataFile "resources/macros/Header.hs" | ||
fileFooter <- liftIO getFileFooter | ||
return $ fileHeader <> show fileContents <> fileFooter | ||
(MonadIO m) => AST.MacroDefinition -> [Parse.Expression] -> m String | ||
generateMacroProgram macroDefinition applicationArguments = | ||
(<>) <$> liftIO getFileHeader <*> liftIO getFileFooter | ||
where | ||
getFileHeader = readDataFile "autogenerated/macros/Header.hs" | ||
getFileFooter = | ||
let insertApplicationArguments = | ||
let applicationArgumentsPlaceholder = "%%%ARGUMENTS%%%" | ||
in unpack . | ||
replace | ||
in replace | ||
applicationArgumentsPlaceholder | ||
(pack $ show applicationArguments) . | ||
pack | ||
(show applicationArguments) | ||
insertDefinitionBody = | ||
let definitionBodyPlaceholder = "%%%MACRO_DEFINITION%%%" | ||
in unpack . | ||
replace definitionBodyPlaceholder (pack $ show macroDefinition) . | ||
pack | ||
in insertApplicationArguments . insertDefinitionBody <$> | ||
readDataFile "resources/macros/Footer.hs" | ||
in replace definitionBodyPlaceholder (show macroDefinition) | ||
insertDefinitionName = | ||
let definitionNamePlaceholder = "%%%MACRO_NAME%%%" | ||
in replace definitionNamePlaceholder (macroDefinition ^. name) | ||
in insertApplicationArguments . | ||
insertDefinitionName . insertDefinitionBody <$> | ||
readDataFile "macros/Footer.hs" | ||
|
||
expandMacroCall :: | ||
expandMacros :: | ||
(MonadError Error m, MonadIO m) | ||
=> MacroDefinition | ||
-> [Expression] | ||
-> m [Parse.Expression] | ||
expandMacroCall macroDefinition args = | ||
generateMacroProgram macroDefinition args >>= evalSource >>= parseProgram | ||
=> [AST.MacroDefinition] | ||
-> Parse.Expression | ||
-> m Parse.Expression | ||
expandMacros environment = | ||
bottomUpTraverse $ \expression -> | ||
case expression of | ||
Parse.LiteralChar _ -> return expression | ||
Parse.LiteralInt _ -> return expression | ||
Parse.SExpression functionApplication -> | ||
lookupMacroDefinition environment (head functionApplication) >>= \case | ||
Just macroDefinition -> | ||
Parse.SExpression <$> | ||
expandMacroApplication macroDefinition (tail functionApplication) | ||
Nothing -> return expression | ||
Parse.Symbol _ -> return expression | ||
where | ||
expandMacroApplication macroDefinition args = | ||
generateMacroProgram macroDefinition args >>= evalSource >>= | ||
Parse.parseProgram | ||
|
||
expandMacros :: Expression -> IO Expression | ||
expandMacros = | ||
traverseExpression $ \expression -> | ||
case macroApplication expression of | ||
Just (macroName, arguments) -> expandMacroCall | ||
Nothing -> return expression | ||
lookupMacroDefinition :: | ||
(MonadError Error m) | ||
=> [AST.MacroDefinition] | ||
-> Parse.Expression | ||
-> m (Maybe AST.MacroDefinition) | ||
lookupMacroDefinition environment identifierExpression = | ||
case identifierExpression of | ||
Parse.LiteralChar _ -> return Nothing | ||
Parse.LiteralInt _ -> return Nothing | ||
Parse.SExpression _ -> return Nothing | ||
Parse.Symbol identifier -> | ||
case filter | ||
(\macroDefinition -> macroDefinition ^. name == identifier) | ||
environment of | ||
[] -> return Nothing | ||
[macroDefinition] -> return $ Just macroDefinition | ||
_ -> throwError (MacroError "0012") |
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.