diff options
author | Ian Moffett <ian@osmora.org> | 2025-10-01 17:08:06 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-10-01 17:08:44 -0400 |
commit | e0f16855cb1e5f0465db13405b6524682847688c (patch) | |
tree | 20bcc76aea8665b3862eb59338ac0b956274d395 /src/sys | |
parent | 8c8d31ed2c4127ba2965bd6a40ff0293418bc1e2 (diff) |
np: parse: Add AST structure and initial logic
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/include/np/ast.h | 66 | ||||
-rw-r--r-- | src/sys/include/os/np.h | 2 | ||||
-rw-r--r-- | src/sys/np/core/np_parse.c | 8 |
3 files changed, 76 insertions, 0 deletions
diff --git a/src/sys/include/np/ast.h b/src/sys/include/np/ast.h new file mode 100644 index 0000000..9647f53 --- /dev/null +++ b/src/sys/include/np/ast.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _NP_AST_H_ +#define _NP_AST_H_ 1 + +#include <sys/cdefs.h> +#include <np/lex.h> +#include <os/np.h> +#include <lib/ptrbox.h> +#include <string.h> + +/* + * Represents an AST node + * + * @ident: Identifier + * @token: Token type + * @left: Left node + * @right: Right node + */ +struct ast_node { + char *ident; + tt_t token; + struct ast_node *left; + struct ast_node *right; +}; + +__always_inline static inline void * +ast_alloc(struct np_work *work) +{ + struct ast_node *np; + + np = ptrbox_alloc(sizeof(*np), work->work_mem); + if (np != NULL) { + memset(np, 0, sizeof(*np)); + } + return np; +} + +#endif /* !_NP_AST_H_ */ diff --git a/src/sys/include/os/np.h b/src/sys/include/os/np.h index 3bb2bb8..c3921cb 100644 --- a/src/sys/include/os/np.h +++ b/src/sys/include/os/np.h @@ -47,6 +47,7 @@ * @source_size: Source size in bytes * @line_no: Current line number * @lex_st: Lexer state + * @ast_root: Parse tree * @ccache: Character cache (temporary store for lexer) */ struct np_work { @@ -55,6 +56,7 @@ struct np_work { size_t line_no; struct lexer_state lex_st; struct ptrbox *work_mem; + struct ast_node *ast_root; char ccache; }; diff --git a/src/sys/np/core/np_parse.c b/src/sys/np/core/np_parse.c index 12732cd..63f93aa 100644 --- a/src/sys/np/core/np_parse.c +++ b/src/sys/np/core/np_parse.c @@ -33,6 +33,7 @@ #include <sys/param.h> #include <np/lex.h> #include <np/parse.h> +#include <np/ast.h> #include <os/np.h> #define pr_trace(fmt, ...) printf("pirho.parse: " fmt, ##__VA_ARGS__) @@ -173,6 +174,13 @@ parse_work(struct np_work *work) return -EINVAL; } + /* Get the AST root node */ + work->ast_root = ast_alloc(work); + if (work->ast_root == NULL) { + pr_error("failed to alloc root AST|n"); + return -ENOMEM; + } + while (error == 0) { error = lex_nom(work, &tok); if (error < 0) { |