• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/include/ucode/compiler.h

  1 /*
  2  * Copyright (C) 2020-2021 Jo-Philipp Wich <jo@mein.io>
  3  *
  4  * Permission to use, copy, modify, and/or distribute this software for any
  5  * purpose with or without fee is hereby granted, provided that the above
  6  * copyright notice and this permission notice appear in all copies.
  7  *
  8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15  */
 16 
 17 #ifndef UCODE_COMPILER_H
 18 #define UCODE_COMPILER_H
 19 
 20 #include <stddef.h>
 21 #include <stdint.h>
 22 #include <stdbool.h>
 23 
 24 #include "source.h"
 25 #include "lexer.h"
 26 #include "types.h"
 27 #include "util.h"
 28 
 29 typedef enum {
 30         P_NONE,
 31 
 32         P_COMMA,        /* , */
 33 
 34         P_ASSIGN,       /* = += -= *= /= %= <<= >>= &= ^= |= ||= &&= **= ??= */
 35 
 36         P_TERNARY,      /* ?: */
 37 
 38         P_OR,           /* || ?? */
 39         P_AND,          /* && */
 40         P_BOR,          /* | */
 41         P_BXOR,         /* ^ */
 42         P_BAND,         /* & */
 43 
 44         P_EQUAL,        /* === !== == != */
 45         P_COMPARE,      /* < <= > >= in */
 46 
 47         P_SHIFT,        /* << >> */
 48 
 49         P_ADD,          /* + - */
 50         P_MUL,          /* * / % */
 51 
 52         P_EXP,          /* ** */
 53 
 54         P_UNARY,        /* ! ~ +… -… ++… --… */
 55 
 56         P_INC,          /* …++ …-- */
 57 
 58         P_CALL,         /* ….…, …[…], …(…) */
 59 
 60         P_PRIMARY       /* (…) */
 61 } uc_precedence_t;
 62 
 63 typedef enum {
 64         F_ASSIGNABLE = (1 << 0),
 65         F_OPTCHAINING = (1 << 1),
 66         F_ALTBLOCKMODE = (1 << 2),
 67 } uc_exprflag_t;
 68 
 69 typedef struct uc_patchlist {
 70         struct uc_patchlist *parent;
 71         size_t depth, count, *entries;
 72         uc_tokentype_t token;
 73 } uc_patchlist_t;
 74 
 75 typedef struct uc_exprstack {
 76         struct uc_exprstack *parent;
 77         uint32_t flags;
 78         uc_tokentype_t token;
 79 } uc_exprstack_t;
 80 
 81 typedef struct {
 82         uc_value_t *name;
 83         ssize_t depth;
 84         size_t from;
 85         bool captured;
 86         bool constant;
 87 } uc_local_t;
 88 
 89 typedef struct {
 90         uc_value_t *name;
 91         size_t index;
 92         bool local;
 93         bool constant;
 94 } uc_upval_t;
 95 
 96 uc_declare_vector(uc_locals_t, uc_local_t);
 97 uc_declare_vector(uc_upvals_t, uc_upval_t);
 98 uc_declare_vector(uc_jmplist_t, size_t);
 99 
100 typedef struct {
101         uc_parse_config_t *config;
102         uc_lexer_t lex;
103         uc_token_t prev, curr;
104         bool synchronizing;
105         uc_stringbuf_t *error;
106 } uc_parser_t;
107 
108 typedef struct uc_compiler {
109         struct uc_compiler *parent;
110         uc_locals_t locals;
111         uc_upvals_t upvals;
112         uc_patchlist_t *patchlist;
113         uc_exprstack_t *exprstack;
114         uc_function_t *function;
115         uc_parser_t *parser;
116         uc_program_t *program;
117         size_t scope_depth, current_srcpos, last_insn;
118 } uc_compiler_t;
119 
120 typedef struct {
121         void (*prefix)(uc_compiler_t *);
122         void (*infix)(uc_compiler_t *);
123         uc_precedence_t precedence;
124 } uc_parse_rule_t;
125 
126 uc_program_t *uc_compile(uc_parse_config_t *config, uc_source_t *source, char **errp);
127 
128 #define uc_compiler_exprstack_push(compiler, token, flags) \
129         uc_exprstack_t expr = { compiler->exprstack, flags, token }; \
130         compiler->exprstack = &expr
131 
132 #define uc_compiler_exprstack_pop(compiler) \
133         if (compiler->exprstack) \
134                 compiler->exprstack = compiler->exprstack->parent
135 
136 #endif /* UCODE_COMPILER_H */
137 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt