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.Parsing.Core

Description

The C languge parser and AST constructor

Synopsis

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 #

program indicates \(\eqref{eq:eigth}\) among the comments of inners.

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 #

stmt indicates \(\eqref{eq:nineth}\) among the comments of inners.

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 #

logicalOr indicates \(\eqref{eq:fifteenth}\) among the comments of inners.

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 #

bitwiseOr indicates \(\eqref{eq:tenth}\) among the comments of inners.

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 #

shift indicates (eqref{eq:thirteenth}\) among the comments of inners.

add :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #

add indicates \(\eqref{eq:first}\) among the comments of inners.

term :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #

term indicates \(\eqref{eq:second}\) amont the comments of inners.

cast :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #

cast indicates \(\eqref{eq:fourteenth}\) amont the comments of inners.

unary :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #

unary indicates \(\eqref{eq:fourth}\) amount the comments of inners.

factor :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ATree i -> ConstructionData i -> ASTConstruction i Source #

factor indicates \(\eqref{eq:third}\) amount the comments of inners.

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 #

assign indicates \(\eqref{eq:seventh}\) among the comments of inners.

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

parse :: (Show i, Read i, Integral i, Bits i) => [TokenLC i] -> ASTResult i Source #

Constructs the abstract syntax tree based on the list of token strings. if construction fails, parse returns the error message and the token at the error location. Otherwise, parse returns a list of abstract syntax trees, a set of global variables, and a list of literals.

Types and synonyms

type ASTs i = [ATree i] Source #

The type of AST list

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.

Utilities

stackSize :: (Show i, Integral i) => ATree i -> Natural Source #

stackSize returns the stack size of variable per function.