Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
V0ldek committed Nov 22, 2020
0 parents commit f0d4425
Show file tree
Hide file tree
Showing 186 changed files with 4,465 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.stack-work/
*~
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changelog for Latte

## Unreleased changes
30 changes: 30 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright Author name here (c) 2020

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Author name here nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88 changes: 88 additions & 0 deletions Latte.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.33.0.
--
-- see: https://github.com/sol/hpack
--
-- hash: 2b502e849a4d30c56ccaaf4ce67a20d259c4113d37531ecb74bd01190542c4c0

name: Latte
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/Latte#readme>
homepage: https://github.com/githubuser/Latte#readme
bug-reports: https://github.com/githubuser/Latte/issues
author: Author name here
maintainer: [email protected]
copyright: 2020 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
ChangeLog.md

source-repository head
type: git
location: https://github.com/githubuser/Latte

library
exposed-modules:
ErrM
Syntax.Abs
Syntax.Lexer
Syntax.Parser
Syntax.Printer
other-modules:
Paths_Latte
hs-source-dirs:
src
build-depends:
array >=0.5 && <0.6
, base >=4.7 && <5
, containers >=0.6 && <0.7
, directory >=1.3 && <1.4
, filepath >=1.4 && <1.5
, hspec >=2.7 && <2.8
, mtl >=2.2 && <2.3
, process >=1.6 && <1.7
default-language: Haskell2010

executable latc
main-is: Main.hs
other-modules:
Paths_Latte
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
build-depends:
Latte
, array >=0.5 && <0.6
, base >=4.7 && <5
, containers >=0.6 && <0.7
, directory >=1.3 && <1.4
, filepath >=1.4 && <1.5
, hspec >=2.7 && <2.8
, mtl >=2.2 && <2.3
, process >=1.6 && <1.7
default-language: Haskell2010

test-suite Latte-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
SemanticAnalysis.AcceptanceSpec
Paths_Latte
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
build-depends:
Latte
, array >=0.5 && <0.6
, base >=4.7 && <5
, containers >=0.6 && <0.7
, directory >=1.3 && <1.4
, filepath >=1.4 && <1.5
, hspec >=2.7 && <2.8
, mtl >=2.2 && <2.3
, process >=1.6 && <1.7
default-language: Haskell2010
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Latte
2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
79 changes: 79 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module Main where

import Control.Monad (unless, when)
import System.Environment (getArgs)
import System.Exit (exitFailure, exitSuccess)
import System.IO (hPutStr, hPutStrLn, stderr)

import ErrM (toEither)
import Syntax.Lexer (Token)
import Syntax.Parser (myLexer, pProgram)
import Syntax.Printer (Print, printTree)

type Err = Either String

type ParseFun a = [Token] -> Err a

type Verbosity = Int

putStrV :: Verbosity -> String -> IO ()
putStrV v s = when (v > 1) $ putStrLn s

putStrErrV :: Verbosity -> String -> IO ()
putStrErrV v s = when (v > 1) $ hPutStrLn stderr s

putStrErr :: String -> IO ()
putStrErr = hPutStr stderr

putStrErrLn :: String -> IO ()
putStrErrLn = hPutStrLn stderr

unlessM :: Monad m => m Bool -> m () -> m ()
unlessM p a = do
b <- p
unless b a

runFile :: (Print a, Show a) => Verbosity -> ParseFun a -> FilePath -> IO ()
runFile v p f = putStrLn f >> readFile f >>= run v p

run :: (Print a, Show a) => Verbosity -> ParseFun a -> String -> IO ()
run v p s = case p ts of
Left err -> do
putStrErrLn "ERROR"
putStrErrV v "Tokens:"
putStrErrV v $ show ts
putStrErrLn err
exitFailure
Right tree -> do
putStrErrLn "OK"
showTree v tree
exitSuccess
where
ts = myLexer s

showTree :: (Show a, Print a) => Int -> a -> IO ()
showTree v tree
= do
putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree
putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree

usage :: IO ()
usage = do
putStrLn $
unlines
[ "Latte compiler.",
"Usage: Call with one of the following argument combinations:",
" --help Display this help message.",
" (file) Compile content of the file.",
" -v (file) Verbose mode. Compile content of the file verbosely."
]
exitFailure

main :: IO ()
main = do
args <- getArgs
case args of
["--help"] -> usage
"-v" : [f] -> runFile 2 (toEither . pProgram) f
[f] -> runFile 0 (toEither . pProgram) f
_ -> usage
57 changes: 57 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Latte
version: 0.1.0.0
github: "githubuser/Latte"
license: BSD3
author: "Author name here"
maintainer: "[email protected]"
copyright: "2020 Author name here"

extra-source-files:
- README.md
- ChangeLog.md

# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/Latte#readme>

dependencies:
- base >= 4.7 && < 5
- array >= 0.5 && < 0.6
- mtl >= 2.2 && < 2.3
- containers >= 0.6 && < 0.7
- directory >= 1.3 && < 1.4
- filepath >= 1.4 && < 1.5
- process >= 1.6 && < 1.7
- hspec >= 2.7 && < 2.8

library:
source-dirs: src

executables:
latc:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
- -Wall
dependencies:
- Latte

tests:
Latte-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
- -Wall
dependencies:
- Latte
44 changes: 44 additions & 0 deletions src/ErrM.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- BNF Converter: Error Monad
-- Copyright (C) 2004 Author: Aarne Ranta

-- This file comes with NO WARRANTY and may be used FOR ANY PURPOSE.
module ErrM where

-- the Error monad: like Maybe type with error msgs

import Control.Applicative (Alternative (..))
import Control.Monad (MonadPlus (..), liftM)

data Err a = Ok a | Bad String
deriving (Read, Show, Eq, Ord)

instance Monad Err where
return = Ok
Ok a >>= f = f a
Bad s >>= _ = Bad s

instance MonadFail Err where
fail = Bad

instance Applicative Err where
pure = Ok
(Bad s) <*> _ = Bad s
(Ok f) <*> o = liftM f o


instance Functor Err where
fmap = liftM

instance MonadPlus Err where
mzero = Bad "Err.mzero"
mplus (Bad _) y = y
mplus x _ = x

instance Alternative Err where
empty = mzero
(<|>) = mplus

toEither :: Err a -> Either String a
toEither e = case e of
Ok a -> Right a
Bad s -> Left s
Loading

0 comments on commit f0d4425

Please sign in to comment.