The 'relative-include' macro works.

This commit is contained in:
Erik Svedäng 2018-03-13 09:36:18 +01:00
parent 139fee6d9f
commit 832212d3fe
4 changed files with 45 additions and 18 deletions

View File

@ -223,4 +223,7 @@
;; Allows inclusion of C headers relative to the Carp file in which this macro is called.
(defmacro relative-include [file]
(list 'local-include (Dynamic.String.join [(Dynamic.String.directory (source-path)) "/" file])))
(list 'local-include
(list 'Dynamic.String.join [(list 'Dynamic.String.directory(list 'source-path))
"/"
file])))

View File

@ -2,17 +2,3 @@
;; (load "Debug.carp")
;; (Debug.sanitize-addresses)
;; (Project.config "print-ast" true)
(def c 1)
(def b c)
(def a b)
(def x b)
(defn f []
x)
(def d (f))
;;(defn main [] (println* &[a b c]))
(macro-error (source-dir))

View File

@ -363,8 +363,15 @@ executeCommand ctx@(Context env typeEnv pathStrings proj lastInput execMode) cmd
-- constitutes a 'def' or 'defn'. So let's evaluate again
-- to make it stick in the environment.
-- To log the intermediate result:
--putStrLnWithColor Yellow ("-> " ++ (pretty evaled))
(result', newCtx') <- runStateT (eval env evaled) newCtx
-- putStrLnWithColor Yellow ("-> " ++ (pretty evaled))
-- Replace info so that macros called at the top-level get the location of the expansion site.
let evaledWithNewInfo =
case info xobj of
Just i -> replaceSourceInfo (infoFile i) (infoLine i) (infoColumn i) evaled
Nothing -> xobj
(result', newCtx') <- runStateT (eval env evaledWithNewInfo) newCtx
case result' of
Left e ->
do putStrLnWithColor Red (show e)

View File

@ -1,4 +1,4 @@
module Expand (expandAll) where
module Expand (expandAll, replaceSourceInfo) where
import Control.Monad.State.Lazy (StateT(..), runStateT, liftIO, modify, get, put)
import Control.Monad.State
@ -175,3 +175,34 @@ setNewIdentifiers root = let final = evalState (visit root) 0
case info xobj of
Just i -> return (xobj { info = Just (i { infoIdentifier = counter })})
Nothing -> return xobj
-- | Replaces the file, line and column info on an XObj an all its children.
replaceSourceInfo :: FilePath -> Int -> Int -> XObj -> XObj
replaceSourceInfo newFile newLine newColumn root = visit root
where
visit :: XObj -> XObj
visit xobj =
case obj xobj of
(Lst _) -> visitList xobj
(Arr _) -> visitArray xobj
_ -> setNewInfo xobj
visitList :: XObj -> XObj
visitList (XObj (Lst xobjs) i t) =
setNewInfo (XObj (Lst (map visit xobjs)) i t)
visitList _ =
error "The function 'visitList' only accepts XObjs with lists in them."
visitArray :: XObj -> XObj
visitArray (XObj (Arr xobjs) i t) =
setNewInfo (XObj (Arr (map visit xobjs)) i t)
visitArray _ = error "The function 'visitArray' only accepts XObjs with arrays in them."
setNewInfo :: XObj -> XObj
setNewInfo xobj =
case info xobj of
Just i -> (xobj { info = Just (i { infoFile = newFile
, infoLine = newLine
, infoColumn = newColumn
})})
Nothing -> xobj