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 # AST missing
- Import - Import
- Record - Record and Union initialization
- Union

View File

@ -12,6 +12,8 @@ module Language.Elna.AST
import Data.Int (Int32) import Data.Int (Int32)
import Data.List (intercalate) import Data.List (intercalate)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NonEmpty
import Data.Word (Word8) import Data.Word (Word8)
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as Text import qualified Data.Text as Text
@ -174,13 +176,29 @@ instance Show VariableDeclaration
<> maybe "" ((" = " <>) . show) initialValue <> ";" <> maybe "" ((" = " <>) . show) initialValue <> ";"
<> showAttributes exports <> 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 deriving Eq
instance Show Program instance Show Program
where where
show (Program constants globals procedures body) show (Program types constants globals procedures body)
= showConstants constants <> showVariables globals = unlines (show <$> types)
<> showConstants constants <> showVariables globals
<> unlines (show <$> procedures) <> show body <> "." <> unlines (show <$> procedures) <> show body <> "."
showAttributes :: Bool -> String showAttributes :: Bool -> String