compiler_experiment

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.deepztream.com/compiler_experiment
Log | Files | Refs | Submodules

commit bd4fd95928ab50f66a890b6deeabef70d0a891b7
parent 9c5e2860156e8ccb4d34a710183dd411f1c2d5c1
Author: William Djupström <william@deepztream.com>
Date:   Mon, 21 Sep 2026 18:21:11 +0200

Added support for declaring custom operators

Diffstat:
A.gitignore | 3+++
A.gitmodules | 3+++
MMakefile | 6+++---
Adlib | 1+
Ddlib.h | 417-------------------------------------------------------------------------------
Mlexer.c | 151++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Mlexer.h | 10+++++++++-
Mmain.c | 20+++++++++++++-------
Mparser.c | 663++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mparser.h | 81+++++++++++++++++++++++++++++++++++++++----------------------------------------
Atest_input/01_test.dc | 4++++
Atest_input/05_syscall.dc | 3+++
Mtest_input/binop.dc | 22++++++++++++++++------
13 files changed, 634 insertions(+), 750 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,3 @@ +tags +dc +build/ diff --git a/.gitmodules b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "dlib"] + path = dlib + url = https://git.deepztream.com/dlib diff --git a/Makefile b/Makefile @@ -1,12 +1,12 @@ .PHONY: run clean -CFLAGS = -Wall -Wextra -Wswitch-enum -ggdb +CFLAGS = -Wall -Wextra -Wswitch-enum -ggdb -I./dlib SOURCE_FILES = main.c SOURCE_FILES += lexer.c SOURCE_FILES += parser.c -HEADER_FILES = dlib.h +HEADER_FILES = dlib/dlib.h HEADER_FILES += lexer.h HEADER_FILES += parser.h @@ -14,7 +14,7 @@ dc: $(SOURCE_FILES) $(HEADER_FILES) gcc $(CFLAGS) -o $@ $(SOURCE_FILES) run: dc - ./dc input.dc + ./dc test_input/binop.dc clean: rm *.o dc diff --git a/dlib b/dlib @@ -0,0 +1 @@ +Subproject commit 17ea5a41be7a78531e1919b84399d09fe36bdfcf diff --git a/dlib.h b/dlib.h @@ -1,417 +0,0 @@ -#ifndef DLIB_H -#define DLIB_H 1 - -#include <assert.h> -#include <ctype.h> -#include <fcntl.h> -#include <limits.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdbool.h> -#include <stdint.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> -#include <unistd.h> - -#define unimplemented() \ - do { \ - fprintf(stderr, "%s:%i:1: Error: not implemented %s\n", __FILE__, __LINE__, __func__); \ - abort(); \ - } while (0) - -#define pop_arg(argc, argv) ((argc)--, *(argv)++) - -#define da_append(da, new)\ - do {\ - if ((da)->capacity <= (da)->count) {\ - (da)->capacity = (da)->capacity ? (da)->capacity * 2 : 16;\ - (da)->items = realloc((da)->items, sizeof(*(da)->items) * (da)->capacity);\ - }\ - (da)->items[(da)->count++] = (new);\ - } while(0) - -#define da_prepend(da, new)\ - do {\ - if ((da)->capacity <= (da)->count) {\ - (da)->capacity = (da)->capacity ? (da)->capacity * 2 : 16;\ - (da)->items = realloc((da)->items, sizeof(*(da)->items) * (da)->capacity);\ - }\ - memmove((da)->items + 1, (da)->items, sizeof(*(da)->items) * ((da)->count++));\ - (da)->items[0] = (new);\ - } while(0) - -#define da_reserve(da, len)\ - do {\ - if ((da)->capacity >= (len)) break;\ - (da)->capacity = (len);\ - (da)->items = realloc((da)->items, sizeof(*(da)->items) * (len));\ - } while(0) - -#define da_expand(da, size)\ - do {\ - if ((da)->count >= (size)) break;\ - da_reserve(da, size);\ - (da)->count = (size);\ - } while(0) - -#define da_delete(da, index)\ - do {\ - assert(index >= 0 && index < (da)->count);\ - (da)->items[index] = (da)->items[--(da)->count];\ - } while (0) - -typedef struct { - union { - char *items; - const char *data; - }; - union { - ssize_t count; - ssize_t length; - }; - ssize_t capacity; -} Sb; -#define String_builder Sb - -typedef struct { - union { - const char *items; - const char *data; - }; - union { - ssize_t count; - ssize_t length; - }; -} Sv; -#define String_view Sv - -typedef struct { - void *base; - void *head; - void *end; -} Temp_allocator; - -#define TA_DEFAULT_SIZE (1024*1024) - -void ta_init(Temp_allocator *ta, size_t size); -void *ta_alloc(Temp_allocator *ta, size_t size); - -#define sb_append_char da_append -void sb_printf(Sb *sb, const char *fmt, ...); -void sb_append_cstr(Sb *sb, const char *cstr); -void sb_append_sv(Sb *sb, Sv sv); - -bool sb_read(Sb *sb, int fd); -bool sb_read_file(Sb *sb, const char *file_name); -void sb_write(int fd, Sb sb); - -void sb_release(Sb *sb); - -#define sv_literal(str) ((Sv){.items = (str), .count = (sizeof (str) - 1)}) - -Sv sv_from_sb(Sb sb); -Sv sv_from_cstr(const char *cstr); -ssize_t sv_find_char(Sv sv, char c); - -Sv sv_chop(Sv sv, ssize_t count, Sv *rest); -Sv sv_chop_delim(Sv sv, char c, Sv *rest); - -Sv sv_getline(Sv *sv); - -Sv sv_trim_left(Sv sv); -Sv sv_trim_right(Sv sv); -Sv sv_trim(Sv sv); - -Sv sv_substring(Sv sv, ssize_t start, ssize_t req_count); - -int sv_compare(Sv sv1, Sv sv2); -bool sv_equal(Sv sv1, Sv sv2); -bool sv_starts_with_cstr(Sv sv, const char *cstr); - -void sv_write(int fd, Sv sv); - -#ifdef DLIB_IMPLEMENTATION - -void -ta_init(Temp_allocator *ta, size_t size) -{ - ta->base = malloc(size); - ta->head = ta->base; - ta->end = ta->base + size; -} - -void * -ta_alloc(Temp_allocator *ta, size_t size) -{ - if (!ta->base) { - ta->base = malloc(TA_DEFAULT_SIZE); - ta->head = ta->base; - ta->end = ta->base + TA_DEFAULT_SIZE; - } - void *result = ta->head; - ta->head += (size + 0xF) & ~0xFul; - if (ta->head >= ta->end) { - fprintf(stderr, "Error: Temp allocator out of memory\n"); - exit(1); - } - return result; -} - -void -sb_printf(Sb *sb, const char *fmt, ...) { - va_list args, args_copy; - va_start(args, fmt); - va_copy(args_copy, args); - int len = vsnprintf(NULL, 0, fmt, args_copy) + 1; - ssize_t new_size = sb->count + len; - da_reserve(sb, new_size); - va_end(args_copy); - vsnprintf(sb->items + sb->count, len, fmt, args); - va_end(args); - sb->count = new_size - 1; -} - -void -sb_append_cstr(Sb *sb, const char *cstr) -{ - ssize_t len = strlen(cstr); - da_reserve(sb, sb->count + len); - strcpy(sb->items + sb->count, cstr); - sb->count += len; -} - -void -sb_append_sv(Sb *sb, Sv sv) -{ - da_reserve(sb, sb->count + sv.count); - memcpy(sb->items + sb->count, sv.items, sv.count); - sb->count += sv.count; -} - -bool -sb_read(Sb *sb, int fd) -{ - ssize_t read_count; - ssize_t buf_size = 4096; - char *buf = malloc(buf_size); - do { - read_count = read(fd, buf, buf_size); - if (read_count < 0) { - if (errno == EINTR) { - continue; - } - perror("sb_read()"); - return false; - } - if (read_count == 0) { - break; - } - sb_append_sv(sb, ((Sv) {.items = buf, .length = read_count})); - if (read_count < buf_size) { - break; - } - } while (1); - free(buf); - return true; -} - -bool -sb_read_file(Sb *sb, const char *file_name) -{ - int fd = open(file_name, O_RDONLY); - if (fd < 0) { - perror("sb_read()"); - return false; - } - ssize_t read_count; - ssize_t buf_size = 4096; - char *buf = malloc(buf_size); - do { - read_count = read(fd, buf, buf_size); - if (read_count < 0) { - if (errno == EINTR) { - continue; - } - perror("sb_read_file()"); - return false; - } - if (read_count == 0) { - break; - } - sb_append_sv(sb, ((Sv) {.items = buf, .length = read_count})); - } while (1); - free(buf); - return true; -} - -void -sb_write(int fd, Sb sb) -{ - write(fd, sb.items, sb.length); -} - -void -sb_release(Sb *sb) -{ - if (sb->capacity) { - free(sb->items); - sb->items = NULL; - sb->count = 0; - sb->capacity = 0; - } -} - -/* String view functions */ - -void -sv_write(int fd, Sv sv) -{ - write(fd, sv.items, sv.count); -} - -Sv -sv_from_sb(Sb sb) -{ - return (Sv) {.items = sb.items, .count = sb.count}; -} - -Sv -sv_from_cstr(const char *cstr) -{ - return (Sv) {.items = cstr, .count = strlen(cstr)}; -} - -ssize_t -sv_find_char(Sv sv, char c) -{ - ssize_t pos = 0; - while (sv.count > pos && sv.items[pos] != c) - pos++; - for (pos = 0; pos < sv.count && sv.items[pos] != c; pos++); - return pos < sv.count ? pos : -1; -} - -Sv -sv_chop_delim(Sv sv, char c, Sv *rest) -{ - ssize_t pos = sv_find_char(sv, c); - Sv res = sv; - if (pos > 0) { - res.count = pos; - - rest->count -= pos + 1; - rest->items += pos + 1; - } - return res; -} - -Sv -sv_chop(Sv sv, ssize_t count, Sv *rest) -{ - Sv left = {0}; - ssize_t lcount, rcount; - if (count > sv.count) { - lcount = sv.count; - rcount = 0; - } else if (-count > sv.count) { - lcount = 0; - rcount = sv.count; - } else if (count < 0) { - lcount = sv.count + count; - rcount = -count; - } else { - lcount = count; - rcount = sv.count - count; - } - left.items = sv.items; - left.count = lcount; - if (rest) { - rest->items = sv.items + lcount; - rest->count = rcount; - } - return left; -} - -Sv -sv_trim_left(Sv sv) -{ - while (sv.count && isspace(sv.items[0])) { - sv.count--; - sv.items++; - } - return sv; -} - -Sv -sv_trim_right(Sv sv) -{ - while (sv.count && isspace(sv.items[sv.count - 1])) { - sv.count--; - } - return sv; -} - -Sv -sv_trim(Sv sv) -{ - return sv_trim_right(sv_trim_left(sv)); -} - -Sv -sv_substring(Sv sv, ssize_t start, ssize_t req_count) -{ - Sv ss = {0}; - if (start >= sv.count) return ss; - ss.count = (req_count < sv.count - start) ? req_count : sv.count - start; - ss.items = sv.items + start; - return ss; -} - -int -sv_compare(Sv sv1, Sv sv2) -{ - if (sv1.items == sv2.items && sv1.count == sv2.count) return 0; - signed char diff; - ssize_t pos; - for (pos = 0; pos < sv1.count; pos++) { - if (pos == sv2.count) return 1; - if ((diff = sv1.items[pos] - sv2.items[pos])) return diff; - } - return pos < sv2.count ? -1 : 0; -} - -bool -sv_equal(Sv sv1, Sv sv2) -{ - if (sv1.count != sv2.count) return false; - if (sv1.items == sv2.items) return true; - ssize_t pos; - for (pos = 0; pos < sv1.count && sv1.items[pos] == sv2.items[pos]; pos++); - return pos == sv1.count; -} - -bool -sv_starts_with_cstr(Sv sv, const char *cstr) -{ - size_t cstr_len = strlen(cstr); - assert(cstr_len <= SSIZE_MAX); - if (sv.count < (ssize_t) cstr_len) return false; - ssize_t pos; - for (pos = 0; pos < (ssize_t) cstr_len && sv.items[pos] == cstr[pos]; pos++); - return pos == (ssize_t) cstr_len; -} - -Sv -sv_getline(Sv *sv) -{ - Sv line = sv_chop_delim(*sv, '\n', sv); - if (line.count && line.items[line.count - 1] == '\r') { - line.count--; - } - return line; -} - -#endif // DLIB_IMPLEMENTATION - -#endif // DLIB_H diff --git a/lexer.c b/lexer.c @@ -91,11 +91,43 @@ debug_print_token(Token t) fprintf(stderr, "'%c'", t.kind); } else if (t.kind == T_EOF) { fprintf(stderr, "EOF"); - } else if (t.kind == T_WHITESPACE) { - fprintf(stderr, "WHITESPACE(%.*s)", (int) t.sv.count, t.sv.items); + } else if (t.kind == T_WHITESPACE || t.kind == T_WHITESPACE_NL) { + if (t.kind == T_WHITESPACE) { + fprintf(stderr, "WHITESPACE("); + } else { + fprintf(stderr, "WHITESPACE_NL("); + } + char last_char = '?'; + int last_char_count = 0; + for (int i = 0; i < t.sv.count; i++) { + char c = '?'; + switch (t.sv.items[i]) { + case ' ' : c = 's'; break; + case '\t': c = 't'; break; + case '\n': c = 'n'; break; + case '\r': c = 'r'; break; + case '\v': c = 'v'; break; + case '\f': c = 'f'; break; + } + if (last_char != c) { + if (last_char_count > 1) { + fprintf(stderr, "(%i)", last_char_count); + } + last_char_count = 0; + fputc(c, stderr); + } else if (!last_char_count) { + fputc(c, stderr); + } + last_char = c; + last_char_count++; + } + if (last_char_count > 1) { + fprintf(stderr, "(%i)", last_char_count); + } + fputc(')', stderr); } else if (t.kind == T_IDENT) { fprintf(stderr, "IDENT(%.*s)", (int) t.sv.count, t.sv.items); - } else if (t.kind == T_EOF) { + } else if (t.kind == T_NUMBER) { fprintf(stderr, "NUMBER(%.*s)", (int) t.sv.count, t.sv.items); } else if (t.kind == T_SQ_STRING || t.kind == T_DQ_STRING) { fprintf(stderr, "STRING(%.*s)", (int) t.sv.count, t.sv.items); @@ -157,6 +189,13 @@ next_char(Lexer *lexer) } int +peek_char(Lexer *lexer) +{ + if (!lexer->source.count || lexer->source.count == 1) return EOF; + return *lexer->source.items; +} + +int next_char_if(Lexer *lexer, int (*pred)(int)) { if (!lexer->source.count) return EOF; @@ -209,6 +248,34 @@ is_symbol_char(int c) 0; } +bool +is_operator_token(Token_kind kind) { + return kind == '+' || + kind == '-' || + kind == '*' || + kind == '/' || + kind == '%' || + /* kind == '(' || */ + /* kind == ')' || */ + /* kind == '[' || */ + /* kind == ']' || */ + kind == '<' || + kind == '>' || + /* kind == '{' || */ + /* kind == '}' || */ + kind == '=' || + kind == '&' || + kind == '|' || + kind == '^' || + kind == '~' || + kind == '?' || + kind == '!' || + /* kind == '.' || */ + /* kind == ',' || */ + kind == T_IDENT || + 0; +} + int skip_comment(Lexer *lexer) { @@ -247,8 +314,13 @@ next_token(Lexer *lexer) Loc start_loc = lexer->current_loc; Token result = { .loc = start_loc, .sv = { .items = lexer->source.items, .count = 0 } }; + if (lexer->last_tok.kind != T_WHITESPACE) { + lexer->last_valid_loc = start_loc; + } + if (!lexer->source.count) { result.kind = T_EOF; + lexer->last_tok = result; return result; } @@ -270,11 +342,18 @@ next_token(Lexer *lexer) len++; } whitespace = true; - while ((c = next_char_if(lexer, isspace)) >= 0) len++; + while ((c = next_char_if(lexer, isspace)) >= 0) { + len++; + } } if (whitespace) { - result.kind = T_WHITESPACE; + if (start_loc.line < lexer->current_loc.line) { + result.kind = T_WHITESPACE_NL; + } else { + result.kind = T_WHITESPACE; + } result.sv.count = len; + lexer->last_tok = result; return result; } } @@ -283,10 +362,15 @@ next_token(Lexer *lexer) c = next_char(lexer); + if (c == '-' && isdigit(peek_char(lexer))) { + c = next_char(lexer); + len++; + } if (isdigit(c)) { while (next_char_if(lexer, isdigit) >= 0) len++; result.kind = T_NUMBER; result.sv.count = len; + lexer->last_tok = result; return result; } @@ -294,12 +378,14 @@ next_token(Lexer *lexer) while (next_char_if(lexer, is_ident_char) >= 0) len++; result.sv.count = len; result.kind = get_keyword_kind(result.sv); + lexer->last_tok = result; return result; } if (is_symbol_char(c)) { result.kind = c; result.sv.count = len; + lexer->last_tok = result; return result; } @@ -323,6 +409,7 @@ next_token(Lexer *lexer) } result.kind = c == '"' ? T_DQ_STRING : T_SQ_STRING; result.sv.count = ++len; + lexer->last_tok = result; return result; } @@ -374,19 +461,20 @@ skip_whitespace(Lexer *lexer) { Lexer peek = *lexer; Token t = next_token(&peek); - if (t.kind == T_WHITESPACE) { + if (t.kind == T_WHITESPACE || t.kind == T_WHITESPACE_NL) { *lexer = peek; } } -Token +bool expect_token_kind(Lexer *lexer, Token_kind kind) { Token t = next_token(lexer); if (t.kind != kind) { Sb error = {0}; - sb_append_loc(&error, t.loc); + /* sb_append_loc(&error, t.loc); */ + sb_append_loc(&error, lexer->last_valid_loc); sb_append_cstr(&error, "Error: expected token "); sb_append_token_kind(&error, kind); sb_append_cstr(&error, ", but got "); @@ -395,10 +483,47 @@ expect_token_kind(Lexer *lexer, Token_kind kind) fprintf(stderr, "%.*s\n", (int) error.count, error.items); sb_release(&error); - return (Token) { - .kind = T_ERROR_, - .loc = t.loc, - }; + return false; + /* return (Token) { */ + /* .kind = T_ERROR_, */ + /* .loc = t.loc, */ + /* }; */ + } + return true; +} + +bool +expect_end_statement_impl(Lexer *lexer, const char *file, int line) +{ + if (lexer->last_tok.kind == T_WHITESPACE_NL) { + fprintf(stderr, "%s:%i: expect_end_statement hack\n", file, line); + return true; + /* return lexer->last_tok; */ } - return t; + Token t = next_token(lexer); + if (t.kind == ';' || t.kind == T_WHITESPACE_NL) { + /* return t; */ + return true; + } + if (t.kind == T_WHITESPACE) { + if (peek_token(lexer).kind == ';') { + next_token(lexer); + return true; + } + } + Sb error = {0}; + + /* sb_append_loc(&error, t.loc); */ + sb_append_loc(&error, lexer->last_valid_loc); + sb_append_cstr(&error, "Error: expected ; or newline, but got "); + sb_append_token_kind(&error, t.kind); + + fprintf(stderr, "%.*s\n", (int) error.count, error.items); + + sb_release(&error); + return false; + /* return (Token) { */ + /* .kind = T_ERROR_, */ + /* .loc = t.loc, */ + /* }; */ } diff --git a/lexer.h b/lexer.h @@ -13,6 +13,7 @@ typedef enum { T_EOF = 256, T_INVALID, T_WHITESPACE, + T_WHITESPACE_NL, T_IDENT, T_NUMBER, T_SQ_STRING, @@ -51,6 +52,8 @@ typedef struct { typedef struct { Sv source; Loc current_loc; + Token last_tok; + Loc last_valid_loc; } Lexer; #define CHAR_INVALID (-2) @@ -59,6 +62,7 @@ int next_char_if(Lexer *lexer, int (*pred)(int)); int is_ident_char(int c); int is_symbol_char(int c); +bool is_operator_token(Token_kind kind); int skip_comment(Lexer *lexer); Token next_token(Lexer *lexer); @@ -68,7 +72,11 @@ Token_kind get_keyword_kind(Sv sv); Token next_token_if(Lexer *lexer, int (*pred)(Token)); Token next_token_if_kind(Lexer *lexer, Token_kind kind); -Token expect_token_kind(Lexer *lexer, Token_kind kind); +bool expect_token_kind(Lexer *lexer, Token_kind kind); + +bool expect_end_statement_impl(Lexer *lexer, const char *file, int line); +#define expect_end_statement(l) expect_end_statement_impl(l, __FILE__, __LINE__) + void skip_whitespace(Lexer *lexer); void sb_append_token_kind(Sb *sb, Token_kind kind); diff --git a/main.c b/main.c @@ -55,16 +55,22 @@ main(int argc, char **argv) .ast_alloc = malloc_ast, .ast_free = free_ast, }; + (void) parser; + Ast_node_list ast = parse(&parser); + if (parser.error) { + return 1; + } for (int i = 0; i < ast.count; ++i) { debug_print_ast_node(ast.items[i], 0); } - /* - Token t; - while ((t = next_token(&lexer)).kind != T_EOF && t.kind != T_ERROR) { - if (t.kind != T_WHITESPACE) debug_print_token(t); - } - debug_print_token(t); - */ + + /* Token t; */ + /* while ((t = next_token(&lexer)).kind != T_EOF && t.kind != T_ERROR_) { */ + /* if (t.kind != T_WHITESPACE) debug_print_token(t); */ + /* #<{(| debug_print_token(t); |)}># */ + /* } */ + /* debug_print_token(t); */ + return 0; } diff --git a/parser.c b/parser.c @@ -1,41 +1,9 @@ #include "lexer.h" #include "parser.h" -int binop_precedence[] = { - [BINOP_INVALID] = 250, - [BINOP_ADD] = 20, - [BINOP_SUB] = 20, - [BINOP_MUL] = 10, - [BINOP_DIV] = 10, - [BINOP_MOD] = 10, - [BINOP_BAND] = 60, - [BINOP_BOR] = 80, - [BINOP_BXOR] = 70, - [BINOP_LSHIFT] = 30, - [BINOP_RSHIFT] = 30, - [BINOP_ASSIGN] = 121, - [BINOP_ADD_ASSIGN] = 121, - [BINOP_SUB_ASSIGN] = 121, - [BINOP_MUL_ASSIGN] = 121, - [BINOP_DIV_ASSIGN] = 121, - [BINOP_MOD_ASSIGN] = 121, - [BINOP_BAND_ASSIGN] = 121, - [BINOP_BOR_ASSIGN] = 121, - [BINOP_BXOR_ASSIGN] = 121, - [BINOP_LSHIFT_ASSIGN] = 121, - [BINOP_RSHIFT_ASSIGN] = 121, - [BINOP_LAND] = 90, - [BINOP_LOR] = 110, - [BINOP_LXOR] = 100, - [BINOP_LT] = 40, - [BINOP_LE] = 40, - [BINOP_EQ] = 50, - [BINOP_NEQ] = 50, - [BINOP_GE] = 40, - [BINOP_GT] = 40, - [BINOP_COMMA] = 130, - [BINOP_ALL_] = 140, -}; +Operator_entry_list preops = {0}; +Operator_entry_list inops = {0}; +Operator_entry_list postops = {0}; Ast_node * new_ast_node(Parser *p, Ast_kind kind, Token loc) @@ -46,6 +14,44 @@ new_ast_node(Parser *p, Ast_kind kind, Token loc) return result; } +Operator +collect_operator(Parser *p, Operator_entry_list *ops) +{ + int i; + int latest_match = -1; + int exact; + Lexer saved_lexer = p->lexer; + skip_whitespace(&p->lexer); + Token tok; + Token start_tok = peek_token(&p->lexer); + Sb sb = {0}; + ssize_t old_len = 0; + bool match; + do { + exact = -1; + match = false; + tok = next_token(&p->lexer); + old_len = sb.count; + sb_append_sv(&sb, tok.sv); + for (i = 0; i < ops->count; i++) { + if (sv_starts_with(ops->items[i].sv, sv_from_sb(sb))) { + if (exact < 0 && sv_equal(ops->items[i].sv, sv_from_sb(sb))) exact = i; + match = true; + } + } + if (exact >= 0) { + latest_match = exact; + saved_lexer = p->lexer; + } + } while (match); + p->lexer = saved_lexer; + if (latest_match < 0) { + return (Operator) { .entry = -1 }; + } + start_tok.sv.count = old_len; + return (Operator) { .entry = latest_match, .tok = start_tok}; +} + Ast_node * parse_postfix(Parser *p, Ast_node *child) { @@ -64,7 +70,9 @@ parse_primitive(Parser *p) result = new_ast_node(p, AST_IDENT, t); } else if (t.kind == '(') { result = parse_expression(p); - expect_token_kind(&p->lexer, ')'); + if (result->kind == AST_ERROR || !expect_token_kind(&p->lexer, ')')) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } } else { Sb sb = {0}; sb_append_loc(&sb, t.loc); @@ -73,7 +81,11 @@ parse_primitive(Parser *p) sb_append_char(&sb, '\n'); sb_write(2, sb); sb_release(&sb); - abort(); + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + if (result->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); } return parse_postfix(p, result); } @@ -81,193 +93,15 @@ parse_primitive(Parser *p) Ast_node * parse_prefix(Parser *p) { - Ast_node *result = parse_primitive(p); - return result; -} - -Binop -collect_binop(Parser *p) -{ - Lexer saved_lexer; - Token peek = peek_token(&p->lexer); - Binop result = { .tok = peek }; - switch((int) peek.kind) { - case '+': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_ADD_ASSIGN; - result.tok.sv.count++; - } else { - result.kind = BINOP_ADD; - } - break; - case '-': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_SUB_ASSIGN; - result.tok.sv.count++; - } else { - result.kind = BINOP_SUB; - } - break; - case '*': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_MUL_ASSIGN; - result.tok.sv.count++; - } else { - result.kind = BINOP_MUL; - } - break; - case '/': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_DIV_ASSIGN; - result.tok.sv.count++; - } else { - result.kind = BINOP_DIV; - } - break; - case '%': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_MOD_ASSIGN; - result.tok.sv.count++; - } else { - result.kind = BINOP_MOD; - } - break; - case '&': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_BAND_ASSIGN; - result.tok.sv.count++; - } else if (peek.kind == '&') { - next_token(&p->lexer); - result.kind = BINOP_LAND; - result.tok.sv.count++; - } else { - result.kind = BINOP_BAND; - } - break; - case '|': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_BOR_ASSIGN; - result.tok.sv.count++; - } else if (peek.kind == '|') { - next_token(&p->lexer); - result.kind = BINOP_LOR; - result.tok.sv.count++; - } else { - result.kind = BINOP_BOR; - } - break; - case '^': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_BXOR_ASSIGN; - result.tok.sv.count++; - } else if (peek.kind == '^') { - next_token(&p->lexer); - result.kind = BINOP_LXOR; - result.tok.sv.count++; - } else { - result.kind = BINOP_BXOR; - } - break; - case '<': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_LE; - result.tok.sv.count++; - } else if (peek.kind == '<') { - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_LSHIFT_ASSIGN; - result.tok.sv.count++; - } else { - result.kind = BINOP_LSHIFT; - result.tok.sv.count++; - } - } else { - result.kind = BINOP_LT; - } - break; - case '>': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_GE; - result.tok.sv.count++; - } else if (peek.kind == '>') { - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_RSHIFT_ASSIGN; - result.tok.sv.count++; - } else { - result.kind = BINOP_RSHIFT; - result.tok.sv.count++; - } - } else { - result.kind = BINOP_GT; - } - break; - case '=': - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_EQ; - result.tok.sv.count++; - } else { - result.kind = BINOP_ASSIGN; - } - break; - case '!': - saved_lexer = p->lexer; - next_token(&p->lexer); - peek = peek_token(&p->lexer); - if (peek.kind == '=') { - next_token(&p->lexer); - result.kind = BINOP_NEQ; - result.tok.sv.count++; - } else { - p->lexer = saved_lexer; - result.kind = BINOP_INVALID; - } - break; - case ',': - next_token(&p->lexer); - result.kind = BINOP_COMMA; - break; - default: - result.kind = BINOP_INVALID; - break; + Ast_node *result; + Operator prefix = collect_operator(p, &preops); + if (prefix.entry == -1) { + return parse_primitive(p); } + result = new_ast_node(p, AST_PREFIX, prefix.tok); + result->preop.op = prefix; + // TODO: Maybe we should return null if this call fails? + result->preop.right = parse_prefix(p); return result; } @@ -278,55 +112,70 @@ parse_binop(Parser *p, int precedence, Ast_node *left_child) Ast_node *left; Ast_node *right = NULL; Ast_node *result; - Binop binop; + Operator binop; int next_prec; if (!left_child) { left = parse_prefix(p); + if (left->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } } else { left = left_child; } do { saved_lexer = p->lexer; - skip_whitespace(&p->lexer); - Binop binop_peek = collect_binop(p); - next_prec = binop_precedence[binop_peek.kind]; - if (binop_peek.kind == BINOP_INVALID) { + Operator binop_peek = collect_operator(p, &inops); + if (binop_peek.entry == -1) { break; } + + next_prec = inops.items[binop_peek.entry].prec; if (next_prec > precedence) { p->lexer = saved_lexer; break; } if (next_prec < precedence) { p->lexer = saved_lexer; - skip_whitespace(&p->lexer); + /* skip_whitespace(&p->lexer); */ if (right) { right = parse_binop(p, next_prec, right); + if (right->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } } else { left = parse_binop(p, next_prec, left); + if (left->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } } continue; } skip_whitespace(&p->lexer); if (right) { - result = new_ast_node(p, AST_BINOP, binop.tok); - result->binop.op = binop; - result->binop.left = left; - result->binop.right = right; + result = new_ast_node(p, AST_INFIX, binop.tok); + result->inop.op = binop; + result->inop.left = left; + result->inop.right = right; left = result; } binop = binop_peek; - if (next_prec & 1) { + if (inops.items[binop.entry].right_assoc) { right = parse_binop(p, next_prec, NULL); + if (right->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } } else { right = parse_prefix(p); + if (right->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } } } while (1); if (right) { - result = new_ast_node(p, AST_BINOP, binop.tok); - result->binop.op = binop; - result->binop.left = left; - result->binop.right = right; + result = new_ast_node(p, AST_INFIX, binop.tok); + result->inop.op = binop; + result->inop.left = left; + result->inop.right = right; left = result; } return left; @@ -335,14 +184,18 @@ parse_binop(Parser *p, int precedence, Ast_node *left_child) Ast_node * parse_expression(Parser *p) { - Ast_node *result = parse_binop(p, binop_precedence[BINOP_ALL_], NULL); + Ast_node *result = parse_binop(p, INT_MAX, NULL); return result; } Ast_node * parse_type(Parser *p) { - Token base = expect_token_kind(&p->lexer, T_IDENT); + if (!expect_token_kind(&p->lexer, T_IDENT)) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + }; + Token base = p->lexer.last_tok; return new_ast_node(p, AST_TYPE, base); } @@ -354,18 +207,28 @@ parse_var_declaration(Parser *p) Ast_node *var_value = NULL; Ast_node *result; - var_name = expect_token_kind(&p->lexer, T_IDENT); + if (!expect_token_kind(&p->lexer, T_IDENT)) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + var_name = p->lexer.last_tok; skip_whitespace(&p->lexer); expect_token_kind(&p->lexer, ':'); skip_whitespace(&p->lexer); if (peek_token(&p->lexer).kind != '=') { var_type = parse_type(p); + if (var_type->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } skip_whitespace(&p->lexer); } if (peek_token(&p->lexer).kind == '=') { next_token(&p->lexer); skip_whitespace(&p->lexer); var_value = parse_expression(p); + if (var_value->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } } result = new_ast_node(p, AST_VAR_DECL, var_name); result->var.name = var_name; @@ -378,13 +241,23 @@ parse_var_declaration(Parser *p) Ast_node * parse_var_declaration_statement(Parser *p) { Token start_tok; - start_tok = expect_token_kind(&p->lexer, T_VAR); + if (!expect_token_kind(&p->lexer, T_VAR)) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + + start_tok = p->lexer.last_tok; skip_whitespace(&p->lexer); Ast_node *result = parse_var_declaration(p); + if (result->kind == AST_ERROR) { + return NULL; + } - skip_whitespace(&p->lexer); - expect_token_kind(&p->lexer, ';'); + if (!expect_end_statement(&p->lexer)) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } result->start_tok = start_tok; return result; @@ -394,7 +267,12 @@ Ast_node * parse_block(Parser *p) { Token end_tok; - Token start_tok = expect_token_kind(&p->lexer, '{'); + if (!expect_token_kind(&p->lexer, '{')) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + Token start_tok = p->lexer.last_tok; + skip_whitespace(&p->lexer); Ast_node *node, *result; Ast_node_list list = {0}; @@ -410,6 +288,8 @@ parse_block(Parser *p) } else if (peek.kind == '{') { node = parse_block(p); } else if (peek.kind == T_EOF) { + p->error = true; + // Leaking nodes free(list.items); @@ -422,12 +302,22 @@ parse_block(Parser *p) return new_ast_node(p, AST_ERROR, peek); } else { node = parse_expression(p); - expect_token_kind(&p->lexer, ';'); + /* expect_token_kind(&p->lexer, ';'); */ + if (expect_end_statement(&p->lexer)) { + p->error = true; + return new_ast_node(p, AST_ERROR, peek); + } } da_append(&list, node); skip_whitespace(&p->lexer); } - end_tok = expect_token_kind(&p->lexer, '}'); + + if (!expect_token_kind(&p->lexer, '}')) { + p->error = true; + return new_ast_node(p, AST_ERROR, peek); + } + end_tok = p->lexer.last_tok; + result = new_ast_node(p, AST_BLOCK, start_tok); result->block.list = list; result->block.end_tok = end_tok; @@ -441,7 +331,14 @@ parse_func_arg_declaraction_list(Parser *p) Ast_node *node; Ast_node_list list = {0}; while ((peek = peek_token(&p->lexer)).kind != ')') { + if (list.count && !expect_token_kind(&p->lexer, ',')) { + p->error = true; + break; + } node = parse_var_declaration(p); + if (node->kind == AST_ERROR) { + break; + } da_append(&list, node); skip_whitespace(&p->lexer); } @@ -453,26 +350,52 @@ parse_func_declaration_statement(Parser *p) { Ast_node *ret_type = NULL; Ast_node *result; - Token start_tok = expect_token_kind(&p->lexer, T_FUNC); + if (!expect_token_kind(&p->lexer, T_FUNC)) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + Token start_tok = p->lexer.last_tok; skip_whitespace(&p->lexer); - Token name = expect_token_kind(&p->lexer, T_IDENT); + if (!expect_token_kind(&p->lexer, T_IDENT)) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + Token name = p->lexer.last_tok; skip_whitespace(&p->lexer); - expect_token_kind(&p->lexer, '('); + if (!expect_token_kind(&p->lexer, '(')) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } skip_whitespace(&p->lexer); Ast_node_list arg_list = parse_func_arg_declaraction_list(p); - expect_token_kind(&p->lexer, ')'); + if (p->error || !expect_token_kind(&p->lexer, ')')) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } skip_whitespace(&p->lexer); if (peek_token(&p->lexer).kind == '-') { - expect_token_kind(&p->lexer, '-'); - expect_token_kind(&p->lexer, '>'); + if (!expect_token_kind(&p->lexer, '-')) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + if (!expect_token_kind(&p->lexer, '>')) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } skip_whitespace(&p->lexer); ret_type = parse_type(p); + if (ret_type->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } skip_whitespace(&p->lexer); } Ast_node *body = parse_block(p); + if (body->kind == AST_ERROR) { + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } result = new_ast_node(p, AST_FUNC_DECL, start_tok); result->func.body = body; @@ -482,6 +405,151 @@ parse_func_declaration_statement(Parser *p) return result; } +Ast_node * +parse_operator_declaration(Parser *p) +{ + int precedence = -1; + int op_kind = -1; + int associativity = -1; + Token tok; + Token error_tok; + if (!expect_token_kind(&p->lexer, T_OPERATOR)) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + Token start_tok = p->lexer.last_tok; + skip_whitespace(&p->lexer); + if (!expect_token_kind(&p->lexer, '<')) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + error_tok = peek_token(&p->lexer); + skip_whitespace(&p->lexer); + while ((tok = next_token(&p->lexer)).kind != '>') { + if (tok.kind == T_IDENT) { + if (sv_equal(tok.sv, sv_literal("precedence"))) { + skip_whitespace(&p->lexer); + if (!expect_token_kind(&p->lexer, '=')) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + skip_whitespace(&p->lexer); + if (!expect_token_kind(&p->lexer, T_NUMBER)) { + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + tok = p->lexer.last_tok; + precedence = strtol(tok.sv.items, NULL, 10); + } else if (sv_equal(tok.sv, sv_literal("prefix"))) { + if (op_kind >= 0) { + todo("error handling"); + } + op_kind = 0; + } else if (sv_equal(tok.sv, sv_literal("infix"))) { + if (op_kind >= 0) { + todo("error handling"); + } + op_kind = 1; + } else if (sv_equal(tok.sv, sv_literal("postfix"))) { + if (op_kind >= 0) { + todo("error handling"); + } + op_kind = 2; + } else if (sv_equal(tok.sv, sv_literal("left"))) { + associativity = 0; + } else if (sv_equal(tok.sv, sv_literal("right"))) { + associativity = 1; + } + error_tok = peek_token(&p->lexer); + skip_whitespace(&p->lexer); + if (peek_token(&p->lexer).kind == ',') { + next_token(&p->lexer); + error_tok = peek_token(&p->lexer); + skip_whitespace(&p->lexer); + } + continue; + } + print_loc(error_tok.loc); + fprintf(stderr, "expected valid operator modifier, or '>', but got "); + debug_print_token(tok); + fprintf(stderr, "\n"); + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + if (precedence < 0 && op_kind == 1) { + print_loc(start_tok.loc); + fprintf(stderr, "infix operator must have a precedence\n"); + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + if (precedence >= 0 && op_kind != 1) { + print_loc(start_tok.loc); + fprintf(stderr, "non-infix operator must not have a precedence\n"); + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + if (op_kind < 0) { + print_loc(start_tok.loc); + fprintf(stderr, "operator must have a op_kind (prefix, infix, or postfix)\n"); + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + if (associativity < 0 && op_kind == 1) { + print_loc(start_tok.loc); + fprintf(stderr, "infix operator must have an associativity (left or right)\n"); + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + if (associativity >= 0 && op_kind != 1) { + print_loc(start_tok.loc); + fprintf(stderr, "non-infix operator must not have a designated associativity\n"); + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + skip_whitespace(&p->lexer); + Token op_start = peek_token(&p->lexer); + if (!is_operator_token(op_start.kind)) { + print_loc(op_start.loc); + fprintf(stderr, "unexpected token in operator name "); + debug_print_token(op_start); + fprintf(stderr, "\n"); + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + Token op_tmp = {0}; + do { + next_token(&p->lexer); + op_start.sv.count += op_tmp.sv.count; + op_tmp = peek_token(&p->lexer); + } while (is_operator_token(op_tmp.kind)); + if (op_tmp.kind != T_WHITESPACE && op_tmp.kind != '(') { + print_loc(op_tmp.loc); + fprintf(stderr, "unexpected token in operator name "); + debug_print_token(op_tmp); + fprintf(stderr, "\n"); + p->error = true; + return new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } + skip_whitespace(&p->lexer); + Ast_node *body = parse_block(p); + Ast_node *result = new_ast_node(p, AST_OPERATOR_DECL, start_tok); + if (op_kind == OP_PREFIX) { + result->operator.entry = preops.count; + da_append(&preops, ((Operator_entry) { .sv = op_start.sv })); + } else if (op_kind == OP_INFIX) { + result->operator.entry = inops.count; + da_append(&inops, ((Operator_entry) { .sv = op_start.sv, .prec = precedence, .right_assoc = associativity })); + } else if (op_kind == OP_POSTFIX) { + result->operator.entry = postops.count; + da_append(&postops, ((Operator_entry) { .sv = op_start.sv })); + } else { + unreachable("invalid operator op_kind"); + } + result->operator.kind = op_kind; + result->operator.body = body; + return result; +} + Ast_node_list parse(Parser *p) { @@ -492,10 +560,15 @@ parse(Parser *p) while ((peek = peek_token(&p->lexer)).kind != T_EOF) { if (peek.kind == T_VAR) { node = parse_var_declaration_statement(p); + } else if (peek.kind == T_OPERATOR) { + node = parse_operator_declaration(p); } else if (peek.kind == T_FUNC) { node = parse_func_declaration_statement(p); } else if (peek.kind == T_ERROR_) { - abort(); + p->error = true; + node = new_ast_node(p, AST_ERROR, p->lexer.last_tok); + } else if (peek.kind == '{') { + node = parse_block(p); } else { Sb sb = {0}; sb_append_loc(&sb, peek.loc); @@ -504,10 +577,14 @@ parse(Parser *p) sb_append_char(&sb, '\n'); sb_write(2, sb); sb_release(&sb); - abort(); + p->error = true; + node = new_ast_node(p, AST_ERROR, p->lexer.last_tok); } da_append(&list, node); skip_whitespace(&p->lexer); + if (p->error) { + break; + } } return list; } @@ -517,7 +594,7 @@ debug_print_ast_node(Ast_node *node, int indent) { if (!node) { fprintf(stderr, "%*s", indent * 2, ""); - fprintf(stderr, "(null)"); + fprintf(stderr, "(null)\n"); return; } int min_width = node->start_tok.loc.filename.count + 11; @@ -558,10 +635,18 @@ debug_print_ast_node(Ast_node *node, int indent) print_loc_pad(node->block.end_tok.loc, min_width); fprintf(stderr, "%*s\n", indent * 2, "}"); break; - case AST_BINOP: - fprintf(stderr, "Binop(%.*s)\n", (int) node->binop.op.tok.sv.count, node->binop.op.tok.sv.items); - debug_print_ast_node(node->binop.left, indent + 1); - debug_print_ast_node(node->binop.right, indent + 1); + case AST_PREFIX: + fprintf(stderr, "Prefix(%.*s)\n", (int) node->preop.op.tok.sv.count, node->preop.op.tok.sv.items); + debug_print_ast_node(node->preop.right, indent + 1); + break; + case AST_INFIX: + fprintf(stderr, "Infix(%.*s)\n", (int) node->inop.op.tok.sv.count, node->inop.op.tok.sv.items); + debug_print_ast_node(node->inop.left, indent + 1); + debug_print_ast_node(node->inop.right, indent + 1); + break; + case AST_POSTFIX: + fprintf(stderr, "Postfix(%.*s)\n", (int) node->postop.op.tok.sv.count, node->postop.op.tok.sv.items); + debug_print_ast_node(node->postop.left, indent + 1); break; case AST_NUMBER: fprintf(stderr, "Number(%.*s)\n", (int) node->start_tok.sv.count, node->start_tok.sv.items); @@ -569,11 +654,65 @@ debug_print_ast_node(Ast_node *node, int indent) case AST_IDENT: fprintf(stderr, "Ident(%.*s)\n", (int) node->start_tok.sv.count, node->start_tok.sv.items); break; + case AST_OPERATOR_DECL: + fprintf(stderr, "Operator("); + if (node->operator.kind == OP_PREFIX) { + fprintf(stderr, "prefix, op: %.*s)\n", + (int) preops.items[node->operator.entry].sv.count, + preops.items[node->operator.entry].sv.items); + break; + } + if (node->operator.kind == OP_INFIX) { + fprintf(stderr, "infix, op: %.*s, prec: %i, assoc: %s)\n", + (int) inops.items[node->operator.entry].sv.count, + inops.items[node->operator.entry].sv.items, + inops.items[node->operator.entry].prec, + inops.items[node->operator.entry].right_assoc ? "right" : "left"); + break; + } + if (node->operator.kind == OP_POSTFIX) { + fprintf(stderr, "prefix, op: %.*s)\n", + (int) postops.items[node->operator.entry].sv.count, + postops.items[node->operator.entry].sv.items); + break; + } + unreachable("unknown AST_OPERATOR_DECL kind"); case AST_ERROR: fprintf(stderr, "Error\n"); break; default: - fprintf(stderr, "Unreachable\n"); - abort(); + unreachable("debug_print_ast_node"); + } +} + +void +dump_preops() +{ + for (int i = 0; i < preops.count; i++) { + fprintf(stderr, "Prefix { op: %.*s }\n", + (int) preops.items[i].sv.count, + preops.items[i].sv.items); + } +} + +void +dump_inops() +{ + for (int i = 0; i < inops.count; i++) { + fprintf(stderr, "Infix { op: %.*s prec: %i assoc: %s }\n", + (int) inops.items[i].sv.count, + inops.items[i].sv.items, + inops.items[i].prec, + inops.items[i].right_assoc ? "right" : "left"); + } +} + +void +dump_postops() +{ + for (int i = 0; i < postops.count; i++) { + fprintf(stderr, "Postfix { op: %.*s }\n", + (int) postops.items[i].sv.count, + postops.items[i].sv.items); } } diff --git a/parser.h b/parser.h @@ -2,50 +2,31 @@ #define DC_PARSER_H typedef enum { - BINOP_INVALID, + OP_PREFIX, + OP_INFIX, + OP_POSTFIX, +} Operator_kind; - BINOP_ADD, - BINOP_SUB, - BINOP_MUL, - BINOP_DIV, - BINOP_MOD, - BINOP_BAND, - BINOP_BOR, - BINOP_BXOR, - BINOP_LSHIFT, - BINOP_RSHIFT, - - BINOP_ASSIGN, - - BINOP_ADD_ASSIGN, - BINOP_SUB_ASSIGN, - BINOP_MUL_ASSIGN, - BINOP_DIV_ASSIGN, - BINOP_MOD_ASSIGN, - BINOP_BAND_ASSIGN, - BINOP_BOR_ASSIGN, - BINOP_BXOR_ASSIGN, - BINOP_LSHIFT_ASSIGN, - BINOP_RSHIFT_ASSIGN, +typedef struct { + Sv sv; + int prec; + bool right_assoc; +} Operator_entry; - BINOP_LAND, - BINOP_LOR, - BINOP_LXOR, - BINOP_LT, - BINOP_LE, - BINOP_EQ, - BINOP_NEQ, - BINOP_GE, - BINOP_GT, +typedef struct { + Operator_entry *items; + int count; + int capacity; +} Operator_entry_list; - BINOP_COMMA, - BINOP_ALL_, -} Binop_kind; +extern Operator_entry_list inops; +extern Operator_entry_list preop; +extern Operator_entry_list postop; typedef struct { - Binop_kind kind; + int entry; Token tok; -} Binop; +} Operator; typedef enum { AST_ERROR = 0, @@ -53,8 +34,11 @@ typedef enum { AST_VAR_DECL, AST_FUNC_DECL, AST_TYPE_DECL, + AST_OPERATOR_DECL, AST_BLOCK, - AST_BINOP, + AST_PREFIX, + AST_INFIX, + AST_POSTFIX, AST_NUMBER, AST_IDENT, } Ast_kind; @@ -72,10 +56,18 @@ struct Ast_node { Token start_tok; union { struct { - Binop op; + Operator op; + Ast_node *right; + } preop; + struct { + Operator op; Ast_node *left; Ast_node *right; - } binop; + } inop; + struct { + Operator op; + Ast_node *left; + } postop; struct { Token name; Ast_node *type; @@ -88,6 +80,11 @@ struct Ast_node { Ast_node *body; } func; struct { + Operator_kind kind; + int entry; + Ast_node *body; + } operator; + struct { Ast_node_list list; Token end_tok; } block; @@ -101,6 +98,7 @@ typedef struct { Lexer lexer; Ast_alloc_fn ast_alloc; Ast_free_fn ast_free; + bool error; } Parser; Ast_node *parse_postfix(Parser *p, Ast_node *child); @@ -114,5 +112,6 @@ Ast_node *parse_var_declaration(Parser *p); Ast_node_list parse(Parser *p); void debug_print_ast_node(Ast_node *node, int indent); +void dump_binops(void); #endif /* DC_PARSER_H */ diff --git a/test_input/01_test.dc b/test_input/01_test.dc @@ -0,0 +1,4 @@ +func main() { + var i := 42; + var j := 69; +} diff --git a/test_input/05_syscall.dc b/test_input/05_syscall.dc @@ -0,0 +1,3 @@ +func main() { + #syscall1(60, 42) +} diff --git a/test_input/binop.dc b/test_input/binop.dc @@ -1,8 +1,18 @@ -var i:= 1+2; -var j:= 1+2+3+4; -var k:= 1+2*3; -var l:= 1+2*3+4; -var m:= 1+2+3*4; -var n:= 1+2+3*4+5; +operator<infix, precedence = 1, left> + {} +operator<infix, precedence = 2, left> * {} +operator<infix, precedence = 3, right> = {} +operator<infix, precedence = 0, right> ++= {} +operator<infix, precedence = 2, right> plus {} + +var i := 1 ++= 32 plus 7 +var j := 1 ++= 32 plus 7; +/* +var i := 1+2; +var j := 1+2+3+4; +var k := 1+2*3; +var l := 1+2*3+4; +var m := 1+2+3*4; +var n := 1+2+3*4+5; var assign := i = j + 4 = k; +*/