| License | MIT |
|---|---|
| Maintainer | laszlo.serdet@epitech.eu |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Ast
Description
The abstract syntax tree serves to define how rizz tokens forms a valid grammar in order to represent a correct language expression. This grammar is heavily inspired from the Clang C's (programming language) AST.
For further information, refer to the Microsoft C grammar definition and the Clang AST documentation.
Example
Taking those simple functions, expressed in rizz code:
fn foo(Int: x) -> Int
{
Int y = 42;
if (x == 0) {
ret 0;
}
ret y / x;
}
fn bar(Int: x) -> Int
{
Int y = 1;
for (; y < x; y++) {
if (foo(y) == 0) {
ret 10;
}
}
ret y;
}
fn baz()
{
bar(foo(100));
}will be translated (each) as these following :Decl
>>>FunctionDecl "foo" [ParmVarDeclExpr Integer "x"] (CompoundStmt [DeclVarExpr (VarDeclStmt Integer "y" Equal (ParmCallDeclLiteral (IntLiteral 42))), IfStmt (BinaryOpExpr (BinaryOpParm (ParmCallDeclIdent "x")) Eq (BinaryOpParm (ParmCallDeclLiteral (IntLiteral 0)))) (CompoundStmt [RetStmt (BinaryOpConst (ParmCallDeclLiteral (IntLiteral 0)))]) Nothing, RetStmt (BinaryOpExpr (BinaryOpParm (ParmCallDeclIdent "y")) Div (BinaryOpParm (ParmCallDeclIdent "x")))]) (Just Integer)
>>>FunctionDecl "bar" [ParmVarDeclExpr Integer "x"] (CompoundStmt [DeclVarExpr (VarDeclStmt Integer "y" Equal (ParmCallDeclLiteral (IntLiteral 1))),ForStmt Nothing (Just (BinaryOpExpr (BinaryOpParm (ParmCallDeclIdent "y")) Lt (BinaryOpParm (ParmCallDeclIdent "x")))) (Just (DeclAssignStmtUnary (UnaryOperatorExpr "y" IdentIncrement))) (CompoundStmt [IfStmt (BinaryOpExpr (BinaryOpParm (ParmCallDeclExpr (CallExprDecl "foo" [ParmCallDeclIdent "y"]))) Eq (BinaryOpParm (ParmCallDeclLiteral (IntLiteral 0)))) (CompoundStmt [RetStmt (BinaryOpConst (ParmCallDeclLiteral (IntLiteral 0)))]) Nothing]),RetStmt (BinaryOpConst (ParmCallDeclIdent "y"))]) (Just Integer)
>>>FunctionDecl "baz" [] (CompoundStmt [CallExpr (CallExprDecl "bar" [ParmCallDeclExpr (CallExprDecl "foo" [ParmCallDeclLiteral (IntLiteral 100)])])]) Nothing
and will be translated as a compilation unit (a '[Decl]') as follow:
>>>[FunctionDecl "foo" [ParmVarDeclExpr Integer "x"] (CompoundStmt [DeclVarExpr (VarDeclStmt Integer "y" Equal (ParmCallDeclLiteral (IntLiteral 42))), IfStmt (BinaryOpExpr (BinaryOpParm (ParmCallDeclIdent "x")) Eq (BinaryOpParm (ParmCallDeclLiteral (IntLiteral 0)))) (CompoundStmt [RetStmt (BinaryOpConst (ParmCallDeclLiteral (IntLiteral 0)))]) Nothing, RetStmt (BinaryOpExpr (BinaryOpParm (ParmCallDeclIdent "y")) Div (BinaryOpParm (ParmCallDeclIdent "x")))]) (Just Integer), FunctionDecl "bar" [ParmVarDeclExpr Integer "x"] (CompoundStmt [DeclVarExpr (VarDeclStmt Integer "y" Equal (ParmCallDeclLiteral (IntLiteral 1))),ForStmt Nothing (Just (BinaryOpExpr (BinaryOpParm (ParmCallDeclIdent "y")) Lt (BinaryOpParm (ParmCallDeclIdent "x")))) (Just (DeclAssignStmtUnary (UnaryOperatorExpr "y" IdentIncrement))) (CompoundStmt [IfStmt (BinaryOpExpr (BinaryOpParm (ParmCallDeclExpr (CallExprDecl "foo" [ParmCallDeclIdent "y"]))) Eq (BinaryOpParm (ParmCallDeclLiteral (IntLiteral 0)))) (CompoundStmt [RetStmt (BinaryOpConst (ParmCallDeclLiteral (IntLiteral 0)))]) Nothing]),RetStmt (BinaryOpConst (ParmCallDeclIdent "y"))]) (Just Integer), FunctionDecl "baz" [] (CompoundStmt [CallExpr (CallExprDecl "bar" [ParmCallDeclExpr (CallExprDecl "foo" [ParmCallDeclLiteral (IntLiteral 100)])])]) Nothing]
Synopsis
- data Decl
- data Stmt
- = DeclVarExpr VarDeclStmt
- | DeclStmt DeclStmt
- | UnaryOperator UnaryOperatorExpr
- | BinaryOperator BinaryOpExpr
- | IfStmt BinaryOpExpr CompoundStmt (Maybe CompoundStmt)
- | WhileStmt BinaryOpExpr CompoundStmt
- | ForStmt (Maybe VarDeclStmt) (Maybe BinaryOpExpr) (Maybe DeclStmt) CompoundStmt
- | ForeachStmt Identifier Identifier CompoundStmt
- | CallExpr CallExprDecl
- | RetStmt BinaryOpExpr
- newtype CompoundStmt = CompoundStmt [Stmt]
- data ParmVarDeclExpr
- data BuiltinType
- data VarDeclStmt = VarDeclStmt BuiltinType Identifier AssignOp ParmCallDecl
- data RecordDeclExpr = RecordDeclExpr Identifier [ParmVarDeclExpr]
- data DeclStmt
- data UnaryOperatorExpr = UnaryOperatorExpr Identifier UnaryOp
- data BinaryOpParm
- data BinaryOpExpr
- data ParmCallDecl
- data CallExprDecl = CallExprDecl Identifier [ParmCallDecl]
BNF definition
For the full rizz grammar syntax definition, see the BNF definition here.
Top level definitions
Defines as a primary Ast node containing language declarations.Decl
Examples
Some GHCI syntax examples for every constructors:Decl
>>>FunctionDecl "foo" [ParmVarDeclExpr Integer "x"] (CompoundStmt []) (Just Integer)>>>ParmVarDeclExpr Integer "foo">>>VarDecl (VarDeclStmt Boolean "foo" Equal (ParmCallDeclLiteral (BoolLiteral True)))
Constructors
| FunctionDecl Identifier [ParmVarDeclExpr] CompoundStmt (Maybe BuiltinType) | Function declaration, expressed in rizz code like A
|
| ParmVarDecl ParmVarDeclExpr | Function parameter, expressed in rizz code like A
Note that a standalone |
| VarDecl VarDeclStmt | Variable (with its type) declaration, expressed in rizz code like A
TODO: Merge both Note that a variable that initializes itself is a semantic violation! |
| RecordDecl RecordDeclExpr | Structure declaration, expressed in rizz code like A |
Instances
Defines as the second-primary Ast node containing language statements.Stmt
Examples
Some GHCI syntax examples for every constructors:Stmt
>>>DeclVarExpr (VarDeclStmt Boolean "foo" Equal (ParmCallDeclLiteral (BoolLiteral True)))>>>DeclStmt (DeclAssignStmtLiteral "var" MulEqual (ParmCallDeclLiteral (BoolLiteral True)))>>>UnaryOperatorExpr "x" IdentIncrement>>>BinaryOpExpr (BinaryOpParm (ParmCallDeclLiteral (BoolLiteral True))) Lt (BinaryOpParm (ParmCallDeclIdent "a"))>>>IfStmt (BinaryOpExpr (BinaryOpParm (ParmCallDeclExpr (CallExprDecl "foo" [ParmCallDeclLiteral (IntLiteral 42)]))) Lt (BinaryOpParm (ParmCallDeclIdent "a"))) (CompoundStmt []) (Just (CompoundStmt []))>>>WhileStmt (BinaryOpExpr (BinaryOpParm (ParmCallDeclLiteral (BoolLiteral True))) NEq (BinaryOpParm (ParmCallDeclLiteral (BoolLiteral False)))) (CompoundStmt [])>>>ForStmt (Just (VarDeclStmt Integer "i" Equal (ParmCallDeclExpr (CallExprDecl "foo" [])))) (Just (BinaryOpExpr (BinaryOpParm (ParmCallDeclIdent "x")) Lt (BinaryOpParm (ParmCallDeclIdent "y")))) Nothing (CompoundStmt [])>>>ForeachStmt "foo" "it" (CompoundStmt [])>>>CallExpr (CallExprDecl "foo" [ParmCallDeclLiteral (IntLiteral 42)])>>>RetStmt (BinaryOpConst (ParmCallDeclIdent "foo"))
Constructors
| DeclVarExpr VarDeclStmt |
|
| DeclStmt DeclStmt | Variable (without its type) assignment, expressed in rizz code like A
TODO: Rename this to Note that this statement differs from |
| UnaryOperator UnaryOperatorExpr | Unary operator, which are A
|
| BinaryOperator BinaryOpExpr | Binary operation, expressed in rizz code like For further details, see |
| IfStmt BinaryOpExpr CompoundStmt (Maybe CompoundStmt) | Conditional branching, expressed in rizz code like A
|
| WhileStmt BinaryOpExpr CompoundStmt | While loop, expressed in rizz code like A
|
| ForStmt (Maybe VarDeclStmt) (Maybe BinaryOpExpr) (Maybe DeclStmt) CompoundStmt | For loop, expressed in rizz code like A
|
| ForeachStmt Identifier Identifier CompoundStmt | Foreach loop to iterate over a list, expressed in rizz code like A
|
| CallExpr CallExprDecl | Function call, expressed in rizz code like A
|
| RetStmt BinaryOpExpr | Return from function, expressed in rizz code like A
|
Instances
Declarations
newtype CompoundStmt Source #
Defines type as a list of CompoundStmt expressions.Stmt
Constructors
| CompoundStmt [Stmt] | Compound statement block, expressed in rizz code as A
|
Instances
| Show CompoundStmt Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> CompoundStmt -> ShowS show :: CompoundStmt -> String showList :: [CompoundStmt] -> ShowS | |
| Eq CompoundStmt Source # | Allows |
Defined in Ast | |
data ParmVarDeclExpr Source #
Defines the expression (extended constructor for ParmVarDeclExpr).ParmVarDecl
Example
A GHCI syntax example to declare a :ParmVarDecl
>>>ParmVarDeclExpr Boolean "x"
Constructors
| ParmVarDeclExpr BuiltinType Identifier | Function's parameter, see |
| ParmVarRecord RecordDeclExpr |
Instances
| Show ParmVarDeclExpr Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> ParmVarDeclExpr -> ShowS show :: ParmVarDeclExpr -> String showList :: [ParmVarDeclExpr] -> ShowS | |
| Eq ParmVarDeclExpr Source # | Allows |
Defined in Ast Methods (==) :: ParmVarDeclExpr -> ParmVarDeclExpr -> Bool (/=) :: ParmVarDeclExpr -> ParmVarDeclExpr -> Bool | |
data BuiltinType Source #
Defines the expression which lists available rizz data types (implementating Keyword BuiltinType types).Token
Constructors
| Boolean | Boolean type keyword, see Bool |
| Character | Character type keyword, see Char |
| Integer | Integer type keyword, see Int |
| SinglePrecision | Float type keyword, see Float |
| ListType BuiltinType | List type, expressed in rizz code as |
Instances
| Show BuiltinType Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> BuiltinType -> ShowS show :: BuiltinType -> String showList :: [BuiltinType] -> ShowS | |
| Eq BuiltinType Source # | Allows |
Defined in Ast | |
data VarDeclStmt Source #
Defines the expression (extended constructor for VarDeclStmt).VarDecl
Constructors
| VarDeclStmt BuiltinType Identifier AssignOp ParmCallDecl | Variable declaration, see |
Instances
| Show VarDeclStmt Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> VarDeclStmt -> ShowS show :: VarDeclStmt -> String showList :: [VarDeclStmt] -> ShowS | |
| Eq VarDeclStmt Source # | Allows |
Defined in Ast | |
data RecordDeclExpr Source #
Constructors
| RecordDeclExpr Identifier [ParmVarDeclExpr] | Structure declaration |
Instances
| Show RecordDeclExpr Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> RecordDeclExpr -> ShowS show :: RecordDeclExpr -> String showList :: [RecordDeclExpr] -> ShowS | |
| Eq RecordDeclExpr Source # | Allows |
Defined in Ast Methods (==) :: RecordDeclExpr -> RecordDeclExpr -> Bool (/=) :: RecordDeclExpr -> RecordDeclExpr -> Bool | |
Statements
Defines the expression (extended constructor for Stmt).Stmt
Example
A GHCI syntax example to declare a :ParmVarDecl
>>>DeclAssignStmtLiteral "var" DivEqual (ParmCallDeclLiteral (BoolLiteral True))>>>DeclAssignStmtUnary (UnaryOperatorExpr "var" IdentIncrement)
Constructors
| DeclAssignStmtLiteral Identifier AssignOp ParmCallDecl | Variable assignment, expressed in rizz code like |
| DeclAssignStmtUnary UnaryOperatorExpr | Variable assignment via a |
data UnaryOperatorExpr Source #
Defines the expression (extended constructor for UnaryOperatorExpr).UnaryOperator
Example
A GHCI syntax example to declare a :UnaryOperatorExpr
>>>UnaryOperatorExpr "foo" IdentIncrement
Constructors
| UnaryOperatorExpr Identifier UnaryOp | Variable declaration, see |
Instances
| Show UnaryOperatorExpr Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> UnaryOperatorExpr -> ShowS show :: UnaryOperatorExpr -> String showList :: [UnaryOperatorExpr] -> ShowS | |
| Eq UnaryOperatorExpr Source # | Allows |
Defined in Ast Methods (==) :: UnaryOperatorExpr -> UnaryOperatorExpr -> Bool (/=) :: UnaryOperatorExpr -> UnaryOperatorExpr -> Bool | |
data BinaryOpParm Source #
Defines 's parameters.BinaryOp
Examples
A GHCI syntax example to declare a :BinaryOpParm
>>>BinaryOpParm (ParmCallDeclLiteral (IntLiteral 42))
Constructors
| BinaryOpParm ParmCallDecl | Function call which should be interpreted and evaluated. |
| BinaryOpParmBOp BinaryOpExpr | Any other |
Instances
| Show BinaryOpParm Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> BinaryOpParm -> ShowS show :: BinaryOpParm -> String showList :: [BinaryOpParm] -> ShowS | |
| Eq BinaryOpParm Source # | Allows |
Defined in Ast | |
data BinaryOpExpr Source #
Defines as any expression that can be evaluated by equality or arithmetic operators.BinaryOpExpr
Examples
A GHCI syntax example to declare a :BinaryOpExpr
>>>BinaryOpExpr (BinaryOpParm (ParmCallDeclIdent "x")) Div (BinaryOpParm (ParmCallDeclLiteral (IntLiteral 42)))>>>BinaryOpConst (ParmCallDeclLiteral (IntLiteral 84))>>>BinaryOpExpr (BinaryOpParm (ParmCallDeclLiteral (IntLiteral 10))) Add (BinaryOpParmBOp (BinaryOpExpr (BinaryOpParm (ParmCallDeclLiteral (IntLiteral 45))) Add (BinaryOpParm (ParmCallDeclExpr (CallExprDecl "foo" [ParmCallDeclIdent "bar"])))))
Constructors
| BinaryOpExpr BinaryOpParm BinaryOp BinaryOpParm | Two |
| BinaryOpConst ParmCallDecl | Any literal value defined by |
Instances
| Show BinaryOpExpr Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> BinaryOpExpr -> ShowS show :: BinaryOpExpr -> String showList :: [BinaryOpExpr] -> ShowS | |
| Eq BinaryOpExpr Source # | Allows |
Defined in Ast | |
data ParmCallDecl Source #
Defines as any value that can be evaluated. It also binds to function call's parameters.ParmCallDecl
Constructors
| ParmCallDeclLiteral Literal | Literal value defined by Literal |
| ParmCallDeclIdent Identifier | Identifier, which is a bound variable, expressed in rizz code like |
| ParmCallDeclExpr CallExprDecl | Function call, defined by |
| ParmCallBExpr BinaryOpParm BinaryOp BinaryOpParm | Binary expression, an exact copy of |
Instances
| Show ParmCallDecl Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> ParmCallDecl -> ShowS show :: ParmCallDecl -> String showList :: [ParmCallDecl] -> ShowS | |
| Eq ParmCallDecl Source # | Allows |
Defined in Ast | |
data CallExprDecl Source #
Defines as a function call expression.CallExprDecl
Constructors
| CallExprDecl Identifier [ParmCallDecl] | Function call expression, actually |
Instances
| Show CallExprDecl Source # | Allows |
Defined in Ast Methods showsPrec :: Int -> CallExprDecl -> ShowS show :: CallExprDecl -> String showList :: [CallExprDecl] -> ShowS | |
| Eq CallExprDecl Source # | Allows |
Defined in Ast | |