elna/lib/Language/Elna/AST.hs

170 lines
5.4 KiB
Haskell
Raw Normal View History

2024-07-23 22:44:42 +02:00
module Language.Elna.AST
2024-09-15 23:03:25 +02:00
( Declaration(..)
2024-07-23 22:44:42 +02:00
, Identifier(..)
, Parameter(..)
2024-09-15 23:03:25 +02:00
, Program(..)
2024-07-23 22:44:42 +02:00
, Statement(..)
2024-09-15 23:03:25 +02:00
, TypeExpression(..)
2024-07-23 22:44:42 +02:00
, VariableDeclaration(..)
2024-09-15 23:03:25 +02:00
{-, VariableAccess(..)
, Condition(..)
, Expression(..)
, Literal(..)-}
2024-07-23 22:44:42 +02:00
) where
import Data.List (intercalate)
2024-09-15 23:03:25 +02:00
import Data.Word ({-Word16, -}Word32)
2024-07-26 12:22:07 +02:00
import Language.Elna.Location (Identifier(..), showArrayType)
2024-09-15 23:03:25 +02:00
newtype Program = Program [Declaration]
deriving Eq
instance Show Program
where
show (Program declarations) = unlines (show <$> declarations)
data Declaration
= ProcedureDeclaration Identifier [Parameter] [VariableDeclaration] [Statement]
-- | TypeDefinition Identifier TypeExpression
deriving Eq
instance Show Declaration
where
{- show (TypeDefinition identifier typeExpression) =
concat ["type ", show identifier, " = ", show typeExpression, ";"] -}
show (ProcedureDeclaration procedureName parameters variables body)
= "proc " <> show procedureName <> showParameters parameters <> " {\n"
<> unlines ((" " <>) . show <$> variables)
<> unlines ((" " <>) . show <$> body)
<> "}"
data Parameter = Parameter Identifier TypeExpression Bool
deriving Eq
instance Show Parameter
where
show (Parameter identifier typeName ref) = concat
[ if ref then "ref " else ""
, show identifier, ": ", show typeName
]
showParameters :: [Parameter] -> String
showParameters parameters =
"(" <> intercalate ", " (show <$> parameters) <> ")"
2024-07-23 22:44:42 +02:00
data TypeExpression
= NamedType Identifier
2024-07-26 12:22:07 +02:00
| ArrayType Word32 TypeExpression
2024-07-23 22:44:42 +02:00
deriving Eq
instance Show TypeExpression
where
show (NamedType typeName) = show typeName
2024-07-26 12:22:07 +02:00
show (ArrayType elementCount typeName) = showArrayType elementCount typeName
2024-07-23 22:44:42 +02:00
2024-09-15 23:03:25 +02:00
data Statement
= EmptyStatement
{-| AssignmentStatement VariableAccess Expression
| IfStatement Condition Statement (Maybe Statement)
| WhileStatement Condition Statement
| CompoundStatement [Statement]
| CallStatement Identifier [Expression]-}
deriving Eq
instance Show Statement
where
show EmptyStatement = ";"
{-show (AssignmentStatement lhs rhs) =
concat [show lhs, " := ", show rhs, ";"]
show (IfStatement condition if' else') = concat
[ "if (", show condition, ") "
, show if'
, maybe "" ((<> " else ") . show) else'
]
show (WhileStatement expression statement) =
concat ["while (", show expression, ") ", show statement, ";"]
show (CompoundStatement statements) =
concat ["{\n", unlines (show <$> statements), " }"]
show (CallStatement name parameters) = show name <> "("
<> intercalate ", " (show <$> parameters) <> ")"-}
data VariableDeclaration =
VariableDeclaration Identifier TypeExpression
deriving Eq
instance Show VariableDeclaration
where
show (VariableDeclaration identifier typeExpression) =
concat ["var ", show identifier, ": " <> show typeExpression, ";"]
{-
import Data.Int (Int32)
import Data.Char (chr)
import Numeric (showHex)
2024-07-23 22:44:42 +02:00
data Literal
= IntegerLiteral Int32
| HexadecimalLiteral Int32
2024-07-25 01:39:53 +02:00
| CharacterLiteral Word16
2024-07-23 22:44:42 +02:00
| BooleanLiteral Bool
deriving Eq
instance Show Literal
where
show (IntegerLiteral integer) = show integer
2024-07-25 01:39:53 +02:00
show (HexadecimalLiteral integer) = '0' : 'x' : showHex integer ""
2024-07-23 22:44:42 +02:00
show (CharacterLiteral character) =
'\'' : chr (fromEnum character) : ['\'']
show (BooleanLiteral boolean)
| boolean = "true"
| otherwise = "false"
2024-08-15 20:13:56 +02:00
data VariableAccess
= VariableAccess Identifier
| ArrayAccess VariableAccess Expression
deriving Eq
instance Show VariableAccess
where
show (VariableAccess variableName) = show variableName
show (ArrayAccess arrayAccess elementIndex) =
concat [show arrayAccess, "[", show elementIndex, "]"]
2024-07-23 22:44:42 +02:00
data Expression
2024-08-15 20:13:56 +02:00
= VariableExpression VariableAccess
2024-07-23 22:44:42 +02:00
| LiteralExpression Literal
| NegationExpression Expression
| SumExpression Expression Expression
| SubtractionExpression Expression Expression
| ProductExpression Expression Expression
| DivisionExpression Expression Expression
deriving Eq
instance Show Expression
where
show (VariableExpression variable) = show variable
show (LiteralExpression literal) = show literal
show (NegationExpression negation) = '-' : show negation
show (SumExpression lhs rhs) = concat [show lhs, " + ", show rhs]
show (SubtractionExpression lhs rhs) = concat [show lhs, " - ", show rhs]
show (ProductExpression lhs rhs) = concat [show lhs, " * ", show rhs]
show (DivisionExpression lhs rhs) = concat [show lhs, " / ", show rhs]
2024-08-15 20:13:56 +02:00
data Condition
= EqualCondition Expression Expression
| NonEqualCondition Expression Expression
| LessCondition Expression Expression
| GreaterCondition Expression Expression
| LessOrEqualCondition Expression Expression
| GreaterOrEqualCondition Expression Expression
deriving Eq
instance Show Condition
where
show (EqualCondition lhs rhs) = concat [show lhs, " = ", show rhs]
show (NonEqualCondition lhs rhs) = concat [show lhs, " # ", show rhs]
show (LessCondition lhs rhs) = concat [show lhs, " < ", show rhs]
show (GreaterCondition lhs rhs) = concat [show lhs, " > ", show rhs]
show (LessOrEqualCondition lhs rhs) = concat [show lhs, " <= ", show rhs]
show (GreaterOrEqualCondition lhs rhs) = concat [show lhs, " >= ", show rhs]
2024-09-08 02:08:13 +02:00
-}