Add printc and exit builtin functions

This commit is contained in:
2024-10-04 18:26:10 +02:00
parent fdf56ce9d0
commit 35742aa525
20 changed files with 139 additions and 53 deletions

View File

@@ -12,10 +12,12 @@ module Language.Elna.Frontend.AST
, Literal(..)
) where
import Data.Char (chr)
import Data.Int (Int32)
import Data.List (intercalate)
import Data.Word ({-Word16, -}Word32)
import Data.Word (Word8, Word32)
import Language.Elna.Location (Identifier(..), showArrayType)
import Numeric (showHex)
newtype Program = Program [Declaration]
deriving Eq
@@ -67,8 +69,8 @@ data Statement
= EmptyStatement
{-| AssignmentStatement VariableAccess Expression
| IfStatement Condition Statement (Maybe Statement)
| WhileStatement Condition Statement
| CompoundStatement [Statement]-}
| WhileStatement Condition Statement -}
| CompoundStatement [Statement]
| CallStatement Identifier [Expression]
deriving Eq
@@ -83,9 +85,9 @@ instance Show Statement
, maybe "" ((<> " else ") . show) else'
]
show (WhileStatement expression statement) =
concat ["while (", show expression, ") ", show statement, ";"]
concat ["while (", show expression, ") ", show statement, ";"]-}
show (CompoundStatement statements) =
concat ["{\n", unlines (show <$> statements), " }"]-}
concat ["{\n", unlines (show <$> statements), " }"]
show (CallStatement name parameters) = show name <> "("
<> intercalate ", " (show <$> parameters) <> ")"
@@ -93,22 +95,18 @@ data VariableDeclaration =
VariableDeclaration Identifier TypeExpression
deriving Eq
newtype Literal
data Literal
= IntegerLiteral Int32
{- | HexadecimalLiteral Int32
| CharacterLiteral Word16
| BooleanLiteral Bool -}
| HexadecimalLiteral Int32
| CharacterLiteral Word8
deriving Eq
instance Show Literal
where
show (IntegerLiteral integer) = show integer
{- show (HexadecimalLiteral integer) = '0' : 'x' : showHex integer ""
show (HexadecimalLiteral integer) = '0' : 'x' : showHex integer ""
show (CharacterLiteral character) =
'\'' : chr (fromEnum character) : ['\'']
show (BooleanLiteral boolean)
| boolean = "true"
| otherwise = "false" -}
instance Show VariableDeclaration
where
@@ -120,8 +118,8 @@ data Expression
| SumExpression Expression Expression
| SubtractionExpression Expression Expression
| NegationExpression Expression
{- | VariableExpression VariableAccess
| ProductExpression Expression Expression
{- | VariableExpression VariableAccess
| DivisionExpression Expression Expression -}
deriving Eq
@@ -131,13 +129,10 @@ instance Show Expression
show (SumExpression lhs rhs) = concat [show lhs, " + ", show rhs]
show (SubtractionExpression lhs rhs) = concat [show lhs, " - ", show rhs]
show (NegationExpression negation) = '-' : show negation
{- show (VariableExpression variable) = show variable
show (ProductExpression lhs rhs) = concat [show lhs, " * ", show rhs]
{- show (VariableExpression variable) = show variable
show (DivisionExpression lhs rhs) = concat [show lhs, " / ", show rhs] -}
{-
import Data.Char (chr)
import Numeric (showHex)
data VariableAccess
= VariableAccess Identifier
| ArrayAccess VariableAccess Expression