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

Implements compile time warning for datum[] #397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CONFIGURING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Raised by DreamChecker:
* `control_condition_static` - Raised on a control condition such as `if`/`while` having a static condition such as `1` or `"string"`
* `if_condition_determinate` - Raised on if condition being always true or always false
* `loop_condition_determinate` - Raised on loop condition such as in `for` being always true or always false
* `improper_index` - Raised on accessing a non list with []

Raised by Lexer:

Expand Down
110 changes: 109 additions & 1 deletion crates/builtins-proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use syn::*;
struct Header {
attrs: Vec<Attribute>,
path: Vec<Ident>,
operator_overload_target: Option<String>,
}

impl Header {
Expand All @@ -24,7 +25,110 @@ impl Header {
input.parse::<Token![/]>()?;
self.path.push(Ident::parse_any(input)?);
}
if let Some(final_ident) = self.path.last() {
// If we find an operator{some token}() pattern we allow the some token part
if final_ident == "operator" {
self.parse_operator(input)?;
}
}
Ok(())
}

fn parse_operator(&mut self, input: ParseStream) -> Result<()> {
let text_token: Option<&str> = if input.parse::<Token![%]>().is_ok() {
if input.parse::<Token![%]>().is_ok() {
Some("%%")
} else if input.parse::<Token![%=]>().is_ok() {
Some("%%=")
} else {
Some("%")
}
} else if input.parse::<Token![&]>().is_ok() {
Some("&")
} else if input.parse::<Token![&=]>().is_ok() {
Some("&=")
} else if input.parse::<Token![*]>().is_ok() {
if input.parse::<Token![*]>().is_ok() {
Some("**")
} else {
Some("*")
}
} else if input.parse::<Token![*=]>().is_ok() {
Some("*=")
} else if input.parse::<Token![/]>().is_ok() {
Some("/")
} else if input.parse::<Token![/=]>().is_ok() {
Some("/=")
} else if input.parse::<Token![+]>().is_ok() {
if input.parse::<Token![+]>().is_ok() {
Some("++")
} else {
Some("+")
}
} else if input.parse::<Token![+=]>().is_ok() {
Some("+=")
} else if input.parse::<Token![-]>().is_ok() {
if input.parse::<Token![-]>().is_ok() {
Some("--")
} else {
Some("-")
}
} else if input.parse::<Token![-=]>().is_ok() {
Some("-=")
} else if input.parse::<Token![<]>().is_ok() {
Some("<")
} else if input.parse::<Token![<<]>().is_ok() {
Some("<<")
} else if input.parse::<Token![<<=]>().is_ok() {
Some("<<=")
} else if input.parse::<Token![<=]>().is_ok() {
Some("<=")
} else if input.parse::<Token![>=]>().is_ok() {
Some(">=")
} else if input.parse::<Token![>>]>().is_ok() {
Some(">>")
} else if input.parse::<Token![>>=]>().is_ok() {
Some(">>=")
} else if input.parse::<Token![^]>().is_ok() {
Some("^")
} else if input.parse::<Token![^=]>().is_ok() {
Some("^=")
} else if input.parse::<Token![|]>().is_ok() {
Some("|")
} else if input.parse::<Token![|=]>().is_ok() {
Some("|=")
} else if input.parse::<Token![~]>().is_ok() {
if input.parse::<Token![=]>().is_ok() {
Some("~=")
} else {
Some("~")
}
} else if input.parse::<Token![~]>().is_ok() {
Some("~")
} else if input.peek(Token![:]) && input.peek2(Token![=]) {
input.parse::<Token![:]>()?;
input.parse::<Token![=]>()?;
Some(":=")
} else if self.brackets_next(input).is_ok() {
if input.parse::<Token![=]>().is_ok() {
Some("[]=")
} else {
Some("[]")
}
} else {
// Todo: Implement operator""() support. Unsure how to expect an empty string
None
};
if let Some(text) = text_token {
self.operator_overload_target = Some(text.to_string());
}
Ok(())
}

fn brackets_next(&mut self, input: ParseStream) -> Result<()> {
// Sorry
let _bracket_dummy;
bracketed!(_bracket_dummy in input);
Ok(())
}
}
Expand Down Expand Up @@ -143,7 +247,11 @@ pub fn builtins_table(input: TokenStream) -> TokenStream {
let mut output = Vec::new();
for entry in builtins {
let span = entry.header.path.first().unwrap().span();
let lit_strs: Vec<_> = entry.header.path.into_iter().map(|x| LitStr::new(&x.to_string(), x.span())).collect();
let mut lit_strs: Vec<_> = entry.header.path.into_iter().map(|x| LitStr::new(&x.to_string(), x.span())).collect();
if let Some(operator) = entry.header.operator_overload_target {
let last_entry = lit_strs.pop().unwrap();
lit_strs.push(LitStr::new((last_entry.value() + operator.as_str()).as_str(), last_entry.span()));
}
let path = quote! {
&[ #(#lit_strs),* ]
};
Expand Down
8 changes: 8 additions & 0 deletions crates/dreamchecker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,14 @@ impl<'o, 's> AnalyzeProc<'o, 's> {
}
res
},
StaticType::Type( typeref ) => {
if !typeref.path.starts_with("/list") && typeref.get_proc("operator[]").is_none() {
error(location, format!("invalid list access on {}", typeref.path))
.with_errortype("improper_index")
.register(self.context);
}
lhs.clone()
},
_ => lhs.clone() // carry through fix_hint
}
},
Expand Down
1 change: 1 addition & 0 deletions crates/dreammaker/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ pub fn register_builtins(tree: &mut ObjectTreeBuilder) {
savefile/var/list/dir;
savefile/var/eof;
savefile/var/name;
savefile/proc/operator[]();
savefile/proc/ExportText(/* path=cd, file */);
savefile/proc/Flush();
savefile/proc/ImportText(/* path=cd, file */);
Expand Down