From 1cf166f0dbc955aac2bea701415124976c82bb59 Mon Sep 17 00:00:00 2001 From: Cat's Eye Technologies Date: Wed, 2 Apr 2014 17:10:21 +0100 Subject: [PATCH] Allow comments. --- README.markdown | 20 +++++++++++++++++++- src/SixtyPical/Parser.hs | 18 ++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index aecfb07..7968c24 100644 --- a/README.markdown +++ b/README.markdown @@ -262,7 +262,6 @@ In these, `absolute` must be a `reserve`d or `locate`d address. TODO ---- -* comments * Initial values for reserved, incl. tables * give length for tables, must be there for reserved * Character tables ("strings" to everybody else) @@ -295,6 +294,25 @@ Tests | } ? missing 'main' routine +A comment may appear at the start of a block. + + | routine main { + | ; this program does nothing + | nop + | } + = True + +A comment may appear after each command. + + | routine main { + | lda #1 ; we assemble the fnord using + | ldx #1 ; multiple lorem ipsums which + | ldy #1 + | lda #1 ; we + | ldx #1 ; found under the bridge by the old mill yesterday + | } + = True + A program may `reserve` and `assign`. | reserve byte lives diff --git a/src/SixtyPical/Parser.hs b/src/SixtyPical/Parser.hs index 0909c7e..8c14949 100644 --- a/src/SixtyPical/Parser.hs +++ b/src/SixtyPical/Parser.hs @@ -16,7 +16,7 @@ Decl := "reserve" StorageType LocationName | "external" RoutineName Address. StorageType := "byte" | "word" | "vector". Routine := "routine" RoutineName Block. -Block := "{" {Command} "}". +Block := "{" [Comment] {Command [Comment]} "}". Command := "if" Branch Block "else" Block | "lda" (LocationName | Immediate) | "ldx" (LocationName | Immediate) @@ -90,11 +90,19 @@ block :: Parser [Instruction] block = do string "{" spaces - cs <- many command + optional comment + cs <- many commented_command string "}" spaces return cs +comment :: Parser () +comment = do + string ";" + manyTill anyChar (try (string "\n")) + spaces + return () + -- -- -- -- -- -- commands -- -- -- -- -- immediate :: (DataValue -> Instruction) -> Parser Instruction @@ -124,6 +132,12 @@ absolute_indexed f = do indexes <- many index return $ f l indexes +commented_command :: Parser Instruction +commented_command = do + c <- command + optional comment + return c + command :: Parser Instruction command = (try lda) <|> (try ldx) <|> (try ldy) <|>