From c4351553cb22b0cb861abf833b33f67f8598a40f Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 11 Jun 2019 12:29:09 -0400 Subject: [PATCH] Compile IfStatement. --- semantic-python/src/Language/Python/Core.hs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/semantic-python/src/Language/Python/Core.hs b/semantic-python/src/Language/Python/Core.hs index 2a3acb6cf..102004ff7 100644 --- a/semantic-python/src/Language/Python/Core.hs +++ b/semantic-python/src/Language/Python/Core.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE DefaultSignatures #-} +{-# LANGUAGE DefaultSignatures, RecordWildCards #-} module Language.Python.Core ( compile ) where @@ -24,5 +24,18 @@ instance Compile Py.Module where compile (Module Nothing) = pure Unit compile (Module (Just statements)) = block <$> traverse compile statements -instance Compile Py.CompoundStatement +instance Compile Py.CompoundStatement where + compile (IfStatementCompoundStatement statement) = compile statement + compile other = defaultCompile other + +instance Compile Py.IfStatement where + compile IfStatement{..} = If <$> compile condition <*> compile consequence <*> case alternative of + Nothing -> pure Unit + Just clauses -> foldr clause (pure Unit) clauses + where clause (Left (ElifClause{..})) rest = If <$> compile condition <*> compile consequence <*> rest + clause (Right (ElseClause body)) _ = compile body + +instance Compile Py.Expression +instance Compile Py.Block + instance Compile Py.SimpleStatement