diff --git a/example/1.hear b/example/1.hear index 210d2c9..62c2d80 100644 --- a/example/1.hear +++ b/example/1.hear @@ -1 +1 @@ -print 5*3-2+-4-4 \ No newline at end of file +(5*(3-2)+-4-4) \ No newline at end of file diff --git a/src/Main.hs b/src/Main.hs index 7c4d935..13e893f 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -8,7 +8,7 @@ import Data.Text.IO qualified as T import Main.LLVMGen import Main.Parser.Megaparsec import System.Environment -import System.Process +import System.Process (callCommand) main :: IO () main = do diff --git a/src/Main/LLVMGen.hs b/src/Main/LLVMGen.hs index 38fe242..818a79f 100644 --- a/src/Main/LLVMGen.hs +++ b/src/Main/LLVMGen.hs @@ -35,6 +35,7 @@ exprToLLVM :: Expr -> m Operand exprToLLVM (Lit prim) = pure $ primToLLVM prim +exprToLLVM (Paren e) = exprToLLVM e exprToLLVM (Expr.Add a b) = mdo lhs <- exprToLLVM a rhs <- exprToLLVM b diff --git a/src/Main/Parser/Megaparsec.hs b/src/Main/Parser/Megaparsec.hs index 931038d..6178a9d 100644 --- a/src/Main/Parser/Megaparsec.hs +++ b/src/Main/Parser/Megaparsec.hs @@ -28,8 +28,15 @@ int = lexeme $ L.signed (return ()) L.decimal string :: Text -> Parser Text string = C.string +container :: Text -> Text -> Parser a -> Parser a +container b e = between (symbol b) (symbol e) + term :: Parser Expr -term = Lit <$> int +term = + choice + [ Lit <$> int, + container "(" ")" expr + ] table :: [[Operator Parser Expr]] table = diff --git a/src/Main/Types/Expr.hs b/src/Main/Types/Expr.hs index 9fc0b32..c91cfb7 100644 --- a/src/Main/Types/Expr.hs +++ b/src/Main/Types/Expr.hs @@ -2,6 +2,7 @@ module Main.Types.Expr (Expr (..)) where data Expr = Lit Int + | Paren Expr | Add Expr Expr | Sub Expr Expr | Mul Expr Expr