hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 3f28ea119380bfc0bf68b7d83d6c0bf8e577b71e
parent f1da658ed2e996c5b9d7c5521268518f524e3ec7
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 11 Apr 2021 13:12:51 -0400

hare::parse: s/want_tok/want/g et al

Diffstat:
Mhare/parse/decl.ha | 32++++++++++++++++----------------
Mhare/parse/expr.ha | 22+++++++++++-----------
Mhare/parse/ident.ha | 4++--
Mhare/parse/import.ha | 14+++++++-------
Mhare/parse/type.ha | 62+++++++++++++++++++++++++++++++-------------------------------
Mhare/parse/util.ha | 6+++---
6 files changed, 70 insertions(+), 70 deletions(-)

diff --git a/hare/parse/decl.ha b/hare/parse/decl.ha @@ -6,8 +6,8 @@ use hare::unparse; use strings; fn attr_symbol(lexer: *lex::lexer) (str | error) = { - want_tok(lexer, ltok::LPAREN)?; - let t = want_tok(lexer, ltok::LITERAL)?; + want(lexer, ltok::LPAREN)?; + let t = want(lexer, ltok::LITERAL)?; let s = match (t.1) { v: lex::value => if (v.storage == lex::storage::STR) v.string else return syntaxerr(t.2, @@ -30,7 +30,7 @@ fn attr_symbol(lexer: *lex::lexer) (str | error) = { ascii::isalnum(r) || r == '$' || r == '.' || r == '_', "Invalid symbol")?, }; - want_tok(lexer, ltok::RPAREN)?; + want(lexer, ltok::RPAREN)?; return s; }; @@ -41,20 +41,20 @@ fn decl_global( let decl: []ast::decl_global = []; for (true) { let symbol = if (tok == ltok::CONST || tok == ltok::LET) { - match (try_tok(lexer, ltok::ATTR_SYMBOL)?) { + match (try(lexer, ltok::ATTR_SYMBOL)?) { void => "", lex::token => attr_symbol(lexer)?, }; } else ""; let ident = ident(lexer)?; - want_tok(lexer, ltok::COLON)?; + want(lexer, ltok::COLON)?; let _type = _type(lexer)?; if (tok == ltok::CONST) { _type.flags |= ast::type_flags::CONST; }; - want_tok(lexer, ltok::EQUAL)?; + want(lexer, ltok::EQUAL)?; let init = expression(lexer)?; - let btok = try_tok(lexer, ltok::COMMA)?; + let btok = try(lexer, ltok::COMMA)?; append(decl, ast::decl_global { is_const = tok == ltok::DEF, symbol = symbol, @@ -74,9 +74,9 @@ fn decl_type(lexer: *lex::lexer) ([]ast::decl_type | error) = { let decl: []ast::decl_type = []; for (true) { let ident = ident(lexer)?; - want_tok(lexer, ltok::EQUAL)?; + want(lexer, ltok::EQUAL)?; let _type = _type(lexer)?; - let btok = try_tok(lexer, ltok::COMMA)?; + let btok = try(lexer, ltok::COMMA)?; append(decl, ast::decl_type { ident = ident, _type = _type, @@ -95,7 +95,7 @@ fn decl_func(lexer: *lex::lexer) (ast::decl_func | error) = { ltok::ATTR_FINI, ltok::ATTR_INIT, ltok::ATTR_TEST, ltok::ATTR_NORETURN, ltok::ATTR_SYMBOL ]; - for (true) match (try_tok(lexer, attrs...)?) { + for (true) match (try(lexer, attrs...)?) { void => break, t: lex::token => switch (t.0) { ltok::ATTR_FINI => attr = ast::fndecl_attrs::FINI, @@ -107,7 +107,7 @@ fn decl_func(lexer: *lex::lexer) (ast::decl_func | error) = { }, }; - want_tok(lexer, ltok::FN)?; + want(lexer, ltok::FN)?; let ident_loc = mkloc(lexer); let ident = ident(lexer)?; let proto_loc = mkloc(lexer); @@ -116,7 +116,7 @@ fn decl_func(lexer: *lex::lexer) (ast::decl_func | error) = { prototype.attrs |= ast::func_attrs::NORETURN; }; - let tok = want_tok(lexer, ltok::EQUAL, ltok::SEMICOLON)?; + let tok = want(lexer, ltok::EQUAL, ltok::SEMICOLON)?; let body = switch (tok.0) { ltok::EQUAL => { synassert(ident_loc, len(ident) == 1, @@ -149,13 +149,13 @@ fn decl_func(lexer: *lex::lexer) (ast::decl_func | error) = { export fn decls(lexer: *lex::lexer) ([]ast::decl | error) = { let decls: []ast::decl = []; for (true) { - if (peek_tok(lexer, ltok::EOF)? is lex::token) break; - let exported = match (try_tok(lexer, ltok::EXPORT)?) { + if (peek(lexer, ltok::EOF)? is lex::token) break; + let exported = match (try(lexer, ltok::EXPORT)?) { void => false, lex::token => true, }; const toks = [ltok::CONST, ltok::LET, ltok::DEF, ltok::TYPE]; - let decl = match (try_tok(lexer, toks...)?) { + let decl = match (try(lexer, toks...)?) { void => decl_func(lexer)?, t: lex::token => if (t.0 == ltok::TYPE) decl_type(lexer)? @@ -166,7 +166,7 @@ export fn decls(lexer: *lex::lexer) ([]ast::decl | error) = { loc = mkloc(lexer), decl = decl, }); - want_tok(lexer, ltok::SEMICOLON)?; + want(lexer, ltok::SEMICOLON)?; }; return decls; }; diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha @@ -46,7 +46,7 @@ fn cast(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = { void => unarithm(lexer)?, e: ast::expr => e, }; - const tok = match (try_tok(lexer, ltok::COLON, + const tok = match (try(lexer, ltok::COLON, ltok::AS, ltok::IS)?) { void => return lvalue, tok: lex::token => tok.0, @@ -65,7 +65,7 @@ fn cast(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = { }; fn constant(lexer: *lex::lexer) (ast::expr | error) = { - want_tok(lexer, ltok::VOID)?; + want(lexer, ltok::VOID)?; return void; }; @@ -78,7 +78,7 @@ fn objsel(lexer: *lex::lexer) (ast::expr | error) = { }; fn plain_expression(lexer: *lex::lexer) (ast::expr | error) = { - let tok = peek_tok(lexer)? as lex::token; + let tok = peek(lexer)? as lex::token; return switch (tok.0) { ltok::TRUE, ltok::FALSE, @@ -88,7 +88,7 @@ fn plain_expression(lexer: *lex::lexer) (ast::expr | error) = { ltok::STRUCT => abort(), // TODO: Struct literal ltok::LPAREN => { let ex = expression(lexer); - return switch (want_tok(lexer, + return switch (want(lexer, ltok::RPAREN, ltok::COMMA)?.0) { ltok::RPAREN => ex, ltok::COMMA => abort(), // TODO: Tuple literal @@ -98,7 +98,7 @@ fn plain_expression(lexer: *lex::lexer) (ast::expr | error) = { ltok::LITERAL => return constant(lexer), ltok::NAME => { let id = ident(lexer)?; - return match (try_tok(lexer, ltok::LBRACE)?) { + return match (try(lexer, ltok::LBRACE)?) { void => id: ast::access_identifier, lex::token => abort(), // TODO: Struct literal }; @@ -111,7 +111,7 @@ fn plain_expression(lexer: *lex::lexer) (ast::expr | error) = { fn postfix(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = { // Built-ins (XXX: these should probably be moved in the Hare grammar) - match (peek_tok(lexer, + match (peek(lexer, ltok::ALLOC, ltok::APPEND, ltok::FREE, ltok::DELETE, ltok::ABORT, ltok::ASSERT, ltok::STATIC, ltok::SIZE, ltok::LEN, @@ -132,7 +132,7 @@ fn postfix(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = ex: ast::expr => ex, }; - match (try_tok(lexer, ltok::LPAREN, ltok::DOT, ltok::LBRACKET, + match (try(lexer, ltok::LPAREN, ltok::DOT, ltok::LBRACKET, ltok::QUESTION)) { tok: lex::token => switch (tok.0) { ltok::LPAREN => abort(), // TODO: Calls @@ -148,7 +148,7 @@ fn postfix(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = }; fn unarithm(lexer: *lex::lexer) (ast::expr | error) = { - const tok = match (try_tok(lexer, + const tok = match (try(lexer, ltok::PLUS, ltok::MINUS, ltok::BNOT, ltok::LNOT, ltok::TIMES, ltok::BAND)) { void => return postfix(lexer, void), @@ -222,7 +222,7 @@ fn expression_list(lexer: *lex::lexer) (ast::expr | error) = { let items: ast::list_expr = []; for (let more = true; more) { - let tok = peek_tok(lexer)? as lex::token; + let tok = peek(lexer)? as lex::token; let item = switch (tok.0) { ltok::RBRACE => break, ltok::BREAK, @@ -234,9 +234,9 @@ fn expression_list(lexer: *lex::lexer) (ast::expr | error) = { * => expression(lexer)?, }; append(items, alloc(item)); - want_tok(lexer, ltok::SEMICOLON)?; + want(lexer, ltok::SEMICOLON)?; }; - want_tok(lexer, ltok::RBRACE)?; + want(lexer, ltok::RBRACE)?; return items; }; diff --git a/hare/parse/ident.ha b/hare/parse/ident.ha @@ -6,13 +6,13 @@ fn ident_trailing(lexer: *lex::lexer) ((ast::ident, bool) | error) = { let ident: []str = []; let z = 0z; for (true) { - let name = match (try_tok(lexer, ltok::NAME)?) { + let name = match (try(lexer, ltok::NAME)?) { t: lex::token => t.1 as str, void => return (ident: ast::ident, true), }; append(ident, name); z += len(name); - match (try_tok(lexer, ltok::DOUBLE_COLON)?) { + match (try(lexer, ltok::DOUBLE_COLON)?) { void => break, * => void, // Grab the next ident }; diff --git a/hare/parse/import.ha b/hare/parse/import.ha @@ -5,9 +5,9 @@ use hare::lex::{ltok}; fn name_list(lexer: *lex::lexer) ([]str | error) = { let names: []str = []; for (true) { - append(names, want_tok(lexer, ltok::NAME)?.1 as str); - switch (want_tok(lexer, ltok::COMMA, ltok::RBRACE)?.0) { - ltok::COMMA => match (try_tok(lexer, ltok::RBRACE)?) { + append(names, want(lexer, ltok::NAME)?.1 as str); + switch (want(lexer, ltok::COMMA, ltok::RBRACE)?.0) { + ltok::COMMA => match (try(lexer, ltok::RBRACE)?) { void => void, * => return names, }, @@ -22,14 +22,14 @@ fn name_list(lexer: *lex::lexer) ([]str | error) = { export fn imports(lexer: *lex::lexer) ([]ast::import | error) = { let imports: []ast::import = []; for (true) { - match (try_tok(lexer, ltok::USE)?) { + match (try(lexer, ltok::USE)?) { void => break, * => void, }; let name = ident_trailing(lexer)?; - switch (want_tok(lexer, ltok::SEMICOLON, ltok::LBRACE, + switch (want(lexer, ltok::SEMICOLON, ltok::LBRACE, ltok::EQUAL)?.0) { ltok::SEMICOLON => { synassert(mkloc(lexer), !name.1, @@ -44,7 +44,7 @@ export fn imports(lexer: *lex::lexer) ([]ast::import | error) = { ident = name.0, objects = objects, }); - want_tok(lexer, ltok::SEMICOLON)?; + want(lexer, ltok::SEMICOLON)?; }, ltok::EQUAL => { synassert(mkloc(lexer), @@ -55,7 +55,7 @@ export fn imports(lexer: *lex::lexer) ([]ast::import | error) = { ident = ident, alias = name.0[0], }); - want_tok(lexer, ltok::SEMICOLON)?; + want(lexer, ltok::SEMICOLON)?; }, * => abort(), // Unreachable }; diff --git a/hare/parse/type.ha b/hare/parse/type.ha @@ -6,42 +6,42 @@ use hare::lex::{ltok}; fn prototype(lexer: *lex::lexer) (ast::func_type | error) = { let variadism = ast::variadism::NONE; let params: []ast::func_param = []; - want_tok(lexer, ltok::LPAREN)?; - for (try_tok(lexer, ltok::RPAREN)? is void) { + want(lexer, ltok::LPAREN)?; + for (try(lexer, ltok::RPAREN)? is void) { let loc = mkloc(lexer); - match (try_tok(lexer, ltok::ELLIPSIS)?) { + match (try(lexer, ltok::ELLIPSIS)?) { void => void, lex::token => { synassert(loc, len(params) > 0, "Expected at least one non-variadic parameter for C-style variadism")?; variadism = ast::variadism::C; - try_tok(lexer, ltok::COMMA)?; - want_tok(lexer, ltok::RPAREN)?; + try(lexer, ltok::COMMA)?; + want(lexer, ltok::RPAREN)?; break; }, }; - let name = match (try_tok(lexer, ltok::UNDERSCORE)?) { - void => want_tok(lexer, ltok::NAME)?.1 as str, + let name = match (try(lexer, ltok::UNDERSCORE)?) { + void => want(lexer, ltok::NAME)?.1 as str, lex::token => "", }; - want_tok(lexer, ltok::COLON); + want(lexer, ltok::COLON); append(params, ast::func_param { loc = loc, name = name, _type = alloc(_type(lexer)?), }); - match (try_tok(lexer, ltok::ELLIPSIS)?) { + match (try(lexer, ltok::ELLIPSIS)?) { void => void, lex::token => { variadism = ast::variadism::HARE; - try_tok(lexer, ltok::COMMA)?; - want_tok(lexer, ltok::RPAREN)?; + try(lexer, ltok::COMMA)?; + want(lexer, ltok::RPAREN)?; break; }, }; - match (try_tok(lexer, ltok::COMMA)?) { + match (try(lexer, ltok::COMMA)?) { void => { - want_tok(lexer, ltok::RPAREN)?; + want(lexer, ltok::RPAREN)?; break; }, lex::token => void, @@ -58,7 +58,7 @@ fn prototype(lexer: *lex::lexer) (ast::func_type | error) = { fn integer_type( lexer: *lex::lexer, -) (builtin_type | error) = switch (want_tok(lexer)?.0) { +) (builtin_type | error) = switch (want(lexer)?.0) { ltok::CHAR => builtin_type::CHAR, ltok::I16 => builtin_type::I16, ltok::I32 => builtin_type::I32, @@ -78,7 +78,7 @@ fn integer_type( }; fn primitive_type(lexer: *lex::lexer) (ast::_type | error) = { - let tok = want_tok(lexer)?; + let tok = want(lexer)?; let builtin = switch (tok.0) { ltok::CHAR, ltok::I16, ltok::I32, ltok::I64, ltok::I64, ltok::I8, ltok::INT, ltok::SIZE, ltok::U16, @@ -107,7 +107,7 @@ fn primitive_type(lexer: *lex::lexer) (ast::_type | error) = { fn alias_type(lexer: *lex::lexer) (ast::_type | error) = { let loc = mkloc(lexer); - let unwrap = match (try_tok(lexer, ltok::ELLIPSIS)?) { + let unwrap = match (try(lexer, ltok::ELLIPSIS)?) { void => false, * => true, }; @@ -123,11 +123,11 @@ fn alias_type(lexer: *lex::lexer) (ast::_type | error) = { fn pointer_type(lexer: *lex::lexer) (ast::_type | error) = { let loc = mkloc(lexer); - let flags = match (try_tok(lexer, ltok::NULLABLE)?) { + let flags = match (try(lexer, ltok::NULLABLE)?) { void => 0: ast::pointer_flags, * => ast::pointer_flags::NULLABLE, }; - want_tok(lexer, ltok::TIMES)?; + want(lexer, ltok::TIMES)?; return ast::_type { loc = loc, flags = 0, @@ -142,11 +142,11 @@ fn tagged_type(lexer: *lex::lexer, first: ast::_type) (ast::_type | error) = { let loc = mkloc(lexer); let tagged: ast::tagged_type = []; append(tagged, alloc(first)); - for (try_tok(lexer, ltok::RPAREN)? is void) { + for (try(lexer, ltok::RPAREN)? is void) { append(tagged, alloc(_type(lexer)?)); - match (try_tok(lexer, ltok::BOR)?) { + match (try(lexer, ltok::BOR)?) { void => { - want_tok(lexer, ltok::RPAREN)?; + want(lexer, ltok::RPAREN)?; break; }, lex::token => void, @@ -163,11 +163,11 @@ fn tuple_type(lexer: *lex::lexer, first: ast::_type) (ast::_type | error) = { let loc = mkloc(lexer); let tuple: ast::tuple_type = []; append(tuple, alloc(first)); - for (try_tok(lexer, ltok::RPAREN)? is void) { + for (try(lexer, ltok::RPAREN)? is void) { append(tuple, alloc(_type(lexer)?)); - match (try_tok(lexer, ltok::COMMA)?) { + match (try(lexer, ltok::COMMA)?) { void => { - want_tok(lexer, ltok::RPAREN)?; + want(lexer, ltok::RPAREN)?; break; }, lex::token => void, @@ -182,11 +182,11 @@ fn tuple_type(lexer: *lex::lexer, first: ast::_type) (ast::_type | error) = { fn fn_type(lexer: *lex::lexer) (ast::_type | error) = { let loc = mkloc(lexer); - let attrs = match (try_tok(lexer, ltok::ATTR_NORETURN)?) { + let attrs = match (try(lexer, ltok::ATTR_NORETURN)?) { void => 0: ast::func_attrs, * => ast::func_attrs::NORETURN, }; - want_tok(lexer, ltok::FN)?; + want(lexer, ltok::FN)?; let proto = prototype(lexer)?; proto.attrs |= attrs; return ast::_type { @@ -198,11 +198,11 @@ fn fn_type(lexer: *lex::lexer) (ast::_type | error) = { // Parses a type export fn _type(lexer: *lex::lexer) (ast::_type | error) = { - let flags: ast::type_flags = match (try_tok(lexer, ltok::CONST)?) { + let flags: ast::type_flags = match (try(lexer, ltok::CONST)?) { void => 0, * => ast::type_flags::CONST, }; - let tok = peek_tok(lexer)? as lex::token; + let tok = peek(lexer)? as lex::token; let typ: ast::_type = switch (tok.0) { ltok::CHAR, ltok::I16, ltok::I32, ltok::I64, ltok::I64, ltok::I8, ltok::INT, ltok::SIZE, @@ -215,9 +215,9 @@ export fn _type(lexer: *lex::lexer) (ast::_type | error) = { ltok::STRUCT, ltok::UNION => abort(), // TODO ltok::LBRACKET => abort(), // TODO ltok::LPAREN => { - want_tok(lexer, ltok::LPAREN)?; + want(lexer, ltok::LPAREN)?; let t = _type(lexer)?; - switch (want_tok(lexer, ltok::BOR, + switch (want(lexer, ltok::BOR, ltok::COMMA)?.0) { ltok::BOR => tagged_type(lexer, t)?, ltok::COMMA => tuple_type(lexer, t)?, @@ -231,7 +231,7 @@ export fn _type(lexer: *lex::lexer) (ast::_type | error) = { lex::tokstr(tok)), }; - match (try_tok(lexer, ltok::LNOT)?) { + match (try(lexer, ltok::LNOT)?) { void => void, * => flags |= ast::type_flags::ERROR, }; diff --git a/hare/parse/util.ha b/hare/parse/util.ha @@ -6,7 +6,7 @@ use strio; // Requires the next token to have a matching ltok. Returns that token, or an // error. -fn want_tok(lexer: *lex::lexer, want: lex::ltok...) (lex::token | error) = { +fn want(lexer: *lex::lexer, want: lex::ltok...) (lex::token | error) = { let tok = lex::lex(lexer)?; if (len(want) == 0) { return tok; @@ -32,7 +32,7 @@ fn want_tok(lexer: *lex::lexer, want: lex::ltok...) (lex::token | error) = { // Looks for a matching ltok from the lexer, and if not present, unlexes the // token and returns void. If found, the token is consumed from the lexer and is // returned. -fn try_tok( +fn try( lexer: *lex::lexer, want: lex::ltok... ) (lex::token | error | void) = { @@ -48,7 +48,7 @@ fn try_tok( // Looks for a matching ltok from the lexer, unlexes the token, and returns // it; or void if it was not a ltok. -fn peek_tok( +fn peek( lexer: *lex::lexer, want: lex::ltok... ) (lex::token | error | void) = {