From d082a9eb2fcd3e9d3236ed3026e405df9ab0a5d8 Mon Sep 17 00:00:00 2001 From: Spu7Nix Date: Wed, 6 Jan 2021 22:51:43 +0100 Subject: [PATCH] bugfixies --- spwn-lang/libraries/std/control_flow.spwn | 1 + spwn-lang/src/fmt.rs | 2 +- spwn-lang/src/main.rs | 24 ++++--- spwn-lang/src/parser.rs | 23 +++---- spwn-lang/test/test.spwn | 77 ++++++++++++++++++++++- 5 files changed, 101 insertions(+), 26 deletions(-) diff --git a/spwn-lang/libraries/std/control_flow.spwn b/spwn-lang/libraries/std/control_flow.spwn index 41c9e288..77a428fb 100644 --- a/spwn-lang/libraries/std/control_flow.spwn +++ b/spwn-lang/libraries/std/control_flow.spwn @@ -77,6 +77,7 @@ call_with_delay = #[desc("Call a function after a delay")] ( i.item.if_is(LARGER_THAN, range.end - 1, !{ if reset { + wait() i.reset(reset_speed) } -> return diff --git a/spwn-lang/src/fmt.rs b/spwn-lang/src/fmt.rs index 70eb58d3..b7bd2a4e 100644 --- a/spwn-lang/src/fmt.rs +++ b/spwn-lang/src/fmt.rs @@ -266,7 +266,7 @@ impl SpwnFmt for ValueBody { TypeIndicator(x) => format!("@{}", x), Null => "null".to_string(), SelfVal => "self".to_string(), - Switch(_, _) => "switch".to_string() + Switch(_, _) => "switch".to_string(), } } } diff --git a/spwn-lang/src/main.rs b/spwn-lang/src/main.rs index af6bccf5..70c17d0d 100644 --- a/spwn-lang/src/main.rs +++ b/spwn-lang/src/main.rs @@ -33,11 +33,8 @@ const ERROR_EXIT_CODE: i32 = 1; use std::io::Write; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; - const HELP: &str = include_str!("../help.txt"); - - fn print_with_color(text: &str, color: Color) { let mut stdout = StandardStream::stdout(ColorChoice::Always); stdout @@ -81,23 +78,27 @@ fn main() -> Result<(), Box> { let mut live_editor = false; let mut save_file = None; - let mut included_paths = vec![std::env::current_dir().expect("Cannot access current directory")]; + let mut included_paths = vec![ + std::env::current_dir().expect("Cannot access current directory"), + std::env::current_exe().expect("Cannot access directory of executable"), + ]; //change to current_exe before release (from current_dir) - while let Some(arg) = args_iter.next() { match arg.as_ref() { "--console-output" | "-c" => gd_enabled = false, "--no-level" | "-l" => { gd_enabled = false; compile_only = true; - }, + } "--no-optimize" | "-o" => opti_enabled = false, "--level-name" | "-n" => level_name = args_iter.next().cloned(), "--live-editor" | "-e" => live_editor = true, "--save-file" | "-s" => save_file = args_iter.next().cloned(), "--included-path" | "-i" => included_paths.push({ - let path = PathBuf::from(args_iter.next().cloned().expect("No path provided")); + let path = PathBuf::from( + args_iter.next().cloned().expect("No path provided"), + ); if path.exists() { path } else { @@ -132,9 +133,9 @@ fn main() -> Result<(), Box> { } let gd_path = if gd_enabled { - Some( if save_file != None{ + Some(if save_file != None { PathBuf::from(save_file.expect("what")) - }else if cfg!(target_os = "windows") { + } else if cfg!(target_os = "windows") { PathBuf::from(std::env::var("localappdata").expect("No local app data")) .join("GeometryDash/CCLocalLevels.dat") } else if cfg!(target_os = "macos") { @@ -231,10 +232,7 @@ fn main() -> Result<(), Box> { std::process::exit(ERROR_EXIT_CODE); } Ok(_) => { - print_with_color( - "Pasted into the editor!", - Color::Green, - ); + print_with_color("Pasted into the editor!", Color::Green); } } } else { diff --git a/spwn-lang/src/parser.rs b/spwn-lang/src/parser.rs index d32fd1a2..394cd587 100644 --- a/spwn-lang/src/parser.rs +++ b/spwn-lang/src/parser.rs @@ -1066,19 +1066,9 @@ fn fix_precedence(mut expr: ast::Expression) -> ast::Expression { values: Vec::new(), }; - for (i, op) in expr.operators.iter().enumerate() { + for (i, op) in expr.operators.iter().enumerate().rev() { if operator_precedence(op) == lowest { new_expr.operators.push(*op); - new_expr.values.push(if i == 0 { - expr.values[0].clone() - } else { - fix_precedence(ast::Expression { - operators: expr.operators[..i].to_vec(), - values: expr.values[..(i + 1)].to_vec(), - }) - .to_variable() - }); - new_expr.values.push(if i == expr.operators.len() - 1 { expr.values.last().unwrap().clone() } else { @@ -1090,10 +1080,21 @@ fn fix_precedence(mut expr: ast::Expression) -> ast::Expression { }) .to_variable() }); + new_expr.values.push(if i == 0 { + expr.values[0].clone() + } else { + fix_precedence(ast::Expression { + operators: expr.operators[..i].to_vec(), + values: expr.values[..(i + 1)].to_vec(), + }) + .to_variable() + }); break; } } + new_expr.operators.reverse(); + new_expr.values.reverse(); new_expr } diff --git a/spwn-lang/test/test.spwn b/spwn-lang/test/test.spwn index c7b0d782..6a9e56de 100644 --- a/spwn-lang/test/test.spwn +++ b/spwn-lang/test/test.spwn @@ -1,4 +1,79 @@ +extract obj_props; +type @binaryCalculator; -$.print(@string(1000)) +impl @binaryCalculator { + new: (bits: @number, x: @number, y: @number) { + let bitCounters = []; + + for i in ..bits { + item = ?i; + + $.add(obj { + OBJ_ID: 1615, + X: x + i * 30, + Y: y, + ITEM: item, + GROUPS: 999g + }); + + bitCounters.push(counter(item)); + } + + return { + bitCounters: bitCounters, + type: @binaryCalculator + } + }, + binaryToDecimal: (self, target: @counter) { + target = 0; + wait(1); + + for i in ..self.bitCounters.length { + if self.bitCounters[self.bitCounters.length - 1 - i] as @bool { + target += 2 ^ i; + } + } + }, + decimalToBinary: (self, source: @counter, calcSpeed: @number = 5) { + let tempcounters = []; + + for i in ..self.bitCounters.length { + tempcounters.push(counter()); + } + + source.copy_to(tempcounters, speed = 10); + + for i in ..self.bitCounters.length { + -> () { + tempcounters[i].divide(2 ^ i, speed = calcSpeed); + tempcounters[i].divide(2, remainder = self.bitCounters[self.bitCounters.length - 1 - i], speed = calcSpeed); + }(); + } + }, + prettyDecimalToBinary: (self, source: @counter, calcSpeed: @number = 10) { + + for i in ..self.bitCounters.length { + source.divide(2, remainder = self.bitCounters[self.bitCounters.length - 1 - i], speed = calcSpeed); + } + + self.binaryToDecimal(source); + } +} + +decimal = ?i; +decimalCounter = counter(decimal); +calculator = @binaryCalculator::new(12, 300, 300); + +$.add(obj { + OBJ_ID: 1615, + X: 300, + Y: 330, + ITEM: decimal, + GROUPS: 999g +}); + +decimalCounter += 1234; +wait(1); +calculator.decimalToBinary(decimalCounter); \ No newline at end of file