glados-2.0.0: Generic Language And Data Operand Syntax
LicenseMIT
Maintainerlaszlo.serdet@epitech.eu
Safe HaskellSafe-Inferred
LanguageHaskell2010

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

Expand

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

BNF definition

For the full rizz grammar syntax definition, see the BNF definition here.

Top level definitions

data Decl Source #

Defines Decl as a primary Ast node containing language declarations.

Examples

Expand

Some GHCI syntax examples for every Decl constructors:

>>> 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 `fn foo(Char: bar) -> Int {}`.

A FunctionDecl's rizz grammar in-code is as follow, in the given order:

ParmVarDecl ParmVarDeclExpr

Function parameter, expressed in rizz code like `Bool: baz`.

A ParmVarDecl's rizz grammar in-code is as follow, in the given order:

  • The parameter's type as a BuiltinType.
  • A Colon.
  • The parameter's name as an Identifier.
  • A trailing Comma if and only if the parameter is not the last one in the list.

Note that a standalone ParmVarDecl (not in a FunctionDecl expression) is a grammar violation!

VarDecl VarDeclStmt

Variable (with its type) declaration, expressed in rizz code like `Float pi = 3.14;`.

A VarDecl rizz grammar in-code is as follow, in the given order:

TODO: Merge both VarDecl and DeclStmt types as they are almost identical.

Note that a variable that initializes itself is a semantic violation!

RecordDecl RecordDeclExpr

Structure declaration, expressed in rizz code like `struct Foo {Int: x; Int y; Bool: alive;}`.

A RecordDecl rizz grammar in-code is as follow, in the given order:

Instances

Instances details
Show Decl Source #

Allows Decl to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> Decl -> ShowS

show :: Decl -> String

showList :: [Decl] -> ShowS

Eq Decl Source #

Allows Decl to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: Decl -> Decl -> Bool

(/=) :: Decl -> Decl -> Bool

data Stmt Source #

Defines Stmt as the second-primary Ast node containing language statements.

Examples

Expand

Some GHCI syntax examples for every Stmt constructors:

>>> 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

VarDeclStmt alias, allows a variable typed declaration to be a Stmt.

DeclStmt DeclStmt

Variable (without its type) assignment, expressed in rizz code like `foo = bar;` or `baz++;`.

A DeclStmt rizz grammar in-code is as follow, in the given order:

TODO: Rename this to AssignStmt to better describe it.

Note that this statement differs from VarDecl as it does not declare the variable's type, it only assigns a new value to it.

UnaryOperator UnaryOperatorExpr

Unary operator, which are Identifier self increment or decrement, expressed in rizz code as `foo++;` or `bar--`.

A DeclStmt rizz grammar in-code is as follow, in the given order:

BinaryOperator BinaryOpExpr

Binary operation, expressed in rizz code like `a < 42` or `True == foo(bar, baz)` (non exhaustive list).

For further details, see BinaryOpExpr.

IfStmt BinaryOpExpr CompoundStmt (Maybe CompoundStmt)

Conditional branching, expressed in rizz code like `if (foo == bar) {...}`.

A IfStmt rizz grammar in-code is as follow, in the given order:

WhileStmt BinaryOpExpr CompoundStmt

While loop, expressed in rizz code like `while (True) {...}`.

A WhileStmt rizz grammar in-code is as follow, in the given order:

ForStmt (Maybe VarDeclStmt) (Maybe BinaryOpExpr) (Maybe DeclStmt) CompoundStmt

For loop, expressed in rizz code like `for (Int i = 0; i < 10; i++) {...}`.

A ForeachStmt's rizz grammar in-code is as follow, in the given order:

ForeachStmt Identifier Identifier CompoundStmt

Foreach loop to iterate over a list, expressed in rizz code like `foreach (foo : it) {...}`.

A ForeachStmt's rizz grammar in-code is as follow, in the given order:

CallExpr CallExprDecl

Function call, expressed in rizz code like `foo(bar, baz);`.

A CallExprDecl's rizz grammar in-code is as follow, in the given order:

RetStmt BinaryOpExpr

Return from function, expressed in rizz code like `ret 42;`.

A RetStmt rizz grammar in-code is as follow, in the given order:

  • A leading Ret.
  • BinaryOpExpr which suits all needs, from constants to arithmetics or even function call.
  • A trailing Semicolon to end the expression.

Instances

Instances details
Show Stmt Source #

Allows Stmt to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> Stmt -> ShowS

show :: Stmt -> String

showList :: [Stmt] -> ShowS

Eq Stmt Source #

Allows Stmt to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: Stmt -> Stmt -> Bool

(/=) :: Stmt -> Stmt -> Bool

Declarations

newtype CompoundStmt Source #

Defines CompoundStmt type as a list of Stmt expressions.

Constructors

CompoundStmt [Stmt]

Compound statement block, expressed in rizz code as `{...}` (any code enclosed in a OpenCBracket and a CloseCBracket).

A CompoundStmt is used for the following special cases:

  • A function body
  • A conditional body
  • A loop body
  • An unnamed scope

Instances

Instances details
Show CompoundStmt Source #

Allows CompoundStmt to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> CompoundStmt -> ShowS

show :: CompoundStmt -> String

showList :: [CompoundStmt] -> ShowS

Eq CompoundStmt Source #

Allows CompoundStmt to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: CompoundStmt -> CompoundStmt -> Bool

(/=) :: CompoundStmt -> CompoundStmt -> Bool

data ParmVarDeclExpr Source #

Defines the ParmVarDeclExpr expression (extended constructor for ParmVarDecl).

Example

Expand

A GHCI syntax example to declare a ParmVarDecl:

>>> ParmVarDeclExpr Boolean "x"

Constructors

ParmVarDeclExpr BuiltinType Identifier

Function's parameter, see ParmVarDecl definition.

ParmVarRecord RecordDeclExpr 

Instances

Instances details
Show ParmVarDeclExpr Source #

Allows ParmVarDecl to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> ParmVarDeclExpr -> ShowS

show :: ParmVarDeclExpr -> String

showList :: [ParmVarDeclExpr] -> ShowS

Eq ParmVarDeclExpr Source #

Allows ParmVarDecl to be compared, needed for unit tests.

Instance details

Defined in Ast

data BuiltinType Source #

Defines the BuiltinType expression which lists available rizz data types (implementating Keyword Token types).

Constructors

Boolean

Boolean type keyword, see Bool Token

Character

Character type keyword, see Char Token

Integer

Integer type keyword, see Int Token

SinglePrecision

Float type keyword, see Float Token

ListType BuiltinType

List type, expressed in rizz code as `[Int]`, `[[Int]]`, etc.

Instances

Instances details
Show BuiltinType Source #

Allows BuiltinType to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> BuiltinType -> ShowS

show :: BuiltinType -> String

showList :: [BuiltinType] -> ShowS

Eq BuiltinType Source #

Allows BuiltinType to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: BuiltinType -> BuiltinType -> Bool

(/=) :: BuiltinType -> BuiltinType -> Bool

data VarDeclStmt Source #

Defines the VarDeclStmt expression (extended constructor for VarDecl).

Constructors

VarDeclStmt BuiltinType Identifier AssignOp ParmCallDecl

Variable declaration, see VarDecl definition.

Instances

Instances details
Show VarDeclStmt Source #

Allows VarDeclStmt to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> VarDeclStmt -> ShowS

show :: VarDeclStmt -> String

showList :: [VarDeclStmt] -> ShowS

Eq VarDeclStmt Source #

Allows VarDeclStmt to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: VarDeclStmt -> VarDeclStmt -> Bool

(/=) :: VarDeclStmt -> VarDeclStmt -> Bool

data RecordDeclExpr Source #

Constructors

RecordDeclExpr Identifier [ParmVarDeclExpr]

Structure declaration

Instances

Instances details
Show RecordDeclExpr Source #

Allows ParmVarDecl to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> RecordDeclExpr -> ShowS

show :: RecordDeclExpr -> String

showList :: [RecordDeclExpr] -> ShowS

Eq RecordDeclExpr Source #

Allows ParmVarDecl to be compared, needed for unit tests.

Instance details

Defined in Ast

Statements

data DeclStmt Source #

Defines the Stmt expression (extended constructor for Stmt).

Example

Expand

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 `foo = bar`.

DeclAssignStmtUnary UnaryOperatorExpr

Variable assignment via a UnaryOp, expressed in rizz code like `foo++`.

Instances

Instances details
Show DeclStmt Source #

Allows Stmt to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> DeclStmt -> ShowS

show :: DeclStmt -> String

showList :: [DeclStmt] -> ShowS

Eq DeclStmt Source #

Allows Stmt to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: DeclStmt -> DeclStmt -> Bool

(/=) :: DeclStmt -> DeclStmt -> Bool

data UnaryOperatorExpr Source #

Defines the UnaryOperatorExpr expression (extended constructor for UnaryOperator).

Example

Expand

A GHCI syntax example to declare a UnaryOperatorExpr:

>>> UnaryOperatorExpr "foo" IdentIncrement

Constructors

UnaryOperatorExpr Identifier UnaryOp

Variable declaration, see UnaryOperator definition.

Instances

Instances details
Show UnaryOperatorExpr Source #

Allows UnaryOperatorExpr to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> UnaryOperatorExpr -> ShowS

show :: UnaryOperatorExpr -> String

showList :: [UnaryOperatorExpr] -> ShowS

Eq UnaryOperatorExpr Source #

Allows UnaryOperatorExpr to be compared, needed for unit tests.BinaryOp

Instance details

Defined in Ast

data BinaryOpParm Source #

Defines BinaryOp's parameters.

Examples

Expand

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 BinaryOpExpr.

Instances

Instances details
Show BinaryOpParm Source #

Allows BinaryOpParm to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> BinaryOpParm -> ShowS

show :: BinaryOpParm -> String

showList :: [BinaryOpParm] -> ShowS

Eq BinaryOpParm Source #

Allows BinaryOpParm to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: BinaryOpParm -> BinaryOpParm -> Bool

(/=) :: BinaryOpParm -> BinaryOpParm -> Bool

data BinaryOpExpr Source #

Defines BinaryOpExpr as any expression that can be evaluated by equality or arithmetic operators.

Examples

Expand

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 BinaryOpParm being compared with any BinaryOp operator.

BinaryOpConst ParmCallDecl

Any literal value defined by ParmCallDecl.

Instances

Instances details
Show BinaryOpExpr Source #

Allows BinaryOpExpr to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> BinaryOpExpr -> ShowS

show :: BinaryOpExpr -> String

showList :: [BinaryOpExpr] -> ShowS

Eq BinaryOpExpr Source #

Allows BinaryOpExpr to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: BinaryOpExpr -> BinaryOpExpr -> Bool

(/=) :: BinaryOpExpr -> BinaryOpExpr -> Bool

data ParmCallDecl Source #

Defines ParmCallDecl as any value that can be evaluated. It also binds to function call's parameters.

Constructors

ParmCallDeclLiteral Literal

Literal value defined by Literal Token, expressed in rizz code like `42`.

ParmCallDeclIdent Identifier

Identifier, which is a bound variable, expressed in rizz code like `foo`.

ParmCallDeclExpr CallExprDecl

Function call, defined by CallExprDecl, see CallExprDecl.

ParmCallBExpr BinaryOpParm BinaryOp BinaryOpParm

Binary expression, an exact copy of BinaryOpExpr without self calling BinaryOpConst allowing expression in rizz code like `x - 1` in function calls.

Instances

Instances details
Show ParmCallDecl Source #

Allows ParmCallDecl to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> ParmCallDecl -> ShowS

show :: ParmCallDecl -> String

showList :: [ParmCallDecl] -> ShowS

Eq ParmCallDecl Source #

Allows ParmCallDecl to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: ParmCallDecl -> ParmCallDecl -> Bool

(/=) :: ParmCallDecl -> ParmCallDecl -> Bool

data CallExprDecl Source #

Defines CallExprDecl as a function call expression.

Constructors

CallExprDecl Identifier [ParmCallDecl]

Function call expression, actually calling another function.

Instances

Instances details
Show CallExprDecl Source #

Allows CallExprDecl to be printed.

Instance details

Defined in Ast

Methods

showsPrec :: Int -> CallExprDecl -> ShowS

show :: CallExprDecl -> String

showList :: [CallExprDecl] -> ShowS

Eq CallExprDecl Source #

Allows CallExprDecl to be compared, needed for unit tests.

Instance details

Defined in Ast

Methods

(==) :: CallExprDecl -> CallExprDecl -> Bool

(/=) :: CallExprDecl -> CallExprDecl -> Bool