Skip to content

Commit

Permalink
Final update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaisia-Estrel committed Mar 23, 2022
1 parent ebd926c commit 10f4f9d
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist-newstyle
16 changes: 12 additions & 4 deletions Atom-Counter.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: >=1.10
-- http://haskell.org/cabal/users-guide/

name: Atom-Counter
version: 0.1.0.0
version: 0.2.0.0

-- synopsis:
-- description:
Expand All @@ -21,17 +21,25 @@ build-type: Simple
extra-source-files: CHANGELOG.md

library
exposed-modules: MyLib
exposed-modules:
Args
Interactive
Parser
Process
ToString

-- other-modules:
-- other-extensions:
build-depends: base ==4.14.*
build-depends:
base >=4.14 && <4.15
, haskeline >=0.8.2
, lens >= 5.1

hs-source-dirs: src
default-language: Haskell2010

executable Atom-Counter
main-is: Main.hs
other-modules: MyLib

-- other-extensions:
build-depends:
Expand Down
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
* 0.1.0.0 -- 2021-11-18
** First version. Released on an unsuspecting world.

* 0.1.1.0 -- 2021-11-19
** First addition to parsing capability

* 0.1.3.0 -- 2021-11-21
** Interactive mode is built only table mode remaining

* 0.1.4.0 -- 2021-11-22
** Table mode almost complete, bugs to be fixed by the next update

* 1.0.0.0 -- 2021-11-25
** Probably final update, table mode is still ridden with bugs and interactive mode is not as pleasant as it can be though other parts seem to be doing just fine

* 2.0.0.0 -- 2022-03-23
** Was not the final update, went though a major rewrite - still riddled with bugs though.
* Fixed a bunch of the parsing bugs
* Now added mode switching in interactive mode

```text
:q -- Quit
:t -- Table Output
:l -- Latex Output
:s -- Raw Output
:f -- Fold formula to max
:F -- Specify number of folds to do
:h -- Help page
:d -- Subscript Output
:i -- Print options
```
* Accidentally made table mode useless
11 changes: 11 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

The MIT License (MIT)

Copyright © 2021 <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Atom-Counter

simple program for parsing formula names
![Testcases](https://i.imgur.com/UL2KW17.png)

** Just a small project, no longer being developed

![Table Mode](https://i.imgur.com/MliTM6t.png)
The basic syntax in table mode is

- Any element on its own does not get affected

```text
Au_2 -> Au_2
|----+----------+---------|
| | Reactant | Product |
|----+----------+---------|
| Au | 2 | 2 |
|----+----------+---------|
```
13 changes: 13 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Main where

import System.Console.GetOpt
import System.Environment

import Interactive
import Args


main :: IO ()
main = do
args <- compilerOpts <$> getArgs
args >>= (either (putStrLn =<<)putStrLn . lobby)
97 changes: 97 additions & 0 deletions src/Args.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{-# LANGUAGE TemplateHaskell #-}

module Args where

import Data.Maybe
import System.Console.GetOpt
import Control.Lens

data Options = Options
{ _optInteractive :: Bool,
_optHelpPage :: Bool,
_optTable :: Bool,
_optLatex :: Bool,
_optShow :: Bool,
_optToIn :: Bool,
_optVersion :: Bool,
_optFold :: Bool,
_optFolds :: Int,
_optSub :: Bool
}
deriving (Show)

makeLenses ''Options

defaultOptions =
Options
{ _optInteractive = False,
_optHelpPage = False,
_optTable = False,
_optLatex = False,
_optShow = False,
_optToIn = False,
_optVersion = False,
_optFold = False,
_optFolds = 0,
_optSub = False
}

options :: [OptDescr (Options -> Options)]
options =
[ Option
['I']
["interactive"]
(NoArg (\opts -> opts {_optInteractive = True}))
"Use STDIN instead of console arguments, type :q to quit",
Option
['i']
["input"]
(NoArg (\opts -> opts {_optToIn = True}))
"Send output as output recognizable by the input",
Option
['t', 'T']
["table"]
(NoArg (\opts -> opts {_optTable = True}))
"Unsubscripted Numbers gets treated as multipliers, arrays of arguments also get accepted with symbols other than parenthesis acting as delimeters",
Option
['l', 'L']
["latex"]
(NoArg (\opts -> opts {_optLatex = True}))
"Send output as a latex formula",
Option
['v', 'V']
["version"]
(NoArg (\opts -> opts {_optVersion = True}))
"Show program version",
Option
['s', 'S']
["show"]
(NoArg (\opts -> opts {_optShow = True}))
"Show output via actual value in haskell",
Option
['f']
["fold"]
(NoArg (\opts -> opts {_optFold = True}))
"Repeatedly fold formula until at lowest term",
Option
['F']
["folds"]
( OptArg
((\f opts -> opts {_optFolds = read f :: Int}) . fromMaybe "0")
"NUMBER"
)
"Fold formula a number of times",
Option
['D', 'd']
["subScript"]
(NoArg (\opts -> opts {_optSub = True}))
"show output in subscript"
]

compilerOpts :: [String] -> IO (Options, [String])
compilerOpts argv =
case getOpt Permute options argv of
(o, n, []) -> return (foldl (flip id) defaultOptions o, n)
(_, _, errs) -> ioError (userError (concat errs ++ usageInfo header options))
where
header = "Usage: [OPTION...] formula..."
91 changes: 91 additions & 0 deletions src/Interactive.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module Interactive where

import Args
import System.Console.Haskeline

import Parser
import Control.Monad.IO.Class
import Data.Maybe
import Process
import ToString
--import Data.Version as V
--import Paths_Atom_Counter
import Data.Either
import Control.Lens

lobby :: (Options, [String]) -> Either (IO String) String
lobby
opts@( Options
{ _optInteractive = isInteractive
, _optTable = isTableMode
, _optLatex = showLatex
, _optShow = showHaskCode
, _optVersion = showVersion
, _optFold = maxFold
, _optToIn = showIn
, _optFolds = numOfFolds
, _optSub = showSub
},
formula
)
| sum (fromEnum <$> [showHaskCode, showLatex, showIn, showSub]) > 1 = Left $ return "Error: output method mixing"
| isInteractive = Left $ interactive $ (fst opts) {_optInteractive = False}
| isTableMode = maybe (Left $ return "UserError: Table Parse Error") (Right . orgTable . snd) $ runParser reactProdP $ concat formula
-- | showVersion = Right $ "Version: " ++ V.showVersion version
| showHaskCode = maybe message (Right . show) $ runParser parser $ concat formula
| otherwise = maybe message (Right . concatMap (toString showFunc) . snd) $ runParser parser $ concat formula
where
message = Left $ return "UserError: Parse Error"
showFunc
| showSub = toSub
| showLatex = toLatex
| otherwise = toBasic

parser :: Parser [Element]
parser = if maxFold
then flattenForm <$> formulaP
else foldr (.) id (replicate numOfFolds (concatMap foldForm)) . groupForm <$> formulaP


interactive :: Options -> IO String
interactive opts = runInputT defaultSettings $ loop opts
where
loop :: Options -> InputT IO String
loop opts = do
minput <- getInputLine "AtomC> "
case minput of
Nothing -> return "Unexpected exit"
Just ":q" -> return "Exited Program"
Just ":t" -> loop $ opts & optTable .~ True
Just ":l" -> loop $ opts & optLatex .~ True & optShow .~ False & optSub .~ False & optToIn .~ False
Just ":s" -> loop $ opts & optLatex .~ False & optShow .~ True & optSub .~ False & optToIn .~ False
Just ":f" -> loop $ opts & optFold .~ True
Just ":F" -> do
x <- runInputT defaultSettings $ getInputLine "Set fold amount:"
case (maybeRead =<< x :: Maybe Int) of
Nothing -> loop opts
Just n -> loop $ opts & optFolds .~ n

Just ":h" -> outputStrLn helpPage >> loop opts
Just ":i" -> outputStrLn (show opts) >> loop opts
Just ":d" -> loop $ opts & optLatex .~ False & optShow .~ False & optSub .~ True & optToIn .~ False

Just (':':x) -> outputStrLn ("Unknown command: '" ++ x ++ "'") >> loop opts

Just input -> do outputStrLn $ fromRight "Parse Error" $ lobby (opts, [input])
loop opts

helpPage = unlines
[":q -- Quit"
,":t -- Table Output"
,":l -- Latex Output"
,":s -- Raw Output"
,":f -- Fold formula to max"
,":F -- Specify number of folds to do"
,":h -- Help page"
,":d -- Subscript Output"
,":i -- Print options"
]

maybeRead :: Read a => String -> Maybe a
maybeRead = fmap fst . listToMaybe . reads
Loading

0 comments on commit 10f4f9d

Please sign in to comment.