cleaned unused code + explicitly defined imports/exports

This commit is contained in:
2024-03-03 12:56:57 -06:00
parent 265e7d4830
commit 41f95419d0
3 changed files with 43 additions and 100 deletions

View File

@@ -8,84 +8,6 @@ import Parser (Parser, parseProgram)
import System.Environment (getArgs)
import Text.Megaparsec (errorBundlePretty, parse)
-- GHCI test inputs
file :: Text
file =
"---\n\
\Here is a brief sample of the\n\
\language and its syntax\n\
\---\n\
\\n\
\-- Import statements\n\
\import std.collections {\n\
\ Heap,\n\
\ HashMap,\n\
\}\n\
\\n\
\-- Function declaration with types\n\
\fn fizz-buzz(i: Num): Str {\n\
\ --- This is technically a legal comment ---\n\
\ let fb = \"\";\n\
\\n\
\ if i % 3 == 0 {\n\
\ fb = fb ++ \"fizz\";\n\
\ }\n\
\\n\
\ if i % 5 == 0 {\n\
\ fb = fb ++ \"buzz\";\n\
\ }\n\
\\n\
\ if is-empty(fb) {\n\
\ return i;\n\
\ } else {\n\
\ return fb;\n\
\ }\n\
\}\n\
\\n\
\-- Function declaration without types\n\
\fn main() {\n\
\ const fb: Str = fizz-buzz();\n\
\ print(fb);\n\
\}"
blockComment :: Text
blockComment =
"---\n\
\Here is a brief sample of the\n\
\language and its syntax\n\
\---"
singleBlockComment :: Text
singleBlockComment = "--- This is technically a legal comment ---"
singleComment :: Text
singleComment = "-- Import statements"
importStatement :: Text
importStatement =
"import std.collections {\n\
\ Heap,\n\
\ HashMap,\n\
\}"
function :: Text
function =
"fn fizz-buzz(i: Num): Str {\n\
\ if i % 3 == 0 {\n\
\ fb = fb ++ \"fizz\";\n\
\ }\n\
\ return false;\n\
\}"
conditional :: Text
conditional =
"if i % 3 == 0 {\n\
\ fb = fb ++ \"fizz\";\n\
\}"
-- Runners
printParser :: (Show s) => Parser s -> Text -> IO ()
printParser parser input = case parse parser "" input of
Left err -> putStrLn $ errorBundlePretty err

View File

@@ -1,21 +1,43 @@
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use <$>" #-}
module Parser (Parser, parseProgram) where
module Parser where
import Control.Applicative hiding (Const, many, some)
import Control.Monad (void)
import Control.Monad.Combinators.Expr (Operator (..), makeExprParser)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Void (Void)
import Syntax
import Text.Megaparsec hiding (State)
( Program(Program),
Statement(..),
Expression(Variable, FunctionCall, UnaryOperation, BinaryOperation,
LiteralInt, LiteralFloat, LiteralBool, LiteralString, LiteralChar),
Identifier,
IdentifierWithType(..),
UnaryOperator(..),
BinaryOperator(..),
Block )
import Text.Megaparsec
( MonadParsec(try),
Parsec,
many,
choice,
between,
sepEndBy,
manyTill,
anySingle,
sepBy1,
optional )
import Text.Megaparsec.Char
( space,
char,
string,
newline,
space1,
letterChar,
alphaNumChar )
import Text.Megaparsec.Debug (MonadParsecDbg (dbg))
import Text.Read (readMaybe)
import qualified Text.Megaparsec.Char.Lexer as L
type Parser = Parsec Void Text
@@ -170,9 +192,8 @@ term =
let parseFunctionCall = try $ FunctionCall <$> parseIdentifier <*> parseList' (char '(') (char ')') (char ',') parseExpression
parseUnaryOperation = try $ UnaryOperation <$> parseUnaryOperator <*> term
in choice
[
parseInt,
-- parseFloat,
[ parseInt,
parseFloat,
parseTrue,
parseFalse,
parseChar,
@@ -219,17 +240,10 @@ parseUnaryOperator =
-- Numbers
parseInt :: Parser Expression
parseInt = do
sign <- optional . try $ char '-'
magnitude <- many digitChar
space
let value = maybe magnitude (: magnitude) sign
in case readMaybe value of
Just val -> return $ LiteralInt val
Nothing -> fail ""
parseInt = LiteralInt <$> L.decimal
parseFloat :: Parser Expression
parseFloat = undefined
parseFloat = LiteralFloat <$> L.float
-- signed
-- intPart <- many1 digit

View File

@@ -1,6 +1,13 @@
{-# LANGUAGE OverloadedStrings #-}
module Syntax where
module Syntax
( Program(Program),
Statement(..),
Expression(Variable, FunctionCall, UnaryOperation, BinaryOperation,
LiteralInt, LiteralFloat, LiteralBool, LiteralString, LiteralChar),
Identifier,
IdentifierWithType(..),
UnaryOperator(..),
BinaryOperator(..),
Block) where
import Data.Text (Text)