Skip to content

Commit

Permalink
add function calls to expr parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ALescoulie committed Mar 6, 2024
1 parent eb9d0f1 commit b26c186
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/Mentat/SyntaxParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ shuntingYard (toT : restT) exprs ops = case toT of
TLeaf (TOp op) -> do
(mergedExprs, mergedOps) <- combineExprs exprs ops $ Just op
shuntingYard restT mergedExprs mergedOps
TNode node innerTok -> do
TNode _ innerTok -> do
innerExpr <- shuntingYard innerTok [] []
shuntingYard restT (innerExpr : exprs) ops
TFxn name args -> do -- if you encounter a fxn
argExprs <- mapM (\x -> shuntingYard x [] []) args
shuntingYard restT (FxnE name argExprs : exprs) ops
_ -> Left EmptyExpr


Expand Down
7 changes: 4 additions & 3 deletions test/SyntaxParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import Mentat.ParseTypes
import Mentat.SyntaxParser
import Prelude hiding (lex)
import Test.Hspec
import Mentat.ParseTypes (Statment(Constraint))

spec :: Spec
spec = do
describe "Testing whole program parsing" $ do
let pg1 = ["x := 1", "y := 2", "f(n) := 2 * n", "2 = x * y"]
let pg1 = ["x := 1", "y := 2", "f(n) := 2 * n", "2 = x * y", "f(2x)"]
let out1 =
Program
[ Declaration "x" (LitE $ RL 1)
, Declaration "y" (LitE $ RL 2)
, Fxn $ Function "f" ["n"] (BinOpE Mul (VarE "n") (LitE $ RL 2))
, Constraint
(BinOpE Eql (BinOpE Mul (VarE "y") (VarE "x")) (LitE $ RL 2))
, Constraint (BinOpE Eql (BinOpE Mul (VarE "y") (VarE "x")) (LitE $ RL 2))
, Constraint $ FxnE "f" [(BinOpE Mul (VarE "x") (LitE $ RL 2))]
]

let maybePg1 = parseProgram pg1
Expand Down

0 comments on commit b26c186

Please sign in to comment.