summaryrefslogtreecommitdiff
path: root/lib/Language/Elna/AST.hs
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2024-07-25 01:39:53 +0200
committerEugen Wissner <belka@caraus.de>2024-07-25 01:39:53 +0200
commitbf774475cc21cf7190144a5a9e16c2a72318f0bb (patch)
treeb0bc8faceb5f86cd8428592798322c43aee54247 /lib/Language/Elna/AST.hs
parent947c5aa7efba507a849463fcf813b3cc61042845 (diff)
downloadelna-bf774475cc21cf7190144a5a9e16c2a72318f0bb.tar.gz
Parse all statements
Diffstat (limited to 'lib/Language/Elna/AST.hs')
-rw-r--r--lib/Language/Elna/AST.hs29
1 files changed, 15 insertions, 14 deletions
diff --git a/lib/Language/Elna/AST.hs b/lib/Language/Elna/AST.hs
index 0add1d2..3189469 100644
--- a/lib/Language/Elna/AST.hs
+++ b/lib/Language/Elna/AST.hs
@@ -12,11 +12,12 @@ module Language.Elna.AST
import Data.Int (Int32)
import Data.List (intercalate)
-import Data.Word (Word8)
+import Data.Word (Word16)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Char (chr)
import Data.String (IsString(..))
+import Numeric (showHex)
newtype Identifier = Identifier { unIdentifier :: Text }
deriving Eq
@@ -43,14 +44,14 @@ instance Show TypeExpression
data Literal
= IntegerLiteral Int32
| HexadecimalLiteral Int32
- | CharacterLiteral Word8
+ | CharacterLiteral Word16
| BooleanLiteral Bool
deriving Eq
instance Show Literal
where
show (IntegerLiteral integer) = show integer
- show (HexadecimalLiteral integer) = show integer
+ show (HexadecimalLiteral integer) = '0' : 'x' : showHex integer ""
show (CharacterLiteral character) =
'\'' : chr (fromEnum character) : ['\'']
show (BooleanLiteral boolean)
@@ -84,7 +85,7 @@ instance Show Expression
show (ProductExpression lhs rhs) = concat [show lhs, " * ", show rhs]
show (DivisionExpression lhs rhs) = concat [show lhs, " / ", show rhs]
show (EqualExpression lhs rhs) = concat [show lhs, " = ", show rhs]
- show (NonEqualExpression lhs rhs) = concat [show lhs, " /= ", show rhs]
+ show (NonEqualExpression lhs rhs) = concat [show lhs, " # ", show rhs]
show (LessExpression lhs rhs) = concat [show lhs, " < ", show rhs]
show (GreaterExpression lhs rhs) = concat [show lhs, " > ", show rhs]
show (LessOrEqualExpression lhs rhs) = concat [show lhs, " <= ", show rhs]
@@ -105,17 +106,16 @@ instance Show Statement
where
show EmptyStatement = ";"
show (AssignmentStatement lhs rhs) =
- concat [show lhs, " := ", show rhs, show 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) = "begin "
- <> intercalate "; " (show <$> statements) <> " end"
+ concat ["while (", show expression, ") ", show statement, ";"]
+ show (CompoundStatement statements) =
+ concat ["{\n", unlines (show <$> statements), " }"]
show (CallStatement name parameters) = show name <> "("
<> intercalate ", " (show <$> parameters) <> ")"
@@ -136,7 +136,7 @@ data VariableDeclaration =
instance Show VariableDeclaration
where
show (VariableDeclaration identifier typeExpression) =
- concat [" var ", show identifier, ": " <> show typeExpression, ";"]
+ concat ["var ", show identifier, ": " <> show typeExpression, ";"]
data Declaration
= TypeDefinition Identifier TypeExpression
@@ -146,11 +146,12 @@ data Declaration
instance Show Declaration
where
show (TypeDefinition identifier typeExpression) =
- concat ["type ", show identifier, " = ", show typeExpression]
+ concat ["type ", show identifier, " = ", show typeExpression, ";"]
show (ProcedureDefinition procedureName parameters variables body)
- = "proc " <> show procedureName <> showParameters parameters <> ";"
- <> unlines (show <$> variables)
- <> unlines (show <$> body) <> ";"
+ = "proc " <> show procedureName <> showParameters parameters <> " {\n"
+ <> unlines ((" " <>) . show <$> variables)
+ <> unlines ((" " <>) . show <$> body)
+ <> "}"
newtype Program = Program [Declaration]
deriving Eq