Commit 58b9c892 authored by Alex Simpson's avatar Alex Simpson Committed by Janoš Vidali
Browse files

Add first homework

parents
module Imp_AbsSyntax where
-- Haskell datatypes representing abstract syntax of expressions and commands
-- arithmetic expressions
data AExp =
Loc String |
Num Integer |
AOp (String, AExp, AExp)
deriving Show
-- boolean expressions
data BExp =
Boolean Bool |
BOp (String, AExp, AExp)
deriving Show
-- commands
data Com =
Assign (String, AExp) |
Cond (BExp, Com, Com) |
Seq (Com, Com) |
Skip |
While (BExp, Com)
deriving Show
module Imp_Evaluator where
import Imp_AbsSyntax
import Imp_State
evalAExp :: State -> AExp -> Integer
evalBExp :: State -> BExp -> Bool
evalCom :: State -> Com -> State
--
-- [[DEFINE EVALUATION FUNCTIONS FOR:
--
-- * ARITHMETIC EXPRESSIONS
-- * BOOLEAN EXPRESSIONS
-- * COMMANDS
--
-- WITH THE TYPES SPECIFIED ABOVE]]
--
module Imp_Interpreter where
import Imp_Lex
import Imp_Parse
import Imp_State
import Imp_Evaluator
-- runImp "file.imp" s
--
-- lexes and parses the Imp program text in file.imp and then
-- executes it starting in state s
runImp filename s = do
progtext <- readFile filename
let lexed = imp_lex progtext
parsed = imp_parse lexed
s' = evalCom s parsed
in print s'
{module Imp_Lex where }
%wrapper "basic"
-- character classes
$digit = [0-9]
$lower = [a-z]
$upper = [A-Z]
:-
-- lexical classes
--
-- [[IMPLEMENT YOUR LEXICAL CLASSES HERE]]
--
{
-- The Token type - the action for each lexical class has type String -> Token
data Token =
--
-- [[IMPLEMENT YOUR Token TYPE HERE]]
--
deriving Show
-- The lexer implements a function
--
-- imp_lex :: String -> [Token]
--
-- which converts a string containing Imp program text into a list of tokens
imp_lex = alexScanTokens
}
{module Imp_Parse where
import Imp_Lex
import Imp_AbsSyntax
}
-- the parser implements a function
--
-- imp_parse :: [Token] -> Com
--
-- that converts a list of tokens to the abstract syntax tree of a command
%name imp_parse Com
%tokentype { Token }
%error { parseError }
%token
--declaration of terminal symbols
--
-- [[DECLARE YOUR TERMINAL SYMBOLS HERE]]
--
-- precedence and associativity declarations, lowest precedence first
--
-- [[MAKE PRECEDENCE AND ASSOCIATIVITY DECLARATIONS HERE]]
--
%%
-- the grammar
--
-- [[DEFINE YOUR GRAMMAR AND ITS ACTIONS HERE]]
--
{
parseError :: [Token] -> a
parseError _ = error "Imp parse error"
}
module Imp_State (State, emp, valof, update) where
emp :: State
valof :: State -> String -> Integer
update :: State -> String -> Integer -> State
-- states represented as lists of (location, value) pairs
-- but the representation is hidden under the State constructor
-- which is not exported from the module (only the type State is exported)
newtype State = State [(String,Integer)] deriving Show
emp = State []
valof (State lns) l =
case (lookup l lns) of
(Just n) -> n
_ -> error ("Lookup error - unassigned location: " ++ l)
update (State lns) l n =
State ((l,n) : [(l',n') | (l',n') <- lns, l' /= l])
Y := 1 ; while 0 < X do Y := Y * X ; X := X - 1
Count := 1 ;
XthPrime := 2 ;
while Count < X do
XthPrime := XthPrime + 1 ;
Y := 2 ;
(
while Y*Y < XthPrime + 1 do
Z := 2 ;
(
while Z < XthPrime do
(if Y*Z == XthPrime then Y := XthPrime ; Z := Y
else skip
);
Z := Z+1
);
Y := Y+1
);
if Y < XthPrime + 1 then Count := Count + 1
else skip
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment