Permalink
Browse files

Added modulo operator (plus some paper and API tweaks)

  • Loading branch information...
1 parent b43ce21 commit 5ec9a10a708bfd377ffeddf8db9d4a1f96a77334 Edwin Brady committed Mar 8, 2011
Showing with 182 additions and 78 deletions.
  1. +1 −0 Epic/CodegenC.lhs
  2. +17 −7 Epic/Epic.lhs
  3. +2 −1 Epic/Language.lhs
  4. +4 −2 Epic/Lexer.lhs
  5. +3 −1 Epic/Parser.y
  6. +151 −63 Papers/Epic/language.tex
  7. +3 −3 evm/closure.c
  8. +1 −1 evm/closure.h
View
@@ -306,6 +306,7 @@
> doOp t Minus l r = tmp t ++ " = INTOP(-,"++tmp l ++ ", "++tmp r++");"
> doOp t Times l r = tmp t ++ " = MULT("++tmp l ++ ", "++tmp r++");"
> doOp t Divide l r = tmp t ++ " = INTOP(/,"++tmp l ++ ", "++tmp r++");"
+> doOp t Modulo l r = tmp t ++ " = INTOP(%,"++tmp l ++ ", "++tmp r++");"
> doOp t FPlus l r = tmp t ++ " = FLOATOP(+,"++tmp l ++ ", "++tmp r++");"
> doOp t FMinus l r = tmp t ++ " = FLOATOP(-,"++tmp l ++ ", "++tmp r++");"
> doOp t FTimes l r = tmp t ++ " = FLOATOP(*,"++tmp l ++ ", "++tmp r++");"
View
@@ -302,14 +302,24 @@ Remaining expression constructs
> sc' <- term sc
> return $ Let n TyAny val' sc'
+> -- | Build expressions, with a name supply
+> class LetExpr e where
+> let_ :: EpicExpr val => val -> e -> State Int Expr
+
+> instance LetExpr (Expr -> Term) where
+> let_ = letV_
+
+> instance EpicExpr sc => LetExpr (Name, sc) where
+> let_ val (n, sc) = letN_ n val sc
+
> -- | Let bindings with higher order syntax
-> let_ :: (EpicExpr e) =>
+> letV_ :: (EpicExpr e) =>
> e -> (Expr -> Term) -> Term
-> let_ e f = do e' <- term e
-> f' <- f (R (MN "DUMMY" 0))
-> let var = MN "loc" (topVar f')
-> fv <- f (R var)
-> return $ Let var TyAny e' fv
+> letV_ e f = do e' <- term e
+> f' <- f (R (MN "DUMMY" 0))
+> let var = MN "loc" (topVar f')
+> fv <- f (R var)
+> return $ Let var TyAny e' fv
> -- | Update a local variable (could be an explicit name or bound with
> -- a lambda, so we let it be an 'Expr'.
@@ -370,7 +380,7 @@ Remaining expression constructs
> -- | Sequence terms --- evaluate the first then second
> (+>) :: (EpicExpr c) => c -> Term -> Term
-> (+>) c k = let_ c (\x -> k)
+> (+>) c k = let_ c (\(x :: Expr) -> k)
> tyInt, tyChar, tyBool, tyFloat, tyString, tyPtr, tyUnit, tyAny :: Type
> tyC :: String -> Type
View
@@ -136,7 +136,8 @@ Get the arity of a definition in the context
> data Allocator = FixedPool | GrowablePool
> deriving Eq
-> data Op = Plus | Minus | Times | Divide | OpEQ | OpLT | OpLE | OpGT | OpGE
+> data Op = Plus | Minus | Times | Divide | Modulo
+> | OpEQ | OpLT | OpLE | OpGT | OpGE
> | FPlus | FMinus | FTimes | FDivide
> | OpFEQ | OpFLT | OpFLE | OpFGT | OpFGE
> | ShL | ShR
View
@@ -79,6 +79,7 @@
> | TokenMinus
> | TokenTimes
> | TokenDivide
+> | TokenMod
> | TokenFPlus
> | TokenFMinus
> | TokenFTimes
@@ -179,7 +180,8 @@
> lexer cont ('|':cs) = cont TokenBar cs
> lexer cont ('.':cs) = cont TokenDot cs
> lexer cont ('\\':cs) = cont TokenLam cs
-> lexer cont ('%':cs) = lexSpecial cont cs
+> lexer cont ('%':c:cs) | isAlpha c = lexSpecial cont (c:cs)
+> lexer cont ('%':cs) = cont TokenMod cs
> lexer cont (c:cs) = lexError c cs
> lexError c s f l = failP (show l ++ ": Unrecognised token '" ++ [c] ++ "'\n") s f l
@@ -284,7 +286,7 @@
> ("fixed",rest) -> cont TokenFixed rest
> ("growable",rest) -> cont TokenGrowable rest
> ("unused", rest) -> cont TokenUnused rest
-> (thing,rest) -> lexError '%' rest
+> (thing,rest) -> cont TokenMod (thing++rest)
> mkname :: String -> Token
> mkname c = TokenName (UN c)
View
@@ -73,6 +73,7 @@ import Epic.Lexer
'-' { TokenMinus }
'*' { TokenTimes }
'/' { TokenDivide }
+ '%' { TokenMod }
fplus { TokenFPlus }
fminus { TokenFMinus }
ftimes { TokenFTimes }
@@ -114,7 +115,7 @@ import Epic.Lexer
%left '<' '>' le ge flt fgt fle fge
%left shl shr
%left '+' '-' fplus fminus
-%left '*' '/' ftimes fdiv
+%left '*' '/' ftimes fdiv '%'
%left NEG
%left '!'
%nonassoc '('
@@ -225,6 +226,7 @@ MathExpr : Expr '+' Expr { Op Plus $1 $3 }
| '-' Expr %prec NEG { Op Minus (Const (MkInt 0)) $2 }
| Expr '*' Expr { Op Times $1 $3 }
| Expr '/' Expr { Op Divide $1 $3 }
+ | Expr '%' Expr { Op Modulo $1 $3 }
| Expr fplus Expr { Op FPlus $1 $3 }
| Expr fminus Expr { Op FMinus $1 $3 }
Oops, something went wrong.

0 comments on commit 5ec9a10

Please sign in to comment.