Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format improvements (WIP) #63

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ast/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ impl ToMeta for f32 {
fn to_meta(&self) -> Meta { Meta::Scalar(sp!((*self).into())) }
}
impl ToMeta for bool {
fn to_meta(&self) -> Meta { Meta::Scalar(sp!(ast::Expr::LitInt { value: *self as i32, radix: ast::IntRadix::Bool })) }
fn to_meta(&self) -> Meta { Meta::Scalar(sp!(ast::Expr::LitInt { value: *self as i32, format: ast::IntFormat { unsigned: true, radix: ast::IntRadix::Bool } })) }
}
impl ToMeta for String {
fn to_meta(&self) -> Meta { Meta::Scalar(sp!(self.to_owned().into())) }
Expand Down
16 changes: 10 additions & 6 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ pub enum Expr {
value: raw::LangInt,
/// A hint to the formatter on how it should write the integer.
/// (may not necessarily represent the original radix of a parsed token)
radix: IntRadix,
format: IntFormat,
},
LitFloat { value: raw::LangFloat },
LitString(LitString),
Expand All @@ -469,14 +469,18 @@ pub enum Expr {
},
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct IntFormat {
pub unsigned: bool,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might prefer signed rather than unsigned to avoid double negatives

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with unsigned since it's more of an exception to the norm than anything. I tend to structure my bools with true as the "do something different" value so that they don't need to be negated whenever being read, but I guess that doesn't matter as much with match since you have to be explicit in both cases.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true as "do something different" makes sense in C-likes where it's easy to zero-initialize things. It doesn't make so much sense in rust.

pub radix: IntRadix,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum IntRadix {
/// Display as decimal.
Dec,
/// Display as hexadecimal, with an `0x` prefix.
Hex,
/// Display as potentially negative hexadecimal, with an `0x` prefix.
SignedHex,
/// Display as binary, with an `0b` prefix.
Bin,
/// Use `true` and `false` if the value is `1` or `0`. Otherwise, fall back to decimal.
Expand Down Expand Up @@ -842,7 +846,7 @@ string_enum! {
}

impl From<raw::LangInt> for Expr {
fn from(value: raw::LangInt) -> Expr { Expr::LitInt { value, radix: IntRadix::Dec } }
fn from(value: raw::LangInt) -> Expr { Expr::LitInt { value, format: IntFormat { unsigned: false, radix: IntRadix::Dec } } }
}
impl From<raw::LangFloat> for Expr {
fn from(value: raw::LangFloat) -> Expr { Expr::LitFloat { value } }
Expand Down Expand Up @@ -1257,7 +1261,7 @@ macro_rules! generate_visitor_stuff {
Expr::XcrementOp { op: _, order: _, var } => {
v.visit_var(var);
},
Expr::LitInt { value: _, radix: _ } => {},
Expr::LitInt { value: _, format: _ } => {},
Expr::LitFloat { value: _ } => {},
Expr::LitString(_s) => {},
Expr::LabelProperty { .. } => {},
Expand Down
22 changes: 3 additions & 19 deletions src/context/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,34 +1114,18 @@ impl Signature {
signature_from_func_ast(ty_keyword, params)
}

pub(crate) fn validate(&self, ctx: &CompilerContext) -> Result<(), ErrorReported> {
self._check_non_optional_after_optional(ctx)
}

fn _check_non_optional_after_optional(&self, ctx: &CompilerContext) -> Result<(), ErrorReported> {
let mut first_optional = None;
for param in self.params.iter() {
if param.default.is_some() {
first_optional = Some(param);
} else if let Some(optional) = first_optional {
return Err(ctx.emitter.emit(error!(
message("invalid function signature"),
secondary(optional.useful_span, "optional parameter"),
primary(param.useful_span, "non-optional parameter after optional"),
)));
}
}
pub(crate) fn validate(&self, _ctx: &CompilerContext) -> Result<(), ErrorReported> {
Ok(())
}

/// Minimum number of arguments accepted.
pub fn min_args(&self) -> usize {
self.params.iter().take_while(|param| param.default.is_none()).count()
self.params.iter().fold(0, |count, param| count + param.default.is_none() as usize)
}

/// Maximum number of arguments accepted.
pub fn max_args(&self) -> usize {
self.params.len()
self.min_args()
}

/// Matches arguments at a call site to their corresponding parameters.
Expand Down
Loading