Skip to content

Commit

Permalink
fix function return types (broke during rebase)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemastero committed Dec 18, 2023
1 parent e4de243 commit aa6d090
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
8 changes: 6 additions & 2 deletions src/Agda/Compiler/Rust/PrettyPrintingUtils.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Agda.Compiler.Rust.PrettyPrintingUtils (
argList,
bracket,
indent,
exprSeparator,
defsSeparator,
exprSeparator,
funReturnTypeSeparator,
indent,
typeSeparator
) where

Expand All @@ -24,3 +25,6 @@ defsSeparator = "\n"

typeSeparator :: String
typeSeparator = ":"

funReturnTypeSeparator :: String
funReturnTypeSeparator = "->"
23 changes: 13 additions & 10 deletions src/Agda/Compiler/Rust/ToRustCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import qualified Data.List.NonEmpty as Nel
import Agda.Compiler.Backend ( IsMain )
import Agda.Syntax.Abstract.Name ( QName )
import Agda.Syntax.Common.Pretty ( prettyShow )
import Agda.Syntax.Common ( Arg(..), Named(..), ArgName )
import Agda.Syntax.Common ( Arg(..), ArgName, Named(..), moduleNameParts )
import Agda.Syntax.Internal (
Clause(..), DeBruijnPattern, DBPatVar(..), Dom(..), unDom, PatternInfo(..), Pattern'(..),
qnameName, qnameModule, Telescope, Tele(..), Term(..), Type, Type''(..) )
Expand All @@ -22,9 +22,10 @@ import Agda.Compiler.Rust.CommonTypes ( Options, CompiledDef, ModuleEnv )
import Agda.Compiler.Rust.PrettyPrintingUtils (
argList,
bracket,
indent,
exprSeparator,
defsSeparator,
exprSeparator,
funReturnTypeSeparator,
indent,
typeSeparator )

compile :: Options -> ModuleEnv -> IsMain -> Definition -> TCM CompiledDef
Expand Down Expand Up @@ -64,10 +65,10 @@ compileFunction defName funDef fc =
<> showName defName
<> argList (
-- TODO handle multiple function clauses and arguments
compileFunctionArgument (head fc)
compileFunctionArgument fc
<> typeSeparator <> exprSeparator
<> compileFunctionArgType (head fc) )
<> typeSeparator <> exprSeparator <> compileFunctionResultType (head fc)
<> compileFunctionArgType fc )
<> exprSeparator <> funReturnTypeSeparator <> exprSeparator <> compileFunctionResultType fc
<> exprSeparator <> bracket (
-- TODO proper indentation for every line of function body
-- including nested expressions
Expand All @@ -86,8 +87,9 @@ compileFunctionArgument [] = ""
compileFunctionArgument [fc] = fromDeBruijnPattern (namedThing (unArg (head (namedClausePats fc))))

Check warning on line 87 in src/Agda/Compiler/Rust/ToRustCompiler.hs

View workflow job for this annotation

GitHub Actions / agda2rust

In the use of ‘head’
compileFunctionArgument xs = error "unsupported compileFunctionArgument" ++ (show xs)

compileFunctionArgType :: Clause -> CompiledDef
compileFunctionArgType Clause{clauseTel = ct} = fromTelescope ct
compileFunctionArgType :: [Clause] -> CompiledDef
compileFunctionArgType [ Clause{clauseTel = ct} ] = fromTelescope ct
compileFunctionArgType xs = error "unsupported compileFunctionArgType" ++ (show xs)

fromTelescope :: Telescope -> CompiledDef
fromTelescope = \case
Expand All @@ -97,8 +99,9 @@ fromTelescope = \case
fromDom :: Dom Type -> CompiledDef
fromDom x = fromType (unDom x)

compileFunctionResultType :: Clause -> CompiledDef
compileFunctionResultType Clause{clauseType = ct} = fromMaybeType ct
compileFunctionResultType :: [Clause] -> CompiledDef
compileFunctionResultType [Clause{clauseType = ct}] = fromMaybeType ct
compileFunctionResultType other = error ("unhandled compileFunctionResultType" ++ show other)

fromMaybeType :: Maybe (Arg Type) -> CompiledDef
fromMaybeType (Just argType) = fromArgType argType
Expand Down
13 changes: 4 additions & 9 deletions test/Hello.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
mod hello {

pub enum TheRgb {
mod test {
enum TheRgb {
Red, Green, Blue
}



pub fn id_rgb(rgb_arg: TheRgb) -> TheRgb {
return rgb_arg
pub fn idRgb(rgbArg: TheRgb) -> TheRgb {
return rgbArg
}

enum TheWeekDay {
Expand All @@ -20,9 +19,5 @@ enum TheWeekDay {



pub fn asFriday(rgbArg: TheRgb) -> TheWeekDay {
return rgbArg
}


}
8 changes: 4 additions & 4 deletions test/Hello.agda → test/hello.agda
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Hello where
module test.hello where

-- simple record type
data TheRgb : Set where
Expand All @@ -14,9 +14,9 @@ data TheWeekDay : Set where
Monday Tuesday Wednesday Thursday Friday Saturday Sunday : TheWeekDay
{-# COMPILE AGDA2RUST TheWeekDay #-}

asFriday : TheRgb TheWeekDay
asFriday rgbArg = Friday -- TODO compile body
{-# COMPILE AGDA2RUST asFriday #-}
-- asFriday : TheRgb → TheWeekDay
-- asFriday rgbArg = Friday -- TODO compile body
-- {-# COMPILE AGDA2RUST asFriday #-}

-- TODO multiple clauses
-- day-color : TheWeekDay → TheRgb
Expand Down

0 comments on commit aa6d090

Please sign in to comment.