@ -123,15 +123,15 @@ instrAnnotation (VMFunction FReturn) = "return"
@@ -123,15 +123,15 @@ instrAnnotation (VMFunction FReturn) = "return"
type Parser = Parsec Error T . Text
parseInstructions :: Parser [ VMInstruction ]
parseInstructions = many ( sp *> parseInstruction <* sp ) <* eof
parseInstructions :: T . Text -> Parser [ VMInstruction ]
parseInstructions modName = many ( sp *> parseInstruction modName <* sp ) <* eof
where
sp :: Parser ()
sp = L . space space1 ( skipLineComment " // " ) empty
parseInstruction :: Parser VMInstruction
parseInstruction =
try parseMemAccess <|>
parseInstruction :: T . Text -> Parser VMInstruction
parseInstruction modName =
try ( parseMemAccess modName ) <|>
try parseArithmetic <|>
try parseBranching <|>
parseFunction
@ -151,23 +151,23 @@ parseInstruction =
@@ -151,23 +151,23 @@ parseInstruction =
string " if-goto " *> space1 *> ( BIfGoto <$> parseId )
]
parseMemAccess :: Parser VMInstruction
parseMemAccess = VMMemAccess <$>
parseMemAccess :: T . Text -> Parser VMInstruction
parseMemAccess modName = VMMemAccess <$>
parseAccessType <*>
parseSegment
parseSegment modName
parseAccessType :: Parser AccessType
parseAccessType = try ( string " push " >> space1 $> ACPush ) <|>
( string " pop " >> space1 $> ACPop )
parseSegment :: Parser Segment
parseSegment =
parseSegment :: T . Text -> Parser Segment
parseSegment modName =
choice [ parseSegmentInt " local " SLocal ,
parseSegmentInt " argument " SArgument ,
parseSegmentInt " this " SThis ,
parseSegmentInt " that " SThat ,
parseSegmentInt " constant " SConstant ,
( string " static " *> space1 ) *> ( SStatic <$> ( try parseId <|> parseStaticOffset ) ) ,
( string " static " *> space1 ) *> ( SStatic <$> ( try parseId <|> parseStaticOffset modName ) ) ,
parseSegmentInt " pointer " SPointer ,
parseSegmentInt " temp " STemp
]
@ -179,10 +179,10 @@ parseInstruction =
@@ -179,10 +179,10 @@ parseInstruction =
hs <- many ( alphaNumChar <|> char '.' <|> char '_' )
return $ T . pack ( h : hs )
parseStaticOffset :: Parser T . Text
parseStaticOffset = do
parseStaticOffset :: T . Text -> Parser T . Text
parseStaticOffset modName = do
h <- L . decimal
return $ " STATIC_ " <> ( T . pack . show ) h
return $ modName <> " . " <> ( T . pack . show ) h
parseSegmentInt :: T . Text -> ( Int -> Segment ) -> Parser Segment
parseSegmentInt sname f = try $ do