|
|
|
|
@@ -1,10 +1,8 @@
|
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
|
|
use anyhow::bail;
|
|
|
|
|
|
|
|
|
|
use crate::cli::CliArgs;
|
|
|
|
|
use crate::common::loc::LocBox;
|
|
|
|
|
use crate::{cli, error, lerror};
|
|
|
|
|
use crate::{error, lerror};
|
|
|
|
|
use crate::parser::ast::{Program, TString, TokenType};
|
|
|
|
|
use crate::parser::ast::statement::Let;
|
|
|
|
|
use crate::parser::expr::parse_expr;
|
|
|
|
|
@@ -48,7 +46,7 @@ pub fn parse_statement(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Progra
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_include(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
pub fn parse_include(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Include), "")?;
|
|
|
|
|
let TokenType::String(include_path) = utils::check_consume_or_err(tokens, TokenType::String(TString::default()), "")?.tt().clone() else {panic!()};
|
|
|
|
|
_ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?;
|
|
|
|
|
@@ -81,7 +79,7 @@ fn parse_include(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) ->
|
|
|
|
|
Ok(LocBox::new(kw.loc(), Statement::Include))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_enum(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
|
|
|
|
|
pub fn parse_enum(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
|
|
|
|
|
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Enum), "")?;
|
|
|
|
|
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
|
|
|
|
|
_ = utils::check_consume(tokens, TokenType::Delim(Delimiter::CurlyL));
|
|
|
|
|
@@ -105,7 +103,7 @@ fn parse_enum(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
|
|
|
|
|
Ok(LocBox::new(kw.loc(), Statement::Enum(Enum { name, fields })))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_struct(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
|
|
|
|
|
pub fn parse_struct(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
|
|
|
|
|
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Struct), "")?;
|
|
|
|
|
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
|
|
|
|
|
_ = utils::check_consume(tokens, TokenType::Delim(Delimiter::CurlyL));
|
|
|
|
|
@@ -131,7 +129,7 @@ fn parse_struct(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
|
|
|
|
|
Ok(LocBox::new(kw.loc(), Statement::Struct(Struct { name, fields })))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_static(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
pub fn parse_static(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Static), "")?;
|
|
|
|
|
|
|
|
|
|
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
|
|
|
|
|
@@ -146,7 +144,7 @@ fn parse_static(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> R
|
|
|
|
|
Ok(LocBox::new(kw.loc(), Statement::StaticVar(StaticVar { name, typ, val })))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_let(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
pub fn parse_let(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Let), "")?;
|
|
|
|
|
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
|
|
|
|
|
let mut typ = None;
|
|
|
|
|
@@ -164,7 +162,7 @@ fn parse_let(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Resu
|
|
|
|
|
_ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Semi), "")?;
|
|
|
|
|
Ok(LocBox::new(kw.loc(), Statement::Let(Let{ name, typ, val })))
|
|
|
|
|
}
|
|
|
|
|
fn parse_constant(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
pub fn parse_constant(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Const), "")?;
|
|
|
|
|
|
|
|
|
|
if let Some(_) = utils::check(tokens, TokenType::Keyword(Keyword::Fn)) {
|
|
|
|
|
@@ -182,7 +180,7 @@ fn parse_constant(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) ->
|
|
|
|
|
Ok(LocBox::new(kw.loc(), Statement::ConstVar(ConstVar { name, typ, val })))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_type_alias(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
|
|
|
|
|
pub fn parse_type_alias(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
|
|
|
|
|
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Type), "")?;
|
|
|
|
|
let name = utils::check_consume_or_err(tokens, TokenType::ident(""), "")?.tt().unwrap_ident();
|
|
|
|
|
_ = utils::check_consume_or_err(tokens, TokenType::Punct(Punctuation::Eq), "")?;
|
|
|
|
|
@@ -192,7 +190,7 @@ fn parse_type_alias(tokens: &mut Vec<Token>) -> Result<LocBox<Statement>> {
|
|
|
|
|
Ok(LocBox::new(kw.loc(), Statement::TypeAlias(TypeAlias { name, typ })))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_fn(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
pub fn parse_fn(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Result<LocBox<Statement>> {
|
|
|
|
|
error!("fnc");
|
|
|
|
|
// Just remove the kw since we checked it before
|
|
|
|
|
let kw = utils::check_consume_or_err(tokens, TokenType::Keyword(Keyword::Fn), "")?;
|
|
|
|
|
@@ -241,7 +239,7 @@ fn parse_fn(tokens: &mut Vec<Token>, cli: &CliArgs, prog: &mut Program) -> Resul
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// usize is: 0 = no self, static; 1 = self, ref; 2 = self, mut ref
|
|
|
|
|
fn parse_fn_params(tokens: &mut Vec<Token>) -> Result<(usize, Vec<(Ident, LocBox<Type>)>)> {
|
|
|
|
|
pub fn parse_fn_params(tokens: &mut Vec<Token>) -> Result<(usize, Vec<(Ident, LocBox<Type>)>)> {
|
|
|
|
|
let mut args = Vec::new();
|
|
|
|
|
|
|
|
|
|
utils::check_consume_or_err(tokens, TokenType::Delim(Delimiter::ParenL), "")?;
|
|
|
|
|
|