From 97719af3b544b9fae80e886bf6a49c51b4a320a9 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Tue, 14 Mar 2006 16:53:17 +0000 Subject: [PATCH] most of the semantics added --- Interpret.hs | 8 ++--- documentation | 95 +++++++++++++++++---------------------------------- 2 files changed, 33 insertions(+), 70 deletions(-) diff --git a/Interpret.hs b/Interpret.hs index 8cf3c1b..c5ef5e0 100644 --- a/Interpret.hs +++ b/Interpret.hs @@ -14,10 +14,6 @@ instance Show Value where type Variables = [Map Ident Value] -inList :: Eq a => a -> [a] -> Bool -inList _ [] = False -inList a (x:xs) = if a == x then True else inList a xs - --eval :: (MonadState Variables m) => Exp -> m Value eval :: Exp -> StateT Variables IO Value eval (EBool b) = return (VBool b) @@ -27,7 +23,7 @@ eval (EAss i e) = setVariableValue i e eval (BiOpExp e o e') = do v <- eval e v'<- eval e' - if inList o [Eq,NEq] then return $ opE o v v' + if elem o [Eq,NEq] then return $ opE o v v' else let (VInt n1) = v in let (VInt n2) = v' in return $ op o n1 n2 eval (EPost i o) = do (VInt n) <- getVariableValue i @@ -49,7 +45,7 @@ eval EReadB = do getWord :: IO String getWord = do c <- getChar - if inList c [' ', '\n', '\t', '\r'] + if elem c [' ', '\n', '\t', '\r'] then return "" else do l <- getWord diff --git a/documentation b/documentation index bbb604f..c3ac4e0 100644 --- a/documentation +++ b/documentation @@ -13,131 +13,98 @@ typing rules ++++++++++++ -(t is used for types, T is the context, and + is used for in) +(v is used for values, e for expressions, s for statements, c is the context) -[Eq, NEq] +[Eq, NEq, Plus, Minus, Times, Div, Lt, ELt, Gt, EGt] -T+ e1 Eq e2:bool <= T+ e1:t & T+ e2:t - -If e1 and e2 are of the same type, then Eq or NEq return bool - - -[Plus, Minus, Times, Div] - -T+ e1 Plus e2:int <= T+ e1:int & T+ e2:int - -The operators Plus/Minus/Times/Div return int if both operands are ints - - -[Lt, ELt, Gt, EGt] - -T+ e1 Lt e2:bool <= T+ e1:int & T+ e2:int - -The operators Lt/ELt/Gt/EGt return bool if both operands are ints + => <= => => v is the result of using operator o on v1 and v2 [Assignment] -T+ i := e:t <= i:t in T & T+ e:t - -The assignemnt of e to i returns type t if both i and e have type t. + => c'[i -> v] <= => +Assign the value v to i in the first scope i is found in. [ENeg] -T+ ENeg e:int <= T+ e:int + => <-v,c'> <= => -ENeg e returns int if e is of type int [ENot] -T+ ENot e:bool <= e:bool - -ENot e returns bool if e is of type bool + => <= => [EVar] -T+ i:t <= i:t in T - -i has type t if i is defined in the context with type t. + => [EInt] -T+ n:int - -n has type int + => [EBool] -T+ b:bool - -b has type bool - -[EReadI] - -T+ n:int + => -EReadI returns an int +[EReadI,EReadB] -[EReadB] - -T+ b:bool - -EReadB returns a bool + => <= => [EPost] -T+ EPost i:int <= i:int in T + => v']> <= c(i) => v, v±1 => v' -EPost i is of type int if i is defined in T with type int. +Look up the variable, add/subtract 1 from the value then return the old value and context with modified value [SExp] -T+ e <= T+ e:t + => c' <= => [SBlock] -T+ s;SBlock ss <= T+ s => T' , T'+ ss => T'' + => c''' <= push(c) => c' => c'' pop(c'') => c''' + +Push a new scope onto the context, execute the statements in this context and the pop the scope from the context -the first statment s, in the block, is typechecked in the context T and returns the context T', the rest of the block is then recursively typeckecked in the context T' +[SEQ] + => c'' <= => c' => c'' [SIf] -T+ if e then s1 else s2 <= T+ e:bool & T+ s1 & T+ s2 + => pop(c''') <= => push(c') => c''' -if e is of type bool and s1 and and s2 typechecks in the context T, then the same context is returned + => pop(c''') <= => push(c') => c''' [SWhile] -T+ while e do s <= T+ e:bool & T+ s - -If e is of type bool and s typechecks in context T then the same context is returned + => c' => => + => pop(c''') => => push(c') => c'' => c''' [SDecl] -T+ t i = e => T,i:t <= i not in T & e:t + => c'[i->v] <= => -if i and e are of the same type and i is not declared in the current scope then i is added with type t to the context. +Adds i with value v to the current scope in the context [SDeclD] -T+ t i => T,i:t <= i not in T + => c[i->0] + => c[i->false] -if i is not declared in the current scope, then i is added to the context with type t +Adds i with default value in the current scope [SNoop] -T+ s + => c SNoops does nothing so the same context is returned [SPrint] -T+ e <= T+ e:t - -if e has type t then SPrint returns the same context + => c'' <= => => c'' -- 2.39.2