lang: replace stack-based parser with a recursive parser

This commit is contained in:
2026-04-06 18:25:43 +01:00
parent 3a549aa6df
commit b904813cef
77 changed files with 2984 additions and 6499 deletions
+103
View File
@@ -0,0 +1,103 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
static bool parse_block_params(
struct ivy_parser *parser, struct ivy_ast_block_node *block)
{
while (1) {
if (parse_symbol(parser, IVY_SYM_PIPE)) {
break;
}
if (!parse_symbol(parser, IVY_SYM_COLON)) {
parse_trace(
parser,
"expected `|`, or `:` followed by parameter "
"name");
return false;
}
struct ivy_token *ident = NULL;
if (!parse_word(parser, &ident)) {
parse_trace(
parser,
"expected parameter identifier after `:`");
return false;
}
struct ivy_ast_ident_node *ident_node = create_node(IVY_AST_IDENT);
ident_node->n_content = ident;
fx_queue_push_back(&block->n_arg, &ident_node->n_base.n_entry);
}
return true;
}
bool parse_block(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_symbol(parser, IVY_SYM_LEFT_BRACKET)) {
parse_trace(parser, "expected `[` before block contents");
return false;
}
struct ivy_ast_block_node *block = create_node(IVY_AST_BLOCK);
if (!block) {
parser->p_status = IVY_ERR_NO_MEMORY;
return false;
}
bool result = true;
if (peek_symbol(parser, IVY_SYM_COLON)) {
result = parse_block_params(parser, block);
}
if (!result) {
parse_trace(parser, "invalid block parameter list");
return false;
}
bool prev_was_expr = false;
while (1) {
struct ivy_ast_node *child = NULL;
if (parse_linefeed(parser)) {
continue;
}
if (parse_symbol(parser, IVY_SYM_RIGHT_BRACKET)) {
break;
}
if (prev_was_expr) {
if (!parse_symbol(parser, IVY_SYM_DOT)) {
parse_trace(
parser,
"expected `.` after expression");
result = false;
break;
}
prev_was_expr = false;
continue;
}
if (peek_expr_start(parser)) {
result = parse_expr(parser, EXPR_PARSE_NORMAL, &child);
prev_was_expr = true;
} else {
parse_trace(parser, "expected `]` or expression");
result = false;
}
if (!result || !child) {
break;
}
fx_queue_push_back(&block->n_expr, &child->n_entry);
}
*out = generic_node(block);
return result;
}
+79
View File
@@ -0,0 +1,79 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_cascade(
struct ivy_parser *parser, struct ivy_ast_node *first_msg,
struct ivy_ast_node **out)
{
if (!peek_symbol(parser, IVY_SYM_SEMICOLON)) {
parse_trace(parser, "expected `;`");
return false;
}
struct ivy_ast_msg_node *first = (struct ivy_ast_msg_node *)first_msg;
if (first->n_base.n_type != IVY_AST_MSG) {
parse_trace(parser, "cascade operand is not a message");
return false;
}
struct ivy_ast_cascade_node *cascade = create_node(IVY_AST_CASCADE);
cascade->n_recipient = first->n_recipient;
first->n_recipient = NULL;
fx_queue_push_back(&cascade->n_msg, &first->n_base.n_entry);
bool ok = true;
while (ok) {
parse_linefeed(parser);
if (!parse_symbol(parser, IVY_SYM_SEMICOLON)) {
break;
}
parse_linefeed(parser);
fx_queue_entry *entry = NULL;
struct ivy_ast_msg_node *msg = NULL;
struct ivy_token *tok = peek_token(parser);
if (!tok) {
break;
}
switch (tok->t_type) {
case IVY_TOK_LABEL:
ok = parse_keyword_msg(
parser, cascade->n_recipient,
EXPR_PARSE_STOP_SEMICOLON,
(struct ivy_ast_node **)&msg);
if (!ok) {
break;
}
break;
case IVY_TOK_IDENT:
ok = parse_unary_msg(
parser, cascade->n_recipient,
(struct ivy_ast_node **)&msg);
if (!ok) {
break;
}
if (peek_symbol(parser, IVY_SYM_LEFT_PAREN)) {
ok = parse_complex_msg(
parser, generic_node(msg),
(struct ivy_ast_node **)&msg);
}
break;
default:
parse_trace(parser, "expected message after `;`");
ok = false;
break;
}
msg->n_recipient = NULL;
fx_queue_push_back(&cascade->n_msg, &msg->n_base.n_entry);
}
*out = generic_node(cascade);
return true;
}
+365
View File
@@ -0,0 +1,365 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
static bool parse_named_selector(
struct ivy_parser *parser, struct ivy_ast_selector_node *sel)
{
struct ivy_token *tok = NULL;
if (!parse_word(parser, &tok)) {
parse_trace(NULL, "expected selector name");
return false;
}
sel->n_msg_name = tok;
if (!parse_symbol(parser, IVY_SYM_LEFT_PAREN)) {
return true;
}
bool ok = true;
while (1) {
if (parse_symbol(parser, IVY_SYM_RIGHT_PAREN)) {
break;
}
struct ivy_token *label = NULL, *arg = NULL;
if (!fx_queue_empty(&sel->n_arg_labels)
&& !parse_symbol(parser, IVY_SYM_COMMA)) {
parse_trace(NULL, "expected comma after selector arg");
ok = false;
break;
}
if (!parse_label(parser, &label)) {
parse_trace(NULL, "expected selector arg label");
ok = false;
break;
}
if (!parse_word(parser, &arg)) {
parse_trace(NULL, "expected selector arg name");
ok = false;
break;
}
fx_queue_push_back(&sel->n_arg_labels, &label->t_entry);
fx_queue_push_back(&sel->n_arg_names, &arg->t_entry);
}
return true;
}
static bool parse_unnamed_selector(
struct ivy_parser *parser, struct ivy_ast_selector_node *sel)
{
bool ok = true;
while (1) {
struct ivy_token *label = NULL, *arg = NULL;
if (!parse_label(parser, &label)) {
break;
}
if (!parse_word(parser, &arg)) {
parse_trace(NULL, "expected selector arg name");
ok = false;
break;
}
fx_queue_push_back(&sel->n_arg_labels, &label->t_entry);
fx_queue_push_back(&sel->n_arg_names, &arg->t_entry);
}
return true;
}
static bool parse_selector(struct ivy_parser *parser, struct ivy_ast_node **out)
{
struct ivy_ast_selector_node *sel = create_node(IVY_AST_SELECTOR);
if (parse_symbol(parser, IVY_SYM_PLUS)) {
sel->n_recipient = IVY_SELECTOR_RECIPIENT_CLASS;
} else if (parse_symbol(parser, IVY_SYM_HYPHEN)) {
sel->n_recipient = IVY_SELECTOR_RECIPIENT_OBJECT;
} else {
parse_trace(NULL, "invalid selector prefix");
return false;
}
bool ok = true;
if (peek_word(parser)) {
ok = parse_named_selector(parser, sel);
} else {
ok = parse_unnamed_selector(parser, sel);
}
if (!ok) {
return false;
}
*out = generic_node(sel);
return true;
}
static bool parse_msg(struct ivy_parser *parser, struct ivy_ast_node **out)
{
struct ivy_ast_node *sel = NULL;
if (!parse_selector(parser, &sel)) {
parse_trace(NULL, "invalid message handler selector");
return false;
}
struct ivy_ast_msgh_node *msgh = create_node(IVY_AST_MSGH);
msgh->n_sel = (struct ivy_ast_selector_node *)sel;
bool ok = true;
struct ivy_ast_node *body = NULL;
if (peek_symbol(parser, IVY_SYM_LEFT_BRACKET)) {
ok = parse_block(parser, &body);
} else if (parse_symbol(parser, IVY_SYM_PIPE)) {
ok = parse_expr(parser, EXPR_PARSE_NORMAL, &body);
ok = ok && parse_symbol(parser, IVY_SYM_DOT);
} else {
parse_trace(NULL, "expected message handler body");
ok = false;
}
if (!ok) {
return false;
}
msgh->n_body = body;
*out = generic_node(msgh);
return true;
}
static bool parse_default_property(
struct ivy_parser *parser, struct ivy_ast_property_node *prop)
{
if (!parse_symbol(parser, IVY_SYM_LEFT_PAREN)) {
parse_trace(parser, "expected '('");
return false;
}
bool ok = true;
while (ok) {
parse_linefeed(parser);
if (parse_symbol(parser, IVY_SYM_RIGHT_PAREN)) {
break;
}
int access_types_set
= prop->n_flags
& (IVY_AST_PROPERTY_SET | IVY_AST_PROPERTY_GET);
if (access_types_set != 0 && !parse_symbol(parser, IVY_SYM_COMMA)) {
parse_trace(
NULL,
"expected `,` or `)` after property access "
"type");
ok = false;
break;
}
parse_linefeed(parser);
int access_type = 0;
if (parse_keyword(parser, IVY_KW_GET)) {
access_type = IVY_AST_PROPERTY_GET;
} else if (parse_keyword(parser, IVY_KW_SET)) {
access_type = IVY_AST_PROPERTY_SET;
} else {
parse_trace(parser, "unknown property access type");
ok = false;
break;
}
parse_linefeed(parser);
if (prop->n_flags & access_type) {
parse_trace(parser, "property access type already set");
ok = false;
break;
}
prop->n_flags |= access_type;
}
return true;
}
static bool parse_property(struct ivy_parser *parser, struct ivy_ast_node **out)
{
struct ivy_token *tok = NULL;
if (!parse_word(parser, &tok)) {
parse_trace(NULL, "expected property name");
return false;
}
struct ivy_ast_property_node *prop = create_node(IVY_AST_PROPERTY);
prop->n_ident = tok;
if (peek_symbol(parser, IVY_SYM_LEFT_PAREN)) {
return parse_default_property(parser, prop);
}
if (!parse_symbol(parser, IVY_SYM_PIPE)) {
parse_trace(NULL, "expected `|` after property name");
return false;
}
bool ok = true;
while (ok) {
if (parse_symbol(parser, IVY_SYM_DOT)) {
break;
}
parse_linefeed(parser);
if ((prop->n_get || prop->n_set)
&& !parse_symbol(parser, IVY_SYM_COMMA)) {
parse_trace(
NULL,
"expected `,` or `.` after property "
"branch");
ok = false;
break;
}
parse_linefeed(parser);
int access_type = 0;
if (parse_keyword(parser, IVY_KW_GET)) {
access_type = IVY_AST_PROPERTY_GET;
} else if (parse_keyword(parser, IVY_KW_SET)) {
access_type = IVY_AST_PROPERTY_SET;
} else {
parse_trace(NULL, "unknown property access type");
ok = false;
break;
}
parse_linefeed(parser);
if (!parse_symbol(parser, IVY_SYM_EQUAL_RIGHT_ANGLE)) {
parse_trace(
NULL,
"expected `=>` after property branch "
"type");
ok = false;
break;
}
parse_linefeed(parser);
struct ivy_ast_node *body = NULL;
if (peek_symbol(parser, IVY_SYM_LEFT_BRACKET)) {
ok = parse_block(parser, &body);
} else {
ok = parse_expr(parser, EXPR_PARSE_STOP_COMMA, &body);
}
if (!ok) {
parse_trace(parser, "invalid property value");
break;
}
prop->n_flags |= access_type;
switch (access_type) {
case IVY_AST_PROPERTY_GET:
if (prop->n_get) {
parse_trace(NULL, "property getter already set");
ok = false;
break;
}
prop->n_get = body;
break;
case IVY_AST_PROPERTY_SET:
if (prop->n_set) {
parse_trace(NULL, "property setter already set");
ok = false;
break;
}
prop->n_set = body;
break;
default:
ok = false;
break;
}
}
if (!ok) {
return false;
}
*out = generic_node(prop);
return true;
}
bool parse_class(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_CLASS)) {
return false;
}
struct ivy_token *ident = NULL;
if (!parse_word(parser, &ident)) {
parse_trace(NULL, "expected class identifier");
return false;
}
if (!parse_symbol(parser, IVY_SYM_LEFT_BRACKET)) {
parse_trace(NULL, "expected `[` after class identifier");
return false;
}
struct ivy_ast_class_node *class = create_node(IVY_AST_CLASS);
if (!class) {
parser->p_status = IVY_ERR_NO_MEMORY;
return false;
}
class->n_ident = ident;
bool result = true;
while (1) {
struct ivy_ast_node *msg = NULL, *property = NULL;
if (parse_linefeed(parser)) {
continue;
}
if (parse_symbol(parser, IVY_SYM_RIGHT_BRACKET)) {
break;
}
if (peek_symbol(parser, IVY_SYM_HYPHEN)) {
result = parse_msg(parser, &msg);
} else if (peek_symbol(parser, IVY_SYM_PLUS)) {
result = parse_msg(parser, &msg);
} else if (peek_word(parser)) {
result = parse_property(parser, &property);
} else {
parse_trace(
parser, "unexpected token %s in class",
ivy_token_to_string(peek_token(parser)));
result = false;
}
if (!result) {
break;
}
if (msg) {
fx_queue_push_back(&class->n_msg_handlers, &msg->n_entry);
} else if (property) {
fx_queue_push_back(
&class->n_msg_handlers, &property->n_entry);
}
}
*out = generic_node(class);
return result;
}
+126
View File
@@ -0,0 +1,126 @@
#include "ctx.h"
#include <ivy/lang/parse.h>
#include <stdlib.h>
#include <string.h>
enum ivy_status ivy_parser_create(struct ivy_parser **parser)
{
struct ivy_parser *out = malloc(sizeof *out);
if (!out) {
return IVY_ERR_NO_MEMORY;
}
memset(out, 0x0, sizeof *out);
*parser = out;
return IVY_OK;
}
void ivy_parser_destroy(struct ivy_parser *parser)
{
free(parser);
}
enum ivy_status ivy_parser_set_diag_ctx(
struct ivy_parser *parser, struct ivy_diag_ctx *ctx)
{
parser->p_diag = ctx;
return IVY_OK;
}
enum ivy_status ivy_parser_get_status(struct ivy_parser *parser)
{
return parser->p_status;
}
enum ivy_status ivy_parser_parse(
struct ivy_parser *parser, struct ivy_lexer *src, struct ivy_ast_node **out)
{
parser->p_src = src;
enum ivy_status status = parse_unit(parser, out);
if (status == IVY_ERR_EOF && *out != NULL) {
status = IVY_OK;
}
return status;
}
struct ivy_token *peek_token(struct ivy_parser *parser)
{
fx_queue_entry *entry = fx_queue_first(&parser->p_tokens);
if (!entry) {
return ivy_lexer_peek(parser->p_src);
}
return fx_unbox(struct ivy_token, entry, t_entry);
}
struct ivy_token *peek_next_token(struct ivy_parser *parser)
{
if (fx_queue_empty(&parser->p_tokens)) {
struct ivy_token *tok = parse_token(parser);
fx_queue_push_back(&parser->p_tokens, &tok->t_entry);
return ivy_lexer_peek(parser->p_src);
}
fx_queue_entry *buf = fx_queue_first(&parser->p_tokens);
if (buf) {
buf = fx_queue_next(buf);
}
if (buf) {
return fx_unbox(struct ivy_token, buf, t_entry);
}
return ivy_lexer_peek(parser->p_src);
}
struct ivy_token *parse_token(struct ivy_parser *parser)
{
fx_queue_entry *entry = fx_queue_pop_front(&parser->p_tokens);
if (!entry) {
return ivy_lexer_read(parser->p_src);
}
return fx_unbox(struct ivy_token, entry, t_entry);
}
void discard_token(struct ivy_parser *parser)
{
struct ivy_token *tok = parse_token(parser);
if (tok) {
ivy_token_destroy(tok);
}
}
void __parse_trace(
struct ivy_parser *parser, const char *func, int line,
const char *format, ...)
{
struct ivy_token *tok = NULL;
if (parser) {
tok = peek_token(parser);
if (tok) {
fprintf(stderr, "[%zu:%zu] ", tok->t_start.c_row,
tok->t_start.c_col);
} else {
fprintf(stderr, "[EOF] ");
}
} else {
fprintf(stderr, "[UNK] ");
}
fprintf(stderr, "%s:%d: ", func, line);
if (tok) {
fprintf(stderr, "(%s) ", ivy_token_to_string(tok));
}
va_list va;
va_start(va, format);
vfprintf(stderr, format, va);
va_end(va);
fputc('\n', stderr);
}
+116
View File
@@ -0,0 +1,116 @@
#ifndef _LANG_PARSE_CTX_H_
#define _LANG_PARSE_CTX_H_
#include <ivy/lang/lex.h>
#include <ivy/status.h>
#include <stdio.h>
#define parse_trace(p, ...) \
__parse_trace(p, __FUNCTION__, __LINE__, __VA_ARGS__)
struct ivy_lexer;
struct ivy_diag_ctx;
struct ivy_ast_node;
struct ivy_parser {
struct ivy_lexer *p_src;
struct ivy_diag_ctx *p_diag;
enum ivy_status p_status;
fx_queue p_tokens;
};
#define create_node(type) ((void *)ivy_ast_node_create(type))
#define destroy_node(p) ivy_ast_node_destroy((struct ivy_ast_node *)(p))
#define generic_node(p) ((struct ivy_ast_node *)(p))
enum expr_parse_flags {
EXPR_PARSE_NORMAL = 0,
EXPR_PARSE_CONSUME_TERMINATOR = 0x01u,
EXPR_PARSE_STOP_KW_CONTROL = 0x02u,
EXPR_PARSE_STOP_KW_IN = 0x04u,
EXPR_PARSE_STOP_COMMA = 0x08u,
EXPR_PARSE_STOP_LABEL = 0x10u,
EXPR_PARSE_STOP_LEFT_BRACKET = 0x20u,
EXPR_PARSE_STOP_TUPLE_TERMINATOR = 0x40u,
EXPR_PARSE_STOP_SEMICOLON = 0x80u,
};
extern bool is_tuple_terminator(struct ivy_token *tok);
extern const struct ivy_operator *get_operator_from_token(struct ivy_token *tok);
extern struct ivy_ast_node *create_operator_node_from_token(struct ivy_token *tok);
extern const struct ivy_operator *get_operator_from_node(struct ivy_ast_node *n);
extern struct ivy_token *peek_token(struct ivy_parser *parser);
extern struct ivy_token *peek_next_token(struct ivy_parser *parser);
extern struct ivy_token *parse_token(struct ivy_parser *parser);
extern void discard_token(struct ivy_parser *parser);
extern bool peek_word(struct ivy_parser *parser);
extern bool peek_keyword(struct ivy_parser *parser, enum ivy_keyword kw);
extern bool peek_symbol(struct ivy_parser *parser, enum ivy_symbol sym);
extern bool peek_linefeed(struct ivy_parser *parser);
extern bool parse_eof(struct ivy_parser *parser);
extern bool parse_keyword(struct ivy_parser *parser, enum ivy_keyword kw);
extern bool parse_symbol(struct ivy_parser *parser, enum ivy_symbol sym);
extern bool parse_word(struct ivy_parser *parser, struct ivy_token **out);
extern bool parse_string(struct ivy_parser *parser, struct ivy_token **out);
extern bool parse_label(struct ivy_parser *parser, struct ivy_token **out);
extern bool parse_str_start(struct ivy_parser *parser);
extern bool parse_str_end(struct ivy_parser *parser);
extern bool parse_linefeed(struct ivy_parser *parser);
extern bool peek_expr_start(struct ivy_parser *parser);
extern bool parse_tuple(
struct ivy_parser *parser, struct ivy_ast_node *first_item,
struct ivy_ast_node **out);
extern bool parse_cascade(
struct ivy_parser *parser, struct ivy_ast_node *first_msg,
struct ivy_ast_node **out);
extern bool parse_unary_msg(
struct ivy_parser *parser, struct ivy_ast_node *recipient,
struct ivy_ast_node **out);
extern bool parse_keyword_msg(
struct ivy_parser *parser, struct ivy_ast_node *recipient,
enum expr_parse_flags flags, struct ivy_ast_node **out);
extern bool parse_complex_msg(
struct ivy_parser *parser, struct ivy_ast_node *unary_msg,
struct ivy_ast_node **out);
extern bool parse_fstring(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_index(
struct ivy_parser *parser, struct ivy_ast_node *container,
struct ivy_ast_node **out);
extern bool parse_unit(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_block(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_class(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_package(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_match(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_for(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_for_inline(
struct ivy_parser *parser, struct ivy_ast_node *body,
struct ivy_ast_node **out);
extern bool parse_while(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_while_inline(
struct ivy_parser *parser, struct ivy_ast_node *body,
struct ivy_ast_node **out);
extern bool parse_if(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_if_inline(
struct ivy_parser *parser, struct ivy_ast_node *body,
struct ivy_ast_node **out);
extern bool parse_try(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_package_id(struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_package_import(
struct ivy_parser *parser, struct ivy_ast_node **out);
extern bool parse_expr(
struct ivy_parser *parser, enum expr_parse_flags flags,
struct ivy_ast_node **out);
extern void __parse_trace(
struct ivy_parser *parser, const char *func, int line,
const char *format, ...);
#endif
View File
+7
View File
@@ -0,0 +1,7 @@
#include "ctx.h"
bool parse_eof(struct ivy_parser *parser)
{
struct ivy_token *tok = peek_token(parser);
return (!tok && ivy_lexer_get_status(parser->p_src) == IVY_ERR_EOF);
}
+966
View File
@@ -0,0 +1,966 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
enum expr_component {
EXPR_C_NONE = 0,
EXPR_C_BINARY_OP,
EXPR_C_UNARY_OP,
EXPR_C_OPERAND,
};
struct expr_parse_ctx {
fx_queue expr_operator_stack, expr_out_queue;
enum expr_component expr_prev;
enum expr_parse_flags expr_flags;
};
static bool finalise_expr(
struct expr_parse_ctx *ctx, struct ivy_ast_node **out,
enum ivy_operator_precedence minimum_precedence);
bool peek_expr_start(struct ivy_parser *parser)
{
struct ivy_token *tok = peek_token(parser);
if (!tok) {
return false;
}
switch (tok->t_type) {
case IVY_TOK_INT:
case IVY_TOK_ATOM:
case IVY_TOK_DOUBLE:
case IVY_TOK_IDENT:
case IVY_TOK_STRING:
case IVY_TOK_STR_START:
return true;
case IVY_TOK_KEYWORD:
switch (tok->t_keyword) {
case IVY_KW_IF:
case IVY_KW_WHILE:
case IVY_KW_FOR:
case IVY_KW_TRY:
case IVY_KW_UNLESS:
case IVY_KW_CONTINUE:
case IVY_KW_BREAK:
case IVY_KW_MATCH:
case IVY_KW_TRUE:
case IVY_KW_FALSE:
case IVY_KW_NULL:
return true;
default:
return false;
}
break;
case IVY_TOK_SYMBOL:
switch (tok->t_symbol) {
case IVY_SYM_LEFT_BRACE:
case IVY_SYM_LEFT_BRACKET:
case IVY_SYM_LEFT_PAREN:
case IVY_SYM_CARET:
case IVY_SYM_UNDERSCORE:
return true;
default:
return false;
}
break;
default:
return false;
}
}
static struct ivy_ast_node *pop_operand(
struct expr_parse_ctx *expr, enum ivy_operator_precedence min_precedence)
{
struct ivy_ast_node *out = NULL;
bool ok = finalise_expr(expr, &out, min_precedence);
if (!ok) {
parse_trace(NULL, "sub-expression is invalid");
return NULL;
}
return out;
}
static bool parse_simple_operand(
struct ivy_parser *parser, struct expr_parse_ctx *expr)
{
if (expr->expr_prev == EXPR_C_OPERAND) {
parse_trace(parser, "encountered two operands in a row");
return false;
}
expr->expr_prev = EXPR_C_OPERAND;
struct ivy_token *tok = peek_token(parser);
switch (tok->t_type) {
case IVY_TOK_INT: {
struct ivy_ast_int_node *v = create_node(IVY_AST_INT);
v->n_value = parse_token(parser);
fx_queue_push_back(&expr->expr_out_queue, &v->n_base.n_entry);
return true;
}
case IVY_TOK_DOUBLE: {
struct ivy_ast_double_node *v = create_node(IVY_AST_DOUBLE);
v->n_value = parse_token(parser);
fx_queue_push_back(&expr->expr_out_queue, &v->n_base.n_entry);
return true;
}
case IVY_TOK_STRING: {
struct ivy_ast_string_node *v = create_node(IVY_AST_STRING);
v->n_value = parse_token(parser);
fx_queue_push_back(&expr->expr_out_queue, &v->n_base.n_entry);
return true;
}
case IVY_TOK_IDENT: {
struct ivy_ast_ident_node *v = create_node(IVY_AST_IDENT);
v->n_content = parse_token(parser);
fx_queue_push_back(&expr->expr_out_queue, &v->n_base.n_entry);
return true;
}
case IVY_TOK_ATOM: {
struct ivy_ast_atom_node *v = create_node(IVY_AST_ATOM);
v->n_content = parse_token(parser);
fx_queue_push_back(&expr->expr_out_queue, &v->n_base.n_entry);
return true;
}
case IVY_TOK_SYMBOL:
switch (tok->t_symbol) {
case IVY_SYM_UNDERSCORE: {
struct ivy_ast_discard_node *v
= create_node(IVY_AST_DISCARD);
discard_token(parser);
fx_queue_push_back(
&expr->expr_out_queue, &v->n_base.n_entry);
return true;
}
default:
parse_trace(
parser, "token %s is not a simple operand",
ivy_symbol_to_string(tok->t_symbol));
return false;
}
break;
default:
parse_trace(
parser, "token %s is not a simple operand",
ivy_lex_token_type_to_string(tok->t_type));
return false;
}
}
void arith_push_operator(struct expr_parse_ctx *state, struct ivy_ast_node *node)
{
const struct ivy_operator *op = get_operator_from_node(node);
if (!op) {
return;
}
while (true) {
fx_queue_entry *top = fx_queue_last(&state->expr_operator_stack);
if (!top) {
break;
}
struct ivy_ast_node *top_node
= fx_unbox(struct ivy_ast_node, top, n_entry);
const struct ivy_operator *top_op = NULL;
switch (top_node->n_type) {
case IVY_AST_OP: {
struct ivy_ast_op_node *op_node
= (struct ivy_ast_op_node *)top_node;
top_op = op_node->n_op;
break;
}
case IVY_AST_MSG: {
struct ivy_ast_msg_node *msg_node
= (struct ivy_ast_msg_node *)top_node;
top_op = ivy_operator_get_by_id(IVY_OP_MSG);
break;
}
default:
return;
}
if (top_op->op_precedence < op->op_precedence
|| (top_op->op_precedence == op->op_precedence
&& op->op_associativity != IVY_ASSOCIATIVITY_LEFT)) {
break;
}
fx_queue_delete(&state->expr_operator_stack, top);
fx_queue_push_back(&state->expr_out_queue, top);
}
fx_queue_push_back(&state->expr_operator_stack, &node->n_entry);
}
static bool parse_binary_operator(
struct ivy_parser *parser, struct expr_parse_ctx *expr)
{
struct ivy_token *tok = parse_token(parser);
if (expr->expr_prev != EXPR_C_OPERAND) {
parse_trace(parser, "expected operand before binary operator");
return false;
}
expr->expr_prev = EXPR_C_BINARY_OP;
struct ivy_ast_node *op_node = create_operator_node_from_token(tok);
if (!op_node) {
parse_trace(parser, "unrecognised binary operator");
return false;
}
arith_push_operator(expr, op_node);
return true;
}
static bool expr_is_empty(struct expr_parse_ctx *expr)
{
return fx_queue_empty(&expr->expr_out_queue)
&& fx_queue_empty(&expr->expr_operator_stack);
}
static bool op_node_is_complete(struct ivy_ast_op_node *node)
{
if (!node->n_op) {
return false;
}
switch (node->n_op->op_arity) {
case IVY_OP_UNARY:
return node->n_right != NULL;
case IVY_OP_BINARY:
return (node->n_left != NULL && node->n_right != NULL);
default:
return false;
}
}
static bool finalise_expr(
struct expr_parse_ctx *ctx, struct ivy_ast_node **out,
enum ivy_operator_precedence minimum_precedence)
{
fx_queue_entry *entry = NULL;
while (true) {
entry = fx_queue_pop_back(&ctx->expr_operator_stack);
if (!entry) {
break;
}
struct ivy_ast_node *node
= fx_unbox(struct ivy_ast_node, entry, n_entry);
if (!node) {
/* this should never happen */
return false;
}
const struct ivy_operator *op = NULL;
switch (node->n_type) {
case IVY_AST_OP: {
struct ivy_ast_op_node *n = (struct ivy_ast_op_node *)node;
op = n->n_op;
break;
}
case IVY_AST_MSG:
/* all unary message operators have the same
* pre-defined precedence */
op = ivy_operator_get_by_id(IVY_OP_MSG);
break;
default:
return false;
}
/* if we aren't processing operators below a certain precedence
* then leave them on the stack and stop here. */
if (op->op_precedence < minimum_precedence) {
fx_queue_push_back(&ctx->expr_operator_stack, entry);
break;
}
fx_queue_push_back(&ctx->expr_out_queue, entry);
}
fx_queue q = FX_QUEUE_INIT;
fx_queue_entry *tmp = NULL;
entry = fx_queue_first(&ctx->expr_out_queue);
int i = 0;
while (entry) {
struct ivy_ast_node *item
= fx_unbox(struct ivy_ast_node, entry, n_entry);
fx_queue_entry *next = fx_queue_next(entry);
fx_queue_delete(&ctx->expr_out_queue, entry);
/* if the node is an operand, just push it to a
* temporary queue and come back to it later. */
if (item->n_type != IVY_AST_OP && item->n_type != IVY_AST_MSG) {
/* operand */
fx_queue_push_back(&q, &item->n_entry);
goto next;
}
const struct ivy_operator *op = NULL;
if (item->n_type == IVY_AST_MSG) {
struct ivy_ast_msg_node *msg
= (struct ivy_ast_msg_node *)item;
/* if the message has no recipient, it is a unary message,
* and the recipient is located on the operand stack.
*
* if the message DOES have a recipient, it is a
* self-contained keyword message, and can be pushed to
* the operand queue as-is. */
if (!msg->n_recipient) {
tmp = fx_queue_pop_back(&q);
msg->n_recipient = fx_unbox(
struct ivy_ast_node, tmp, n_entry);
}
fx_queue_push_back(&q, &msg->n_base.n_entry);
goto next;
}
struct ivy_ast_op_node *op_node = (struct ivy_ast_op_node *)item;
/* if an operator node is already complete (i.e. it
* already has all the operands it needs, it can be
* pushed to the operand queue as-is */
if (op_node_is_complete(op_node)) {
fx_queue_push_back(&q, &item->n_entry);
goto next;
}
/* otherwise, pop the relevant operands from the operand
* queue... */
op = op_node->n_op;
tmp = fx_queue_pop_back(&q);
op_node->n_right = fx_unbox(struct ivy_ast_node, tmp, n_entry);
if (op_node->n_right) {
op_node->n_right->n_parent = (struct ivy_ast_node *)op_node;
ivy_ast_node_extend_bounds_recursive(
(struct ivy_ast_node *)op_node,
(struct ivy_ast_node *)tmp);
}
if (op->op_arity == IVY_OP_BINARY) {
tmp = fx_queue_pop_back(&q);
op_node->n_left
= fx_unbox(struct ivy_ast_node, tmp, n_entry);
if (op_node->n_left) {
op_node->n_left->n_parent
= (struct ivy_ast_node *)op_node;
ivy_ast_node_extend_bounds_recursive(
(struct ivy_ast_node *)op_node,
(struct ivy_ast_node *)tmp);
}
}
/* ...and push the newly-completed operator node to the
* operand queue */
fx_queue_push_back(&q, &op_node->n_base.n_entry);
next:
entry = next;
}
#if 0
debug_printf("** after hierarchisation:\n");
print_expr_queues(state);
#endif
/* if we are not processing operators below a certain precedence,
* i.e. when determining the recipient of a keyword-message), these
* operators will still be on the parser state's operator stack, but
* their operands have just been moved to the temporary operand stack
* used above. move them back to the parser state's output queue here
* so they can be used later. */
entry = fx_queue_first(&ctx->expr_operator_stack);
while (entry) {
fx_queue_entry *entry2 = fx_queue_pop_front(&q);
if (!entry2) {
return false;
}
fx_queue_push_back(&ctx->expr_out_queue, entry2);
entry = fx_queue_next(entry);
}
#if 0
debug_printf("** after de-linearisation:\n");
print_expr_queues(state);
ivy_ast_node_print(*expr_tree);
debug_printf("------\n");
#endif
/* the final node remaining on the temp operand stack is the
* root node of the new expression tree */
tmp = fx_queue_pop_back(&q);
*out = fx_unbox(struct ivy_ast_node, tmp, n_entry);
return true;
}
static bool parse_call(
struct ivy_parser *parser, struct ivy_ast_node *target,
struct ivy_ast_node **out)
{
if (!parse_symbol(parser, IVY_SYM_LEFT_PAREN)) {
parse_trace(parser, "expected `(` before call arguments");
}
struct ivy_ast_msg_node *call = create_node(IVY_AST_MSG);
call->n_recipient = target;
call->n_sel = create_node(IVY_AST_SELECTOR);
call->n_sel->n_msg_name = ivy_token_create_ident("call");
bool ok = true;
while (ok) {
if (parse_symbol(parser, IVY_SYM_RIGHT_PAREN)) {
break;
}
if (!fx_queue_empty(&call->n_arg)
&& !parse_symbol(parser, IVY_SYM_COMMA)) {
parse_trace(
parser,
"expected `,` or `)` after call argument");
ok = false;
break;
}
struct ivy_ast_node *arg = NULL;
if (!parse_expr(parser, EXPR_PARSE_STOP_COMMA, &arg)) {
parse_trace(parser, "invalid call argument");
ok = false;
break;
}
struct ivy_token *label = ivy_token_create_discard();
fx_queue_push_back(&call->n_sel->n_arg_labels, &label->t_entry);
fx_queue_push_back(&call->n_arg, &arg->n_entry);
}
*out = generic_node(call);
return true;
}
static bool cleanup_expr(struct expr_parse_ctx *ctx)
{
return false;
}
bool parse_expr(
struct ivy_parser *parser, enum expr_parse_flags flags,
struct ivy_ast_node **out)
{
struct expr_parse_ctx ctx = {0};
ctx.expr_flags = flags;
bool ok = true;
bool done = false;
bool return_expr = false;
bool end_without_dot = false;
while (ok && !done) {
struct ivy_token *tok = peek_token(parser);
if (!tok) {
break;
}
if ((flags & EXPR_PARSE_STOP_TUPLE_TERMINATOR)
&& is_tuple_terminator(tok)) {
done = true;
break;
}
switch (tok->t_type) {
case IVY_TOK_LINEFEED:
discard_token(parser);
break;
case IVY_TOK_IDENT:
if (ctx.expr_prev == EXPR_C_OPERAND) {
struct ivy_ast_node *msg = NULL;
ok = parse_unary_msg(parser, NULL, &msg);
if (!ok) {
break;
}
fx_queue_push_back(
&ctx.expr_out_queue, &msg->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
} else {
ok = parse_simple_operand(parser, &ctx);
}
break;
case IVY_TOK_INT:
case IVY_TOK_STRING:
case IVY_TOK_DOUBLE:
case IVY_TOK_ATOM:
ok = parse_simple_operand(parser, &ctx);
break;
case IVY_TOK_STR_START: {
struct ivy_ast_node *v = NULL;
ok = parse_fstring(parser, &v);
if (!ok) {
break;
}
fx_queue_push_back(&ctx.expr_out_queue, &v->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
break;
}
case IVY_TOK_LABEL: {
if (flags & EXPR_PARSE_STOP_LABEL) {
done = true;
break;
}
struct ivy_ast_node *recipient
= pop_operand(&ctx, IVY_PRECEDENCE_KEYWORD_MSG);
if (!recipient) {
parse_trace(
parser,
"no valid recipient for keyword msg");
ok = false;
break;
}
struct ivy_ast_node *v = NULL;
ok = parse_keyword_msg(
parser, recipient, ctx.expr_flags, &v);
if (!ok) {
break;
}
fx_queue_push_back(&ctx.expr_out_queue, &v->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
break;
}
case IVY_TOK_KEYWORD: {
struct ivy_ast_node *operand = NULL;
if (expr_is_empty(&ctx)) {
end_without_dot = true;
}
switch (tok->t_keyword) {
case IVY_KW_ELSE:
case IVY_KW_CATCH:
done = true;
break;
case IVY_KW_MATCH:
ok = parse_match(parser, &operand);
break;
case IVY_KW_DO: {
discard_token(parser);
struct ivy_ast_do_node *do_node
= create_node(IVY_AST_DO);
ok = parse_block(parser, &do_node->n_block);
if (!ok) {
break;
}
operand = generic_node(do_node);
break;
}
case IVY_KW_IF:
if (flags & EXPR_PARSE_STOP_KW_CONTROL) {
done = true;
break;
}
if (ctx.expr_prev == EXPR_C_OPERAND) {
struct ivy_ast_node *body = pop_operand(
&ctx, IVY_PRECEDENCE_IF_ELSE);
if (!body) {
parse_trace(
parser,
"invalid "
"inline-if "
"body");
}
ok = parse_if_inline(parser, body, &operand);
} else {
ok = parse_if(parser, &operand);
}
break;
case IVY_KW_FOR:
if (flags & EXPR_PARSE_STOP_KW_CONTROL) {
done = true;
break;
}
if (ctx.expr_prev == EXPR_C_OPERAND) {
struct ivy_ast_node *body = pop_operand(
&ctx, IVY_PRECEDENCE_IF_ELSE);
if (!body) {
parse_trace(
parser,
"invalid "
"inline-for "
"body");
}
ok = parse_for_inline(
parser, body, &operand);
} else {
ok = parse_for(parser, &operand);
}
break;
case IVY_KW_WHILE:
if (flags & EXPR_PARSE_STOP_KW_CONTROL) {
done = true;
break;
}
if (ctx.expr_prev == EXPR_C_OPERAND) {
struct ivy_ast_node *body = pop_operand(
&ctx, IVY_PRECEDENCE_IF_ELSE);
if (!body) {
parse_trace(
parser,
"invalid "
"inline-while "
"body");
}
ok = parse_while_inline(
parser, body, &operand);
} else {
ok = parse_while(parser, &operand);
}
break;
case IVY_KW_IN:
if (flags & EXPR_PARSE_STOP_KW_IN) {
done = true;
break;
}
case IVY_KW_IS:
ok = parse_binary_operator(parser, &ctx);
break;
case IVY_KW_TRY:
if (flags & EXPR_PARSE_STOP_KW_CONTROL) {
done = true;
break;
}
ok = parse_try(parser, &operand);
break;
default:
parse_trace(
parser, "unrecognised keyword %s",
ivy_keyword_to_string(tok->t_keyword));
ok = false;
break;
}
if (ok && operand) {
fx_queue_push_back(
&ctx.expr_out_queue, &operand->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
if (end_without_dot && parse_linefeed(parser)) {
done = true;
}
}
break;
}
case IVY_TOK_SYMBOL:
switch (tok->t_symbol) {
case IVY_SYM_DOUBLE_COLON:
case IVY_SYM_PLUS:
case IVY_SYM_HYPHEN:
case IVY_SYM_FORWARD_SLASH:
case IVY_SYM_ASTERISK:
case IVY_SYM_PERCENT:
case IVY_SYM_AMPERSAND:
case IVY_SYM_EQUAL:
case IVY_SYM_HYPHEN_RIGHT_ANGLE:
case IVY_SYM_DOUBLE_EQUAL:
case IVY_SYM_LEFT_ANGLE:
case IVY_SYM_RIGHT_ANGLE:
case IVY_SYM_DOUBLE_LEFT_ANGLE:
case IVY_SYM_DOUBLE_RIGHT_ANGLE:
case IVY_SYM_LEFT_ANGLE_EQUAL:
case IVY_SYM_RIGHT_ANGLE_EQUAL:
case IVY_SYM_DOUBLE_LEFT_ANGLE_EQUAL:
case IVY_SYM_DOUBLE_RIGHT_ANGLE_EQUAL:
case IVY_SYM_PLUS_EQUAL:
case IVY_SYM_HYPHEN_EQUAL:
case IVY_SYM_FORWARD_SLASH_EQUAL:
case IVY_SYM_ASTERISK_EQUAL:
case IVY_SYM_AMPERSAND_EQUAL:
case IVY_SYM_PIPE_EQUAL:
case IVY_SYM_PERCENT_EQUAL:
case IVY_SYM_CARET_EQUAL:
case IVY_SYM_BANG_EQUAL:
ok = parse_binary_operator(parser, &ctx);
break;
case IVY_SYM_EQUAL_RIGHT_ANGLE:
done = true;
break;
case IVY_SYM_UNDERSCORE:
ok = parse_simple_operand(parser, &ctx);
break;
case IVY_SYM_COMMA: {
if (flags & EXPR_PARSE_STOP_COMMA) {
done = true;
break;
}
struct ivy_ast_node *first = pop_operand(
&ctx, IVY_PRECEDENCE_KEYWORD_MSG);
if (!first) {
parse_trace(
parser,
"no valid item for tuple");
return false;
}
struct ivy_ast_node *v = NULL;
ok = parse_tuple(parser, first, &v);
if (!ok) {
break;
}
fx_queue_push_back(&ctx.expr_out_queue, &v->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
break;
}
case IVY_SYM_SEMICOLON: {
if (flags & EXPR_PARSE_STOP_SEMICOLON) {
done = true;
break;
}
struct ivy_ast_node *first = pop_operand(
&ctx, IVY_PRECEDENCE_CASCADE);
if (!first) {
parse_trace(
parser,
"no valid recipient for "
"cascade");
return false;
}
struct ivy_ast_node *v = NULL;
ok = parse_cascade(parser, first, &v);
if (!ok) {
break;
}
fx_queue_push_back(&ctx.expr_out_queue, &v->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
break;
}
case IVY_SYM_LEFT_PAREN: {
if (ctx.expr_prev == EXPR_C_OPERAND) {
fx_queue_entry *entry = fx_queue_pop_back(
&ctx.expr_out_queue);
if (!entry) {
parse_trace(
parser,
"no valid recipient "
"for complex msg");
return false;
}
struct ivy_ast_node *target = fx_unbox(
struct ivy_ast_node, entry,
n_entry);
if (target->n_type == IVY_AST_MSG) {
ok = parse_complex_msg(
parser, target, &target);
} else {
ok = parse_call(
parser, target, &target);
}
if (!ok) {
break;
}
fx_queue_push_back(
&ctx.expr_out_queue,
&target->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
break;
}
struct ivy_ast_node *subexpr = NULL;
discard_token(parser);
ok = parse_expr(parser, EXPR_PARSE_NORMAL, &subexpr);
if (!ok) {
break;
}
if (!parse_symbol(parser, IVY_SYM_RIGHT_PAREN)) {
parse_trace(
parser,
"expected `)` after "
"parenthesized "
"expression");
ok = false;
break;
}
fx_queue_push_back(
&ctx.expr_out_queue, &subexpr->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
break;
}
case IVY_SYM_LEFT_BRACKET: {
if (flags & EXPR_PARSE_STOP_LEFT_BRACKET) {
done = true;
break;
}
if (ctx.expr_prev == EXPR_C_OPERAND) {
struct ivy_ast_node *container = pop_operand(
&ctx, IVY_PRECEDENCE_SUBSCRIPT);
if (!container) {
parse_trace(
parser,
"invalid subscript "
"container");
return false;
}
struct ivy_ast_node *v = NULL;
ok = parse_index(parser, container, &v);
if (!ok) {
break;
}
fx_queue_push_back(
&ctx.expr_out_queue, &v->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
break;
}
struct ivy_ast_node *block = NULL;
ok = parse_block(parser, &block);
if (ok) {
fx_queue_push_back(
&ctx.expr_out_queue,
&block->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
}
break;
}
case IVY_SYM_LEFT_BRACE: {
if (ctx.expr_prev == EXPR_C_OPERAND) {
parse_trace(
parser,
"package cannot be "
"used after "
"operand");
ok = false;
break;
}
struct ivy_ast_node *pkg = NULL;
ok = parse_package(parser, &pkg);
if (ok) {
fx_queue_push_back(
&ctx.expr_out_queue, &pkg->n_entry);
ctx.expr_prev = EXPR_C_OPERAND;
}
break;
}
case IVY_SYM_DOT: {
struct ivy_token *dot = tok;
struct ivy_token *next = peek_next_token(parser);
if (next && next->t_type == IVY_TOK_LINEFEED) {
if (flags & EXPR_PARSE_CONSUME_TERMINATOR) {
discard_token(parser);
}
done = true;
break;
}
ok = parse_binary_operator(parser, &ctx);
break;
}
case IVY_SYM_CARET: {
if (expr_is_empty(&ctx)) {
if (return_expr) {
parse_trace(
parser,
"`^` can only "
"be used "
"once in "
"an "
"expression");
ok = false;
break;
}
discard_token(parser);
return_expr = true;
break;
}
ok = parse_binary_operator(parser, &ctx);
break;
}
case IVY_SYM_RIGHT_PAREN:
case IVY_SYM_RIGHT_BRACE:
case IVY_SYM_RIGHT_BRACKET:
done = true;
break;
default:
parse_trace(
parser, "unrecognised symbol %s",
ivy_symbol_to_string(tok->t_symbol));
ok = false;
break;
}
break;
default:
parse_trace(
parser, "unrecognised token %s",
ivy_lex_token_type_to_string(tok->t_type));
ok = false;
break;
}
if (!ok) {
break;
}
}
if (!ok) {
cleanup_expr(&ctx);
return false;
}
ok = finalise_expr(&ctx, out, IVY_PRECEDENCE_ASSIGN);
if (!ok) {
cleanup_expr(&ctx);
return false;
}
if (return_expr) {
struct ivy_ast_return_node *ret = create_node(IVY_AST_RETURN);
ret->n_val = *out;
*out = generic_node(ret);
}
return true;
}
+67
View File
@@ -0,0 +1,67 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_for(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_FOR)) {
parse_trace(parser, "expected `for` keyword");
return false;
}
struct ivy_ast_for_loop_node *for_node = create_node(IVY_AST_FOR_LOOP);
if (!parse_expr(parser, EXPR_PARSE_STOP_KW_IN, &for_node->n_iterator)) {
parse_trace(parser, "invalid for-loop iterator");
return false;
}
if (!parse_keyword(parser, IVY_KW_IN)) {
parse_trace(parser, "expected `in` after for-loop iterator");
return false;
}
if (!parse_expr(parser, EXPR_PARSE_STOP_LEFT_BRACKET, &for_node->n_iterable)) {
parse_trace(parser, "invalid for-loop iterable");
return false;
}
if (!parse_block(parser, &for_node->n_body)) {
parse_trace(parser, "invalid for-loop body");
return false;
}
*out = generic_node(for_node);
return true;
}
bool parse_for_inline(
struct ivy_parser *parser, struct ivy_ast_node *body,
struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_FOR)) {
parse_trace(parser, "expected `for` keyword");
return false;
}
struct ivy_ast_for_loop_node *for_node = create_node(IVY_AST_FOR_LOOP);
for_node->n_body = body;
if (!parse_expr(parser, EXPR_PARSE_STOP_KW_IN, &for_node->n_iterator)) {
parse_trace(parser, "invalid for-loop iterator");
return false;
}
if (!parse_keyword(parser, IVY_KW_IN)) {
parse_trace(parser, "expected `in` after for-loop iterator");
return false;
}
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &for_node->n_iterable)) {
parse_trace(parser, "invalid for-loop iterable");
return false;
}
*out = generic_node(for_node);
return true;
}
+64
View File
@@ -0,0 +1,64 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_fstring(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_str_start(parser)) {
parse_trace(parser, "expected STR_START");
return false;
}
struct ivy_ast_fstring_node *fstring = create_node(IVY_AST_FSTRING);
bool ok = true;
while (1) {
if (parse_str_end(parser)) {
break;
}
struct ivy_token *tok = NULL;
if (parse_string(parser, &tok)) {
struct ivy_ast_string_node *string
= create_node(IVY_AST_STRING);
string->n_value = tok;
fx_queue_push_back(
&fstring->n_value, &string->n_base.n_entry);
continue;
}
if (parse_symbol(parser, IVY_SYM_LEFT_BRACE)) {
struct ivy_ast_node *expr = NULL;
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &expr)) {
parse_trace(
parser,
"invalid expression in fstring");
ok = false;
break;
}
if (!parse_symbol(parser, IVY_SYM_RIGHT_BRACE)) {
parse_trace(
parser,
"expected `}` after expression in "
"fstring");
ok = false;
break;
}
fx_queue_push_back(&fstring->n_value, &expr->n_entry);
continue;
}
parse_trace(parser, "unexpected token in fstring");
ok = false;
break;
}
if (!ok) {
return ok;
}
*out = generic_node(fstring);
return ok;
}
+99
View File
@@ -0,0 +1,99 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_if(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_IF)) {
parse_trace(parser, "expected `if` keyword");
return false;
}
struct ivy_ast_cond_group_node *cond_group
= create_node(IVY_AST_COND_GROUP);
struct ivy_ast_cond_node *cond = create_node(IVY_AST_COND);
if (!parse_expr(parser, EXPR_PARSE_STOP_LEFT_BRACKET, &cond->n_cond)) {
parse_trace(parser, "invalid if predicate");
return false;
}
if (!parse_block(parser, &cond->n_body)) {
parse_trace(parser, "invalid if body");
return false;
}
fx_queue_push_back(&cond_group->n_branches, &cond->n_base.n_entry);
bool ok = true;
while (1) {
struct ivy_ast_node *branch_pred = NULL;
struct ivy_ast_node *branch_block = NULL;
if (parse_keyword(parser, IVY_KW_ELIF)) {
if (!parse_expr(
parser, EXPR_PARSE_STOP_LEFT_BRACKET,
&branch_pred)) {
parse_trace(
parser,
"invalid elif branch predicate");
ok = false;
break;
}
} else if (!parse_keyword(parser, IVY_KW_ELSE)) {
break;
}
if (!parse_block(parser, &branch_block)) {
parse_trace(parser, "invalid branch body");
ok = false;
break;
}
struct ivy_ast_cond_node *cond = create_node(IVY_AST_COND);
cond->n_cond = branch_pred;
cond->n_body = branch_block;
fx_queue_push_back(&cond_group->n_branches, &cond->n_base.n_entry);
if (!branch_pred) {
break;
}
}
*out = generic_node(cond_group);
return true;
}
bool parse_if_inline(
struct ivy_parser *parser, struct ivy_ast_node *body,
struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_IF)) {
parse_trace(parser, "expected `if` keyword");
return false;
}
struct ivy_ast_cond_group_node *cond_group
= create_node(IVY_AST_COND_GROUP);
struct ivy_ast_cond_node *cond = create_node(IVY_AST_COND);
cond->n_body = body;
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &cond->n_cond)) {
parse_trace(parser, "invalid if predicate");
return false;
}
fx_queue_push_back(&cond_group->n_branches, &cond->n_base.n_entry);
if (parse_keyword(parser, IVY_KW_ELSE)) {
struct ivy_ast_cond_node *cond = create_node(IVY_AST_COND);
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &cond->n_body)) {
parse_trace(parser, "invalid else body");
return false;
}
fx_queue_push_back(&cond_group->n_branches, &cond->n_base.n_entry);
}
*out = generic_node(cond_group);
return true;
}
+37
View File
@@ -0,0 +1,37 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_index(
struct ivy_parser *parser, struct ivy_ast_node *container,
struct ivy_ast_node **out)
{
if (!parse_symbol(parser, IVY_SYM_LEFT_BRACKET)) {
parse_trace(parser, "expected `[` before subscript value");
return false;
}
struct ivy_ast_node *subscript = NULL;
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &subscript)) {
parse_trace(parser, "invalid subscript value");
return false;
}
if (!parse_symbol(parser, IVY_SYM_RIGHT_BRACKET)) {
parse_trace(parser, "expected `]` after subscript value");
return false;
}
struct ivy_ast_op_node *op_node = create_node(IVY_AST_OP);
if (!op_node) {
return false;
}
op_node->n_op = ivy_operator_get_by_id(IVY_OP_SUBSCRIPT);
op_node->n_left = container;
op_node->n_right = subscript;
*out = generic_node(op_node);
return true;
}
+31
View File
@@ -0,0 +1,31 @@
#include "ctx.h"
bool peek_keyword(struct ivy_parser *parser, enum ivy_keyword kw)
{
struct ivy_token *tok = peek_token(parser);
if (!tok || tok->t_type != IVY_TOK_KEYWORD) {
return false;
}
if (tok->t_keyword != kw) {
return false;
}
return true;
}
bool parse_keyword(struct ivy_parser *parser, enum ivy_keyword kw)
{
struct ivy_token *tok = peek_token(parser);
if (!tok || tok->t_type != IVY_TOK_KEYWORD) {
return false;
}
if (tok->t_keyword != kw) {
return false;
}
parse_token(parser);
ivy_token_destroy(tok);
return true;
}
+18
View File
@@ -0,0 +1,18 @@
#include "ctx.h"
bool peek_label(struct ivy_parser *parser)
{
struct ivy_token *tok = peek_token(parser);
return (!tok || tok->t_type == IVY_TOK_LABEL);
}
bool parse_label(struct ivy_parser *parser, struct ivy_token **out)
{
struct ivy_token *tok = peek_token(parser);
if (!tok || tok->t_type != IVY_TOK_LABEL) {
return false;
}
*out = parse_token(parser);
return true;
}
+19
View File
@@ -0,0 +1,19 @@
#include "ctx.h"
bool peek_linefeed(struct ivy_parser *parser)
{
struct ivy_token *tok = peek_token(parser);
return (tok && tok->t_type == IVY_TOK_LINEFEED);
}
bool parse_linefeed(struct ivy_parser *parser)
{
struct ivy_token *tok = peek_token(parser);
if (!tok || tok->t_type != IVY_TOK_LINEFEED) {
return false;
}
ivy_lexer_read(parser->p_src);
ivy_token_destroy(tok);
return true;
}
+75
View File
@@ -0,0 +1,75 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_match(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_MATCH)) {
parse_trace(parser, "expected `match` keyword");
return false;
}
struct ivy_ast_match_node *match = create_node(IVY_AST_MATCH);
if (!parse_expr(parser, EXPR_PARSE_STOP_LEFT_BRACKET, &match->n_cond)) {
parse_trace(parser, "invalid match predicate");
return false;
}
if (!parse_symbol(parser, IVY_SYM_LEFT_BRACKET)) {
parse_trace(parser, "expected `[` after match predicate");
return false;
}
bool ok = true;
while (1) {
if (parse_symbol(parser, IVY_SYM_RIGHT_BRACKET)) {
break;
}
if (!fx_queue_empty(&match->n_branches)
&& !parse_symbol(parser, IVY_SYM_COMMA)) {
parse_trace(
parser,
"expected `,` or `]` after match branch value");
ok = false;
break;
}
struct ivy_ast_node *branch_pred = NULL;
struct ivy_ast_node *branch_value = NULL;
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &branch_pred)) {
parse_trace(parser, "invalid match branch predicate");
ok = false;
break;
}
if (!parse_symbol(parser, IVY_SYM_EQUAL_RIGHT_ANGLE)) {
parse_trace(
parser,
"expected `=>` after match branch predicate");
ok = false;
break;
}
if (peek_symbol(parser, IVY_SYM_LEFT_BRACKET)) {
ok = parse_block(parser, &branch_value);
} else {
ok = parse_expr(
parser, EXPR_PARSE_STOP_COMMA, &branch_value);
}
if (!ok) {
parse_trace(parser, "invalid match branch value");
break;
}
struct ivy_ast_cond_node *cond = create_node(IVY_AST_COND);
cond->n_cond = branch_pred;
cond->n_body = branch_value;
fx_queue_push_back(&match->n_branches, &cond->n_base.n_entry);
}
*out = generic_node(match);
return true;
}
+143
View File
@@ -0,0 +1,143 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_unary_msg(
struct ivy_parser *parser, struct ivy_ast_node *recipient,
struct ivy_ast_node **out)
{
struct ivy_token *selector_tok = NULL;
if (!parse_word(parser, &selector_tok)) {
parse_trace(parser, "expected unary message selector");
return false;
}
struct ivy_ast_selector_node *sel = create_node(IVY_AST_SELECTOR);
sel->n_msg_name = selector_tok;
struct ivy_ast_msg_node *msg = create_node(IVY_AST_MSG);
msg->n_sel = sel;
msg->n_recipient = recipient;
*out = generic_node(msg);
return true;
}
bool parse_keyword_msg(
struct ivy_parser *parser, struct ivy_ast_node *recipient,
enum expr_parse_flags flags, struct ivy_ast_node **out)
{
#if 0
if (!recipient) {
recipient = pop_operand(expr, IVY_PRECEDENCE_KEYWORD_MSG);
}
if (!recipient) {
parse_trace(parser, "no valid recipient for keyword msg");
return false;
}
#endif
struct ivy_ast_selector_node *sel = create_node(IVY_AST_SELECTOR);
struct ivy_ast_msg_node *msg = create_node(IVY_AST_MSG);
msg->n_recipient = recipient;
msg->n_sel = sel;
bool ok = true;
while (1) {
struct ivy_token *label = NULL;
if (!parse_label(parser, &label)) {
break;
}
struct ivy_ast_node *value = NULL;
flags |= EXPR_PARSE_STOP_LABEL;
flags |= EXPR_PARSE_STOP_SEMICOLON;
flags |= EXPR_PARSE_STOP_KW_CONTROL;
flags &= ~EXPR_PARSE_CONSUME_TERMINATOR;
if (!parse_expr(parser, flags, &value)) {
parse_trace(parser, "invalid keyword msg arg value");
ok = false;
break;
}
fx_queue_push_back(&sel->n_arg_labels, &label->t_entry);
fx_queue_push_back(&msg->n_arg, &value->n_entry);
}
*out = generic_node(msg);
return true;
}
bool parse_complex_msg(
struct ivy_parser *parser, struct ivy_ast_node *msg_node,
struct ivy_ast_node **out)
{
if (!parse_symbol(parser, IVY_SYM_LEFT_PAREN)) {
parse_trace(parser, "expected `(` before complex msg arg list");
return false;
}
#if 0
if (!msg) {
fx_queue_entry *entry = fx_queue_pop_back(&expr->expr_out_queue);
if (!entry) {
parse_trace(
parser, "no valid recipient for complex msg");
return false;
}
msg = fx_unbox(struct ivy_ast_msg_node, entry, n_base.n_entry);
if (msg->n_base.n_type != IVY_AST_MSG) {
parse_trace(
parser,
"previous expression is not a unary message");
return false;
}
}
#endif
struct ivy_ast_msg_node *msg = (struct ivy_ast_msg_node *)msg_node;
struct ivy_ast_selector_node *sel = msg->n_sel;
msg->n_sel = sel;
bool ok = true;
while (1) {
struct ivy_token *label = NULL;
if (parse_symbol(parser, IVY_SYM_RIGHT_PAREN)) {
break;
}
if (!fx_queue_empty(&msg->n_arg)
&& !parse_symbol(parser, IVY_SYM_COMMA)) {
parse_trace(
parser, "expected `,` after complex msg arg");
ok = false;
break;
}
if (parse_label(parser, &label)) {
fx_queue_push_back(&sel->n_arg_labels, &label->t_entry);
} else {
label = ivy_token_create_discard();
fx_queue_push_back(&sel->n_arg_labels, &label->t_entry);
}
struct ivy_ast_node *value = NULL;
if (!parse_expr(parser, EXPR_PARSE_STOP_COMMA, &value)) {
parse_trace(parser, "invalid complex msg arg value");
ok = false;
break;
}
fx_queue_push_back(&msg->n_arg, &value->n_entry);
}
*out = generic_node(msg);
return true;
}
+43
View File
@@ -0,0 +1,43 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
#include <ivy/lang/operator.h>
const struct ivy_operator *get_operator_from_token(struct ivy_token *tok)
{
switch (tok->t_type) {
case IVY_TOK_SYMBOL:
return ivy_operator_get_by_token(tok->t_symbol);
case IVY_TOK_KEYWORD:
return ivy_operator_get_by_token(tok->t_keyword);
default:
return ivy_operator_get_by_token(tok->t_type);
}
}
struct ivy_ast_node *create_operator_node_from_token(struct ivy_token *tok)
{
const struct ivy_operator *op = get_operator_from_token(tok);
if (!op) {
return NULL;
}
struct ivy_ast_op_node *op_node = create_node(IVY_AST_OP);
op_node->n_op = op;
ivy_ast_node_set_bounds_from_token(generic_node(op_node), tok);
return generic_node(op_node);
}
const struct ivy_operator *get_operator_from_node(struct ivy_ast_node *n)
{
switch (n->n_type) {
case IVY_AST_OP: {
struct ivy_ast_op_node *op = (struct ivy_ast_op_node *)n;
return op->n_op;
}
case IVY_AST_MSG:
return ivy_operator_get_by_id(IVY_OP_MSG);
default:
return NULL;
}
}
+49
View File
@@ -0,0 +1,49 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_package_id(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_PACKAGE)) {
parse_trace(NULL, "expected `package` before package statement");
return false;
}
struct ivy_ast_unit_package_node *unit_pkg
= create_node(IVY_AST_UNIT_PACKAGE);
struct ivy_token *tok = NULL;
if (!parse_word(parser, &tok)) {
parse_trace(NULL, "expected word after `package` keyword");
return false;
}
fx_queue_push_back(&unit_pkg->n_ident, &tok->t_entry);
while (1) {
if (parse_linefeed(parser)) {
break;
}
if (!parse_symbol(parser, IVY_SYM_DOT)) {
parse_trace(
NULL,
"expected linefeed or `.` after package "
"identifier word");
destroy_node(unit_pkg);
return false;
}
if (!parse_word(parser, &tok)) {
parse_trace(
NULL, "expected word after `package` keyword");
destroy_node(unit_pkg);
return false;
}
fx_queue_push_back(&unit_pkg->n_ident, &tok->t_entry);
}
*out = generic_node(unit_pkg);
return true;
}
+49
View File
@@ -0,0 +1,49 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_package_import(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_USE)) {
parse_trace(
NULL, "expected `use` before package import statement");
return false;
}
struct ivy_ast_unit_import_node *unit_import
= create_node(IVY_AST_UNIT_IMPORT);
struct ivy_token *tok = NULL;
if (!parse_word(parser, &tok)) {
parse_trace(NULL, "expected word after `use` keyword");
return false;
}
fx_queue_push_back(&unit_import->n_ident, &tok->t_entry);
while (1) {
if (parse_linefeed(parser)) {
break;
}
if (!parse_symbol(parser, IVY_SYM_DOT)) {
parse_trace(
NULL,
"expected linefeed or `.` after package "
"identifier word");
destroy_node(unit_import);
return false;
}
if (!parse_word(parser, &tok)) {
parse_trace(NULL, "expected word after `use` keyword");
destroy_node(unit_import);
return false;
}
fx_queue_push_back(&unit_import->n_ident, &tok->t_entry);
}
*out = generic_node(unit_import);
return true;
}
+101
View File
@@ -0,0 +1,101 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
static bool parse_dynamic_package(
struct ivy_parser *parser, struct ivy_ast_node *expr,
struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_FOR)) {
parse_trace(
parser,
"expected `for` after package comprehension "
"expression");
return false;
}
struct ivy_ast_pkg_dynamic_node *pkg = create_node(IVY_AST_PKG_DYNAMIC);
pkg->n_transform = expr;
if (!parse_expr(parser, EXPR_PARSE_STOP_KW_IN, &pkg->n_item)) {
parse_trace(parser, "invalid package comprehension item");
return false;
}
if (!parse_keyword(parser, IVY_KW_IN)) {
parse_trace(
parser,
"expected `in` after package comprehension item");
return false;
}
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &pkg->n_item)) {
parse_trace(parser, "invalid package comprehension container");
return false;
}
if (!parse_symbol(parser, IVY_SYM_RIGHT_BRACE)) {
parse_trace(
parser,
"expected `}` after package comprehension container");
return false;
}
*out = generic_node(pkg);
return true;
}
bool parse_package(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_symbol(parser, IVY_SYM_LEFT_BRACE)) {
parse_trace(parser, "expected `{`");
return false;
}
struct ivy_ast_node *item = NULL;
if (!parse_expr(
parser, EXPR_PARSE_STOP_COMMA | EXPR_PARSE_STOP_KW_CONTROL,
&item)) {
parse_trace(parser, "invalid package item");
return false;
}
if (peek_keyword(parser, IVY_KW_FOR)) {
return parse_dynamic_package(parser, item, out);
}
struct ivy_ast_pkg_static_node *pkg = create_node(IVY_AST_PKG_STATIC);
if (item) {
fx_queue_push_back(&pkg->n_items, &item->n_entry);
}
bool ok = true;
while (ok) {
parse_linefeed(parser);
if (parse_symbol(parser, IVY_SYM_RIGHT_BRACE)) {
break;
}
if (!parse_symbol(parser, IVY_SYM_COMMA)) {
parse_trace(
parser,
"expected `,` or `}` after package item");
ok = false;
break;
}
if (!parse_expr(
parser,
EXPR_PARSE_STOP_COMMA | EXPR_PARSE_STOP_KW_CONTROL,
&item)) {
parse_trace(parser, "invalid package item");
return false;
}
fx_queue_push_back(&pkg->n_items, &item->n_entry);
}
*out = generic_node(pkg);
return true;
}
+36
View File
@@ -0,0 +1,36 @@
#include "ctx.h"
bool parse_string(struct ivy_parser *parser, struct ivy_token **out)
{
struct ivy_token *tok = peek_token(parser);
if (!tok || tok->t_type != IVY_TOK_STRING) {
return false;
}
*out = parse_token(parser);
return true;
}
bool parse_str_start(struct ivy_parser *parser)
{
struct ivy_token *tok = peek_token(parser);
if (tok->t_type != IVY_TOK_STR_START) {
return false;
}
parse_token(parser);
ivy_token_destroy(tok);
return true;
}
bool parse_str_end(struct ivy_parser *parser)
{
struct ivy_token *tok = peek_token(parser);
if (tok->t_type != IVY_TOK_STR_END) {
return false;
}
parse_token(parser);
ivy_token_destroy(tok);
return true;
}
+31
View File
@@ -0,0 +1,31 @@
#include "ctx.h"
bool peek_symbol(struct ivy_parser *parser, enum ivy_symbol sym)
{
struct ivy_token *tok = peek_token(parser);
if (!tok || tok->t_type != IVY_TOK_SYMBOL) {
return false;
}
if (tok->t_symbol != sym) {
return false;
}
return true;
}
bool parse_symbol(struct ivy_parser *parser, enum ivy_symbol sym)
{
struct ivy_token *tok = peek_token(parser);
if (!tok || tok->t_type != IVY_TOK_SYMBOL) {
return false;
}
if (tok->t_symbol != sym) {
return false;
}
parse_token(parser);
ivy_token_destroy(tok);
return true;
}
+49
View File
@@ -0,0 +1,49 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_try(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_TRY)) {
parse_trace(parser, "expected `try` keyword");
return false;
}
struct ivy_ast_try_node *try = create_node(IVY_AST_TRY);
if (!parse_expr(parser, EXPR_PARSE_STOP_KW_CONTROL, &try->n_try)) {
parse_trace(parser, "invalid try block");
return false;
}
bool ok = true;
while (1) {
if (!parse_keyword(parser, IVY_KW_CATCH)) {
break;
}
struct ivy_ast_node *catch_guard = NULL;
struct ivy_ast_node *catch_block = NULL;
if (!parse_expr(parser, EXPR_PARSE_STOP_LEFT_BRACKET, &catch_guard)) {
parse_trace(parser, "invalid catch guard");
ok = false;
break;
}
if (!parse_block(parser, &catch_block)) {
parse_trace(parser, "invalid catch block");
ok = false;
break;
}
struct ivy_ast_try_catch_node *catch
= create_node(IVY_AST_TRY_CATCH);
catch->n_pattern = catch_guard;
catch->n_block = catch_block;
fx_queue_push_back(&try->n_catch, &catch->n_base.n_entry);
}
*out = generic_node(try);
return true;
}
+75
View File
@@ -0,0 +1,75 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool is_tuple_terminator(struct ivy_token *tok)
{
const struct ivy_operator *op = get_operator_from_token(tok);
switch (tok->t_type) {
case IVY_TOK_SYMBOL:
switch (tok->t_symbol) {
case IVY_SYM_DOT:
case IVY_SYM_RIGHT_PAREN:
case IVY_SYM_RIGHT_BRACE:
case IVY_SYM_RIGHT_BRACKET:
return true;
default:
op = get_operator_from_token(tok);
break;
}
break;
default:
break;
}
if (op && op->op_precedence < IVY_PRECEDENCE_KEYWORD_MSG) {
return true;
}
return false;
}
bool parse_tuple(
struct ivy_parser *parser, struct ivy_ast_node *first_item,
struct ivy_ast_node **out)
{
struct ivy_ast_tuple_node *tuple = create_node(IVY_AST_TUPLE);
fx_queue_push_back(&tuple->n_members, &first_item->n_entry);
bool ok = true;
bool done = false;
while (!done) {
struct ivy_token *tok = peek_token(parser);
if (!tok) {
parse_trace(
parser, "unexpected EOF while parsing tuple");
ok = false;
break;
}
if (is_tuple_terminator(tok)) {
break;
}
if (!parse_symbol(parser, IVY_SYM_COMMA)) {
parse_trace(parser, "expected `,` after tuple item");
ok = false;
break;
}
struct ivy_ast_node *value = NULL;
if (!parse_expr(
parser,
EXPR_PARSE_STOP_COMMA | EXPR_PARSE_STOP_TUPLE_TERMINATOR,
&value)) {
parse_trace(parser, "invalid tuple item");
ok = false;
break;
}
fx_queue_push_back(&tuple->n_members, &value->n_entry);
}
*out = generic_node(tuple);
return true;
}
+50
View File
@@ -0,0 +1,50 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_unit(struct ivy_parser *parser, struct ivy_ast_node **out)
{
struct ivy_ast_unit_node *unit
= (struct ivy_ast_unit_node *)ivy_ast_node_create(IVY_AST_UNIT);
if (!unit) {
parser->p_status = IVY_ERR_NO_MEMORY;
return false;
}
bool result = true;
bool prev_was_expr = false;
while (1) {
struct ivy_ast_node *child = NULL;
if (parse_eof(parser)) {
break;
}
if (parse_linefeed(parser)) {
continue;
}
if (peek_keyword(parser, IVY_KW_PACKAGE)) {
result = parse_package_id(parser, &child);
} else if (peek_keyword(parser, IVY_KW_USE)) {
result = parse_package_import(parser, &child);
} else if (peek_keyword(parser, IVY_KW_CLASS)) {
result = parse_class(parser, &child);
} else if (peek_expr_start(parser)) {
result = parse_expr(
parser, EXPR_PARSE_CONSUME_TERMINATOR, &child);
} else {
parse_trace(parser, "unexpected token");
result = false;
}
if (!result || !child) {
break;
}
fx_queue_push_back(&unit->n_children, &child->n_entry);
}
*out = generic_node(unit);
return result;
}
+49
View File
@@ -0,0 +1,49 @@
#include "ctx.h"
#include <ivy/lang/ast.h>
bool parse_while(struct ivy_parser *parser, struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_WHILE)) {
parse_trace(parser, "expected `while` keyword");
return false;
}
struct ivy_ast_while_loop_node *while_node
= create_node(IVY_AST_WHILE_LOOP);
if (!parse_expr(parser, EXPR_PARSE_STOP_LEFT_BRACKET, &while_node->n_cond)) {
parse_trace(parser, "invalid while condition");
return false;
}
if (!parse_block(parser, &while_node->n_body)) {
parse_trace(parser, "invalid while body");
return false;
}
*out = generic_node(while_node);
return true;
}
bool parse_while_inline(
struct ivy_parser *parser, struct ivy_ast_node *body,
struct ivy_ast_node **out)
{
if (!parse_keyword(parser, IVY_KW_WHILE)) {
parse_trace(parser, "expected `while` keyword");
return false;
}
struct ivy_ast_while_loop_node *while_node
= create_node(IVY_AST_WHILE_LOOP);
while_node->n_body = body;
if (!parse_expr(parser, EXPR_PARSE_NORMAL, &while_node->n_cond)) {
parse_trace(parser, "invalid while condition");
return false;
}
*out = generic_node(while_node);
return true;
}
+18
View File
@@ -0,0 +1,18 @@
#include "ctx.h"
bool peek_word(struct ivy_parser *parser)
{
struct ivy_token *tok = peek_token(parser);
return (!tok || tok->t_type == IVY_TOK_IDENT);
}
bool parse_word(struct ivy_parser *parser, struct ivy_token **out)
{
struct ivy_token *tok = peek_token(parser);
if (!tok || tok->t_type != IVY_TOK_IDENT) {
return false;
}
*out = parse_token(parser);
return true;
}