Rain: added parsing support for function declarations

This commit is contained in:
Neil Brown 2007-09-02 14:55:17 +00:00
parent a14c33bc27
commit 989bb1c757
3 changed files with 29 additions and 2 deletions

View File

@ -38,7 +38,8 @@ $hexDigit = [0-9 a-f A-F]
| ">=" | "<="
| "<" | ">"
| ".."
| "process" | "pareach" | "seqeach" | "par" | "seq"
| "process" | "function"
| "pareach" | "seqeach" | "par" | "seq"
| "run"
| "if" | "while" | "else"
| "sint8" | "sint16" | "sint32" | "sint64"

View File

@ -82,6 +82,7 @@ sIf = reserved "if"
sElse = reserved "else"
sWhile = reserved "while"
sProcess = reserved "process"
sFunction = reserved "function"
sRun = reserved "run"
--}}}
@ -345,8 +346,14 @@ processDecl = do {m <- sProcess ; procName <- name ; params <- tupleDef ; body <
(A.Specification m procName (A.Proc m A.PlainSpec (formaliseTuple params) body))
terminator}
functionDecl :: RainParser A.Structured
functionDecl = do {m <- sFunction ; retType <- dataType ; sColon ; funcName <- name ; params <- tupleDef ; body <- block ;
return $ A.Spec m
(A.Specification m funcName (A.Function m A.PlainSpec [retType] (formaliseTuple params) (A.OnlyP (findMeta body) body)))
terminator}
topLevelDecl :: RainParser A.Structured
topLevelDecl = do decls <- many processDecl
topLevelDecl = do decls <- many (processDecl <|> functionDecl <?> "process or function declaration")
eof
return $ A.Several emptyMeta decls

View File

@ -413,6 +413,25 @@ testTopLevelDecl =
, fail ("process foo (int: x)", RP.topLevelDecl)
, fail ("process foo (int x) {}", RP.topLevelDecl)
,pass ("function uint8: cons() {}", RP.topLevelDecl,
assertPatternMatch "testTopLevelDecl 100" $ tag2 A.Several DontCare [tag3 A.Spec DontCare
(tag3 A.Specification DontCare (simpleNamePattern "cons") $
tag5 A.Function DontCare A.PlainSpec [A.Byte] ([] :: [A.Formal]) $
(tag2 A.OnlyP DontCare $ tag2 A.Seq DontCare $ tag2 A.Several DontCare ([] :: [A.Structured]))
) (tag2 A.OnlyP DontCare $ tag1 A.Main DontCare)
]
)
,pass ("function uint8: f(uint8: x) {}", RP.topLevelDecl,
assertPatternMatch "testTopLevelDecl 101" $ tag2 A.Several DontCare [tag3 A.Spec DontCare
(tag3 A.Specification DontCare (simpleNamePattern "f") $
tag5 A.Function DontCare A.PlainSpec [A.Byte] [tag3 A.Formal A.ValAbbrev A.Byte (simpleNamePattern "x")] $
(tag2 A.OnlyP DontCare $ tag2 A.Seq DontCare $ tag2 A.Several DontCare ([] :: [A.Structured]))
) (tag2 A.OnlyP DontCare $ tag1 A.Main DontCare)
]
)
]
nonShared :: A.ChanAttributes