Skip to content

Commit a65429b

Browse files
author
cadsit
committed
Made Interpreter exportable
With multistep, stepText, interpretText functions
1 parent 831e63e commit a65429b

File tree

6 files changed

+35
-14
lines changed

6 files changed

+35
-14
lines changed

Grammar.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
module Parser (Exp(..), AppExp(..), Value(..), Var(..), parseFromFile) where
2+
module Parser (Exp(..), AppExp(..), Value(..), Var(..), parse, parseFromFile) where
33
import Scanner
44
}
55

@@ -113,5 +113,5 @@ instance Show Value where
113113
parseFromFile fil =
114114
do
115115
toks <- Scanner.lexFromFile fil
116-
return (p_lam toks)
116+
return (parse toks)
117117
}

Interpreter.hs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
Provides Operational Semantics for basic Lambda Calculus
77
-}
88

9-
module Interpreter where
9+
module Interpreter (stepExp, multistepExp, stepText, interpretText) where
10+
import Scanner
1011
import Parser
1112

1213
-- Checks to see if an expression cannot be evaluated more
@@ -234,3 +235,22 @@ stepAppExp ae =
234235
Just val -> Nothing
235236
-- A value cannot take a step
236237
Parser.Value v -> Nothing
238+
239+
multistepExp :: Parser.Exp -> Parser.Exp
240+
multistepExp exp =
241+
let exp' = stepExp exp
242+
in case exp' of
243+
Nothing -> exp
244+
Just exp'' -> multistepExp exp''
245+
246+
stepText :: String -> Maybe Parser.Exp
247+
stepText str =
248+
let toks = Scanner.alexScanTokens str
249+
exp = Parser.parse toks
250+
in stepExp exp
251+
252+
interpretText :: String -> Parser.Exp
253+
interpretText str =
254+
let toks = Scanner.alexScanTokens str
255+
exp = Parser.parse toks
256+
in multistepExp exp

Lexer.x

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ data Token =
4343
deriving (Eq,Show)
4444

4545
lexFromFile fil =
46-
do
47-
s <- readFile fil
48-
let toks = (alexScanTokens s)
49-
return toks
46+
do
47+
s <- readFile fil
48+
let toks = (alexScanTokens s)
49+
return toks
5050

5151
lexFromFileToFile infile outfile =
5252
do

Parser.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{-# OPTIONS_GHC -w #-}
2-
module Parser (Exp(..), AppExp(..), Value(..), Var(..), parseFromFile) where
2+
module Parser (Exp(..), AppExp(..), Value(..), Var(..), parse, parseFromFile) where
33
import Scanner
44

55
-- parser produced by Happy Version 1.18.9
@@ -355,7 +355,7 @@ happyReturn1 = \a tks -> (return) a
355355
happyError' :: () => [(Scanner.Token)] -> HappyIdentity a
356356
happyError' = HappyIdentity . parseError
357357

358-
p_lam tks = happyRunIdentity happySomeParser where
358+
parse tks = happyRunIdentity happySomeParser where
359359
happySomeParser = happyThen (happyParse action_0 tks) (\x -> case x of {HappyAbsSyn4 z -> happyReturn z; _other -> notHappyAtAll })
360360

361361
happySeq = happyDontSeq
@@ -426,7 +426,7 @@ instance Show Value where
426426
parseFromFile fil =
427427
do
428428
toks <- Scanner.lexFromFile fil
429-
return (p_lam toks)
429+
return (parse toks)
430430
{-# LINE 1 "templates/GenericTemplate.hs" #-}
431431
{-# LINE 1 "templates/GenericTemplate.hs" #-}
432432
{-# LINE 1 "<built-in>" #-}

Scanner.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ data Token =
177177
deriving (Eq,Show)
178178

179179
lexFromFile fil =
180-
do
181-
s <- readFile fil
182-
let toks = (alexScanTokens s)
183-
return toks
180+
do
181+
s <- readFile fil
182+
let toks = (alexScanTokens s)
183+
return toks
184184

185185
lexFromFileToFile infile outfile =
186186
do

test-files/test0.lc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
&&&

0 commit comments

Comments
 (0)