|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
| 2 | + |
1 | 3 | module Parser
|
2 | 4 | ( run_parser
|
| 5 | + , parser |
3 | 6 | ) where
|
4 | 7 |
|
5 |
| -import Lexer -- We only use a few features of the Lexer |
| 8 | +import Lexer |
| 9 | + |
| 10 | +import Data.Functor.Identity |
6 | 11 | import Text.Parsec
|
7 | 12 | import Text.Parsec.String
|
8 | 13 | import Text.Parsec.Expr hiding (Operator)
|
9 | 14 |
|
10 |
| -run_parser :: Parser a -> String -> a |
11 |
| -run_parser p str = case parse p "source name" str of |
| 15 | +-- A TMatlab (Typed Matlab) program consists of |
| 16 | +-- a list of statements separated by semicolons or newlines |
| 17 | +-- If the semicolon is missing from the statement, the result of |
| 18 | +-- the expression is printed in the "OUTPUT" section of the Satire DSL |
| 19 | +-- Statements consist only of the form V = E; where V is a variable |
| 20 | +-- and E is an expression or initialization statement |
| 21 | +type TMatlab = [Command] |
| 22 | +data Command = |
| 23 | + Statement Variable Expression Print |
| 24 | + | Pragma Variable Precision Dims |
| 25 | + deriving Show |
| 26 | +data Precision = MSingle | MDouble |
| 27 | + deriving (Show, Eq) |
| 28 | +data Print = Output | Silent |
| 29 | + deriving (Show, Eq) |
| 30 | +type Dims = (Integer, Integer) |
| 31 | +type Variable = String |
| 32 | + |
| 33 | +data Expression = |
| 34 | + Initialize Initializer Dims -- zeros(n,m) or ones(n,1) or eye(n,n) |
| 35 | + | UnaryOp Operator Expression |
| 36 | + | BinaryOp Operator Expression Expression |
| 37 | + | Variable String |
| 38 | + | Scalar Double |
| 39 | + deriving Show |
| 40 | +data Operator = |
| 41 | + Times -- A*B, matrix-matrix or matrix-vector product |
| 42 | + | Minus -- A-B, elementwise subtraction |
| 43 | + | Plus -- A+B, elementwise addition |
| 44 | + | Divide -- A/b, elementwise division |
| 45 | + | Transpose -- A', transpose of matrix or vector |
| 46 | + deriving Show |
| 47 | +data Initializer = Zeros | Ones | Eye | Rand |
| 48 | +instance Show Initializer where |
| 49 | + show Zeros = "zeros" |
| 50 | + show Ones = "ones" |
| 51 | + show Eye = "eye" |
| 52 | + show Rand = "rand" |
| 53 | + |
| 54 | +expr_parser :: Parser Expression |
| 55 | +expr_parser = buildExpressionParser expr_table term <?> "expression" |
| 56 | +expr_table = [ |
| 57 | + [postfix "'" (UnaryOp Transpose)], |
| 58 | + [ |
| 59 | + binary "*" (BinaryOp Times) AssocLeft, |
| 60 | + binary "/" (BinaryOp Divide) AssocLeft |
| 61 | + ], |
| 62 | + [ |
| 63 | + binary "+" (BinaryOp Plus) AssocLeft, |
| 64 | + binary "-" (BinaryOp Minus) AssocLeft |
| 65 | + ] ] |
| 66 | + |
| 67 | +initializer :: Parser Initializer |
| 68 | +initializer = |
| 69 | + (reserved "zeros" >> return Zeros) |
| 70 | + <|> (reserved "ones" >> return Ones) |
| 71 | + <|> (reserved "eye" >> return Eye) |
| 72 | + <|> (reserved "rand" >> return Rand) |
| 73 | + <?> "zeros, ones, eye, rand as initializer" |
| 74 | + |
| 75 | +precision :: Parser Precision |
| 76 | +precision = |
| 77 | + (reserved "float" >> return MSingle) |
| 78 | + <|> (reserved "double" >> return MDouble) |
| 79 | + <?> "expected 'float' or 'double' precision" |
| 80 | + |
| 81 | +dims :: Parser Dims |
| 82 | +dims = do |
| 83 | + args <- parens $ commaSep $ natural |
| 84 | + case length args of |
| 85 | + 1 -> return ((args !! 0), (args !! 0)) |
| 86 | + 2 -> return ((args !! 0), (args !! 1)) |
| 87 | + _ -> error "shape should be (N) for NxN or (M,N) for MxN" |
| 88 | + |
| 89 | +term :: Parser Expression |
| 90 | +term = parens expr_parser |
| 91 | + <|> do |
| 92 | + i <- initializer |
| 93 | + shape <- dims |
| 94 | + return $ Initialize i shape |
| 95 | + <|> fmap Scalar float |
| 96 | + <|> fmap Variable identifier |
| 97 | + <?> "variable, initializer, or numeric scalar" |
| 98 | +binary op fun assoc = Infix (do { reservedOp op; return fun }) assoc |
| 99 | +postfix op fun = Postfix (do { reservedOp op; return fun }) |
| 100 | + |
| 101 | +command :: Parser Command |
| 102 | +command = |
| 103 | + try statement <|> pragma |
| 104 | + <?> "expecting statement or type information" |
| 105 | + |
| 106 | +pragma :: Parser Command |
| 107 | +pragma = do |
| 108 | + v <- identifier |
| 109 | + reservedOp "::" |
| 110 | + p <- precision |
| 111 | + shape <- dims |
| 112 | + return $ Pragma v p shape |
| 113 | + |
| 114 | +statement :: Parser Command |
| 115 | +statement = do |
| 116 | + l <- identifier |
| 117 | + reservedOp "=" |
| 118 | + e <- expr_parser |
| 119 | + print <- (semi >> return Silent) <|> (return Output) |
| 120 | + return $ Statement l e print |
| 121 | + |
| 122 | +-- IDEA: Make a comment a "statement", inserting it in the outputted file to help |
| 123 | +-- annotate it |
| 124 | + |
| 125 | +parser :: Parser TMatlab |
| 126 | +parser = do |
| 127 | + whiteSpace |
| 128 | + ss <- many command |
| 129 | + eof |
| 130 | + return ss |
| 131 | + |
| 132 | +run_parser :: Stream s Identity t => Parsec s () p -> SourceName -> s -> p |
| 133 | +run_parser p fn st = case parse p fn st of |
12 | 134 | Left error_string -> error ("parse error: " ++ show error_string)
|
13 |
| - Right parsed -> parsed |
| 135 | + Right parsed -> parsed |
14 | 136 |
|
15 |
| -someFunc :: IO () |
16 |
| -someFunc = putStrLn "someFunc" |
|
0 commit comments