Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/language/omega_parse.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / language / omega_parse.c` 445 lines ยท 17151 bytes Raw Blame History 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 /* omega_parse.c -- Omega surface language V0 parser. See omega_parse.h. */ 18 #include "omega_parse.h" 19 20 #include 21 #include 22 23 typedef struct { 24 const OmegaToken *t; 25 size_t count; 26 size_t pos; 27 OmegaAst *ast; 28 char *err; 29 size_t n; 30 } Parser; 31 32 static int fail(Parser *p, uint32_t col, int rc, const char *msg) { 33 if (p->err && p->n) snprintf(p->err, p->n, "column %u: %s", (unsigned)col, msg); 34 return rc; 35 } 36 37 static const OmegaToken *peek(Parser *p) { return &p->t[p->pos < p->count ? p->pos : p->count - 1]; } 38 static const OmegaToken *peek2(Parser *p) { 39 size_t i = p->pos + 1; 40 return &p->t[i < p->count ? i : p->count - 1]; 41 } 42 static const OmegaToken *next(Parser *p) { 43 const OmegaToken *t = peek(p); 44 if (p->pos < p->count - 1) p->pos++; 45 return t; 46 } 47 48 static int expect(Parser *p, OmegaTokKind k, const char *what) { 49 const OmegaToken *t = peek(p); 50 if (t->kind != k) { 51 char msg[160]; 52 snprintf(msg, sizeof msg, "expected %s but found '%s'", what, 53 t->kind == OTOK_IDENT ? t->text : omega_language_tok_text(t->kind)); 54 return fail(p, t->col, -1, msg); 55 } 56 next(p); 57 return 0; 58 } 59 60 static const char *const RESERVED_EXTRA[] = { 61 /* types */ 62 "u8", "u16", "u32", "u64", "bool", "i8", "i16", "i32", "i64", 63 /* contract */ 64 "result", 65 /* effects / authority -- never expressible in V0 */ 66 "effect", "effects", "cap", "capability", "mint", "revoke", "grant", "publish", 67 "promote", "authority", "admin", "syscall", "io", "print", "import", "extern", 68 "unsafe", "asm", 69 /* control flow / mutation -- not in V0 */ 70 "if", "else", "while", "for", "loop", "return", "match", "struct", "enum", 71 "mut", "var", "const", "self", "rec", 72 NULL 73 }; 74 75 bool omega_language_is_reserved(const char *w) { 76 if (!w) return true; 77 static const char *const kw[] = { "let", "fn", "requires", "ensures", "true", "false", NULL }; 78 for (int i = 0; kw[i]; i++) if (strcmp(w, kw[i]) == 0) return true; 79 for (int i = 0; RESERVED_EXTRA[i]; i++) if (strcmp(w, RESERVED_EXTRA[i]) == 0) return true; 80 return false; 81 } 82 83 const char *omega_language_type_text(const OmegaLangType *t) { 84 if (!t) return "?"; 85 if (t->tag == TYPE_BOOL) return "bool"; 86 if (t->tag == TYPE_UNSIGNED_INT) { 87 switch (t->width) { 88 case 8: return "u8"; 89 case 16: return "u16"; 90 case 32: return "u32"; 91 case 64: return "u64"; 92 default: break; 93 } 94 } 95 return "?"; 96 } 97 98 static int parse_type(Parser *p, OmegaLangType *out) { 99 const OmegaToken *t = peek(p); 100 if (t->kind != OTOK_IDENT) 101 return fail(p, t->col, -1, "expected a type (u8, u16, u32, u64 or bool)"); 102 const char *w = t->text; 103 memset(out, 0, sizeof(*out)); 104 if (strcmp(w, "u8") == 0) { out->tag = TYPE_UNSIGNED_INT; out->width = 8; } 105 else if (strcmp(w, "u16") == 0) { out->tag = TYPE_UNSIGNED_INT; out->width = 16; } 106 else if (strcmp(w, "u32") == 0) { out->tag = TYPE_UNSIGNED_INT; out->width = 32; } 107 else if (strcmp(w, "u64") == 0) { out->tag = TYPE_UNSIGNED_INT; out->width = 64; } 108 else if (strcmp(w, "bool") == 0) { out->tag = TYPE_BOOL; out->width = 1; } 109 else if (w[0] == 'i' && (strcmp(w, "i8") == 0 || strcmp(w, "i16") == 0 || 110 strcmp(w, "i32") == 0 || strcmp(w, "i64") == 0)) 111 return fail(p, t->col, -2, 112 "signed integers are not supported in V0 (the existing builders and evaluator are unsigned-only)"); 113 else { 114 char msg[160]; 115 snprintf(msg, sizeof msg, "'%s' is not a V0 type (use u8, u16, u32, u64 or bool)", w); 116 return fail(p, t->col, -2, msg); 117 } 118 next(p); 119 return 0; 120 } 121 122 static int new_node(Parser *p, OmegaAstKind k, uint32_t col) { 123 OmegaAst *a = p->ast; 124 if (a->node_count >= OMEGA_AST_MAX_NODES) return -1; 125 OmegaAstNode *nd = &a->nodes[a->node_count]; 126 memset(nd, 0, sizeof(*nd)); 127 nd->kind = k; 128 nd->col = col; 129 nd->lhs = nd->rhs = -1; 130 return (int)a->node_count++; 131 } 132 133 static int alloc_or_fail(Parser *p, OmegaAstKind k, uint32_t col, int *out) { 134 int idx = new_node(p, k, col); 135 if (idx < 0) return fail(p, col, -3, "expression is too large for V0 (node limit reached)"); 136 *out = idx; 137 return 0; 138 } 139 140 static int parse_expr(Parser *p, int depth, int *out); 141 142 static int reserved_msg(Parser *p, const OmegaToken *t) { 143 char msg[200]; 144 snprintf(msg, sizeof msg, 145 "'%s' is reserved: effects, authority, control flow and type names are not values in Omega V0", 146 t->text); 147 return fail(p, t->col, -2, msg); 148 } 149 150 static int parse_primary(Parser *p, int depth, int *out) { 151 const OmegaToken *t = peek(p); 152 int rc, idx; 153 switch (t->kind) { 154 case OTOK_INT: 155 if ((rc = alloc_or_fail(p, OAST_INT, t->col, &idx))) return rc; 156 p->ast->nodes[idx].value = t->value; 157 next(p); 158 *out = idx; 159 return 0; 160 case OTOK_TRUE: 161 case OTOK_FALSE: 162 if ((rc = alloc_or_fail(p, OAST_BOOL, t->col, &idx))) return rc; 163 p->ast->nodes[idx].bval = (t->kind == OTOK_TRUE); 164 next(p); 165 *out = idx; 166 return 0; 167 case OTOK_IDENT: 168 if (peek2(p)->kind == OTOK_LPAREN) 169 return fail(p, t->col, -2, "function calls are not supported in V0"); 170 if (omega_language_is_reserved(t->text)) return reserved_msg(p, t); 171 if ((rc = alloc_or_fail(p, OAST_NAME, t->col, &idx))) return rc; 172 snprintf(p->ast->nodes[idx].name, sizeof p->ast->nodes[idx].name, "%s", t->text); 173 next(p); 174 *out = idx; 175 return 0; 176 case OTOK_LPAREN: { 177 next(p); 178 int inner; 179 if ((rc = parse_expr(p, depth + 1, &inner))) return rc; 180 if (peek(p)->kind == OTOK_COLON) { 181 uint32_t ccol = peek(p)->col; 182 next(p); 183 OmegaLangType ty; 184 if ((rc = parse_type(p, &ty))) return rc; 185 if ((rc = alloc_or_fail(p, OAST_ASCRIBE, ccol, &idx))) return rc; 186 p->ast->nodes[idx].lhs = inner; 187 p->ast->nodes[idx].type = ty; 188 inner = idx; 189 } 190 if ((rc = expect(p, OTOK_RPAREN, "')'"))) return rc; 191 *out = inner; 192 return 0; 193 } 194 case OTOK_MINUS: 195 return fail(p, t->col, -2, "negative numbers are not supported in V0 (unsigned integers only)"); 196 case OTOK_BANG: 197 return fail(p, t->col, -2, "'!' (logical not) is not supported in V0"); 198 case OTOK_LBRACE: 199 return fail(p, t->col, -2, "blocks and records are not supported in V0 expressions"); 200 case OTOK_LET: 201 case OTOK_FN: 202 case OTOK_REQUIRES: 203 case OTOK_ENSURES: 204 return fail(p, t->col, -1, "keyword cannot be used inside an expression"); 205 default: { 206 char msg[128]; 207 snprintf(msg, sizeof msg, "expected a value but found '%s'", omega_language_tok_text(t->kind)); 208 return fail(p, t->col, -1, msg); 209 } 210 } 211 } 212 213 /* Binary precedence levels, lowest first. Each level is a loop (left-assoc), 214 * so a long operator chain does not recurse. */ 215 static int level_of(OmegaTokKind k) { 216 switch (k) { 217 case OTOK_EQEQ: case OTOK_NE: case OTOK_LT: case OTOK_LE: case OTOK_GT: case OTOK_GE: return 0; 218 case OTOK_PIPE: return 1; 219 case OTOK_AMP: return 2; 220 case OTOK_PLUS: case OTOK_MINUS: return 3; 221 case OTOK_STAR: case OTOK_SLASH: return 4; 222 default: return -1; 223 } 224 } 225 226 static int parse_level(Parser *p, int level, int depth, int *out) { 227 int rc, lhs; 228 if (level > 4) return parse_primary(p, depth, out); 229 if ((rc = parse_level(p, level + 1, depth, &lhs))) return rc; 230 int seen_cmp = 0; 231 while (level_of(peek(p)->kind) == level) { 232 const OmegaToken *op = next(p); 233 if (level == 0 && seen_cmp++) 234 return fail(p, op->col, -1, "comparisons cannot be chained; use parentheses"); 235 int rhs, idx; 236 if ((rc = parse_level(p, level + 1, depth, &rhs))) return rc; 237 if ((rc = alloc_or_fail(p, OAST_BINARY, op->col, &idx))) return rc; 238 p->ast->nodes[idx].op = op->kind; 239 p->ast->nodes[idx].lhs = lhs; 240 p->ast->nodes[idx].rhs = rhs; 241 lhs = idx; 242 } 243 *out = lhs; 244 return 0; 245 } 246 247 static int parse_expr(Parser *p, int depth, int *out) { 248 if (depth > OMEGA_AST_MAX_DEPTH) 249 return fail(p, peek(p)->col, -3, "expression is nested too deeply for V0 (limit 32)"); 250 return parse_level(p, 0, depth, out); 251 } 252 253 static int expect_end(Parser *p) { 254 const OmegaToken *t = peek(p); 255 if (t->kind == OTOK_EOF) return 0; 256 char msg[128]; 257 snprintf(msg, sizeof msg, "unexpected '%s' after the end of the statement", 258 t->kind == OTOK_IDENT ? t->text : omega_language_tok_text(t->kind)); 259 return fail(p, t->col, -1, msg); 260 } 261 262 static int check_binding_name(Parser *p, const OmegaToken *t) { 263 if (t->kind != OTOK_IDENT) return fail(p, t->col, -1, "expected a name"); 264 if (strcmp(t->text, "_") == 0) return fail(p, t->col, -1, "'_' is reserved for the last result"); 265 if (omega_language_is_reserved(t->text)) return reserved_msg(p, t); 266 return 0; 267 } 268 269 /* Canonical contract clause: tokens joined by single spaces (no space after 270 * '(' or before ')'), literals in decimal, parameter renamed to `x`. 271 * Stops at `stop1` or `stop2` at paren depth 0. */ 272 static int parse_clause(Parser *p, OmegaTokKind stop1, OmegaTokKind stop2, bool allow_result, 273 char *out, size_t out_len) { 274 size_t len = 0; 275 int parens = 0; 276 bool first = true, prev_lparen = false; 277 const OmegaToken *start = peek(p); 278 out[0] = '\0'; 279 while (1) { 280 const OmegaToken *t = peek(p); 281 if (parens == 0 && (t->kind == stop1 || t->kind == stop2)) break; 282 char piece[OMEGA_LANG_IDENT_MAX + 24]; 283 switch (t->kind) { 284 case OTOK_EOF: 285 return fail(p, t->col, -1, "contract clause is not followed by the function body '{ ... }'"); 286 case OTOK_IDENT: 287 if (strcmp(t->text, p->ast->param) == 0) snprintf(piece, sizeof piece, "x"); 288 else if (strcmp(t->text, "result") == 0) { 289 if (!allow_result) return fail(p, t->col, -2, "'result' can only appear in 'ensures'"); 290 snprintf(piece, sizeof piece, "result"); 291 } else { 292 char msg[160]; 293 snprintf(msg, sizeof msg, 294 "'%s' is not allowed in a contract (only the parameter, 'result', numbers and operators)", 295 t->text); 296 return fail(p, t->col, -2, msg); 297 } 298 break; 299 case OTOK_INT: 300 snprintf(piece, sizeof piece, "%llu", (unsigned long long)t->value); 301 break; 302 case OTOK_TRUE: case OTOK_FALSE: 303 case OTOK_PLUS: case OTOK_MINUS: case OTOK_STAR: case OTOK_SLASH: 304 case OTOK_AMP: case OTOK_PIPE: case OTOK_EQEQ: case OTOK_NE: 305 case OTOK_LT: case OTOK_LE: case OTOK_GT: case OTOK_GE: case OTOK_BANG: 306 snprintf(piece, sizeof piece, "%s", omega_language_tok_text(t->kind)); 307 break; 308 case OTOK_LPAREN: 309 parens++; 310 snprintf(piece, sizeof piece, "("); 311 break; 312 case OTOK_RPAREN: 313 if (parens == 0) return fail(p, t->col, -1, "unbalanced ')' in contract clause"); 314 parens--; 315 snprintf(piece, sizeof piece, ")"); 316 break; 317 default: { 318 char msg[128]; 319 snprintf(msg, sizeof msg, "'%s' is not allowed in a contract clause", omega_language_tok_text(t->kind)); 320 return fail(p, t->col, -1, msg); 321 } 322 } 323 bool space = !first && !prev_lparen && t->kind != OTOK_RPAREN; 324 size_t plen = strlen(piece); 325 if (len + (space ? 1 : 0) + plen >= out_len) 326 return fail(p, start->col, -2, "contract clause is longer than 63 characters (V0 contract text limit)"); 327 if (space) out[len++] = ' '; 328 memcpy(out + len, piece, plen); 329 len += plen; 330 out[len] = '\0'; 331 first = false; 332 prev_lparen = (t->kind == OTOK_LPAREN); 333 next(p); 334 } 335 if (first) return fail(p, peek(p)->col, -1, "empty contract clause"); 336 return 0; 337 } 338 339 static int parse_fn(Parser *p) { 340 int rc; 341 OmegaAst *a = p->ast; 342 next(p); /* fn */ 343 const OmegaToken *nm = peek(p); 344 if ((rc = check_binding_name(p, nm))) return rc; 345 snprintf(a->name, sizeof a->name, "%s", nm->text); 346 next(p); 347 if ((rc = expect(p, OTOK_LPAREN, "'(' after the function name"))) return rc; 348 if (peek(p)->kind == OTOK_RPAREN) 349 return fail(p, peek(p)->col, -2, "V0 functions take exactly one parameter"); 350 const OmegaToken *pn = peek(p); 351 if ((rc = check_binding_name(p, pn))) return rc; 352 if (strcmp(pn->text, a->name) == 0) 353 return fail(p, pn->col, -2, "parameter cannot share the function's name (recursion is not supported)"); 354 snprintf(a->param, sizeof a->param, "%s", pn->text); 355 next(p); 356 if ((rc = expect(p, OTOK_COLON, "':' and a parameter type"))) return rc; 357 if ((rc = parse_type(p, &a->param_type))) return rc; 358 if (peek(p)->kind == OTOK_COMMA) 359 return fail(p, peek(p)->col, -2, "V0 functions take exactly one parameter"); 360 if ((rc = expect(p, OTOK_RPAREN, "')'"))) return rc; 361 if ((rc = expect(p, OTOK_ARROW, "'->' and a result type"))) return rc; 362 if ((rc = parse_type(p, &a->ret_type))) return rc; 363 snprintf(a->requires_text, sizeof a->requires_text, "true"); 364 snprintf(a->ensures_text, sizeof a->ensures_text, "true"); 365 if (peek(p)->kind == OTOK_REQUIRES) { 366 next(p); 367 if ((rc = parse_clause(p, OTOK_ENSURES, OTOK_LBRACE, false, a->requires_text, sizeof a->requires_text))) 368 return rc; 369 } 370 if (peek(p)->kind == OTOK_ENSURES) { 371 next(p); 372 if ((rc = parse_clause(p, OTOK_LBRACE, OTOK_LBRACE, true, a->ensures_text, sizeof a->ensures_text))) 373 return rc; 374 } 375 if ((rc = expect(p, OTOK_LBRACE, "'{' starting the function body"))) return rc; 376 if ((rc = parse_expr(p, 0, &a->root))) return rc; 377 if ((rc = expect(p, OTOK_RBRACE, "'}' closing the function body"))) return rc; 378 return expect_end(p); 379 } 380 381 int omega_language_parse(const OmegaToken *toks, size_t count, OmegaAst *ast, char *err, size_t n) { 382 if (err && n) err[0] = '\0'; 383 if (!ast) return -1; 384 memset(ast, 0, sizeof(*ast)); 385 ast->root = -1; 386 if (!toks || count == 0 || toks[count - 1].kind != OTOK_EOF) { 387 if (err && n) snprintf(err, n, "column 0: internal: token stream must end with end-of-line"); 388 return -1; 389 } 390 Parser p = { toks, count, 0, ast, err, n }; 391 const OmegaToken *t = peek(&p); 392 ast->stmt_col = t->col; 393 int rc; 394 switch (t->kind) { 395 case OTOK_EOF: 396 ast->stmt = OSTMT_EMPTY; 397 return 0; 398 case OTOK_LET: { 399 ast->stmt = OSTMT_LET; 400 next(&p); 401 const OmegaToken *nm = peek(&p); 402 if ((rc = check_binding_name(&p, nm))) return rc; 403 snprintf(ast->name, sizeof ast->name, "%s", nm->text); 404 next(&p); 405 if ((rc = expect(&p, OTOK_COLON, "':' and a type (V0 bindings need an explicit type: let x: u64 = 7)"))) 406 return rc; 407 if ((rc = parse_type(&p, &ast->let_type))) return rc; 408 if ((rc = expect(&p, OTOK_ASSIGN, "'='"))) return rc; 409 if ((rc = parse_expr(&p, 0, &ast->root))) return rc; 410 return expect_end(&p); 411 } 412 case OTOK_FN: 413 ast->stmt = OSTMT_FN; 414 return parse_fn(&p); 415 default: { 416 ast->stmt = OSTMT_EXPR; 417 int root; 418 if ((rc = parse_expr(&p, 0, &root))) return rc; 419 if (peek(&p)->kind == OTOK_COLON) { 420 uint32_t ccol = peek(&p)->col; 421 next(&p); 422 OmegaLangType ty; 423 if ((rc = parse_type(&p, &ty))) return rc; 424 int idx; 425 if ((rc = alloc_or_fail(&p, OAST_ASCRIBE, ccol, &idx))) return rc; 426 ast->nodes[idx].lhs = root; 427 ast->nodes[idx].type = ty; 428 root = idx; 429 } 430 ast->root = root; 431 return expect_end(&p); 432 } 433 } 434 } 435 436 int omega_language_parse_line(const char *src, OmegaAst *ast, char *err, size_t n) { 437 OmegaToken toks[OMEGA_LANG_MAX_TOKENS]; 438 size_t count = 0; 439 int rc = omega_language_lex(src, toks, OMEGA_LANG_MAX_TOKENS, &count, err, n); 440 if (rc) { 441 if (ast) { memset(ast, 0, sizeof(*ast)); ast->root = -1; } 442 return rc; 443 } 444 return omega_language_parse(toks, count, ast, err, n); 445 }