Add record and variant AST types

This commit is contained in:
Eugen Wissner 2024-07-21 22:45:31 +02:00
parent be4957ee59
commit f673ca48a9
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 22 additions and 5 deletions

3
TODO
View File

@ -1,5 +1,4 @@
# AST missing
- Import
- Record
- Union
- Record and Union initialization

View File

@ -12,6 +12,8 @@ module Language.Elna.AST
import Data.Int (Int32)
import Data.List (intercalate)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NonEmpty
import Data.Word (Word8)
import Data.Text (Text)
import qualified Data.Text as Text
@ -174,13 +176,29 @@ instance Show VariableDeclaration
<> maybe "" ((" = " <>) . show) initialValue <> ";"
<> showAttributes exports
data Program = Program [ConstantDefinition] [VariableDeclaration] [ProcedureDeclaration] Statement
data TypeDefinition
= RecordDefinition Identifier (NonEmpty Parameter)
| VariantDefinition Identifier (NonEmpty Parameter)
deriving Eq
instance Show TypeDefinition
where
show (RecordDefinition identifier fields) = show identifier
<> " = record " <> intercalate "; " (NonEmpty.toList $ show <$> fields)
<> " end;"
show (VariantDefinition identifier fields) = show identifier
<> " = variant " <> intercalate "; " (NonEmpty.toList $ show <$> fields)
<> " end;"
data Program =
Program [TypeDefinition] [ConstantDefinition] [VariableDeclaration] [ProcedureDeclaration] Statement
deriving Eq
instance Show Program
where
show (Program constants globals procedures body)
= showConstants constants <> showVariables globals
show (Program types constants globals procedures body)
= unlines (show <$> types)
<> showConstants constants <> showVariables globals
<> unlines (show <$> procedures) <> show body <> "."
showAttributes :: Bool -> String