Changeset 32 for src/Parser.hs

Show
Ignore:
Timestamp:
02/16/05 18:48:28 (4 years ago)
Author:
autrijus
svk:copy_cache_prev:
1041
Message:

* Code literals -- "sub", "pointy" and "bare" variants all works
* Lexical subroutine declarations via "my sub"
* Global subroutine and variables work again, via the envGlobal pad
* The "say", "exit", "die" primitives
* Bool.perl now prints correct literals (lwall)
* Blocks under void contexts now evaluates automatically
* The "..." (dotdotdot) literal

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Parser.hs

    r31 r32  
    1313import Internals 
    1414import AST 
     15import Help 
    1516import Lexer 
    1617 
     
    5657    , rulePackageDeclaration 
    5758    , ruleVarDeclaration 
     59    , ruleUseDeclaration 
    5860    ] 
    5961 
     
    8789        , ruleSubGlobal 
    8890        ] 
    89     pos     <- getPosition 
    9091    cxt2    <- option cxt1 $ ruleBareTrait "returns" 
    9192    formal  <- option Nothing $ return . Just =<< parens ruleSubParameters 
     
    148149    scope   <- ruleScope 
    149150    name    <- parseVarName 
    150     return $ Syn "sym" [Sym (Symbol scope name (Val VUndef))] 
     151    exp     <- option (Val VUndef) $ do 
     152        tryChoice $ map symbol $ words " = := ::= " 
     153        ruleExpression 
     154    return $ Syn "sym" [Sym $ Symbol scope name exp] 
     155 
     156ruleUseDeclaration :: RuleParser Exp 
     157ruleUseDeclaration = rule "use declaration" $ do 
     158    symbol "use" 
     159    option ' ' $ char 'v' 
     160    version <- many1 (choice [ digit, char '.' ]) 
     161    when (version > versnum) $ do 
     162        pos <- getPosition 
     163        error $ "Perl v" ++ version ++ " required--this is only v" ++ versnum ++ ", stopped at " ++ (show pos) 
     164    return $ Val VUndef 
    151165 
    152166rulePackageDeclaration = rule "package declaration" $ fail "" 
     
    156170ruleConstruct = rule "construct" $ tryChoice 
    157171    [ ruleGatherConstruct 
    158     , ruleBlockConstruct 
    159172    ] 
    160173 
     
    163176    block <- ruleBlock 
    164177    retSyn "gather" [block] 
    165  
    166 ruleBlockConstruct = rule "block construct" $ do 
    167     formal <- option Nothing $ choice [ ruleBlockFormalStandard, ruleBlockFormalPointy ] 
    168     block <- ruleBlock 
    169     fail "" 
    170  
    171 ruleBlockFormalStandard = rule "standard block parameters" $ do 
    172     symbol "sub" 
    173     return . Just =<< parens ruleSubParameters 
    174  
    175 ruleBlockFormalPointy = rule "pointy block parameters" $ do 
    176     symbol "->" 
    177     return . Just =<< ruleSubParameters 
    178178 
    179179ruleCondConstruct = rule "conditional construct" $ fail "" 
     
    187187ruleExpression = (<?> "expression") $ parseOp 
    188188 
     189ruleBlockLiteral = rule "block construct" $ do 
     190    (typ, formal) <- option (SubBlock, Nothing) $ choice 
     191        [ ruleBlockFormalStandard 
     192        , ruleBlockFormalPointy 
     193        ] 
     194    body <- ruleBlock 
     195    let (fun, names) = extract (body, []) 
     196        params = (maybe [] id formal) ++ map nameToParam names 
     197    -- Check for placeholder vs formal parameters 
     198    unless (isNothing formal || null names || names == ["$_"] ) $ 
     199        fail "Cannot mix placeholder variables with formal parameters" 
     200    let sub = Sub { isMulti       = False 
     201                  , subName       = "<anon>" 
     202                  , subPad        = [] 
     203                  , subType       = typ 
     204                  , subAssoc      = "pre" 
     205                  , subReturns    = "Any" 
     206                  , subParams     = if null params then [defaultArrayParam] else params 
     207                  , subFun        = fun 
     208                  } 
     209    return (Val $ VSub sub) 
     210 
     211ruleBlockFormalStandard = rule "standard block parameters" $ do 
     212    symbol "sub" 
     213    params <- option Nothing $ return . Just =<< parens ruleSubParameters 
     214    return $ (SubRoutine, params) 
     215 
     216ruleBlockFormalPointy = rule "pointy block parameters" $ do 
     217    symbol "->" 
     218    params <- ruleSubParameters 
     219    return $ (SubBlock, Just params) 
     220 
    189221 
    190222 
     
    211243    , postOps  " ++ -- "                                -- Auto-Increment 
    212244    , rightOps " ** "                                   -- Exponentiation 
    213     , preOps   " ! + - ~ ? * ** +^ ~^ ?^ \\ "           -- Symbolic Unary 
     245--  , preOps   " ! + - ~ ? * ** +^ ~^ ?^ \\ "           -- Symbolic Unary 
     246    , preOps   " ! + ~ ? * ** +^ ~^ ?^ \\ "             -- Symbolic Unary 
    214247    , leftOps  " * / % x xx +& +< +> ~& ~< ~> "         -- Multiplicative 
    215248    , leftOps  " + - ~ +| +^ ~| ~^ "                    -- Additive 
     
    344377    sigil   <- oneOf "$@%&" 
    345378    caret   <- option "" $ choice [ string "^", string "*" ] 
    346     name    <- many1 wordAny 
    347     return $ (sigil:caret) ++ name 
     379    name    <- many1 (choice [ wordAny, char ':' ]) 
     380    return $ if sigil == '&' && not (':' `elem` name) 
     381        then (sigil:caret) ++ "prefix:" ++ name 
     382        else (sigil:caret) ++ name 
    348383 
    349384parseVar = do 
     
    356391 
    357392parseLit = choice 
    358     [ numLiteral 
     393    [ ruleBlockLiteral 
     394    , numLiteral 
    359395    , strLiteral 
    360396    , arrayLiteral