211 return assign(token, user_input);
224 char LETTER_ASS[] =
"=";
226 if (
consume(token, LETTER_ASS))
285 Node *node =
add(token, user_input);
311 Node *node =
mul(token, user_input);
360 return primary(token, user_input);
374 Node *node =
expr(token, user_input);
404 node->
blocks.emplace_back(
stmt(token, user_input));
412 node->
lhs =
expr(token, user_input);
418 node->
cond =
expr(token, user_input);
420 node->
then =
stmt(token, user_input);
422 node->
els =
stmt(token, user_input);
425 node =
expr(token, user_input);
440 std::vector<Node *> &code) {
441 while (!
at_eof(token)) code.emplace_back(
stmt(token, user_input));
442 code.emplace_back(
nullptr);
Definition: compiler.h:11
Node * stmt(Token *&token, char *user_input)
Parse and construct an AST node representing a statement.
Definition: parser.h:397
bool consume_tok(Token *&token, TokenKind tok)
Consumes the current token if it matches tok.
Definition: tokenizer.h:120
NodeKind
Enumeration representing different node kinds in the Abstract Syntax Tree (AST).
Definition: parser.h:113
@ ND_BLOCK
Definition: parser.h:131
@ ND_MUL
Definition: parser.h:116
@ ND_DIV
Definition: parser.h:117
@ ND_NUM
Definition: parser.h:127
@ ND_SUB
Definition: parser.h:115
@ ND_ASSIGN
Definition: parser.h:125
@ ND_LVAR
Definition: parser.h:126
@ ND_RETURN
Definition: parser.h:128
@ ND_EQ
Definition: parser.h:121
@ ND_NE
Definition: parser.h:122
@ ND_ADD
Definition: parser.h:114
@ ND_LT
Definition: parser.h:123
@ ND_OR
Definition: parser.h:119
@ ND_LE
Definition: parser.h:124
@ ND_IF
Definition: parser.h:129
@ ND_AND
Definition: parser.h:118
@ ND_FOR
Definition: parser.h:130
@ ND_NOT
Definition: parser.h:120
Node * new_binary(NodeKind kind, Node *lhs, Node *rhs)
Create a new binary AST node with the given kind, left-hand side, and right-hand side.
Definition: parser.h:181
Node * new_node(NodeKind kind)
Create a new AST node with the given kind.
Definition: parser.h:166
bool consume(Token *&token, char *op)
Consumes the current token if it matches op.
Definition: tokenizer.h:105
Node * assign(Token *&token, char *user_input)
Parses an assignment statement from a C-like language program.
Definition: parser.h:223
bool at_eof(Token *token)
Checks if the current token is at the end of the program.
Definition: tokenizer.h:183
Node * mul(Token *&token, char *user_input)
Parse and construct an AST node representing multiplication or division.
Definition: parser.h:333
void generate_ast(Token *&token, char *user_input, std::vector< Node * > &code)
Parses a C-like language program into an AST.
Definition: parser.h:439
Node * relational(Token *&token, char *user_input)
Parses a relational expression from a C-like language program.
Definition: parser.h:284
Node * add(Token *&token, char *user_input)
Parse and construct an AST node representing addition or subtraction.
Definition: parser.h:310
float expect_number(Token *&token, char *user_input)
Ensures that the current token is a number.
Definition: tokenizer.h:166
@ TOKEN_ELSE
Token representing the 'else' keyword.
Definition: tokenizer.h:79
@ TOKEN_RETURN
Token representing the 'return' keyword.
Definition: tokenizer.h:77
@ TOKEN_IF
Token representing the 'if' keyword.
Definition: tokenizer.h:78
Node * equality(Token *&token, char *user_input)
Parses an equality expression from a C-like language program.
Definition: parser.h:262
Node * expr(Token *&token, char *user_input)
Parses an expression from a C-like language program.
Definition: parser.h:210
Node * primary(Token *&token, char *user_input)
Parse and construct an AST node representing primary expressions.
Definition: parser.h:372
Node * unary(Token *&token, char *user_input)
Parse and construct an AST node representing unary operations.
Definition: parser.h:356
Node * new_num(float val)
Create a new AST node representing a numeric value with the given value.
Definition: parser.h:195
void expect(Token *&token, char *user_input, char *op)
Ensures that the current token matches op.
Definition: tokenizer.h:150
Token * consume_ident(Token *&token)
Consumes the current token if it is an identifier.
Definition: tokenizer.h:135
Node * logical(Token *&token, char *user_input)
Parses a logical expression from a C-like language program.
Definition: parser.h:240
char LETTER_NEQ[]
Array representing the inequality operator "!=".
Definition: parser.h:20
char LETTER_OR[]
Array representing the logical OR operator "||".
Definition: parser.h:70
char LETTER_LEQ[]
Array representing the less than or equal to operator "<=".
Definition: parser.h:30
char LETTER_RB[]
Array representing the right brace "}".
Definition: parser.h:105
char LETTER_DIV[]
Array representing the division operator "/".
Definition: parser.h:60
char LETTER_NOT[]
Array representing the logical NOT operator "!".
Definition: parser.h:75
char LETTER_LQ[]
Array representing the less than operator "<".
Definition: parser.h:25
char LETTER_EQ[]
Array representing the equality operator "==".
Definition: parser.h:15
char LETTER_LB[]
Array representing the left brace "{".
Definition: parser.h:100
char LETTER_MINUS[]
Array representing the subtraction operator "-".
Definition: parser.h:50
char LETTER_RP[]
Array representing the right parenthesis ")".
Definition: parser.h:85
char LETTER_ELSE[]
Array representing the keyword "else".
Definition: parser.h:95
char LETTER_GQ[]
Array representing the greater than operator ">".
Definition: parser.h:35
char LETTER_MUL[]
Array representing the multiplication operator "*".
Definition: parser.h:55
char LETTER_LP[]
Array representing the left parenthesis "(".
Definition: parser.h:80
char LETTER_PLUS[]
Array representing the addition operator "+".
Definition: parser.h:45
char LETTER_GEQ[]
Array representing the greater than or equal to operator ">=".
Definition: parser.h:40
char LETTER_SC[]
Array representing the semicolon ";".
Definition: parser.h:90
char LETTER_AND[]
Array representing the logical AND operator "&&".
Definition: parser.h:65
Structure representing a node in the Abstract Syntax Tree (AST).
Definition: parser.h:137
Node * lhs
Left-hand side.
Definition: parser.h:139
std::vector< Node * > blocks
Vector of child blocks.
Definition: parser.h:144
int offset
Offset.
Definition: parser.h:146
NodeKind kind
Node kind.
Definition: parser.h:138
Node * cond
Condition.
Definition: parser.h:141
float val
Used if kind is ND_NUM.
Definition: parser.h:145
Node * rhs
Right-hand side.
Definition: parser.h:140
Node * els
'Else' branch
Definition: parser.h:143
Node * then
'Then' branch
Definition: parser.h:142
Structure representing a token.
Definition: tokenizer.h:89
int var_id
Variable ID.
Definition: tokenizer.h:95
Implementation of tokenizer.