c-repl/src/include/parser.h

139 lines
2.6 KiB
C
Raw Normal View History

2023-10-27 17:07:58 +02:00
#ifndef DEF_PARSER_H
#define DEF_PARSER_H
2023-10-27 17:16:41 +02:00
#include <vector>
2023-11-10 13:42:53 +01:00
#include <variant>
2023-10-27 17:07:58 +02:00
#include "tokenize.h"
using namespace std;
/** Grammar:
2023-11-10 16:56:50 +01:00
Prog -> Instruction Prog | Instruction
2023-10-27 17:07:58 +02:00
2023-11-10 16:56:50 +01:00
Instruction -> Statement | ExprStatement; | Expr; | ;
2023-10-27 17:07:58 +02:00
Statement -> // Rien pour l'instant, mais "for", "if" etc
ExprStatement ->
| Type Identifier = Expr // AssignedDeclaration
| Type Identifier // Declaration
Expr ->
| T
2023-10-27 17:16:41 +02:00
| T + Expr
| T - Expr
2023-10-27 17:07:58 +02:00
T ->
2023-11-10 16:56:50 +01:00
| U
| U * T
| U / T
| U % T
2023-10-27 17:07:58 +02:00
U ->
2023-10-27 17:16:41 +02:00
| F
| - U
| + U
F ->
2023-10-27 17:07:58 +02:00
| (Expr)
| Identifier
| Number
| Identifier = Expr // Assignment
*/
2023-11-10 16:56:50 +01:00
/**
* Type de Noeuds
*/
2023-10-27 17:07:58 +02:00
enum class NodeType {
2023-11-10 16:56:50 +01:00
/* On ne créé pas de nouveau noeud -> ; Prog */
Prog, // -> Instruction Prog
Epsilon, // -> ;
2023-10-27 17:07:58 +02:00
AssignedDeclaration, // -> Type Identifier = Expr
Declaration, // -> Type Identifier
2023-10-27 17:16:41 +02:00
Plus, // -> T + Expr
Minus, // -> T - Expr
2023-10-27 17:07:58 +02:00
Mult, // -> F * T
Div, // -> F / T
Mod, // -> F % T
UnaryMinus, // -> -F
UnaryPlus, // -> +F
2023-10-27 17:16:41 +02:00
Assignment // -> Identifier = Expr
2023-10-27 17:07:58 +02:00
};
2023-11-10 13:42:53 +01:00
struct InnerNode;
2023-11-10 16:56:50 +01:00
/**
* InnerNode: noeud interne
* Token: feuille
*/
2023-11-10 13:42:53 +01:00
using Node = variant<InnerNode, Token>;
2023-11-10 16:56:50 +01:00
/**
* Noeud interne
*/
2023-10-27 17:16:41 +02:00
struct InnerNode {
2023-10-27 17:07:58 +02:00
NodeType type;
2023-10-27 17:16:41 +02:00
vector<Node> children;
2023-10-27 17:07:58 +02:00
};
// A Leaf is always corresponding to a Token
2023-11-10 16:56:50 +01:00
/**
* Node: AST
* tokens: tokens pas encore parsés
*/
struct ParseReturn {
Node node;
vector<Token> tokens;
};
/**
* Utilisé pour revenir en arrière quand quelque chose n'est pas reconnu
*/
class ParseException : public std::exception {};
/**
* Parse a list of tokens and return the associated AST
*/
Node parse(vector<Token> tokens);
/**
* Parse something derivated from Instruction
*/
ParseReturn parse_instruction(vector<Token> tokens);
/**
* Parse something derivated from Statement
*/
ParseReturn parse_statement(vector<Token> tokens);
/**
* Parse something derivated from ExprStatement
*/
ParseReturn parse_expr_statement(vector<Token> tokens);
/**
* Parse something derivated from Expr
*/
ParseReturn parse_expr(vector<Token> tokens);
/**
* Parse something derivated from T
*/
ParseReturn parse_t(vector<Token> tokens);
/**
* Parse something derivated from U
*/
ParseReturn parse_u(vector<Token> tokens);
/**
* Parse something derivated from F
*/
ParseReturn parse_f(vector<Token> tokens);
2023-11-10 19:04:24 +01:00
/**
* Prints a tree for debugging it
*/
void _debug_print_tree(const Node& node, int depth, const string& prefix);
2023-10-27 17:07:58 +02:00
#endif