Skip to content

Commit 7e57a97

Browse files
committed
Check command and raw mode for files
1 parent 86e6e9d commit 7e57a97

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

app/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Data.List ( isPrefixOf )
1010
import System.Environment ( getArgs )
1111

1212
import Lam.Context ( emptyContext )
13-
import Lam.Handler ( replWrapper, handleFile )
13+
import Lam.Handler ( replWrapper, handleFileWrapper )
1414
import Lam.Result
1515

1616
setup :: IO ()
@@ -27,7 +27,7 @@ main = do
2727
let flagSet = [Untyped | "--untyped" `elem` flags]
2828
case nonFlags of
2929
[] -> run replWrapper flagSet
30-
[fName] -> run (handleFile fName) flagSet
30+
[fName] -> run (handleFileWrapper fName) flagSet
3131
_ -> error wrongUsageMsg
3232
where
3333
wrongUsageMsg :: String

src/Lam/Agda/Lam/Data.agda

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ data Command : Set where
131131
TypedefC : Id × RawTypeL Command
132132
DefineC : Id × RawExpr Command
133133
EvalC : RawExpr Command
134+
CheckC : RawExpr Command
134135
LoadC : Id Command
135136
ReadC : Id Command
136137
ExitC : Command

src/Lam/Handler.hs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{-# LANGUAGE RankNTypes #-}
55

66
module Lam.Handler ( replWrapper
7-
, handleFile
7+
, handleFileWrapper
88
) where
99

1010
import Control.Monad.RWS ( get, put )
@@ -57,9 +57,23 @@ handleEval rExpr = do
5757
else
5858
case typeCheck expr of
5959
Nothing -> throwError "Typing error."
60-
Just t ->
60+
Just _ ->
6161
let normalizedExpr = eval expr in
62-
let msg = untypedPrettyPrint normalizedExpr <> " :: " <> prettyPrintType t in
62+
let msg = untypedPrettyPrint normalizedExpr in
63+
liftIO (putStrLnFlush msg)
64+
65+
handleCheck :: RawExpr -> Result ()
66+
handleCheck rExpr = do
67+
gctx <- get
68+
expr <- liftEither (eraseNames gctx rExpr)
69+
isUntyped <- askUntyped
70+
if isUntyped then
71+
throwError "Trying to check type of an expr in an untyped context."
72+
else
73+
case typeCheck expr of
74+
Nothing -> throwError "Typing error."
75+
Just t ->
76+
let msg = prettyPrintType t in
6377
liftIO (putStrLnFlush msg)
6478

6579
handleCommand :: Command -> Result ()
@@ -68,12 +82,13 @@ handleCommand c =
6882
TypedefC (macroName, macroType) -> handleTypedef macroName macroType
6983
DefineC (macroName, macroExpr) -> handleDefine macroName macroExpr
7084
EvalC rExpr -> handleEval rExpr
85+
CheckC rExpr -> handleCheck rExpr
7186
LoadC path -> loadFile path
7287
ReadC varName -> do
73-
exprS <- liftIO (readRepl [] "")
74-
isUntyped <- askUntyped
75-
expr <- liftEither (parseRawExpr isUntyped exprS)
76-
handleDefine varName expr
88+
exprS <- liftIO (readRepl [] "")
89+
isUntyped <- askUntyped
90+
expr <- liftEither (parseRawExpr isUntyped exprS)
91+
handleDefine varName expr
7792
ExitC -> return ()
7893

7994
repl :: CmdHistory -> Result ()
@@ -99,3 +114,6 @@ handleFile fName = do
99114
sc <- liftIO $ readFile fName
100115
prog <- liftEither (parseProg isUntyped sc)
101116
mapM_ handleCommand prog
117+
118+
handleFileWrapper :: String -> Result ()
119+
handleFileWrapper fName = runInRawMode (handleFile fName)

src/Lam/Parser/Lexer.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ tokens :-
2424
<0> "lam" { tok Lam }
2525
<0> "fix" { tok Fix }
2626
<0> "EVAL" { tok Eval }
27+
<0> "CHECK" { tok Check }
2728
<0> "EXIT" { tok Exit }
2829
<0> "TYPEDEF" { tok Typedef }
2930
<0> "DEFINE" { tok Define }
@@ -75,6 +76,7 @@ data Token =
7576
| Lam
7677
| Fix
7778
| Eval
79+
| Check
7880
| Exit
7981
| Typedef
8082
| Load

src/Lam/Parser/Parser.y

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import Lam.Parser.Lexer qualified as L
4545
":=" { L.ColonEq }
4646
"DEFINE" { L.Define }
4747
"EVAL" { L.Eval }
48+
"CHECK" { L.Check }
4849
"EXIT" { L.Exit }
4950
"TYPEDEF" { L.Typedef }
5051
"LOAD" { L.Load }
@@ -96,6 +97,7 @@ UntypedCommand :: { Command }
9697
| UntypedEvalCommand { EvalC $1 }
9798
| LoadCommand { LoadC $1 }
9899
| ReadCommand { ReadC $1 }
100+
| CheckCommand { CheckC $1 }
99101
| ExitCommand { ExitC }
100102
-- we allow this here but throw an error later
101103
| TypedefCommand { TypedefC $1 }
@@ -106,6 +108,9 @@ UntypedDefineCommand :: { (Id, RawExpr) }
106108
UntypedEvalCommand :: { RawExpr }
107109
: "EVAL" ":" UntypedRawExpr ";" { $3 }
108110

111+
UntypedCheckCommand :: { RawExpr }
112+
: "CHECK" ":" UntypedRawExpr ";" { $3 }
113+
109114
Program :: { [Command] }
110115
: Command Program { $1 : $2 }
111116
| { [] }
@@ -114,6 +119,7 @@ Command :: { Command }
114119
: TypedefCommand { TypedefC $1 }
115120
| DefineCommand { DefineC $1 }
116121
| EvalCommand { EvalC $1 }
122+
| CheckCommand { CheckC $1 }
117123
| LoadCommand { LoadC $1 }
118124
| ReadCommand { ReadC $1 }
119125
| ExitCommand { ExitC }
@@ -129,6 +135,9 @@ DefineCommand :: { (Id, RawExpr) }
129135
EvalCommand :: { RawExpr }
130136
: "EVAL" ":" RawExpr ";" { $3 }
131137

138+
CheckCommand :: { RawExpr }
139+
: "CHECK" ":" RawExpr ";" { $3 }
140+
132141
LoadCommand :: { String }
133142
: "LOAD" ":" path ";" { init (tail $3) {- Removes quotes -} }
134143
-- yes i will change that later to FilePath thank you

0 commit comments

Comments
 (0)