commit 6f43623

shrub  ·  2026-10-15 15:37:26 +0000 UTC
parent 60b84ac
expand macros, better default target
2 files changed,  +49, -21
+7, -5
 1@@ -1,7 +1,9 @@
 2-all: test
 3+MESSAGE = test
 4 
 5-test: poop 
 6-	echo test
 7+poo: 
 8+	echo $(MESSAGE)
 9 
10-poop:
11-	echo poop
12+pee:
13+	echo pee
14+
15+all: poo
+42, -16
 1@@ -96,6 +96,29 @@ collectAssigns (Assignment text : rest) = parseAssign text (collectAssigns rest)
 2 -- if not assignment ignore
 3 collectAssigns (_ : rest) = collectAssigns rest
 4 
 5+expandMacro :: Map.Map String String -> String -> String
 6+expandMacro _ [] = []
 7+-- we found a macro
 8+expandMacro macros ('$':'(':rest) = 
 9+    let (name, afterName) = break (== ')') rest
10+    in case afterName of
11+        ')':after ->
12+            -- search for the macro
13+            case Map.lookup name macros of 
14+               Just value -> 
15+                   -- expand the macro's value and the remaining text
16+                   expandMacro macros value ++ expandMacro macros after
17+               Nothing ->
18+                   -- replace with an empty string and expand the rest
19+                   expandMacro macros after
20+        -- for a case where theres no closing )
21+        _ -> "$(" ++ rest
22+expandMacro macros (char:rest) = char : expandMacro macros rest
23+
24+expandLine :: Map.Map String String -> TypedLine -> TypedLine
25+expandLine macros (Statement text) = Statement (expandMacro macros text)
26+expandLine macros (Recipe    text) = Recipe    (expandMacro macros text)
27+expandLine _ line = line
28 
29 findRule :: String -> [Rule] -> Maybe Rule
30 findRule name rules = find (\rule -> ruleName rule == name) rules
31@@ -139,20 +162,23 @@ isLeaf rule = null (rulePrereqs rule) && null (ruleRecipes rule)
32 main :: IO ()
33 main = do
34     mkf <- readFile "Makefile"
35-    let rawLines    = lines mkf
36-    let parsedLines = map parseLine rawLines
37-    let macros      = collectAssigns parsedLines
38-    -- xxx dont print em use em!
39-    print macros
40-    case parseRules parsedLines of
41+    let rawLines      = lines mkf
42+    let parsedLines   = map parseLine rawLines
43+    let macros        = collectAssigns parsedLines
44+    let expandedLines = map (expandLine macros) parsedLines
45+    case parseRules expandedLines of
46         Left message -> putStrLn ("parse error: " ++ message)
47-        Right rules  -> do
48-            -- xxx cli option for requested target 
49-            let order = (orderTarget rules "all")
50-            let leafs = filter isLeaf order
51-            let leafNames = map ruleName leafs
52-            exists <- mapM doesFileExist leafNames
53-            let recipes = concatMap ruleRecipes order
54-            if and exists
55-                then mapM_ runRecipe recipes
56-                else putStrLn "a leaf node is missing"
57+        Right rules  ->  
58+            case rules of 
59+                [] -> putStrLn "no targets" 
60+                firstRule: _ -> do
61+                -- xxx cli option for requested target
62+                    let wanted    = ruleName firstRule 
63+                    let order     = (orderTarget rules wanted)
64+                    let leafs     = filter isLeaf order
65+                    let leafNames = map ruleName leafs
66+                    exists <- mapM doesFileExist leafNames
67+                    let recipes = concatMap ruleRecipes order
68+                    if and exists
69+                        then mapM_ runRecipe recipes
70+                        else putStrLn "a leaf node is missing"