Skip to content

Commit

Permalink
Added idId runtime function. Reved version.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomahawkins committed May 18, 2012
1 parent a530b79 commit 74a7114
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions Language/CIRC/Runtime.hs
Expand Up @@ -5,28 +5,41 @@ module Language.CIRC.Runtime
, runCIRC
, Id
, newId
, idId
) where

import Control.Monad.State

-- | The CIRC transform monad. Used to create fresh ids.
type CIRC = State Int
type CIRC = State (Int, [(String, Int)])

-- | Evaluates a CIRC transform.
evalCIRC :: CIRC a -> Int -> a
evalCIRC = evalState
evalCIRC a i = evalState a (i, [])

-- | Evaluates a CIRC transform, also returning the fresh next id.
runCIRC :: CIRC a -> Int -> (a, Int)
runCIRC = runState
runCIRC a i = (b, j)
where
(b, (j, _)) = runState a (i, [])

-- | Identifiers.
type Id = String

-- | Produces a fresh id.
newId :: CIRC Id
newId = do
id <- get
put $ id + 1
return $ "__" ++ show id
(i, table) <- get
put (i + 1, table)
return $ "__" ++ show i

-- | Returns a unqiue int for a given id.
idId :: Id -> CIRC Int
idId name = do
(i, table) <- get
case lookup name table of
Nothing -> do
put (i + 1, (name, i) : table)
return i
Just i -> return i

2 changes: 1 addition & 1 deletion circ.cabal
@@ -1,5 +1,5 @@
name: circ
version: 0.0.3
version: 0.0.4
synopsis: A Compiler IR Compiler.
description: A Compiler IR Compiler.
license: BSD3
Expand Down

0 comments on commit 74a7114

Please sign in to comment.