htcc-0.0.0.1: The full scratch implementation of tiny C compiler (x86_64)
Copyright(c) roki 2019
LicenseMIT
Maintainerfalgon53@yahoo.co.jp
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Htcc.Parser.AST.Core

Description

The AST data type and its utilities

Synopsis

Abstract tree types and its relational type class

data ATKindFor a Source #

Specially for syntax tree type

Constructors

ATForkw

The for keyword

ATForInit (ATree a)

The initial section of for statement

ATForCond (ATree a)

The conditional section of for statement

ATForIncr (ATree a)

The incremental section of for statement

ATForStmt (ATree a)

The statement section of for statement

Instances

Instances details
Show a => Show (ATKindFor a) Source # 
Instance details

Defined in Htcc.Parser.AST.Core

data ATKind a Source #

The syntax tree type. Let \(x,y\in\mathbb{N}\), Let \(p\) and \(q\) be pointers to variables \(a\) and \(b\), respectively (p=&a,q=&b).

Constructors

ATAdd

\(x+y\): x + y

ATAddPtr

add operation for pointer \(p+x,x+p\): p + x, x + p

ATSub

\(x-y\): x - y

ATSubPtr

sub operation for pointer \(p-x\): p - x

ATPtrDis

The distance of pointers \(p-q\): p - q

ATMul

\(x\times y\): x * y

ATDiv

\(x\div y\): x / y

ATMod

\(x\bmod y\): x % y

ATAddAssign

addition assignment: a += b

ATSubAssign

subtraction assignment: a -= b

ATMulAssign

multiplication assignment: a *= b

ATDivAssign

division assignment: a /= b

ATAddPtrAssign

addition assignment for pointer: p += q

ATSubPtrAssign

subtraction assignment for pointer: p -= q

ATLAnd

logical and: x && y

ATLOr

logical or: x || y

ATAnd

bitwise and: x & y

ATAndAssign

bitwise and assignment: a &= b

ATOr

bitwise or: x | y

ATOrAssign

bitwise or assignment: a |= b

ATXor

bitwise xor: x ^ y

ATXorAssign

bitwise xor assignment: a ^= b

ATBitNot

bitwise not: ~x

ATShl

left shift: x << y

ATShlAssign

left shift assignment: a <<= b

ATShr

right shift: x >> y

ATShrAssign

right shift assignment: a >>= b

ATLT

\(x\lt y\): x < y

ATLEQ

\(x\leq y\): x <= y

ATGT

\(x\gt y\): x > y

ATGEQ

\(x\geq y\): x >= y

ATEQ

\(x=y\): x == y

ATNEQ

\(x\not= y\): x != y

ATNot

not operator !: !x

ATAddr

addressing operator &: &x

ATDeref

dereferencing operator *: *p

ATAssign

assign operator: x=y

ATPreInc

pre-increment operator: ++a

ATPreDec

pre-decrement operator: --a

ATPostInc

post-increment operator: a++

ATPostDec

post-decrement operator: a--

ATNum a

The number

ATConditional (ATree a) (ATree a) (ATree a)

conditional operator: a ? x : y;. It has three AST (cond, then and else)

ATComma

comma operator: x,b

ATCast

the cast operation: (type) x

ATMemberAcc (StructMember a)

accessing the member of the struct

ATReturn

the return keyword

ATIf

the if keyword

ATElse

the else keyword

ATSwitch (ATree a) [ATree a]

the switch keyword, it has the conditional expression and compound statement by cases or defaults

ATCase a a

the case keyword, it has the value of label number and a constant value

ATDefault a

the default keyword

ATWhile

the while keyword

ATFor [ATKindFor a]

the for keyword

ATBreak

the break keyword

ATContinue

the continue keyword

ATGoto Text

the goto keyword, it has name of the target label

ATLabel Text

the label, it has name of label.

ATBlock [ATree a]

the compound statement

ATLVar (StorageClass a) a

the local variable. It has a type information (as StorageClass) and an offset value

ATGVar (StorageClass a) Text

the global variable. It has a type information (as StorageClass) and an name

ATDefFunc Text (Maybe [ATree a])

the function definition

ATCallFunc Text (Maybe [ATree a])

the function call. It has a offset value and arguments (Maybe)

ATExprStmt

the expression of a statement

ATStmtExpr [ATree a]

the statement of a expression (GNU extension)

ATNull (ATree a)

indicates nothing to do

Instances

Instances details
IncompleteBase ATKind Source # 
Instance details

Defined in Htcc.Parser.AST.Core

Show a => Show (ATKind a) Source # 
Instance details

Defined in Htcc.Parser.AST.Core

Methods

showsPrec :: Int -> ATKind a -> ShowS #

show :: ATKind a -> String #

showList :: [ATKind a] -> ShowS #

data ATree a Source #

The data structure of abstract syntax tree

Constructors

ATEmpty

The empty node

ATNode

ATKind representing the kind of node and the two branches ATree it has

Fields

Instances

Instances details
Show a => Show (ATree a) Source # 
Instance details

Defined in Htcc.Parser.AST.Core

Methods

showsPrec :: Int -> ATree a -> ShowS #

show :: ATree a -> String #

showList :: [ATree a] -> ShowS #

class Treealizable a where Source #

A class whose type can be converted to ATree

Methods

treealize :: a i -> ATree i Source #

Convert to ATree

Instances

Instances details
Treealizable LVar Source # 
Instance details

Defined in Htcc.Parser.ConstructionData.Scope.Var

Methods

treealize :: LVar i -> ATree i Source #

Treealizable Enumerator Source # 
Instance details

Defined in Htcc.Parser.ConstructionData.Scope.Enumerator

Methods

treealize :: Enumerator i -> ATree i Source #

Constructor

atBinary :: ATKind i -> StorageClass i -> ATree i -> ATree i -> ATree i Source #

atBinary is equivalent to ATNode

atUnary :: ATKind i -> StorageClass i -> ATree i -> ATree i Source #

atUnary is a shortcut for unary node

atNoLeaf :: ATKind i -> StorageClass i -> ATree i Source #

atNoLeaf is equivalent to ATNode k t ATEmpty ATEmpty

atLVar :: StorageClass i -> i -> ATree i Source #

atLVar is a shortcut for local variable node

atGVar :: StorageClass i -> Text -> ATree i Source #

atGVar is a shortcut for global variable node

atAssign :: ATree i -> ATree i -> ATree i Source #

atAssign is a shortcut for constructing a assign node

atNumLit :: i -> ATree i Source #

atNumLit is a shortcut for constructing a numeric literal node

atMemberAcc :: StructMember i -> ATree i -> ATree i Source #

atMemberAcc is a shortcut for constructing a ATMemberAcc node

atExprStmt :: ATree i -> ATree i Source #

atExprStmt is a shortcut for constructing a expression statement node

atBlock :: [ATree i] -> ATree i Source #

atBlock is a shortcut for constructing a block node

atNull :: ATree i -> ATree i Source #

atNull is a shortcut for constructing a null node

atDefFunc :: Text -> Maybe [ATree i] -> StorageClass i -> ATree i -> ATree i Source #

atDefFunc is a shortcut for constructing a function node

atReturn :: StorageClass i -> ATree i -> ATree i Source #

atReturn is a shortcut for constructing a return node

atIf :: ATree i -> ATree i -> ATree i Source #

atIf is a shortcut for constructing a if node

atElse :: ATree i -> ATree i -> ATree i Source #

atElse is a shortcut for constructing a else node

atWhile :: ATree i -> ATree i -> ATree i Source #

atWhile is a shortcut for constructing a while node

atFor :: [ATKindFor i] -> ATree i Source #

atFor is a shortcut for constructing a for node

atBreak :: ATree i Source #

atBreak is a shortcut for constructing a break node

atContinue :: ATree i Source #

atContinue is a shortcut for constructing a continue node

atSwitch :: ATree i -> [ATree i] -> StorageClass i -> ATree i Source #

atSwitch is a shortcut for constructing a switch node

atCase :: i -> i -> ATree i -> ATree i Source #

atCase is a shortcut for constructing a case node

atDefault :: i -> ATree i -> ATree i Source #

atDefault is a shortcut for constructing a default node

atGoto :: Text -> ATree i Source #

atGoto is a shortcut for constructing a goto node

atLabel :: Text -> ATree i Source #

atLabel is a shortcut for constructing a label node

atComma :: StorageClass i -> ATree i -> ATree i -> ATree i Source #

atComma is a shortcut for constructing a , node

atConditional :: StorageClass i -> ATree i -> ATree i -> ATree i -> ATree i Source #

atConditional is a shortcut for constructing a ?: node

atCast :: StorageClass i -> ATree i -> ATree i Source #

atCast is a shortcut for constructing a cast node

Utilities

isATForInit :: ATKindFor a -> Bool Source #

An utility of ATForInit. When an argument is ATForInit, return True otherwise False

isATForCond :: ATKindFor a -> Bool Source #

An utility of ATForCond. When an argument is ATForCond, return True otherwise False

isATForStmt :: ATKindFor a -> Bool Source #

An utility ATForStmt. When an argument is ATForStmt, return True otherwise False

isATForIncr :: ATKindFor a -> Bool Source #

An utility ATForIncr. When an argument is ATForIncr, return True otherwise False

fromATKindFor :: ATKindFor a -> ATree a Source #

take ATree data from ATKindFor.

isComplexAssign :: ATKind a -> Bool Source #

Returns True if the given ATKind is an assignment operator other than simple assignment. Otherwise, returns False.

isEmptyExprStmt :: ATree a -> Bool Source #

isEmptyExprStmt returns True only if both sides of ATExprStmt are ATEmpty. Otherwise, returns False.

isEmptyReturn :: ATree a -> Bool Source #

isEmptyReturn returns True only if the ATKind of the given argument is ATReturn and the left side hand node of the given argument is ATEmpty. Otherwise, returns False.

isNonEmptyReturn :: ATree a -> Bool Source #

isNonEmptyReturn returns True only if the ATKind of the given argument is ATReturn and the left side hand node of the given argument is not ATEmpty. Otherwise, returns False.

mapATKind :: (ATKind i -> ATKind i) -> ATree i -> ATree i Source #

mapping for ATKind