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

A61 Fix: Handle Warnings #37

Merged
merged 2 commits into from
Sep 28, 2022
Merged
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
heraclitus-compiler = "1.5.1"
heraclitus-compiler = "1.5.2"
similar-string = "1.4.2"
colored = "2.0.0"
8 changes: 6 additions & 2 deletions src/cli/cli_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ impl CLI {
match self.flags.get_flag("-e").unwrap().value.clone() {
Some(code) => {
match AmberCompiler::new(code, None).compile() {
Ok(code) => AmberCompiler::execute(code, self.flags.get_args()),
Ok((code, messages)) => {
messages.iter().for_each(|m| m.show());
AmberCompiler::execute(code, self.flags.get_args())
},
Err(err) => {
err.show();
std::process::exit(1);
Expand All @@ -64,7 +67,8 @@ impl CLI {
match self.read_file(input.clone()) {
Ok(code) => {
match AmberCompiler::new(code, Some(input)).compile() {
Ok(code) => {
Ok((code, messages)) => {
messages.iter().for_each(|m| m.show());
// Save to the output file
if self.args.len() >= 3 {
Self::save_to_file(self.args[2].clone(), code)
Expand Down
6 changes: 3 additions & 3 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ impl AmberCompiler {
block.translate(&mut meta)
}

pub fn compile(&self) -> Result<String, Message> {
pub fn compile(&self) -> Result<(String, Vec<Message>), Message> {
self.tokenize()
.and_then(|tokens| self.parse(tokens))
.map(|(block, meta)| self.translate(block, meta))
.map(|(block, meta)| (self.translate(block, meta.clone()), meta.messages))
}

pub fn execute(code: String, flags: &[String]) {
Expand All @@ -90,7 +90,7 @@ impl AmberCompiler {

#[allow(dead_code)]
pub fn test_eval(&self) -> Result<String, Message> {
self.compile().map_or_else(Err, |code| {
self.compile().map_or_else(Err, |(code, _)| {
let child = Command::new("/bin/bash")
.arg("-c").arg::<&str>(code.as_ref())
.output().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::statement::stmt::Statement;

#[derive(Debug, Clone)]
pub struct Block {
statements: Vec<Statement>
pub statements: Vec<Statement>
}

impl Block {
Expand Down
11 changes: 10 additions & 1 deletion src/modules/condition/ifcond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ impl IfCondition {
let is_not_if_chain = matches!(statement.value.as_ref().unwrap(), StatementType::IfCondition(_) | StatementType::IfChain(_));
if is_not_if_chain {
// TODO: [A34] Add a comment pointing to the website documentation
return error!(meta, tok, "You should use if-chain instead of nested if else statements")
let message = Message::new_warn_at_token(meta, tok)
.message("You should use if-chain instead of nested if else statements");
meta.add_message(message);
}
Ok(())
}
Expand Down Expand Up @@ -56,7 +58,14 @@ impl SyntaxModule<ParserMetadata> for IfCondition {
match token(meta, "{") {
Ok(_) => {
let mut false_block = Box::new(Block::new());
let tok = meta.get_current_token();
syntax(meta, &mut *false_block)?;
// Check if the statement is using if chain syntax sugar
if false_block.statements.len() == 1 {
if let Some(statement) = false_block.statements.first() {
self.prevent_not_using_if_chain(meta, statement, tok)?;
}
}
self.false_block = Some(false_block);
token(meta, "}")?;
}
Expand Down
10 changes: 8 additions & 2 deletions src/utils/metadata/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub struct ParserMetadata {
pub trace: Vec<PositionInfo>,
pub import_history: ImportHistory,
pub loop_ctx: bool,
pub function_ctx: bool
pub function_ctx: bool,
pub messages: Vec<Message>
}

impl ParserMetadata {
Expand All @@ -25,6 +26,10 @@ impl ParserMetadata {
pub fn pop_trace(&mut self) -> Option<PositionInfo> {
self.trace.pop()
}

pub fn add_message(&mut self, message: Message) {
self.messages.push(message);
}
}

impl Metadata for ParserMetadata {
Expand All @@ -40,7 +45,8 @@ impl Metadata for ParserMetadata {
trace: Vec::new(),
import_history: ImportHistory::new(path),
loop_ctx: false,
function_ctx: false
function_ctx: false,
messages: Vec::new(),
}
}

Expand Down