diff options
Diffstat (limited to 'lib/Language/Elna/AST.hs')
| -rw-r--r-- | lib/Language/Elna/AST.hs | 167 |
1 files changed, 83 insertions, 84 deletions
diff --git a/lib/Language/Elna/AST.hs b/lib/Language/Elna/AST.hs index a13798c..8f66bdc 100644 --- a/lib/Language/Elna/AST.hs +++ b/lib/Language/Elna/AST.hs @@ -1,25 +1,56 @@ module Language.Elna.AST - ( Program(..) - {-, VariableAccess(..) - , Condition(..) - , Declaration(..) - , Expression(..) + ( Declaration(..) , Identifier(..) - , Literal(..) , Parameter(..) + , Program(..) , Statement(..) + , TypeExpression(..) , VariableDeclaration(..) - , TypeExpression(..)-} + {-, VariableAccess(..) + , Condition(..) + , Expression(..) + , Literal(..)-} ) where -data Program = Program -{- -import Data.Int (Int32) import Data.List (intercalate) -import Data.Word (Word16, Word32) -import Data.Char (chr) +import Data.Word ({-Word16, -}Word32) import Language.Elna.Location (Identifier(..), showArrayType) -import Numeric (showHex) + +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) <> ")" data TypeExpression = NamedType Identifier @@ -31,6 +62,45 @@ instance Show TypeExpression show (NamedType typeName) = show typeName show (ArrayType elementCount typeName) = showArrayType elementCount typeName +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) + data Literal = IntegerLiteral Int32 | HexadecimalLiteral Int32 @@ -96,75 +166,4 @@ instance Show Condition 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] - -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 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 - ] - -data VariableDeclaration = - VariableDeclaration Identifier TypeExpression - deriving Eq - -instance Show VariableDeclaration - where - show (VariableDeclaration identifier typeExpression) = - concat ["var ", show identifier, ": " <> show typeExpression, ";"] - -data Declaration - = TypeDefinition Identifier TypeExpression - | ProcedureDefinition Identifier [Parameter] [VariableDeclaration] [Statement] - deriving Eq - -instance Show Declaration - where - show (TypeDefinition identifier typeExpression) = - concat ["type ", show identifier, " = ", show typeExpression, ";"] - show (ProcedureDefinition procedureName parameters variables body) - = "proc " <> show procedureName <> showParameters parameters <> " {\n" - <> unlines ((" " <>) . show <$> variables) - <> unlines ((" " <>) . show <$> body) - <> "}" - -newtype Program = Program [Declaration] - deriving Eq - -instance Show Program - where - show (Program declarations) = unlines (show <$> declarations) - -showParameters :: [Parameter] -> String -showParameters parameters = - "(" <> intercalate ", " (show <$> parameters) <> ")" -} |
