Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+200
View File
@@ -0,0 +1,200 @@
#! @file primary.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the parsing of primaries, which operators are applied to: literals,
#! interpolated strings, names and paths with their struct literals, arrays,
#! lambdas, `type_of`, and expressions in parentheses.
module sakura::analyzer;
#! What an expression is built from: a literal, a name or path, a struct
#! literal, an expression in parentheses, a lambda, an array, a match used as a
#! value, or `type_of(d)`, a type named to test it with `is`.
#! @param this The parser.
#! @param structs Whether a name followed by `{` is a struct literal.
#! @returns The node, or an `error` node.
def parser::primary(&mut this, structs: bool): u32 {
# `type_of(d)` is a type, which an expression names only to test it, as
# `type_of(d) is copyable`.
if this.check_word(names::type_of_id) && this.peek(1) == tk::lparen {
return this.type_primary();
}
# Within an `ensures`, `old(e)` is what `e` was where it was reached.
if this.in_ensures && this.check_word(names::old_id) && this.peek(1) == tk::lparen {
return this.old_value();
}
val token := this.index;
match this.kind {
tk::signed, tk::unsigned, tk::float => return this.literal(nk::number);
tk::char => return this.literal(nk::character);
tk::string => return this.literal(nk::string);
tk::kw_true, tk::kw_false => return this.literal(nk::boolean);
tk::kw_null => return this.literal(nk::null_lit);
tk::istring, tk::istring_beg => return this.istring();
tk::kw_match => return this.match_expr(true);
tk::kw_def => return this.lambda();
tk::lbracket => return this.array();
tk::name => return this.named(structs);
# What is between parentheses may be a struct literal.
tk::lparen => {
val angled := this.angled;
this.angled = false;
this.advance();
val inner := this.expression();
this.expect(tk::rparen, "Expected `)` to close the expression.");
this.angled = angled;
return inner;
}
# The lexer reported what is wrong with a malformed literal, so it stands as
# an error with nothing more said.
tk::bad_number, tk::bad_quote => {}
else => {
this.fail(ec::expr, "Expected an expression.");
return this.error_here();
}
}
this.advance();
return this.error(token);
}
#! `old(e)` within an `ensures`: what `e` was where the `ensures` was reached.
#! @param this The parser.
#! @returns The `old_value` node.
def parser::old_value(&mut this): u32 {
val token := this.index;
this.advance();
this.advance();
val inner := this.expression();
this.expect(tk::rparen, "Expected `)` to close `old`, which takes one expression.");
return this.make_node(nk::old_value, token, inner);
}
#! A literal of the kind, which its token alone describes.
#! @param this The parser.
#! @param kind The node's kind.
#! @returns The node.
def parser::literal(&mut this, kind: nk): u32 {
this.advance();
return this.make_node(kind, this.index - 1);
}
#! An interpolated string, whose run holds its text as `string` nodes, the token
#! of each a piece of it, between which come the expressions of its holes: the
#! `istring_beg` before the first hole, an `istring_mid` between two holes, and
#! the `istring_end` after the last. One with no holes is one `istring` piece.
#! @param this The parser.
#! @returns The `istring` node.
def parser::istring(&mut this): u32 {
val token := this.index;
val mark := this.mark;
this.push(this.literal(nk::string));
val angled := this.angled;
this.angled = false;
if this.syntax.token_kinds[token] == tk::istring_beg {
while true {
this.push(this.expression());
if this.check(tk::istring_mid) {
this.push(this.literal(nk::string));
continue;
}
if this.check(tk::istring_end) {
this.push(this.literal(nk::string));
break;
}
this.fail(ec::token, "Expected `}` after the interpolated expression.");
this.skip_hole();
break;
}
}
this.angled = angled;
return this.make_node(nk::istring, token, this.make_run(mark));
}
#! Skips past the end of the interpolated string whose hole the parser is
#! within, stepping over the strings within it.
#! @param this The parser.
def parser::skip_hole(&mut this): void {
var depth: u32 = 0;
while !this.check(tk::eof) {
if this.check(tk::istring_beg) {
depth++;
} else if this.check(tk::istring_end) {
if depth == 0 {
this.advance();
break;
}
depth--;
}
this.advance();
}
}
#! A name, a path, as `math::max::<u8>`, or a struct literal of either, where
#! they are allowed.
#! @param this The parser.
#! @param structs Whether a struct literal may follow.
#! @returns The `name`, `path`, or `construct` node.
def parser::named(&mut this, structs: bool): u32 {
val token := this.index;
var made: u32 = 0;
if this.peek(1) == tk::scope {
made = this.make_node(nk::path, token, this.segments(parser::args::values));
} else {
this.advance();
made = this.make_node(nk::name, token);
}
return made if !structs || !this.check(tk::lbrace);
# The literal's type is the name read as a type: a path's segments are a type
# name's, and a name is a type name of one segment, which holds no run.
this.syntax.node_kinds[made] = nk::type_name;
val brace := this.index;
return this.make_node(nk::construct, brace, made, this.field_list());
}
#! `[a, b, c]`.
#! @param this The parser.
#! @returns The `array` node.
def parser::array(&mut this): u32 {
val token := this.index;
this.advance();
val elements := this.list(parser::item::element, tk::rbracket, "Expected `]` to close the array.");
return this.make_node(nk::array, token, elements);
}
#! `def(params): result { body }`, or `def(params) => value`, which returns the
#! value, or evaluates it for `void`. Its parameter types and result may be left
#! out.
#! @param this The parser.
#! @returns The `lambda` node.
def parser::lambda(&mut this): u32 {
val token := this.index;
val angled := this.angled;
this.angled = false;
# A lambda's body is checked where it is called, which no `old` reaches back
# from.
val ensures := this.in_ensures;
this.in_ensures = false;
this.advance();
val signature := this.params(true, false);
val body := this.accept(tk::arrow)
? this.expression()
: this.block();
this.angled = angled;
this.in_ensures = ensures;
return this.make_node(nk::lambda, token, signature.params, this.make_record(signature.result, body));
}