diff options
author | Marvin Borner | 2022-08-20 22:30:31 +0200 |
---|---|---|
committer | Marvin Borner | 2022-08-20 22:30:31 +0200 |
commit | b68307db49807c83860f4303a05d08f25dbf6375 (patch) | |
tree | 240891b0fd979016502a1e1ec0f207d432936a3e /src/Helper.hs | |
parent | 7e5cae744c3943eae7806c533f65acc5ff8fbe8a (diff) |
Parser shenanigans
Diffstat (limited to 'src/Helper.hs')
-rw-r--r-- | src/Helper.hs | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/src/Helper.hs b/src/Helper.hs index 418735e..d7064c1 100644 --- a/src/Helper.hs +++ b/src/Helper.hs @@ -34,13 +34,13 @@ printContext (Context inp path) = p $ lines inp errPrefix :: String errPrefix = "\ESC[41mERROR\ESC[0m " -data Error = SyntaxError String | UndeclaredFunction String | InvalidIndex Int | FailedTest Expression Expression Expression Expression | ContextualError Error Context | ImportError String +data Error = SyntaxError String | UndeclaredIdentifier String | InvalidIndex Int | FailedTest Expression Expression Expression Expression | ContextualError Error Context | ImportError String instance Show Error where show (ContextualError err ctx) = show err <> "\n" <> (printContext ctx) show (SyntaxError err) = errPrefix <> "invalid syntax\n\ESC[45mnear\ESC[0m " <> err - show (UndeclaredFunction func) = - errPrefix <> "undeclared function " <> show func + show (UndeclaredIdentifier ident) = + errPrefix <> "undeclared identifier " <> ident show (InvalidIndex err) = errPrefix <> "invalid index " <> show err show (FailedTest exp1 exp2 red1 red2) = errPrefix @@ -89,19 +89,35 @@ printBundle ParseErrorBundle {..} = <> pointer <> "\n" -data Expression = Bruijn Int | Variable String | Abstraction Expression | Application Expression Expression | Infix Expression String Expression | Prefix String Expression +data Identifier = NormalFunction String | InfixFunction String | PrefixFunction String | NamespacedFunction String Identifier deriving (Ord, Eq) -data Instruction = Define String Expression [Instruction] | Evaluate Expression | Comment | Input String | Import String String | Test Expression Expression | ContextualInstruction Instruction String +functionName :: Identifier -> String +functionName = \case + NormalFunction f -> f + InfixFunction i -> "(" <> i <> ")" + PrefixFunction p -> p <> "(" + NamespacedFunction n f -> n <> functionName f +instance Show Identifier where + show ident = "\ESC[95m" <> functionName ident <> "\ESC[0m" +data Expression = Bruijn Int | Function Identifier | Abstraction Expression | Application Expression Expression | Infix Expression Identifier Expression | Prefix Identifier Expression + deriving (Ord, Eq) +data Instruction = Define Identifier Expression [Instruction] | Evaluate Expression | Comment | Input String | Import String String | Test Expression Expression | ContextualInstruction Instruction String deriving (Show) instance Show Expression where - show (Bruijn x ) = "\ESC[91m" <> show x <> "\ESC[0m" - show (Variable var) = "\ESC[95m" <> var <> "\ESC[0m" - show (Abstraction e ) = "\ESC[36m[\ESC[0m" <> show e <> "\ESC[36m]\ESC[0m" + show (Bruijn x ) = "\ESC[91m" <> show x <> "\ESC[0m" + show (Function ident) = "\ESC[95m" <> show ident <> "\ESC[0m" + show (Abstraction e ) = "\ESC[36m[\ESC[0m" <> show e <> "\ESC[36m]\ESC[0m" show (Application exp1 exp2) = "\ESC[33m(\ESC[0m" <> show exp1 <> " " <> show exp2 <> "\ESC[33m)\ESC[0m" show (Infix le i re) = - show le <> " \ESC[95m(" <> i <> ")" <> "\ESC[0m " <> show re - show (Prefix p e) = "\ESC[95m" <> p <> show e <> "\ESC[0m" + "\ESC[33m(\ESC[0m" + <> show i + <> " " + <> show le + <> " " + <> show re + <> "\ESC[33m)\ESC[0m" + show (Prefix p e) = show p <> " " <> show e type EnvDef = (String, Expression) data EvalConf = EvalConf @@ -193,7 +209,7 @@ maybeHumanifyExpression e = ternaryToDecimal e <|> decodeStdout e humanifyExpression :: Expression -> String humanifyExpression e = case maybeHumanifyExpression e of - Nothing -> "" + Nothing -> show e Just h -> h --- |