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

Add constants #148

Merged
merged 6 commits into from
Aug 30, 2024
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
122 changes: 55 additions & 67 deletions core/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::fmt::Write;
use std::iter::{Iterator, Peekable};

use crate::env::LuaVersion;
use crate::{
env::{ContinueMode, Options},
format_clue,
Expand Down Expand Up @@ -102,7 +103,8 @@ impl<'a> Compiler<'a> {
args: FunctionArgs,
code: CodeBlock,
) -> Result<(String, String), String> {
let mut code = self.compile_code_block(scope + self.options.env_debug as usize, "", code)?;
let mut code =
self.compile_code_block(scope + self.options.env_debug as usize, "", code)?;
let args = self.compile_list(args, ", ", &mut |(arg, default)| {
if let Some((default, line)) = default {
let default = self.compile_expression(scope + 2, default)?;
Expand Down Expand Up @@ -357,6 +359,7 @@ impl<'a> Compiler<'a> {
SYMBOL(lexeme) => lexeme,
VARIABLE {
local,
r#const,
names,
values,
line,
Expand Down Expand Up @@ -387,14 +390,28 @@ impl<'a> Compiler<'a> {
result
} else {
let end = self.indentate_if(ctokens, scope);
let pre = if local { "local " } else { "" };
let post = if r#const
&& self
.options
.env_target
.is_some_and(|lua| lua == LuaVersion::Lua54)
{
" <const> "
} else {
""
};
let pre = if local || !post.is_empty() {
"local "
} else {
""
};
if values.is_empty() {
let ident = self.compile_identifiers(names)?;
format_clue!(debug, pre, ident, ";", line, end)
} else {
let values = self.compile_expressions(scope, values)?;
let names = self.compile_identifiers(names)?;
format_clue!(debug, pre, names, " = ", values, ";", line, end)
format_clue!(debug, pre, names, post, " = ", values, ";", line, end)
}
}
}
Expand Down Expand Up @@ -489,72 +506,47 @@ impl<'a> Compiler<'a> {
let expr = self.compile_expression(scope, expr)?;
Ok(format_clue!("(", name, " == ", expr, ") "))
})?;
format_clue!("if ", if let Some(extraif) = extraif {
condition.pop();
let extraif = self.compile_expression(scope, extraif)?;
if empty {
extraif + " "
format_clue!(
"if ",
if let Some(extraif) = extraif {
condition.pop();
let extraif = self.compile_expression(scope, extraif)?;
if empty {
extraif + " "
} else {
format_clue!("(", condition, ") and ", extraif, " ")
}
} else {
format_clue!("(", condition, ") and ", extraif, " ")
}
} else {
condition
}, "then")
};
let end = if i >= last {
"end"
} else {
"else"
condition
},
"then"
)
};
let end = if i >= last { "end" } else { "else" };
let pre = self.indentate(scope + i);
let internal_code = if internal_expr.is_empty() {
None
} else {
Some(self.compile_tokens(scope + i, internal_expr)?)
};
let code = if i == 0 {
let code = self.compile_code_block(
scope,
&condition,
code,
)? + end;
let code = self.compile_code_block(scope, &condition, code)? + end;
if let Some(internal_code) = internal_code {
format_clue!(
internal_code,
'\n',
pre,
code
)
format_clue!(internal_code, '\n', pre, code)
} else {
code
}
} else if default {
self.compile_code_block(
scope + i - 1,
"",
code,
)? + end
self.compile_code_block(scope + i - 1, "", code)? + end
} else {
let code = self.compile_code_block(
scope + i,
&condition,
code,
)?;
let mut code = format_clue!(
'\n',
pre,
code,
end
);
let code = self.compile_code_block(scope + i, &condition, code)?;
let mut code = format_clue!('\n', pre, code, end);
if let Some(internal_code) = internal_code {
code = format_clue!(
'\n',
internal_code,
code
)
code = format_clue!('\n', internal_code, code)
}
if i >= last {
code += &format_clue!('\n', self.indentate(scope + i - 1), "end");
code +=
&format_clue!('\n', self.indentate(scope + i - 1), "end");
}
code
};
Expand All @@ -575,7 +567,11 @@ impl<'a> Compiler<'a> {
debug, "local ", name, " = ", value, ';', line, '\n', branches, end
)
}
WHILE_LOOP { condition, code, line } => {
WHILE_LOOP {
condition,
code,
line,
} => {
let condition = self.compile_expression(scope, condition)?;
let debug = self.compile_debug_line(line, scope, true);
let code = self.compile_code_block(scope, "do", code)?;
Expand All @@ -590,7 +586,11 @@ impl<'a> Compiler<'a> {
self.indentate_if(ctokens, scope)
)
}
LOOP_UNTIL { condition, code, line } => {
LOOP_UNTIL {
condition,
code,
line,
} => {
let condition = self.compile_expression(scope, condition)?;
let debug = self.compile_debug_line(line, scope, true);
let code = self.compile_code_block(scope, "", code)?;
Expand Down Expand Up @@ -618,20 +618,8 @@ impl<'a> Compiler<'a> {
let code = self.compile_code_block(scope, "do", code)?;
let end = self.indentate_if(ctokens, scope);
format_clue!(
debug,
"for ",
iterator,
" = ",
start,
", ",
endexpr,
", ",
alter,
" ",
code,
debug,
"end",
end
debug, "for ", iterator, " = ", start, ", ", endexpr, ", ", alter, " ",
code, debug, "end", end
)
}
FOR_FUNC_LOOP {
Expand Down
Loading