omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / language / omega_parse.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * omega_parse.h -- Omega surface language V0 parser (spec/omega-language-v0.md). |
| 19 | * |
| 20 | * The syntax tree lives in a fixed arena inside OmegaAst and is DISPOSABLE: |
| 21 | * it is never hashed, stored, or used as identity. Lowering (omega_lower.h) |
| 22 | * turns it into existing canonical OmegaGraph objects / OmegaProgram and the |
| 23 | * tree is then thrown away. |
| 24 | * |
| 25 | * Recursion is bounded by OMEGA_AST_MAX_DEPTH (nesting of parentheses) and the |
| 26 | * arena by OMEGA_AST_MAX_NODES; exceeding either fails closed with -3. |
| 27 | */ |
| 28 | #ifndef OMEGA_LANG_PARSE_H |
| 29 | #define OMEGA_LANG_PARSE_H |
| 30 | |
| 31 | #include <stdbool.h> |
| 32 | #include <stddef.h> |
| 33 | #include <stdint.h> |
| 34 | |
| 35 | #include "omega_types.h" |
| 36 | #include "omega_lex.h" |
| 37 | |
| 38 | #define OMEGA_AST_MAX_NODES 128 |
| 39 | #define OMEGA_AST_MAX_DEPTH 32 |
| 40 | #define OMEGA_CLAUSE_MAX 64 /* matches OmegaContract.precondition[64] */ |
| 41 | |
| 42 | typedef enum { |
| 43 | OAST_INT = 1, /* integer literal (value) */ |
| 44 | OAST_BOOL, /* true / false (bval) */ |
| 45 | OAST_NAME, /* reference to an existing binding */ |
| 46 | OAST_BINARY, /* lhs op rhs */ |
| 47 | OAST_ASCRIBE /* (lhs : type) -- explicit typing */ |
| 48 | } OmegaAstKind; |
| 49 | |
| 50 | typedef enum { |
| 51 | OSTMT_EMPTY = 0, /* blank / comment-only line */ |
| 52 | OSTMT_EXPR, /* expr [: type] */ |
| 53 | OSTMT_LET, /* let name : type = expr */ |
| 54 | OSTMT_FN /* fn name(p: T) -> T [requires ..] [ensures ..] { expr } */ |
| 55 | } OmegaStmtKind; |
| 56 | |
| 57 | /* A V0 surface type. tag is TYPE_UNSIGNED_INT (width 8/16/32/64) or TYPE_BOOL. */ |
| 58 | typedef struct { |
| 59 | TypeTag tag; |
| 60 | uint16_t width; |
| 61 | } OmegaLangType; |
| 62 | |
| 63 | typedef struct { |
| 64 | OmegaAstKind kind; |
| 65 | uint32_t col; |
| 66 | OmegaTokKind op; /* OAST_BINARY */ |
| 67 | int lhs, rhs; /* child indices; OAST_ASCRIBE uses lhs */ |
| 68 | uint64_t value; /* OAST_INT */ |
| 69 | bool bval; /* OAST_BOOL */ |
| 70 | char name[OMEGA_LANG_IDENT_MAX]; /* OAST_NAME */ |
| 71 | OmegaLangType type; /* OAST_ASCRIBE */ |
| 72 | } OmegaAstNode; |
| 73 | |
| 74 | typedef struct { |
| 75 | OmegaStmtKind stmt; |
| 76 | uint32_t stmt_col; |
| 77 | OmegaAstNode nodes[OMEGA_AST_MAX_NODES]; |
| 78 | size_t node_count; |
| 79 | int root; /* expression root (let RHS, fn body, expr line) */ |
| 80 | |
| 81 | /* let */ |
| 82 | char name[OMEGA_LANG_IDENT_MAX]; /* let binding name or fn name */ |
| 83 | OmegaLangType let_type; |
| 84 | |
| 85 | /* fn */ |
| 86 | char param[OMEGA_LANG_IDENT_MAX]; |
| 87 | OmegaLangType param_type; |
| 88 | OmegaLangType ret_type; |
| 89 | /* Canonical contract text: tokens re-joined with single spaces, comments |
| 90 | * dropped, literals in decimal, the parameter renamed to `x`. "true" when |
| 91 | * the clause is omitted. */ |
| 92 | char requires_text[OMEGA_CLAUSE_MAX]; |
| 93 | char ensures_text[OMEGA_CLAUSE_MAX]; |
| 94 | } OmegaAst; |
| 95 | |
| 96 | /* Parse a token stream (from omega_language_lex) into `ast`. |
| 97 | * Returns 0 ok, -1 syntax error, -2 unsupported construct, -3 capacity |
| 98 | * (too deep / too many nodes). `err` gets a one-line message with column. */ |
| 99 | int omega_language_parse(const OmegaToken *toks, size_t count, OmegaAst *ast, |
| 100 | char *err, size_t n); |
| 101 | |
| 102 | /* Convenience: lex + parse one line. Same return codes. */ |
| 103 | int omega_language_parse_line(const char *src, OmegaAst *ast, char *err, size_t n); |
| 104 | |
| 105 | /* "u64", "bool", ... */ |
| 106 | const char *omega_language_type_text(const OmegaLangType *t); |
| 107 | |
| 108 | /* True for words V0 reserves (keywords, type names, effect/authority/control |
| 109 | * words). Such words cannot be binding names. */ |
| 110 | bool omega_language_is_reserved(const char *word); |
| 111 | |
| 112 | #endif /* OMEGA_LANG_PARSE_H */ |