Improve parsing of complex references like "a^[b[c], d]"
authorunc0rr
Thu, 10 Nov 2011 23:14:40 +0300
changeset 6317 83b93a2d2741
parent 6316 ac23ba018ed2
child 6318 ca12368acc16
Improve parsing of complex references like "a^[b[c], d]"
hedgewars/uAI.pas
tools/PascalParser.hs
tools/pas2c.hs
--- a/hedgewars/uAI.pas	Wed Nov 09 21:36:20 2011 +0300
+++ b/hedgewars/uAI.pas	Thu Nov 10 23:14:40 2011 +0300
@@ -267,7 +267,7 @@
       begin
       WalkMe:= BackMe;
       Walk(@WalkMe);
-      if (StartTicks > GameTicks - 1500) and not StopThinking then SDL_Delay(1000);
+      if (StartTicks > GameTicks - 1500) and (not StopThinking) then SDL_Delay(1000);
       if BestActions.Score < -1023 then
          begin
          BestActions.Count:= 0;
--- a/tools/PascalParser.hs	Wed Nov 09 21:36:20 2011 +0300
+++ b/tools/PascalParser.hs	Thu Nov 10 23:14:40 2011 +0300
@@ -65,7 +65,7 @@
     | Reference Reference
     | Null
     deriving Show
-data Reference = ArrayElement Identifier Expression
+data Reference = ArrayElement [Expression] Reference
     | FunCall [Expression] Reference
     | SimpleReference Identifier
     | Dereference Reference
@@ -147,7 +147,6 @@
     where
     term = comments >> choice [
         parens pas reference 
-        , try $ iD >>= \i -> (brackets pas) expression >>= return . ArrayElement i
         , char '@' >> reference >>= return . Address
         , iD >>= return . SimpleReference
         ] <?> "simple reference"
@@ -155,6 +154,7 @@
     table = [ 
             [Postfix $ (parens pas) (option [] parameters) >>= return . FunCall]
           , [Postfix (char '^' >> return Dereference)]
+          , [Postfix $ (brackets pas) (commaSep1 pas $ expression) >>= return . ArrayElement]
           , [Infix (try (char '.' >> notFollowedBy (char '.')) >> return RecordField) AssocLeft]
         ]
 
@@ -190,7 +190,7 @@
 
 
 constsDecl = do
-    vs <- many (try (aConstDecl >>= \i -> semi pas >> return i) >>= \i -> comments >> return i)
+    vs <- many1 (try (aConstDecl >>= \i -> semi pas >> return i) >>= \i -> comments >> return i)
     comments
     return vs
     where
@@ -294,7 +294,7 @@
         return t
         
     procDecl = do
-        string "procedure"
+        try $ string "procedure"
         comments
         i <- iD
         optional $ do
@@ -315,7 +315,7 @@
         return $ [FunctionDeclaration i UnknownType b]
         
     funcDecl = do
-        string "function"
+        try $ string "function"
         comments
         i <- iD
         optional $ do
--- a/tools/pas2c.hs	Wed Nov 09 21:36:20 2011 +0300
+++ b/tools/pas2c.hs	Thu Nov 10 23:14:40 2011 +0300
@@ -57,12 +57,10 @@
 
 expr2C :: Expression -> Doc
 expr2C (Expression s) = text s
-expr2C (FunCall ref params) = ref2C ref <> parens (hsep . punctuate (char ',') . map expr2C $ params)
 expr2C (BinOp op expr1 expr2) = parens $ (expr2C expr1) <+> op2C op <+> (expr2C expr2)
 expr2C (NumberLiteral s) = text s
 expr2C (HexNumber s) = text "0x" <> (text . map toLower $ s)
 expr2C (StringLiteral s) = doubleQuotes $ text s 
-expr2C (Address ref) = text "&" <> ref2C ref
 expr2C (Reference ref) = ref2C ref
 expr2C (PrefixOp op expr) = op2C op <+> expr2C expr
     {-
@@ -73,11 +71,13 @@
 
 
 ref2C :: Reference -> Doc
-ref2C (ArrayElement (Identifier name) expr) = text name <> brackets (expr2C expr)
+ref2C (ArrayElement exprs ref) = ref2C ref <> (brackets . hcat) (punctuate comma $ map expr2C exprs)
 ref2C (SimpleReference (Identifier name)) = text name
 ref2C (RecordField (Dereference ref1) ref2) = ref2C ref1 <> text "->" <> ref2C ref2
 ref2C (RecordField ref1 ref2) = ref2C ref1 <> text "." <> ref2C ref2
 ref2C (Dereference ref) = parens $ text "*" <> ref2C ref
+ref2C (FunCall params ref) = ref2C ref <> parens (hsep . punctuate (char ',') . map expr2C $ params)
+ref2C (Address ref) = text "&" <> ref2C ref
 
 op2C "or" = text "|"
 op2C "and" = text "&"