Parser
Parsing and AST representation.
Classes
| Name | Description |
|---|---|
Expr |
Expression node in AST. |
Parser |
Parser state over pre-tokenized input. |
BP |
Enumerations
| Name | Description |
|---|---|
TypeKind |
Language-level type kinds for expressions. |
UnaryOp |
Unary operators supported by the parser. |
BinaryOp |
Binary operators supported by the parser. |
ExprKind |
Discriminator for expression union payload. |
TypeKind
enum TypeKind
Language-level type kinds for expressions.
| Value | Description |
|---|---|
TYPE_INT |
|
TYPE_FLOAT |
|
TYPE_BOOL |
|
TYPE_VOID |
UnaryOp
enum UnaryOp
Unary operators supported by the parser.
| Value | Description |
|---|---|
UNOP_NEG |
|
UNOP_BITNOT |
|
UNOP_LOGICNOT |
BinaryOp
enum BinaryOp
Binary operators supported by the parser.
| Value | Description |
|---|---|
BINOP_ADD |
|
BINOP_SUB |
|
BINOP_MUL |
|
BINOP_DIV |
|
BINOP_MOD |
|
BINOP_BAND |
|
BINOP_BOR |
|
BINOP_BXOR |
|
BINOP_SHL |
|
BINOP_SHR |
|
BINOP_EQ |
|
BINOP_NEQ |
|
BINOP_LT |
|
BINOP_GT |
|
BINOP_LE |
|
BINOP_GE |
|
BINOP_AND |
|
BINOP_OR |
ExprKind
enum ExprKind
Discriminator for expression union payload.
| Value | Description |
|---|---|
EXPR_INT_LIT |
|
EXPR_FLOAT_LIT |
|
EXPR_BOOL_LIT |
|
EXPR_IDENT |
|
EXPR_UNARY |
|
EXPR_BINARY |
|
EXPR_CAST |
Typedefs
| Return | Name | Description |
|---|---|---|
struct Expr |
Expr |
Expr
struct Expr Expr()
Functions
| Return | Name | Description |
|---|---|---|
void |
parser_init |
Initializes parser state. |
Expr * |
parse_expr |
Parses expression using Pratt binding power recursion. |
Expr * |
alloc_expr static |
|
Token |
peek static |
|
Token |
advance static |
|
Token |
expect static |
|
BP |
infix_bp static |
|
int |
prefix_bp static |
|
UnaryOp |
tok_to_unop static |
|
BinaryOp |
tok_to_binop static |
|
void |
parser_init |
Initializes parser state. |
Expr * |
parse_expr |
Parses expression using Pratt binding power recursion. |
parser_init
void parser_init(Parser * p, const Token * tokens, int len, Arena * arena)
Initializes parser state.
Parameters
-
pParser output state. -
tokensToken stream to parse. -
lenNumber of entries intokens. -
arenaArena allocator used for AST nodes.
parse_expr
Expr * parse_expr(Parser * p, int min_bp)
Parses expression using Pratt binding power recursion.
Parses expression using Pratt binding power recursion.
Parameters
-
pParser to advance. -
min_bpMinimum binding power for this parse level.
Returns
Root AST expression node.
Parses expression using Pratt binding power recursion.
Parameters
-
pParser to advance. -
min_bpMinimum binding power for this parse level.
Returns
Root AST expression node.
Parses expression using Pratt binding power recursion.
alloc_expr
static
static Expr * alloc_expr(Parser * p, ExprKind kind, Token tok)
peek
static
static Token peek(const Parser * p)
advance
static
static Token advance(Parser * p)
expect
static
static Token expect(Parser * p, TokenKind kind)
infix_bp
static
static BP infix_bp(TokenKind k)
prefix_bp
static
static int prefix_bp(TokenKind k)
tok_to_unop
static
static UnaryOp tok_to_unop(TokenKind k)
tok_to_binop
static
static BinaryOp tok_to_binop(TokenKind k)
parser_init
void parser_init(Parser * p, const Token * tokens, int len, Arena * arena)
Initializes parser state.
Parameters
-
pParser output state. -
tokensToken stream to parse. -
lenNumber of entries intokens. -
arenaArena allocator used for AST nodes.
parse_expr
Expr * parse_expr(Parser * p, int min_bp)
Parses expression using Pratt binding power recursion.
Parses expression using Pratt binding power recursion.
Parameters
-
pParser to advance. -
min_bpMinimum binding power for this parse level.
Returns
Root AST expression node.
Parses expression using Pratt binding power recursion.
Parses expression using Pratt binding power recursion.
Parameters
-
pParser to advance. -
min_bpMinimum binding power for this parse level.
Returns
Root AST expression node.
Expr
#include <ast.h>
Expression node in AST.
Public Attributes
| Return | Name | Description |
|---|---|---|
ExprKind |
kind |
Node kind discriminator. |
Token |
token |
Source token associated with this node. |
TypeKind |
type |
Static type inferred/attached to this expression. |
int64_t |
ival |
Integer literal value. |
double |
fval |
Floating-point literal value. |
int |
bval |
Boolean literal value (0/1). |
const char * |
name |
|
int |
len |
|
struct Expr::@117100160201032120035371163051376233243362113273::@223377066143171234374152317375217301261204052325 |
ident |
Identifier name slice. |
UnaryOp |
op |
|
Expr * |
operand |
|
struct Expr::@117100160201032120035371163051376233243362113273::@216354312270004267302005327062207145041341154170 |
unary |
Unary expression payload. |
BinaryOp |
op |
|
Expr * |
lhs |
|
Expr * |
rhs |
|
struct Expr::@117100160201032120035371163051376233243362113273::@223146152302255324140367167352242003342124214244 |
binary |
Binary expression payload. |
TypeKind |
to |
|
struct Expr::@117100160201032120035371163051376233243362113273::@273223275321355065363357362177156102340064001275 |
cast |
Explicit cast payload. |
union Expr::@117100160201032120035371163051376233243362113273 |
as |
kind
ExprKind kind
Node kind discriminator.
token
Token token
Source token associated with this node.
type
TypeKind type
Static type inferred/attached to this expression.
ival
int64_t ival
Integer literal value.
fval
double fval
Floating-point literal value.
bval
int bval
Boolean literal value (0/1).
name
const char * name
len
int len
ident
struct Expr::@117100160201032120035371163051376233243362113273::@223377066143171234374152317375217301261204052325 ident
Identifier name slice.
op
UnaryOp op
operand
Expr * operand
unary
struct Expr::@117100160201032120035371163051376233243362113273::@216354312270004267302005327062207145041341154170 unary
Unary expression payload.
op
BinaryOp op
lhs
Expr * lhs
rhs
Expr * rhs
binary
struct Expr::@117100160201032120035371163051376233243362113273::@223146152302255324140367167352242003342124214244 binary
Binary expression payload.
to
TypeKind to
cast
struct Expr::@117100160201032120035371163051376233243362113273::@273223275321355065363357362177156102340064001275 cast
Explicit cast payload.
as
union Expr::@117100160201032120035371163051376233243362113273 as
Parser
#include <parser.h>
Parser state over pre-tokenized input.
Public Attributes
| Return | Name | Description |
|---|---|---|
const Token * |
tokens |
Token stream. |
int |
pos |
Current token index. |
int |
len |
Total token count. |
Arena * |
arena |
Arena used for AST node allocation. |
tokens
const Token * tokens
Token stream.
pos
int pos
Current token index.
len
int len
Total token count.
arena
Arena * arena
Arena used for AST node allocation.
BP
Public Attributes
| Return | Name | Description |
|---|---|---|
int |
lbp |
|
int |
rbp |
lbp
int lbp
rbp
int rbp