Copyright | (c) roki 2019 |
---|---|
License | MIT |
Maintainer | falgon53@yahoo.co.jp |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
The C languge parser and AST constructor
Synopsis
- program :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ConstructionData i -> Either (ASTError i) (ASTs i, ConstructionData i)
- globalDef :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- stmt :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- inners :: ([TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i) -> [(Text, ATKind i)] -> [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- logicalOr :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- logicalAnd :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- bitwiseOr :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- bitwiseXor :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- bitwiseAnd :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- shift :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- add :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- term :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- cast :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- unary :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- factor :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- relational :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- equality :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- conditional :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- assign :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- expr :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i
- parse :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ASTResult i
- type ASTs i = [ATree i]
- type ASTSuccess i = ([TokenLC i], ATree i, ConstructionData i)
- type ASTConstruction i = Either (ASTError i) (ASTSuccess i)
- type ASTResult i = Either (ASTError i) (Warnings i, ASTs i, GlobalVars i, Literals i)
- stackSize :: (Show i, Integral i) => ATree i -> Natural
Recursive descent implementation functions
program :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ConstructionData i -> Either (ASTError i) (ASTs i, ConstructionData i) Source #
globalDef :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
globalDef
parses global definitions (include functions and global variables)
\[
\text{global-def}=\left(\text{global-var}\ \mid\ \text{function}\right)\text{*}
\]
stmt :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
inners :: ([TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i) -> [(Text, ATKind i)] -> [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
inners
is a general function for creating equality
, relational
, add
and term
in the following syntax (EBNF) of \({\rm LL}\left(k\right)\) where \(k\in\mathbb{N}\).
\[ \begin{eqnarray} {\rm program} &=& \text{global-def*}\label{eq:eigth}\tag{1}\\ {\rm stmt} &=& \begin{array}{l} {\rm expr}?\ {\rm ";"}\\ \mid\ {\rm "\{"\ stmt}^\ast\ {\rm "\}"}\\ \mid\ {\rm "return"}\ {\rm expr}\ ";"\\ \mid\ "{\rm if}"\ "("\ {\rm expr}\ ")"\ {\rm stmt}\ ("{\rm else}"\ {\rm stmt})?\\ \mid\ {\rm "while"\ "("\ expr\ ")"\ stmt}\\ \mid\ {\rm "for"\ "("\ expr?\ ";" expr?\ ";"\ expr?\ ")"\ stmt? ";"} \end{array}\label{eq:nineth}\tag{2}\\ {\rm expr} &=& {\rm assign}\\ {\rm assign} &=& {\rm conditional} \left(\left("="\ \mid\ "+="\ \mid\ "-="\ \mid\ "*="\ \mid\ "/="\right)\ {\rm assign}\right)?\label{eq:seventh}\tag{3}\\ {\rm conditional} &=& {\rm logicalOr} \left("?"\ {\rm expr}\ ":"\ {\rm conditional}\right)?\label{eq:seventeenth}\tag{4}\\ {\rm logicalOr} &=& {\rm logicalAnd}\ \left("||"\ {\rm logicalAnd}\right)^\ast\label{eq:fifteenth}\tag{5}\\ {\rm logicalAnd} &=& {\rm bitwiseOr}\ \left("|"\ {\rm bitwiseOr}\right)^\ast\label{eq:sixteenth}\tag{6}\\ {\rm bitwiseOr} &=& {\rm bitwiseXor}\ \left("|"\ {\rm bitwiseXor}\right)^\ast\label{eq:tenth}\tag{7}\\ {\rm bitwiseXor} &=& {\rm bitwiseAnd}\ \left("\hat{}"\ {\rm bitwiseAnd}\right)^\ast\label{eq:eleventh}\tag{8}\\ {\rm bitwiseAnd} &=& {\rm equality}\ \left("\&"\ {\rm equality}\right)^\ast\label{eq:twelveth}\tag{9}\\ {\rm equality} &=& {\rm relational}\ \left("=="\ {\rm relational}\ \mid\ "!="\ {\rm relational}\right)^\ast\label{eq:fifth}\tag{10}\\ {\rm relational} &=& {\rm shift}\ \left("\lt"\ {\rm shift}\mid\ "\lt ="\ {\rm shift}\mid\ "\gt"\ {\rm shift}\mid\ "\gt ="\ {\rm shift}\right)^\ast\label{eq:sixth}\tag{11}\\ {\rm shift} &=& {\rm add}\ \left("\lt\lt"\ {\rm add}\mid\ "\gt\gt"\ {\rm add}\right)^\ast\label{eq:thirteenth}\tag{12}\\ {\rm add} &=& {\rm term}\ \left("+"\ {\rm term}\ \mid\ "-"\ {\rm term}\right)^\ast\label{eq:first}\tag{13} \\ {\rm term} &=& {\rm factor}\ \left("\ast"\ {\rm factor}\ \mid\ "/"\ {\rm factor}\right)^\ast\label{eq:second}\tag{14} \\ {\rm cast} &=& "(" {\rm type-name} ")"\ {\rm cast}\ \mid\ {\rm unary}\label{eq:fourteenth}\tag{15} \\ {\rm unary} &=& \left(\text{"+"}\ \mid\ \text{"-"}\ \mid\ \text{"*"}\ \mid\ \text{"&"}\ \mid\ \text{"!"}\ \mid\ \text{"-"}\right)\text{?}\ \text{cast}\ \mid\ \left(\text{"++"}\ \mid\ \text{"--"}\right)\ \text{unary}\ \mid\ \text{primary} \left(\text{"["}\ \text{expr}\ \text{"]"}\ \mid\ \text{"."}\ \text{ident}\ \mid\ \text{"->"}\ \text{ident}\ \mid\ \text{"++"}\ \mid\ \text{"--"}\right)\ast\label{eq:fourth}\tag{16} \\ {\rm factor} &=& {\rm num} \mid\ {\rm ident}\ \left({\rm "(" \left(expr\ \left(\left(","\ expr\right)^\ast\right)?\right)? ")"}\right)?\ \mid\ "(" {\rm expr} ")"\ \mid \text{string}\ \mid\ \text{"sizeof"}\ \text{"("}\ \text{type}\ \text{")"}\ \mid\ \text{"sizeof"}\ \text{unary}\ \mid\ \text{stmt-expr}\label{eq:third}\tag{17} \end{eqnarray} \]
logicalOr :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
logicalAnd :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
logicalAnd
indicates \(\eqref{eq:sixteenth}\) among the comments of inners
.
bitwiseOr :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
bitwiseXor :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
bitwiseXor
indicates \(\eqref{eq:eleventh}\) amont the comments of inners
.
bitwiseAnd :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
bitwiseAnd
indicates \(\eqref{eq:twelveth}\) among the comments of inners
.
shift :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
add :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
term :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
cast :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
unary :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
factor :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
relational :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
relational
indicates \(\eqref{eq:sixth}\) among the comments of inners
.
equality :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
equality
indicates \(\eqref{eq:fifth}\) among the comments of inners
.
This is equivalent to the following code:
equality :: [HT.TokenLC i] -> ATree i -> [LVar i] -> Either (ASTError i) ([HT.TokenLC i], ATree i) equality xs atn scp = (>>=) (relational xs atn scp) $ uncurry3 equality' where equality' ((_, HT.TKReserved "=="):ys) era ars = either Left (uncurry3 id . first3 equality' . second3 (ATNode ATEQ era)) $ relational ys era ars equality' ((_, HT.TKReserved "!="):ys) era ars = either Left (uncurry3 id . first3 equality' . second3 (ATNode ATNEQ era)) $ relational ys era ars equality' ert era ars = Right (ert, era, ars)
conditional :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
conditional
indicates \(\eqref{eq:seventeenth}\) among the comments of inners
.
assign :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
expr :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #
\({\rm expr} = {\rm assign}\left("," {\rm assign}\right)\ast\)
Parser
Types and synonyms
type ASTSuccess i = ([TokenLC i], ATree i, ConstructionData i) Source #
The type to be used when the AST construction is successful
type ASTConstruction i = Either (ASTError i) (ASTSuccess i) Source #
Types used during AST construction
type ASTResult i = Either (ASTError i) (Warnings i, ASTs i, GlobalVars i, Literals i) Source #
A type that represents the result after AST construction. Quadraple of warning list, constructed abstract syntax tree list, global variable map, literal list.