Skip to content

Commit

Permalink
Merge pull request #183 from EpitechPromo2027/182-relative-imports-do…
Browse files Browse the repository at this point in the history
…-not-work

fix: relative imports are now relative to `SourceFile`
  • Loading branch information
oriollinan authored Jan 13, 2025
2 parents c08307b + 7300767 commit 97ea3be
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
12 changes: 11 additions & 1 deletion examples/array.ff
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
printf: foreign *byte ... -> int
%%
% Hello world with arguments
%%

%%
% Standard Library
%%
import "std/io.ff"

%%
% Main function
%%
main: int **byte -> int = argc argv {
printf("Hello, %s!\n" argv.#(argc - 1))
}
2 changes: 1 addition & 1 deletion examples/hello.ff
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%%
% Standard Library
%%
import "./examples/std/io.ff"
import "std/io.ff"

%%
% Main function
Expand Down
6 changes: 3 additions & 3 deletions examples/julia.ff
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
%%
% Standard Library
%%
import "./examples/std/io.ff"
import "./examples/std/math.ff"
import "./examples/std/uni.ff"
import "std/io.ff"
import "std/math.ff"
import "std/uni.ff"

%%
% Function to clear the screen
Expand Down
2 changes: 1 addition & 1 deletion examples/mandelbrot.ff
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%%
% Standard Library
%%
import "./examples/std/io.ff"
import "std/io.ff"

%%
% Function to render the Mandelbrot set
Expand Down
6 changes: 3 additions & 3 deletions examples/putstr.ff
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
%%
% Standard Library
%%
import "./examples/std/io.ff"
import "./examples/std/string.ff"
import "./examples/std/uni.ff"
import "std/io.ff"
import "std/string.ff"
import "std/uni.ff"

%%
% Function to write a string to stdout
Expand Down
2 changes: 1 addition & 1 deletion examples/sierpinski.ff
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%%
% Standard Library
%%
import "./examples/std/io.ff"
import "std/io.ff"

%%
% Draw the Sierpinski Triangle
Expand Down
6 changes: 3 additions & 3 deletions examples/sine.ff
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
%%
% Standard Library
%%
import "./examples/std/io.ff"
import "./examples/std/math.ff"
import "./examples/std/uni.ff"
import "std/io.ff"
import "std/math.ff"
import "std/uni.ff"

%%
% Function to draw the sine wave
Expand Down
19 changes: 10 additions & 9 deletions lib/Ast/Parser/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import qualified Data.ByteString.Char8 as BS
import qualified Data.CaseInsensitive as CI
import qualified Network.HTTP.Simple as N
import qualified System.Environment as E
import qualified System.IO.Error as IOE
import qualified Text.Megaparsec as M

parseImport :: PU.Parser String -> PU.Parser String
parseImport p = do
parseImport :: String -> PU.Parser String -> PU.Parser String
parseImport sourceFile p = do
import' <- PU.symbol "import" *> M.between (PU.symbol "\"") (PU.symbol "\"") (M.some $ M.anySingleBut '\"')
state <- S.get
let visited = PS.lookupImport import' state
Expand All @@ -25,9 +26,7 @@ parseImport p = do
else do
S.modify $ PS.insertImport import'
S.modify $ PS.setImportDepth $ depth + 1
source <- case import' of
('.' : _) -> IO.liftIO $ localImport import'
_ -> IO.liftIO $ externalImport import'
source <- IO.liftIO $ IOE.catchIOError (localImport sourceFile import') (\_ -> externalImport import')
input <- M.getInput
M.setInput $ source ++ input
source' <- p
Expand All @@ -36,8 +35,10 @@ parseImport p = do
where
maxDepth = 25

localImport :: String -> IO String
localImport = readFile
localImport :: String -> String -> IO String
localImport sourceFile import' = readFile $ dir sourceFile ++ import'
where
dir = reverse . dropWhile (/= '/') . reverse

externalImport :: String -> IO String
externalImport url = do
Expand All @@ -46,7 +47,7 @@ externalImport url = do
Just token ->
N.setRequestHeaders
[(CI.mk $ BS.pack "Authorization", BS.pack $ "Bearer " ++ token)]
<$> N.parseRequest url
Nothing -> N.parseRequest url
<$> N.parseRequestThrow url
Nothing -> N.parseRequestThrow url
res <- N.httpBS req
return $ BS.unpack $ N.getResponseBody res
8 changes: 4 additions & 4 deletions lib/Ast/Parser/Program.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ parseProgram :: String -> PU.Parser AT.Program
parseProgram sourceFile = do
_ <- PU.sc
S.modify $ PS.insertImport sourceFile
source <- preprocess
source <- preprocess sourceFile
M.setInput source
components <- M.many $ M.choice [M.try parseTypeDefinition, parseExpr]
return $ AT.Program (concatMap AT.globals components) (concatMap AT.types components) sourceFile

preprocess :: PU.Parser String
preprocess = do
sources <- M.many $ M.choice [PI.parseImport preprocess, (: []) <$> M.anySingle]
preprocess :: String -> PU.Parser String
preprocess sourceFile = do
sources <- M.many $ M.choice [PI.parseImport sourceFile (preprocess sourceFile), (: []) <$> M.anySingle]
return $ concat sources

parseTypeDefinition :: PU.Parser AT.Program
Expand Down

0 comments on commit 97ea3be

Please sign in to comment.