This commit is contained in:
Mirek Kratochvil 2022-12-14 20:47:29 +01:00
parent 60ff47250b
commit 71992db7d0
4 changed files with 24 additions and 4 deletions

View file

@ -6,6 +6,16 @@ import qualified Data.Map as M
import Code (Code, Datum(..), Heap, Instr(..), heapStruct) import Code (Code, Datum(..), Heap, Instr(..), heapStruct)
import IR (Id(..), PrlgInt(..), StrTable(..)) import IR (Id(..), PrlgInt(..), StrTable(..))
desugarPrlg :: Int -> PrlgInt -> PrlgInt
desugarPrlg list = go
where
go (CallI id ps) = CallI id $ map go ps
go (ListI (x:xs) t) =
CallI Id {str = list, arity = 2} [go x, go (ListI xs t)]
go (ListI [] Nothing) = LiteralI list
go (ListI [] (Just x)) = go x
go x = x
varname :: String -> Bool varname :: String -> Bool
varname ('_':_) = True varname ('_':_) = True
varname (c:_) = isUpper c varname (c:_) = isUpper c

View file

@ -47,9 +47,12 @@ interpret = (>> return True) . lex
intern prlgs = do intern prlgs = do
prlgi <- withStrTable $ \st -> IR.internPrlg st prlgs prlgi <- withStrTable $ \st -> IR.internPrlg st prlgs
underscore <- findAtom "_" underscore <- findAtom "_"
list <- findAtom "[]"
prlgv <- prlgv <-
withStrTable $ \st -> withStrTable $ \st ->
(st, C.squashVars $ C.variablizePrlg underscore st prlgi) ( st
, C.squashVars $
C.variablizePrlg underscore st $ C.desugarPrlg list prlgi)
compile prlgv compile prlgv
compile prlgv = do compile prlgv = do
commaId <- findStruct "," 2 commaId <- findStruct "," 2

View file

@ -6,6 +6,7 @@ import qualified Data.Map as M
data PrlgStr data PrlgStr
= CallS String [PrlgStr] = CallS String [PrlgStr]
| LiteralS String | LiteralS String
| ListS [PrlgStr] (Maybe PrlgStr)
deriving (Show) deriving (Show)
data Id = data Id =
@ -18,6 +19,7 @@ data Id =
data PrlgInt data PrlgInt
= CallI Id [PrlgInt] --TODO this should be Int = CallI Id [PrlgInt] --TODO this should be Int
| LiteralI Int | LiteralI Int
| ListI [PrlgInt] (Maybe PrlgInt) -- only exists before desugaring
| VarI Int Int -- VarI localIndex strTableString | VarI Int Int -- VarI localIndex strTableString
| VoidI | VoidI
deriving (Show) deriving (Show)
@ -40,3 +42,7 @@ internPrlg = go
go t (CallS str ps) = go t (CallS str ps) =
let (t', i) = strtablize t str let (t', i) = strtablize t str
in CallI (Id i $ length ps) <$> mapAccumL go t' ps in CallI (Id i $ length ps) <$> mapAccumL go t' ps
go t (ListS elems Nothing) = flip ListI Nothing <$> mapAccumL go t elems
go t (ListS elems (Just tail)) =
let (t', tail') = go t tail
in flip ListI (Just tail') <$> mapAccumL go t' elems

View file

@ -145,7 +145,7 @@ instance TraversableStream [Lexeme] where
data PAST data PAST
= Call String [[PAST]] = Call String [[PAST]]
| Seq [PAST] | Seq [PAST]
| List [PAST] (Maybe [PAST]) | List [[PAST]] (Maybe [PAST])
| Literal String | Literal String
deriving (Show, Eq) deriving (Show, Eq)
@ -181,7 +181,7 @@ list = do
free lBracket free lBracket
choice choice
[ List [] Nothing <$ free rBracket [ List [] Nothing <$ free rBracket
, do items <- some seqItem , do items <- splitOn [Literal ","] <$> some seqItem
choice choice
[ List items Nothing <$ free rBracket [ List items Nothing <$ free rBracket
, List items . Just <$> , List items . Just <$>
@ -223,7 +223,8 @@ shuntPrlg :: Ops -> PAST -> ShuntResult
shuntPrlg ot = shuntPrlg' (("", Op 0 $ Infix X Y) : ot) shuntPrlg ot = shuntPrlg' (("", Op 0 $ Infix X Y) : ot)
shuntPrlg' :: Ops -> PAST -> ShuntResult shuntPrlg' :: Ops -> PAST -> ShuntResult
shuntPrlg' ot (List _ _) = err "no lists yet" shuntPrlg' ot (List hs t) =
ListS <$> traverse (shunt ot) hs <*> traverse (shunt ot) t
shuntPrlg' ot (Seq ss) = shunt ot ss shuntPrlg' ot (Seq ss) = shunt ot ss
shuntPrlg' ot (Literal s) = pure (LiteralS s) shuntPrlg' ot (Literal s) = pure (LiteralS s)
shuntPrlg' ot (Call fn ss) = CallS fn <$> traverse (shunt ot) ss shuntPrlg' ot (Call fn ss) = CallS fn <$> traverse (shunt ot) ss