Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81248 / clex / src
diff options
context:
space:
mode:
Commitaeb0d4f887a0c18c36b59acc7b5df08e62020c50 (patch)
Author
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2020-10-11 21:32:48 +0300
Committer
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2020-10-11 21:33:38 +0300
Filesb6abdc6e1810326b587f2a160f39570f1f568f27 /homework/81248/clex/src
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.tar
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.tar.gz
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.zip
Parent154b7cd30f21af1554ca26986ff1643e28857727 (diff)
Removed old homeworks
2 files changed, 0 insertions, 558 deletions
-rw-r--r--homework/81248/clex/src/Clex/Lex.hs181
-rw-r--r--homework/81248/clex/src/Clex/Types.hs377
diff --git a/homework/81248/clex/src/Clex/Lex.hs b/homework/81248/clex/src/Clex/Lex.hs
deleted file mode 100644
index ae78c5d..0000000
--- a/homework/81248/clex/src/Clex/Lex.hs
+++ /dev/null
@@ -1,181 +0,0 @@
-{-# LANGUAGE OverloadedStrings #-}
-
-module Clex.Lex
- ( lexC
- ) where
-
-
-import Prelude hiding (takeWhile)
-
-import Data.Foldable (asum)
-import Data.Word8 ( Word8, isHexDigit, isOctDigit, isAlpha
- , _e, _E, _x, _X,_f, _F, _u, _U, _l, _L, _0
- , _plus, _hyphen, _underscore, _quotesingle
- , _quotedbl, _backslash, _period)
-
-import Data.Attoparsec.ByteString
-import Data.Attoparsec.ByteString.Char8 (isDigit_w8, isSpace_w8)
-
-import Data.ByteString (ByteString)
-import qualified Data.ByteString as BS
-
-import Control.Applicative ((<|>))
-
-import Clex.Types
-
-
-lexC :: ByteString -> Either String [Token]
-lexC = parseOnly (cParser <* endOfInput)
-
-cParser :: Parser [Token]
-cParser = many' tokenParser
-
--- Order is important! For example we need to first check for a floating point number
--- with a leading digit, instead of one without, because the second one will always match the first one.
-tokenParser :: Parser Token
-tokenParser = asum $ concat
- [ [whitespaceParser]
- , [ scientificParser, leadingDotParser, dotFloatParser
- , hexNumParser, octNumParser, numParser, charParser, stringParser
- ]
- , map keywordParser allKeyWords
- , map operParser allOpers
- , [identifierParser]]
-
-whitespaceParser :: Parser Token
-whitespaceParser = Whitespace <$> takeWhile1 isSpace_w8
-
-keywordParser :: ByteString -> Parser Token
-keywordParser kw = do
- parsed <- recognize <$> string kw
- -- we need to check if we still have more letters after the keyword
- next <- peekWord8
- let parsed' = pure parsed
- case next of
- Nothing -> parsed'
- (Just x) -> if (not . isAlpha) x then parsed'
- else fail "more input after keyword"
-
-operParser :: ByteString -> Parser Token
-operParser = (recognize <$>) . string
-
-isDigit :: Word8 -> Bool
-isDigit = isDigit_w8
-
-isLetter :: Word8 -> Bool
-isLetter b = isAlpha b
- || b == _underscore
-
-exponentParser :: Parser ByteString
-exponentParser = do
- e <- satisfy (\c -> c == _E || c == _e)
- sign <- option "" plusMinus
- digits <- takeWhile1 isDigit_w8
- pure $ BS.singleton e <> sign <> digits
- where
- plusMinus = BS.singleton <$> satisfy (\c -> c == _plus || c == _hyphen) -- plus or minus
-
-float :: Parser Word8
-float = satisfy $
- \c -> c == _F
- || c == _f
- || c == _L
- || c == _l
-
-unsigned :: Parser ByteString
-unsigned = BS.pack <$> many' (satisfy $
- \c -> c == _U
- || c == _u
- || c == _L
- || c == _l)
-
-isxX :: Word8 -> Bool
-isxX c = c == _X
- || c == _x
-
-singleQuote :: Parser Word8
-singleQuote = word8 _quotesingle
-
-doubleQuote :: Parser Word8
-doubleQuote = word8 _quotedbl
-
-charParser :: Parser Token
-charParser = do
- l <- option "" (BS.singleton <$> word8 _L)
- _ <- singleQuote
- str <- BS.concat <$> many1' ((BS.snoc <$> string "\\" <*> anyWord8) <|> notInWeirdClass)
- sq <- singleQuote
-
- pure $ CharLit ((l <> (sq `BS.cons` str)) `BS.snoc` sq)
- where
- notInWeirdClass = BS.singleton <$>
- satisfy (\c -> c /= _backslash
- && c /= _quotesingle)
-
-stringParser :: Parser Token
-stringParser = do
- l <- option "" (BS.singleton <$> word8 _L)
- _ <- doubleQuote
- str <- BS.concat <$> many' ((BS.snoc <$> string "\\" <*> anyWord8) <|> notInWeirdClass)
- dq <- doubleQuote
-
- pure $ StringLit ((l <> (dq `BS.cons` str)) `BS.snoc` dq)
- where
- notInWeirdClass = BS.singleton <$>
- satisfy (\c -> c /= _backslash
- && c /= _quotedbl)
-
-zero :: Parser Word8
-zero = word8 _0
-
-hexNumParser :: Parser Token
-hexNumParser = do
- z <- zero
- x <- satisfy isxX
- hex <- takeWhile1 isHexDigit
- f <- unsigned
-
- pure $ HexNumber (z `BS.cons` x `BS.cons` hex <> f)
-
-octNumParser :: Parser Token
-octNumParser = do
- z <- zero
- oct <- takeWhile1 isOctDigit
- f <- unsigned
-
- pure $ OctNumber (z `BS.cons` oct <> f)
-
-numParser :: Parser Token
-numParser = do
- num <- takeWhile1 isDigit
- f <- unsigned
-
- pure $ Number (num <> f)
-
-scientificParser :: Parser Token
-scientificParser = do
- digits <- takeWhile1 isDigit
- expon <- exponentParser
- f <- option "" (BS.singleton <$> float)
- pure $ Scientific $ digits <> expon <> f
-
-dotFloatParser :: Parser Token
-dotFloatParser = do
- leadingDigits <- takeWhile isDigit
- d <- BS.singleton <$> word8 _period
- afterDigits <- takeWhile1 isDigit
- expon <- option "" exponentParser
- f <- option "" (BS.singleton <$> float)
- pure $ DotFloat $ leadingDigits <> d <> afterDigits <> expon <> f
-
-leadingDotParser :: Parser Token
-leadingDotParser = do
- leadingDigits <- takeWhile1 isDigit
- d <- BS.singleton <$> word8 _period
- afterDigits <- takeWhile isDigit
- expon <- option "" exponentParser
- f <- option "" (BS.singleton <$> float)
- pure $ LeadingDot $ leadingDigits <> d <> afterDigits <> expon <> f
-
-identifierParser :: Parser Token
-identifierParser = Identifier <$> takeWhile1 isLetter
diff --git a/homework/81248/clex/src/Clex/Types.hs b/homework/81248/clex/src/Clex/Types.hs
deleted file mode 100644
index 9083514..0000000
--- a/homework/81248/clex/src/Clex/Types.hs
+++ /dev/null
@@ -1,377 +0,0 @@
-{-# LANGUAGE OverloadedStrings #-}
-
-module Clex.Types
- ( Token(..)
- , allKeyWords
- , allOpers
- , recognize
- , tokenToBS
- , tokensToBS
- ) where
-
-import Data.ByteString (ByteString)
-import qualified Data.ByteString as BS
-
-data Token =
- Whitespace ByteString
- | Auto
- | Break
- | Case
- | Char
- | Const
- | Continue
- | Default
- | Do
- | Double
- | Else
- | Enum
- | Extern
- | Float
- | For
- | Goto
- | If
- | Int
- | Long
- | Register
- | Return
- | Short
- | Signed
- | Sizeof
- | Static
- | Struct
- | Switch
- | Typedef
- | Union
- | Unsigned
- | Void
- | Volatile
- | While
- | Ellipsis
- | RightShiftAssign
- | LeftShiftAssign
- | AddAssign
- | SubAssign
- | MulAssign
- | DivAssign
- | ModAssign
- | AndAssign
- | XorAssign
- | OrAssign
- | Incr
- | Decr
- | Ptr
- | Dot
- | And
- | Or
- | Not
- | LtEq
- | GtEq
- | Eq
- | NEq
- | Lt
- | Gt
- | Semicolon
- | LeftCurly
- | RightCurly
- | Comma
- | Colon
- | Assign
- | LeftBracket
- | RightBracket
- | LeftSquare
- | RightSquare
- | RightShift
- | LeftShift
- | BitAnd
- | BitOr
- | BitNot
- | Sub
- | Add
- | Mul
- | Div
- | Mod
- | Xor
- | Ternary
- | HexNumber ByteString
- | OctNumber ByteString
- | Number ByteString
- | CharLit ByteString
- | Scientific ByteString
- | DotFloat ByteString
- | LeadingDot ByteString
- | StringLit ByteString
- | Identifier ByteString
- deriving (Show, Eq)
-
--- Mapping from ByteStrings to Tokens
-recognize :: ByteString -> Token
--- keywords
-recognize "auto" = Auto
-recognize "break" = Break
-recognize "case" = Case
-recognize "char" = Char
-recognize "const" = Const
-recognize "continue" = Continue
-recognize "default" = Default
-recognize "do" = Do
-recognize "double" = Double
-recognize "else" = Else
-recognize "enum" = Enum
-recognize "extern" = Extern
-recognize "float" = Float
-recognize "for" = For
-recognize "goto" = Goto
-recognize "if" = If
-recognize "int" = Int
-recognize "long" = Long
-recognize "register" = Register
-recognize "return" = Return
-recognize "short" = Short
-recognize "signed" = Signed
-recognize "sizeof" = Sizeof
-recognize "static" = Static
-recognize "struct" = Struct
-recognize "switch" = Switch
-recognize "typedef" = Typedef
-recognize "union" = Union
-recognize "unsigned" = Unsigned
-recognize "void" = Void
-recognize "volatile" = Volatile
-recognize "while" = While
-
--- operators
---
--- two-byte
-recognize "..." = Ellipsis
-recognize ">>=" = RightShiftAssign
-recognize "<<=" = LeftShiftAssign
-recognize "+=" = AddAssign
-recognize "-=" = SubAssign
-recognize "*=" = MulAssign
-recognize "/=" = DivAssign
-recognize "%=" = ModAssign
-recognize "&=" = AndAssign
-recognize "^=" = XorAssign
-recognize "|=" = OrAssign
-recognize "++" = Incr
-recognize "--" = Decr
-recognize "->" = Ptr
-recognize "&&" = And
-recognize "||" = Or
-recognize "<=" = LtEq
-recognize ">=" = GtEq
-recognize "==" = Eq
-recognize "!=" = NEq
-recognize ">>" = RightShift
-recognize "<<" = LeftShift
-
--- one-byte
-recognize "." = Dot
-recognize "<" = Lt
-recognize ">" = Gt
-recognize "&" = BitAnd
-recognize "|" = BitOr
-recognize "~" = BitNot
-recognize "!" = Not
-recognize ";" = Semicolon
-recognize "{" = LeftCurly
-recognize "}" = RightCurly
-recognize "," = Comma
-recognize ":" = Colon
-recognize "=" = Assign
-recognize "(" = LeftBracket
-recognize ")" = RightBracket
-recognize "[" = LeftSquare
-recognize "]" = RightSquare
-recognize "-" = Sub
-recognize "+" = Add
-recognize "*" = Mul
-recognize "/" = Div
-recognize "%" = Mod
-recognize "^" = Xor
-recognize "?" = Ternary
-
--- if all else fails an identifier
-recognize x = Identifier x
-
-allKeyWords :: [ByteString]
-allKeyWords =
- [ "auto"
- , "break"
- , "case"
- , "char"
- , "const"
- , "continue"
- , "default"
- , "do"
- , "double"
- , "else"
- , "enum"
- , "extern"
- , "float"
- , "for"
- , "goto"
- , "if"
- , "int"
- , "long"
- , "register"
- , "return"
- , "short"
- , "signed"
- , "sizeof"
- , "static"
- , "struct"
- , "switch"
- , "typedef"
- , "union"
- , "unsigned"
- , "void"
- , "volatile"
- , "while"
- ]
-
--- Opers need to be seperate because they have different rules
--- for formation. For example you can have @"()()()"@, but you can't
--- have @"constconstconst"@.
-allOpers :: [ByteString]
-allOpers =
- [ "..."
- , ">>="
- , "<<="
- , "+="
- , "-="
- , "*="
- , "/="
- , "%="
- , "&="
- , "^="
- , "|="
- , "++"
- , "--"
- , "->"
- , "&&"
- , "||"
- , "<="
- , ">="
- , "=="
- , "!="
- , ">>"
- , "<<"
- , "."
- , "<"
- , ">"
- , "&"
- , "|"
- , "~"
- , "!"
- , ";"
- , "{"
- , "}"
- , ","
- , ":"
- , "="
- , "("
- , ")"
- , "["
- , "]"
- , "-"
- , "+"
- , "*"
- , "/"
- , "%"
- , "^"
- , "?"
- ]
-
-tokensToBS :: [Token] -> ByteString
-tokensToBS = BS.concat . map tokenToBS
-
-tokenToBS :: Token -> ByteString
-tokenToBS (Whitespace bs) = bs
-tokenToBS Auto = "auto"
-tokenToBS Break = "break"
-tokenToBS Case = "case"
-tokenToBS Char = "char"
-tokenToBS Const = "const"
-tokenToBS Continue = "continue"
-tokenToBS Default = "default"
-tokenToBS Do = "do"
-tokenToBS Double = "double"
-tokenToBS Else = "else"
-tokenToBS Enum = "enum"
-tokenToBS Extern = "extern"
-tokenToBS Float = "float"
-tokenToBS For = "for"
-tokenToBS Goto = "goto"
-tokenToBS If = "if"
-tokenToBS Int = "int"
-tokenToBS Long = "long"
-tokenToBS Register = "register"
-tokenToBS Return = "return"
-tokenToBS Short = "short"
-tokenToBS Signed = "signed"
-tokenToBS Sizeof = "sizeof"
-tokenToBS Static = "static"
-tokenToBS Struct = "struct"
-tokenToBS Switch = "switch"
-tokenToBS Typedef = "typedef"
-tokenToBS Union = "union"
-tokenToBS Unsigned = "unsigned"
-tokenToBS Void = "void"
-tokenToBS Volatile = "volatile"
-tokenToBS While = "while"
-tokenToBS Ellipsis = "..."
-tokenToBS RightShiftAssign = ">>="
-tokenToBS LeftShiftAssign = "<<="
-tokenToBS AddAssign = "+="
-tokenToBS SubAssign = "-="
-tokenToBS MulAssign = "*="
-tokenToBS DivAssign = "/="
-tokenToBS ModAssign = "%="
-tokenToBS AndAssign = "&="
-tokenToBS XorAssign = "^="
-tokenToBS OrAssign = "|="
-tokenToBS Incr = "++"
-tokenToBS Decr = "--"
-tokenToBS Ptr = "->"
-tokenToBS Dot = "."
-tokenToBS And = "&&"
-tokenToBS Or = "||"
-tokenToBS Not = "!"
-tokenToBS LtEq = "<="
-tokenToBS GtEq = ">="
-tokenToBS Eq = "=="
-tokenToBS NEq = "!="
-tokenToBS Lt = "<"
-tokenToBS Gt = ">"
-tokenToBS Semicolon = ";"
-tokenToBS LeftCurly = "{"
-tokenToBS RightCurly = "}"
-tokenToBS Comma = ","
-tokenToBS Colon = ":"
-tokenToBS Assign = "="
-tokenToBS LeftBracket = "("
-tokenToBS RightBracket = ")"
-tokenToBS LeftSquare = "["
-tokenToBS RightSquare = "]"
-tokenToBS RightShift = ">>"
-tokenToBS LeftShift = "<<"
-tokenToBS BitAnd = "&"
-tokenToBS BitOr = "|"
-tokenToBS BitNot = "~"
-tokenToBS Sub = "-"
-tokenToBS Add = "+"
-tokenToBS Mul = "*"
-tokenToBS Div = "/"
-tokenToBS Mod = "%"
-tokenToBS Xor = "^"
-tokenToBS Ternary = "?"
-tokenToBS (HexNumber bs) = bs
-tokenToBS (OctNumber bs) = bs
-tokenToBS (Number bs) = bs
-tokenToBS (CharLit bs) = bs
-tokenToBS (Scientific bs) = bs
-tokenToBS (DotFloat bs) = bs
-tokenToBS (LeadingDot bs) = bs
-tokenToBS (StringLit bs) = bs
-tokenToBS (Identifier bs) = bs