Fix the a[b] ambiguity by tweaking the parser; also make AST Conversions include the type

This commit is contained in:
Adam Sampson 2006-10-05 12:45:30 +00:00
parent 5f697e8286
commit ed54025468
4 changed files with 55 additions and 13 deletions

View File

@ -67,7 +67,7 @@ data Expression =
| MostPos Type
| MostNeg Type
| Size Type
| Conversion ConversionMode Expression
| Conversion ConversionMode Type Expression
| ExprVariable Variable
| ExprLiteral Literal
| True

View File

@ -7,7 +7,7 @@ import qualified AST as O
doName :: N.Node -> O.Name
doName (N.Node _ (N.Name s)) = O.Name s
doName n = error $ "Can't do name: " ++ (show n)
doName n = error $ "Failed to translate to Name: " ++ (show n)
doTag :: N.Node -> O.Tag
doTag (N.Node _ (N.Name s)) = O.Tag s
@ -84,6 +84,7 @@ doVariable :: N.Node -> O.Variable
doVariable n@(N.Node _ nt) = case nt of
N.Name _ -> O.Variable (doName n)
N.Sub s v -> O.SubscriptedVariable (doSubscript s) (doVariable v)
_ -> error $ "Failed to translate to Variable: " ++ (show n)
doExpression :: N.Node -> O.Expression
doExpression n@(N.Node _ nt) = case nt of
@ -92,9 +93,9 @@ doExpression n@(N.Node _ nt) = case nt of
N.MostPos t -> O.MostPos (doType t)
N.MostNeg t -> O.MostNeg (doType t)
N.Size t -> O.Size (doType t)
N.Conv t e -> O.Conversion O.DefaultConversion (doExpression e)
N.Round t e -> O.Conversion O.Round (doExpression e)
N.Trunc t e -> O.Conversion O.Trunc (doExpression e)
N.Conv t e -> O.Conversion O.DefaultConversion (doType t) (doExpression e)
N.Round t e -> O.Conversion O.Round (doType t) (doExpression e)
N.Trunc t e -> O.Conversion O.Trunc (doType t) (doExpression e)
N.TypedLit _ _ -> O.ExprLiteral $ doLiteral n
N.LitReal _ -> O.ExprLiteral $ doLiteral n
N.LitInt _ -> O.ExprLiteral $ doLiteral n

View File

@ -323,10 +323,9 @@ conditional
<|> do { r <- replicator ; eol ; indent ; c <- occamChoice ; outdent ; return $ nd m $ N.IfRep r c } }
<?> "conditional"
-- This uses operandNotTable to resolve the "x[y]" ambiguity.
conversion
= do m <- md
t <- dataType
do { sROUND ; o <- operand ; return $ nd m $ N.Round t o } <|> do { sTRUNC ; o <- operand ; return $ nd m $ N.Trunc t o } <|> do { o <- operand ; return $ nd m $ N.Conv t o }
= try (do { m <- md ; t <- dataType; do { sROUND ; o <- operand ; return $ nd m $ N.Round t o } <|> do { sTRUNC ; o <- operand ; return $ nd m $ N.Trunc t o } <|> do { o <- operandNotTable ; return $ nd m $ N.Conv t o } })
<?> "conversion"
occamCount
@ -524,20 +523,28 @@ occamString
= lexeme (do { m <- md ; char '"' ; s <- many (noneOf "\"") ; char '"' ; return $ nd m $ N.LitString s })
<?> "string"
operand
= do { m <- md ; v <- operand' ; es <- many (do { sLeft ; e <- expression ; sRight ; return e }) ; return $ foldl (\e s -> nd m $ N.Sub (nd m $ N.SubPlain s) e) v es }
<?> "operand"
operandNotTable
= do { m <- md ; v <- operandNotTable' ; es <- many (do { sLeft ; e <- expression ; sRight ; return e }) ; return $ foldl (\e s -> nd m $ N.Sub (nd m $ N.SubPlain s) e) v es }
<?> "operandNotTable"
operand'
operandNotTable'
= try variable
<|> try literal
<|> try table
<|> try (do { sLeftR ; e <- expression ; sRightR ; return e })
-- XXX value process
<|> try (do { m <- md ; n <- name ; sLeftR ; as <- sepBy expression sComma ; sRightR ; return $ nd m $ N.Call n as })
<|> try (do { m <- md ; sBYTESIN ; sLeftR ; o <- operand ; sRightR ; return $ nd m $ N.BytesIn o })
<|> try (do { m <- md ; sBYTESIN ; sLeftR ; o <- dataType ; sRightR ; return $ nd m $ N.BytesIn o })
<|> try (do { m <- md ; sOFFSETOF ; sLeftR ; n <- name ; sComma ; f <- fieldName ; sRightR ; return $ nd m $ N.OffsetOf n f })
<?> "operandNotTable'"
operand
= do { m <- md ; v <- operand' ; es <- many (do { sLeft ; e <- expression ; sRight ; return e }) ; return $ foldl (\e s -> nd m $ N.Sub (nd m $ N.SubPlain s) e) v es }
<?> "operand"
operand'
= try table
<|> operandNotTable'
<?> "operand'"
occamOption

34
fco/test-ambiguous.occ Normal file
View File

@ -0,0 +1,34 @@
-- Some ambiguities in the occam2.1 syntax as specified.
PROC x ()
SEQ
[10]INT a:
INT b, r:
SEQ
r := a[b]
DATA TYPE a IS [1]INT:
INT b:
a r:
SEQ
-- Permitted by the syntax but not by the language: you can't do a data
-- type conversion on an array (perhaps specifically to resolve this
-- ambiguity!).
r := a[b]
PROTOCOL P
CASE
a; INT
:
CHAN OF P c:
INT b:
SEQ
c ! a; b
PROTOCOL P IS INT; INT:
CHAN OF P c:
INT a, b:
SEQ
c ! a; b
: