mirror of
				https://github.com/rm-dr/daisy
				synced 2025-10-30 14:04:47 -07:00 
			
		
		
		
	Compare commits
	
		
			6 Commits
		
	
	
		
			newfloat
			...
			599c9742d2
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 599c9742d2 | |||
| 9f9cc5d084 | |||
| c8ebec59ae | |||
| fde1220a96 | |||
| 2f1f8a0801 | |||
| d906c474c5 | 
							
								
								
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @ -22,7 +22,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" | |||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "daisycalc" | name = "daisycalc" | ||||||
| version = "1.0.1" | version = "1.1.0" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "cfg-if", |  "cfg-if", | ||||||
|  "num", |  "num", | ||||||
|  | |||||||
							
								
								
									
										14
									
								
								Cargo.toml
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								Cargo.toml
									
									
									
									
									
								
							| @ -1,6 +1,6 @@ | |||||||
| [package] | [package] | ||||||
| name = "daisycalc" | name = "daisycalc" | ||||||
| version = "1.0.1" | version = "1.1.0" | ||||||
| edition = "2021" | edition = "2021" | ||||||
| build = "buildscript/main.rs" | build = "buildscript/main.rs" | ||||||
| license = "GPL-3.0-only" | license = "GPL-3.0-only" | ||||||
| @ -13,6 +13,10 @@ readme = "README.md" | |||||||
| name = "daisy" | name = "daisy" | ||||||
| path = "src/main.rs" | path = "src/main.rs" | ||||||
|  |  | ||||||
|  | [lib] | ||||||
|  | name = "daisycalc" | ||||||
|  | path = "src/lib.rs" | ||||||
|  |  | ||||||
| [profile.release] | [profile.release] | ||||||
| opt-level = 3 | opt-level = 3 | ||||||
| debug = 0 | debug = 0 | ||||||
| @ -25,11 +29,13 @@ panic = "abort" | |||||||
|  |  | ||||||
| [dependencies] | [dependencies] | ||||||
| cfg-if = "1.0.0" | cfg-if = "1.0.0" | ||||||
|  |  | ||||||
| [target.'cfg(target_family = "unix")'.dependencies] |  | ||||||
| termion = "2.0.1" |  | ||||||
| num = "0.4.1" | num = "0.4.1" | ||||||
| #astro-float = "0.7.1" | #astro-float = "0.7.1" | ||||||
|  |  | ||||||
|  |  | ||||||
|  | [target.'cfg(target_family = "unix")'.dependencies] | ||||||
|  | termion = "2.0.1" | ||||||
|  |  | ||||||
|  |  | ||||||
| [build-dependencies] | [build-dependencies] | ||||||
| toml = "0.7.4" | toml = "0.7.4" | ||||||
| @ -16,6 +16,7 @@ pub fn is_command( | |||||||
| 		| "vars" | 		| "vars" | ||||||
| 		| "consts" | "constants" | 		| "consts" | "constants" | ||||||
| 		| "del" | "delete" | 		| "del" | "delete" | ||||||
|  | 		| "flags" | ||||||
| 		=> true, | 		=> true, | ||||||
| 		_ => false | 		_ => false | ||||||
| 	} | 	} | ||||||
| @ -81,6 +82,27 @@ pub fn do_command( | |||||||
| 			return t; | 			return t; | ||||||
| 		}, | 		}, | ||||||
|  |  | ||||||
|  | 		"flags" => { | ||||||
|  | 			return FormattedText::new( | ||||||
|  | 				concat!( | ||||||
|  | 					"\n", | ||||||
|  | 					"A list of command-line arguments is below\n", | ||||||
|  | 					"\n", | ||||||
|  | 					"╞════ [t]Flag[n] ════╪════════════════ [t]Function[n] ════════════════╡\n", | ||||||
|  | 					"  [c]--help[n]        Show help\n", | ||||||
|  | 					"  [c]--version[n]     Show version\n", | ||||||
|  | 					"  [c]--info[n]        Show system information\n", | ||||||
|  | 					"  [c]--256color[n]    Use full color support (default)\n", | ||||||
|  | 					"  [c]--8color[n]      Use reduced colors (ANSI, no styling)\n", | ||||||
|  | 					"  [c]--nocolor[n]     Do not use colors\n", | ||||||
|  | 					"  [c]--nosub[n]       Disable inline substitution\n", | ||||||
|  | 					"  [c]--nosuper[n]     Disable superscript powers\n", | ||||||
|  | 					"  [c]--nooneover[n]   Disable \"one-over\" fractions as -1 power\n", | ||||||
|  | 					"\n\n" | ||||||
|  | 				).to_string() | ||||||
|  | 			); | ||||||
|  | 		}, | ||||||
|  |  | ||||||
| 		"clear" => { | 		"clear" => { | ||||||
| 			return FormattedText::new("[clear]".to_string()); | 			return FormattedText::new("[clear]".to_string()); | ||||||
| 		}, | 		}, | ||||||
|  | |||||||
| @ -1,13 +0,0 @@ | |||||||
|  |  | ||||||
|  |  | ||||||
| // Select main script for target system |  | ||||||
| cfg_if::cfg_if! { |  | ||||||
| 	if #[cfg(target_family = "unix")] { |  | ||||||
| 		mod unix; |  | ||||||
| 		pub use unix::main as main_e; |  | ||||||
| 	} else { |  | ||||||
| 		pub fn main_e() -> Result<(), std::io::Error> { |  | ||||||
| 			unimplemented!("Not yet implemented."); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| @ -1,3 +0,0 @@ | |||||||
| mod unix; |  | ||||||
| mod promptbuffer; |  | ||||||
| pub use self::unix::main; |  | ||||||
| @ -1,115 +0,0 @@ | |||||||
| use std::io::stdout; |  | ||||||
| use std::io::stdin; |  | ||||||
| use std::env; |  | ||||||
|  |  | ||||||
| use termion::{ |  | ||||||
| 	event::Key, |  | ||||||
| 	input::TermRead, |  | ||||||
| 	raw::IntoRawMode, |  | ||||||
| 	color::DetectColors |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| use super::promptbuffer::PromptBuffer; |  | ||||||
| use crate::command; |  | ||||||
| use crate::context::Context; |  | ||||||
| use crate::FormattedText; |  | ||||||
|  |  | ||||||
| #[inline(always)] |  | ||||||
| pub fn main() -> Result<(), std::io::Error> { |  | ||||||
| 	let mut stdout = stdout().into_raw_mode().unwrap(); |  | ||||||
| 	let mut pb: PromptBuffer = PromptBuffer::new(64); |  | ||||||
| 	let mut context = Context::new(); |  | ||||||
|  |  | ||||||
| 	// Set color compatibilty |  | ||||||
| 	let term_colors = stdout.available_colors().unwrap_or(0); |  | ||||||
| 	if term_colors >= 256 { |  | ||||||
| 		context.config.term_color_type = 2; |  | ||||||
| 	} else if term_colors >= 8 { |  | ||||||
| 		context.config.term_color_type = 1; |  | ||||||
| 	} else { |  | ||||||
| 		context.config.term_color_type = 0; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	context.config.check(); |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 	// Handle command-line arguments |  | ||||||
| 	let args: Vec<String> = env::args().collect(); |  | ||||||
| 	if args.iter().any(|s| s == "--help") { |  | ||||||
| 		let t = command::do_command(&mut context, &String::from("help")); |  | ||||||
| 		t.write(&context, &mut stdout)?; |  | ||||||
| 		return Ok(()); |  | ||||||
| 	} else if args.iter().any(|s| s == "--version") { |  | ||||||
| 		let t = FormattedText::new(format!( |  | ||||||
| 			"Daisy v{}\n", env!("CARGO_PKG_VERSION") |  | ||||||
| 		)); |  | ||||||
| 		t.write(&context, &mut stdout)?; |  | ||||||
| 		return Ok(()); |  | ||||||
| 	} else if args.iter().any(|s| s == "--debug") { |  | ||||||
| 		let t = FormattedText::new(format!( |  | ||||||
| 			concat!( |  | ||||||
| 				"Daisy v{}\n", |  | ||||||
| 				"Your terminal supports {} colors.\n" |  | ||||||
| 			), |  | ||||||
| 			env!("CARGO_PKG_VERSION"), |  | ||||||
| 			term_colors |  | ||||||
| 		)); |  | ||||||
| 		t.write(&context, &mut stdout)?; |  | ||||||
| 		return Ok(()); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	'outer: loop { |  | ||||||
|  |  | ||||||
| 		pb.write_prompt(&mut context, &mut stdout)?; |  | ||||||
|  |  | ||||||
| 		let stdin = stdin(); |  | ||||||
| 		for c in stdin.keys() { |  | ||||||
| 			if let Key::Char(q) = c.as_ref().unwrap() { |  | ||||||
| 				match q { |  | ||||||
| 					'\n' => { |  | ||||||
| 						// Print again without cursor, in case we pressed enter |  | ||||||
| 						// while inside a substitution |  | ||||||
| 						pb.write_prompt_nocursor(&mut context, &mut stdout)?; |  | ||||||
| 						let in_str = pb.enter(); |  | ||||||
| 						FormattedText::newline(&mut stdout)?; |  | ||||||
| 						if in_str == "" { break; } |  | ||||||
|  |  | ||||||
| 						if in_str.trim() == "quit" { |  | ||||||
| 							break 'outer; |  | ||||||
| 						} else { |  | ||||||
| 							let r = crate::do_string(&mut context, &in_str); |  | ||||||
|  |  | ||||||
| 							match r { |  | ||||||
| 								Ok(t) | Err(t) => { |  | ||||||
| 									t.write(&context, &mut stdout).unwrap(); |  | ||||||
| 								} |  | ||||||
| 							} |  | ||||||
| 						} |  | ||||||
|  |  | ||||||
| 						break; |  | ||||||
| 					}, |  | ||||||
| 					_ => { pb.add_char(*q); } |  | ||||||
| 				}; |  | ||||||
| 			} else { |  | ||||||
| 				match c.unwrap() { |  | ||||||
| 					Key::Backspace => { pb.backspace(); }, |  | ||||||
| 					Key::Delete => { pb.delete(); }, |  | ||||||
| 					Key::Left => { pb.cursor_left(); }, |  | ||||||
| 					Key::Right => { pb.cursor_right(); }, |  | ||||||
| 					Key::Up => { pb.hist_up(); }, |  | ||||||
| 					Key::Down => { pb.hist_down(); }, |  | ||||||
|  |  | ||||||
| 					Key::Ctrl('d') | |  | ||||||
| 					Key::Ctrl('c') => { break 'outer; }, |  | ||||||
| 					_ => {} |  | ||||||
| 				}; |  | ||||||
| 			}; |  | ||||||
|  |  | ||||||
| 			pb.write_prompt(&mut context, &mut stdout)?; |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	FormattedText::newline(&mut stdout)?; |  | ||||||
| 	return Ok(()); |  | ||||||
| } |  | ||||||
| @ -102,6 +102,15 @@ fn format_map_full(c: char) -> Option<String> { | |||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | impl FormattedText { | ||||||
|  | 	pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> { | ||||||
|  | 		write!(stdout, "\n")?; | ||||||
|  | 		return Ok(()); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	pub fn format_map(c: char, context: &Context) -> Option<String> { | 	pub fn format_map(c: char, context: &Context) -> Option<String> { | ||||||
| 		match context.config.term_color_type { | 		match context.config.term_color_type { | ||||||
| 			0 => format_map_none(c), | 			0 => format_map_none(c), | ||||||
| @ -110,13 +119,6 @@ pub fn format_map(c: char, context: &Context) -> Option<String> { | |||||||
| 			_ => unreachable!("Invalid term_color_type") | 			_ => unreachable!("Invalid term_color_type") | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
| impl FormattedText { |  | ||||||
| 	pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> { |  | ||||||
| 		write!(stdout, "\n")?; |  | ||||||
| 		return Ok(()); |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| @ -161,7 +163,7 @@ impl FormattedText { | |||||||
| 					match (a, b) { | 					match (a, b) { | ||||||
| 						(c, ']') => { // Normal text | 						(c, ']') => { // Normal text | ||||||
|  |  | ||||||
| 							let q = format_map(c, context); | 							let q = Self::format_map(c, context); | ||||||
|  |  | ||||||
| 							if q.is_some() { | 							if q.is_some() { | ||||||
| 								s.push_str(&q.unwrap()); | 								s.push_str(&q.unwrap()); | ||||||
|  | |||||||
							
								
								
									
										283
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										283
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,283 @@ | |||||||
|  | pub mod parser; | ||||||
|  | pub mod command; | ||||||
|  | pub mod quantity; | ||||||
|  |  | ||||||
|  | use crate::parser::substitute; | ||||||
|  | use crate::parser::LineLocation; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | mod context; | ||||||
|  | mod formattedtext; | ||||||
|  | mod errors; | ||||||
|  | mod evaluate; | ||||||
|  |  | ||||||
|  | pub use crate::formattedtext::FormattedText; | ||||||
|  | pub use crate::context::Context; | ||||||
|  | pub use crate::errors::DaisyError; | ||||||
|  | pub use crate::evaluate::evaluate; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #[inline(always)] | ||||||
|  | pub fn do_string( | ||||||
|  | 	context: &mut Context, | ||||||
|  | 	s: &String | ||||||
|  | ) -> Result<FormattedText, FormattedText> { | ||||||
|  |  | ||||||
|  | 	let r: (LineLocation, DaisyError); | ||||||
|  | 	if command::is_command(s) { | ||||||
|  | 		return Ok(command::do_command(context, s)); | ||||||
|  | 	} else if s.contains("=") { | ||||||
|  | 		let x = do_assignment(context, s); | ||||||
|  | 		match x { | ||||||
|  | 			Ok(t) => { return Ok(t) }, | ||||||
|  | 			Err(t) => { r = t } | ||||||
|  | 		}; | ||||||
|  | 	} else { | ||||||
|  | 		let x = do_expression(context, s); | ||||||
|  | 		match x { | ||||||
|  | 			Ok((t, e)) => { context.push_hist(e); return Ok(t) }, | ||||||
|  | 			Err(t) => { r = t } | ||||||
|  | 		}; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	let (l, e) = r; | ||||||
|  | 	let mut t = FormattedText::new("".to_string()); | ||||||
|  | 	if l.zero() { | ||||||
|  | 		t.push(&format!( | ||||||
|  | 			"\n  {}\n\n", | ||||||
|  | 			e.text().to_string(), | ||||||
|  | 		)); | ||||||
|  | 	} else { | ||||||
|  | 		t.push(&format!( | ||||||
|  | 			concat!( | ||||||
|  | 				"{}[e]{}[n]\n", | ||||||
|  | 				"  {}\n\n" | ||||||
|  | 			), | ||||||
|  | 			" ".repeat(l.pos + 4), | ||||||
|  | 			"^".repeat(l.len), | ||||||
|  | 			e.text().to_string(), | ||||||
|  | 		)); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return Err(t); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Handle a simple evaluation string. | ||||||
|  | // Returns a FormattedText with output that should be printed. | ||||||
|  | #[inline(always)] | ||||||
|  | fn do_expression( | ||||||
|  | 	context: &mut Context, | ||||||
|  | 	s: &String | ||||||
|  | ) -> Result<(FormattedText, parser::Expression), (LineLocation, DaisyError)> { | ||||||
|  |  | ||||||
|  | 	let mut output = FormattedText::new("".to_string()); | ||||||
|  |  | ||||||
|  | 	let g = parser::parse(context, &s)?; | ||||||
|  | 	let g_evaluated = evaluate::evaluate(context, &g)?; | ||||||
|  |  | ||||||
|  | 	// Display parsed string | ||||||
|  | 	output.push(&format!( | ||||||
|  | 		" [s]=>[n] {}\n\n", | ||||||
|  | 		g.display(context) | ||||||
|  | 	)); | ||||||
|  |  | ||||||
|  | 	// Display result | ||||||
|  | 	output.push(&format!( | ||||||
|  | 		"  [r]=[n] {}\n\n", | ||||||
|  | 		g_evaluated.display_outer(context), | ||||||
|  | 	)); | ||||||
|  |  | ||||||
|  | 	return Ok((output, g_evaluated)); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | // Handle a variable or function definition string. | ||||||
|  | // Returns a FormattedText with output that should be printed. | ||||||
|  | #[inline(always)] | ||||||
|  | fn do_assignment( | ||||||
|  | 	context: &mut Context, | ||||||
|  | 	s: &String | ||||||
|  | ) -> Result<FormattedText, (LineLocation, DaisyError)> { | ||||||
|  |  | ||||||
|  | 	let mut output = FormattedText::new("".to_string()); | ||||||
|  |  | ||||||
|  | 	let parts = s.split("=").collect::<Vec<&str>>(); | ||||||
|  | 	if parts.len() != 2 { | ||||||
|  | 		return Err(( | ||||||
|  | 			LineLocation::new_zero(), | ||||||
|  | 			DaisyError::Syntax | ||||||
|  | 		)); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Index of first non-whitespace character in left | ||||||
|  | 	// (relative to whole prompt) | ||||||
|  | 	let starting_left = parts[0] | ||||||
|  | 		.char_indices() | ||||||
|  | 		.find(|(_, ch)| !(ch.is_whitespace() && *ch != '\n')) | ||||||
|  | 		.map(|(i, _)| i) | ||||||
|  | 		.unwrap_or_else(|| parts[0].len()); | ||||||
|  |  | ||||||
|  | 	// Index of first non-whitespace character in right | ||||||
|  | 	// (relative to whole prompt) | ||||||
|  | 	// +1 accounts for equals sign | ||||||
|  | 	let starting_right = parts[0].chars().count() + 1 + | ||||||
|  | 		parts[1] | ||||||
|  | 			.char_indices() | ||||||
|  | 			.find(|(_, ch)| !(ch.is_whitespace() && *ch != '\n')) | ||||||
|  | 			.map(|(i, _)| i) | ||||||
|  | 			.unwrap_or_else(|| parts[0].len()); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	let left = substitute(context, &parts[0].trim().to_string()); | ||||||
|  | 	let right = substitute(context, &parts[1].trim().to_string()); | ||||||
|  | 	let is_function = left.contains("("); | ||||||
|  |  | ||||||
|  | 	// The order of methods below is a bit odd. | ||||||
|  | 	// This is intentional, since we want to check a definition's | ||||||
|  | 	// variable name before even attempting to parse its content. | ||||||
|  | 	if is_function { | ||||||
|  | 		let mut mode = 0; | ||||||
|  | 		let mut name = String::new(); | ||||||
|  | 		let mut args = String::new(); | ||||||
|  | 		for c in left.chars() { | ||||||
|  | 			match mode { | ||||||
|  |  | ||||||
|  | 				// Mode 0: reading function name | ||||||
|  | 				0 => { | ||||||
|  | 					if c == '(' { | ||||||
|  | 						mode = 1; continue; | ||||||
|  | 					} else { name.push(c); } | ||||||
|  | 				}, | ||||||
|  |  | ||||||
|  | 				// Mode 1: reading arguments | ||||||
|  | 				1 => { | ||||||
|  | 					if c == ')' { | ||||||
|  | 						mode = 2; continue; | ||||||
|  | 					} else { args.push(c); } | ||||||
|  | 				}, | ||||||
|  |  | ||||||
|  | 				// Mode 2: we should be done by now. | ||||||
|  | 				// That close paren should've been the last character. | ||||||
|  | 				2 => { | ||||||
|  | 					return Err(( | ||||||
|  | 						LineLocation{ pos: starting_left, len: left.chars().count() }, | ||||||
|  | 						DaisyError::Syntax | ||||||
|  | 					)); | ||||||
|  | 				}, | ||||||
|  |  | ||||||
|  | 				_ => unreachable!() | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 		let args = args | ||||||
|  | 			.split(",").collect::<Vec<&str>>() | ||||||
|  | 			.iter().map(|x| x.trim().to_string()).collect::<Vec<String>>(); | ||||||
|  |  | ||||||
|  | 		if name.len() == 0 { | ||||||
|  | 			return Err(( | ||||||
|  | 				LineLocation{ pos: starting_left, len: left.chars().count() }, | ||||||
|  | 				DaisyError::Syntax | ||||||
|  | 			)); | ||||||
|  | 		}; | ||||||
|  |  | ||||||
|  | 		if !context.valid_function(&name) { | ||||||
|  | 			return Err(( | ||||||
|  | 				LineLocation{ pos: starting_left, len: left.chars().count() }, | ||||||
|  | 				DaisyError::BadFunction | ||||||
|  | 			)); | ||||||
|  | 		}; | ||||||
|  |  | ||||||
|  | 		if args.iter().find(|x| &x[..] == "").is_some() { | ||||||
|  | 			return Err(( | ||||||
|  | 				LineLocation{ pos: starting_left, len: left.chars().count() }, | ||||||
|  | 				DaisyError::Syntax | ||||||
|  | 			)); | ||||||
|  | 		}; | ||||||
|  |  | ||||||
|  | 		for a in &args { | ||||||
|  | 			if !context.valid_varible(a) { | ||||||
|  | 				return Err(( | ||||||
|  | 					LineLocation{ pos: starting_left, len: left.chars().count() }, | ||||||
|  | 					DaisyError::BadVariable | ||||||
|  | 				)); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		// Parse right hand side | ||||||
|  | 		let g = parser::parse(context, &right); | ||||||
|  | 		let Ok(g) = g else { | ||||||
|  | 			let Err((l, e)) = g else { unreachable!() }; | ||||||
|  | 			return Err(( | ||||||
|  | 				LineLocation{ pos: l.pos + starting_right, len: l.len}, | ||||||
|  | 				e | ||||||
|  | 			)); | ||||||
|  | 		}; | ||||||
|  |  | ||||||
|  | 		// Display parsed string | ||||||
|  | 		output.push(&format!( | ||||||
|  | 			" [s]=>[n] {left} = {}\n\n", | ||||||
|  | 			g.display(context) | ||||||
|  | 		)); | ||||||
|  |  | ||||||
|  | 		// Evaluate expression with shadow variables | ||||||
|  | 		for a in &args { context.add_shadow(a.to_string(), None);} | ||||||
|  | 		let g_evaluated = evaluate::evaluate(context, &g); | ||||||
|  | 		context.clear_shadow(); | ||||||
|  | 		let Ok(_g_evaluated) = g_evaluated else { | ||||||
|  | 			let Err((l, e)) = g_evaluated else { unreachable!() }; | ||||||
|  | 			return Err(( | ||||||
|  | 				LineLocation{ pos: l.pos + starting_right, len: l.len}, | ||||||
|  | 				e | ||||||
|  | 			)); | ||||||
|  | 		}; | ||||||
|  |  | ||||||
|  | 		// We could push g_evaluated instead, but an un-evaluated string | ||||||
|  | 		// makes the 'vars' command prettier. | ||||||
|  | 		// | ||||||
|  | 		// We still need to evaluate g above, though, to make sure it works. | ||||||
|  | 		context.push_function(name, args, g).unwrap(); | ||||||
|  | 	} else { | ||||||
|  |  | ||||||
|  | 		if !context.valid_varible(&left) { | ||||||
|  | 			return Err(( | ||||||
|  | 				LineLocation{ pos: starting_left, len: left.chars().count() }, | ||||||
|  | 				DaisyError::BadVariable | ||||||
|  | 			)); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		// Parse right hand side | ||||||
|  | 		let g = parser::parse(context, &right); | ||||||
|  | 		let Ok(g) = g else { | ||||||
|  | 			let Err((l, e)) = g else { unreachable!() }; | ||||||
|  | 			return Err(( | ||||||
|  | 				LineLocation{ pos: l.pos + starting_right, len: l.len}, | ||||||
|  | 				e | ||||||
|  | 			)); | ||||||
|  | 		}; | ||||||
|  |  | ||||||
|  | 		// Display parsed string | ||||||
|  | 		output.push(&format!( | ||||||
|  | 			" [t]=>[n] {left} = {}\n\n", | ||||||
|  | 			g.display(context) | ||||||
|  | 		)); | ||||||
|  |  | ||||||
|  | 		// Evaluate expression | ||||||
|  | 		let g_evaluated = evaluate::evaluate(context, &g); | ||||||
|  | 		let Ok(g_evaluated) = g_evaluated else { | ||||||
|  | 			let Err((l, e)) = g_evaluated else { unreachable!() }; | ||||||
|  | 			return Err(( | ||||||
|  | 				LineLocation{ pos: l.pos + starting_right, len: l.len}, | ||||||
|  | 				e | ||||||
|  | 			)); | ||||||
|  | 		}; | ||||||
|  |  | ||||||
|  | 		context.push_variable(left.to_string(), g_evaluated).unwrap(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return Ok(output); | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
							
								
								
									
										372
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										372
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -1,289 +1,149 @@ | |||||||
| pub mod parser; | pub mod promptbuffer; | ||||||
| pub mod command; |  | ||||||
| pub mod quantity; |  | ||||||
| pub mod evaluate; |  | ||||||
| pub mod context; |  | ||||||
| pub mod errors; |  | ||||||
| pub mod formattedtext; |  | ||||||
|  |  | ||||||
| use crate::parser::substitute; | use std::io::stdout; | ||||||
| use crate::errors::DaisyError; | use std::io::stdin; | ||||||
| use crate::formattedtext::FormattedText; | use std::env; | ||||||
| use crate::context::Context; |  | ||||||
| use crate::parser::LineLocation; |  | ||||||
|  |  | ||||||
|  | use termion::{ | ||||||
|  | 	event::Key, | ||||||
|  | 	input::TermRead, | ||||||
|  | 	raw::IntoRawMode, | ||||||
|  | 	color::DetectColors | ||||||
|  | }; | ||||||
|  |  | ||||||
| // Run main script for target system | use promptbuffer::PromptBuffer; | ||||||
| mod entrypoint; | use daisycalc::command; | ||||||
| use crate::entrypoint::main_e; | use daisycalc::Context; | ||||||
|  | use daisycalc::FormattedText; | ||||||
|  |  | ||||||
|  | use daisycalc::do_string; | ||||||
|  |  | ||||||
| #[cfg(test)] | #[cfg(test)] | ||||||
| mod tests; | mod tests; | ||||||
|  |  | ||||||
| fn main() -> Result<(), std::io::Error> { |  | ||||||
| 	return main_e(); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| #[inline(always)] | #[inline(always)] | ||||||
| pub fn do_string( | pub fn main() -> Result<(), std::io::Error> { | ||||||
| 	context: &mut Context, | 	let mut stdout = stdout().into_raw_mode().unwrap(); | ||||||
| 	s: &String | 	let mut pb: PromptBuffer = PromptBuffer::new(64); | ||||||
| ) -> Result<FormattedText, FormattedText> { | 	let mut context = Context::new(); | ||||||
|  |  | ||||||
| 	let r: (LineLocation, DaisyError); | 	// Detect color compatibilty | ||||||
| 	if command::is_command(s) { | 	// Currently unused, this is slow. | ||||||
| 		return Ok(command::do_command(context, s)); | 	/* | ||||||
| 	} else if s.contains("=") { | 	let term_colors = stdout.available_colors().unwrap_or(0); | ||||||
| 		let x = do_assignment(context, s); | 	if term_colors >= 256 { | ||||||
| 		match x { | 		context.config.term_color_type = 2; | ||||||
| 			Ok(t) => { return Ok(t) }, | 	} else if term_colors >= 8 { | ||||||
| 			Err(t) => { r = t } | 		context.config.term_color_type = 1; | ||||||
| 		}; |  | ||||||
| 	} else { | 	} else { | ||||||
| 		let x = do_expression(context, s); | 		context.config.term_color_type = 0; | ||||||
| 		match x { |  | ||||||
| 			Ok((t, e)) => { context.push_hist(e); return Ok(t) }, |  | ||||||
| 			Err(t) => { r = t } |  | ||||||
| 		}; |  | ||||||
| 	} | 	} | ||||||
|  | 	*/ | ||||||
|  |  | ||||||
| 	let (l, e) = r; |  | ||||||
| 	let mut t = FormattedText::new("".to_string()); | 	// Handle command-line arguments | ||||||
| 	if l.zero() { | 	let args: Vec<String> = env::args().collect(); | ||||||
| 		t.push(&format!( | 	if args.iter().any(|s| s == "--help") { | ||||||
| 			"\n  {}\n\n", | 		let t = command::do_command(&mut context, &String::from("help")); | ||||||
| 			e.text().to_string(), | 		t.write(&context, &mut stdout)?; | ||||||
|  | 		let t = command::do_command(&mut context, &String::from("flags")); | ||||||
|  | 		t.write(&context, &mut stdout)?; | ||||||
|  | 		return Ok(()); | ||||||
|  | 	} else if args.iter().any(|s| s == "--version") { | ||||||
|  | 		let t = FormattedText::new(format!( | ||||||
|  | 			"Daisy v{}\n", env!("CARGO_PKG_VERSION") | ||||||
| 		)); | 		)); | ||||||
| 	} else { | 		t.write(&context, &mut stdout)?; | ||||||
| 		t.push(&format!( | 		return Ok(()); | ||||||
|  | 	} else if args.iter().any(|s| s == "--info") { | ||||||
|  | 		let t = FormattedText::new(format!( | ||||||
| 			concat!( | 			concat!( | ||||||
| 				"{}[e]{}[n]\n", | 				"Daisy v{}\n", | ||||||
| 				"  {}\n\n" | 				"Your terminal supports {} colors.\n" | ||||||
| 			), | 			), | ||||||
| 			" ".repeat(l.pos + 4), | 			env!("CARGO_PKG_VERSION"), | ||||||
| 			"^".repeat(l.len), | 			stdout.available_colors().unwrap_or(0) | ||||||
| 			e.text().to_string(), |  | ||||||
| 		)); | 		)); | ||||||
|  | 		t.write(&context, &mut stdout)?; | ||||||
|  | 		return Ok(()); | ||||||
|  | 	} else if args.iter().any(|s| s == "--256color") { | ||||||
|  | 		context.config.term_color_type = 2; | ||||||
|  | 	} else if args.iter().any(|s| s == "--8color") { | ||||||
|  | 		context.config.term_color_type = 1; | ||||||
|  | 	} else if args.iter().any(|s| s == "--0color") { | ||||||
|  | 		context.config.term_color_type = 0; | ||||||
|  | 	} else if args.iter().any(|s| s == "--nosub") { | ||||||
|  | 		context.config.enable_substituion = false; | ||||||
|  | 	} else if args.iter().any(|s| s == "--nosuper") { | ||||||
|  | 		context.config.enable_super_powers = false; | ||||||
|  | 	} else if args.iter().any(|s| s == "--nooneover") { | ||||||
|  | 		context.config.enable_one_over_power = false; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return Err(t); | 	context.config.check(); | ||||||
| } |  | ||||||
|  |  | ||||||
| // Handle a simple evaluation string. |  | ||||||
| // Returns a FormattedText with output that should be printed. |  | ||||||
| #[inline(always)] |  | ||||||
| fn do_expression( |  | ||||||
| 	context: &mut Context, |  | ||||||
| 	s: &String |  | ||||||
| ) -> Result<(FormattedText, parser::Expression), (LineLocation, DaisyError)> { |  | ||||||
|  |  | ||||||
| 	let mut output = FormattedText::new("".to_string()); |  | ||||||
|  |  | ||||||
| 	let g = parser::parse(context, &s)?; |  | ||||||
| 	let g_evaluated = evaluate::evaluate(context, &g)?; |  | ||||||
|  |  | ||||||
| 	// Display parsed string |  | ||||||
| 	output.push(&format!( |  | ||||||
| 		" [s]=>[n] {}\n\n", |  | ||||||
| 		g.display(context) |  | ||||||
| 	)); |  | ||||||
|  |  | ||||||
| 	// Display result |  | ||||||
| 	output.push(&format!( |  | ||||||
| 		"  [r]=[n] {}\n\n", |  | ||||||
| 		g_evaluated.display_outer(context), |  | ||||||
| 	)); |  | ||||||
|  |  | ||||||
| 	return Ok((output, g_evaluated)); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  |  | ||||||
| // Handle a variable or function definition string. | 	'outer: loop { | ||||||
| // Returns a FormattedText with output that should be printed. |  | ||||||
| #[inline(always)] |  | ||||||
| fn do_assignment( |  | ||||||
| 	context: &mut Context, |  | ||||||
| 	s: &String |  | ||||||
| ) -> Result<FormattedText, (LineLocation, DaisyError)> { |  | ||||||
|  |  | ||||||
| 	let mut output = FormattedText::new("".to_string()); | 		pb.write_prompt(&mut context, &mut stdout)?; | ||||||
|  |  | ||||||
| 	let parts = s.split("=").collect::<Vec<&str>>(); | 		let stdin = stdin(); | ||||||
| 	if parts.len() != 2 { | 		for c in stdin.keys() { | ||||||
| 		return Err(( | 			if let Key::Char(q) = c.as_ref().unwrap() { | ||||||
| 			LineLocation::new_zero(), | 				match q { | ||||||
| 			DaisyError::Syntax | 					'\n' => { | ||||||
| 		)); | 						// Print again without cursor, in case we pressed enter | ||||||
| 	} | 						// while inside a substitution | ||||||
|  | 						pb.write_prompt_nocursor(&mut context, &mut stdout)?; | ||||||
|  | 						let in_str = pb.enter(); | ||||||
|  | 						FormattedText::newline(&mut stdout)?; | ||||||
|  | 						if in_str == "" { break; } | ||||||
|  |  | ||||||
| 	// Index of first non-whitespace character in left | 						if in_str.trim() == "quit" { | ||||||
| 	// (relative to whole prompt) | 							break 'outer; | ||||||
| 	let starting_left = parts[0] |  | ||||||
| 		.char_indices() |  | ||||||
| 		.find(|(_, ch)| !(ch.is_whitespace() && *ch != '\n')) |  | ||||||
| 		.map(|(i, _)| i) |  | ||||||
| 		.unwrap_or_else(|| parts[0].len()); |  | ||||||
|  |  | ||||||
| 	// Index of first non-whitespace character in right |  | ||||||
| 	// (relative to whole prompt) |  | ||||||
| 	// +1 accounts for equals sign |  | ||||||
| 	let starting_right = parts[0].chars().count() + 1 + |  | ||||||
| 		parts[1] |  | ||||||
| 			.char_indices() |  | ||||||
| 			.find(|(_, ch)| !(ch.is_whitespace() && *ch != '\n')) |  | ||||||
| 			.map(|(i, _)| i) |  | ||||||
| 			.unwrap_or_else(|| parts[0].len()); |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 	let left = substitute(context, &parts[0].trim().to_string()); |  | ||||||
| 	let right = substitute(context, &parts[1].trim().to_string()); |  | ||||||
| 	let is_function = left.contains("("); |  | ||||||
|  |  | ||||||
| 	// The order of methods below is a bit odd. |  | ||||||
| 	// This is intentional, since we want to check a definition's |  | ||||||
| 	// variable name before even attempting to parse its content. |  | ||||||
| 	if is_function { |  | ||||||
| 		let mut mode = 0; |  | ||||||
| 		let mut name = String::new(); |  | ||||||
| 		let mut args = String::new(); |  | ||||||
| 		for c in left.chars() { |  | ||||||
| 			match mode { |  | ||||||
|  |  | ||||||
| 				// Mode 0: reading function name |  | ||||||
| 				0 => { |  | ||||||
| 					if c == '(' { |  | ||||||
| 						mode = 1; continue; |  | ||||||
| 					} else { name.push(c); } |  | ||||||
| 				}, |  | ||||||
|  |  | ||||||
| 				// Mode 1: reading arguments |  | ||||||
| 				1 => { |  | ||||||
| 					if c == ')' { |  | ||||||
| 						mode = 2; continue; |  | ||||||
| 					} else { args.push(c); } |  | ||||||
| 				}, |  | ||||||
|  |  | ||||||
| 				// Mode 2: we should be done by now. |  | ||||||
| 				// That close paren should've been the last character. |  | ||||||
| 				2 => { |  | ||||||
| 					return Err(( |  | ||||||
| 						LineLocation{ pos: starting_left, len: left.chars().count() }, |  | ||||||
| 						DaisyError::Syntax |  | ||||||
| 					)); |  | ||||||
| 				}, |  | ||||||
|  |  | ||||||
| 				_ => unreachable!() |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 		let args = args |  | ||||||
| 			.split(",").collect::<Vec<&str>>() |  | ||||||
| 			.iter().map(|x| x.trim().to_string()).collect::<Vec<String>>(); |  | ||||||
|  |  | ||||||
| 		if name.len() == 0 { |  | ||||||
| 			return Err(( |  | ||||||
| 				LineLocation{ pos: starting_left, len: left.chars().count() }, |  | ||||||
| 				DaisyError::Syntax |  | ||||||
| 			)); |  | ||||||
| 		}; |  | ||||||
|  |  | ||||||
| 		if !context.valid_function(&name) { |  | ||||||
| 			return Err(( |  | ||||||
| 				LineLocation{ pos: starting_left, len: left.chars().count() }, |  | ||||||
| 				DaisyError::BadFunction |  | ||||||
| 			)); |  | ||||||
| 		}; |  | ||||||
|  |  | ||||||
| 		if args.iter().find(|x| &x[..] == "").is_some() { |  | ||||||
| 			return Err(( |  | ||||||
| 				LineLocation{ pos: starting_left, len: left.chars().count() }, |  | ||||||
| 				DaisyError::Syntax |  | ||||||
| 			)); |  | ||||||
| 		}; |  | ||||||
|  |  | ||||||
| 		for a in &args { |  | ||||||
| 			if !context.valid_varible(a) { |  | ||||||
| 				return Err(( |  | ||||||
| 					LineLocation{ pos: starting_left, len: left.chars().count() }, |  | ||||||
| 					DaisyError::BadVariable |  | ||||||
| 				)); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Parse right hand side |  | ||||||
| 		let g = parser::parse(context, &right); |  | ||||||
| 		let Ok(g) = g else { |  | ||||||
| 			let Err((l, e)) = g else { unreachable!() }; |  | ||||||
| 			return Err(( |  | ||||||
| 				LineLocation{ pos: l.pos + starting_right, len: l.len}, |  | ||||||
| 				e |  | ||||||
| 			)); |  | ||||||
| 		}; |  | ||||||
|  |  | ||||||
| 		// Display parsed string |  | ||||||
| 		output.push(&format!( |  | ||||||
| 			" [s]=>[n] {left} = {}\n\n", |  | ||||||
| 			g.display(context) |  | ||||||
| 		)); |  | ||||||
|  |  | ||||||
| 		// Evaluate expression with shadow variables |  | ||||||
| 		for a in &args { context.add_shadow(a.to_string(), None);} |  | ||||||
| 		let g_evaluated = evaluate::evaluate(context, &g); |  | ||||||
| 		context.clear_shadow(); |  | ||||||
| 		let Ok(_g_evaluated) = g_evaluated else { |  | ||||||
| 			let Err((l, e)) = g_evaluated else { unreachable!() }; |  | ||||||
| 			return Err(( |  | ||||||
| 				LineLocation{ pos: l.pos + starting_right, len: l.len}, |  | ||||||
| 				e |  | ||||||
| 			)); |  | ||||||
| 		}; |  | ||||||
|  |  | ||||||
| 		// We could push g_evaluated instead, but an un-evaluated string |  | ||||||
| 		// makes the 'vars' command prettier. |  | ||||||
| 		// |  | ||||||
| 		// We still need to evaluate g above, though, to make sure it works. |  | ||||||
| 		context.push_function(name, args, g).unwrap(); |  | ||||||
| 						} else { | 						} else { | ||||||
|  | 							let r = crate::do_string(&mut context, &in_str); | ||||||
|  |  | ||||||
| 		if !context.valid_varible(&left) { | 							match r { | ||||||
| 			return Err(( | 								Ok(t) | Err(t) => { | ||||||
| 				LineLocation{ pos: starting_left, len: left.chars().count() }, | 									t.write(&context, &mut stdout).unwrap(); | ||||||
| 				DaisyError::BadVariable | 								} | ||||||
| 			)); | 							} | ||||||
| 						} | 						} | ||||||
|  |  | ||||||
| 		// Parse right hand side | 						break; | ||||||
| 		let g = parser::parse(context, &right); | 					}, | ||||||
| 		let Ok(g) = g else { |  | ||||||
| 			let Err((l, e)) = g else { unreachable!() }; | 					// Only process sane characters | ||||||
| 			return Err(( | 					'a'..='z' | 'A'..='Z' | '0'..='9' | ||||||
| 				LineLocation{ pos: l.pos + starting_right, len: l.len}, | 					|'!'|'@'|'#'|'$'|'%'|'^'|'&'|'*'|'('|')' | ||||||
| 				e | 					|'?'|'~'|','|'.'|'['|']' | ||||||
| 			)); | 					|'<'|'>'|'/'|'_'|'-'|':'|'|'|'='|'+'|';' | ||||||
|  | 					=> { pb.add_char(*q); }, | ||||||
|  |  | ||||||
|  | 					_ => {} | ||||||
|  | 				}; | ||||||
|  | 			} else { | ||||||
|  | 				match c.unwrap() { | ||||||
|  | 					Key::Backspace => { pb.backspace(); }, | ||||||
|  | 					Key::Delete => { pb.delete(); }, | ||||||
|  | 					Key::Left => { pb.cursor_left(); }, | ||||||
|  | 					Key::Right => { pb.cursor_right(); }, | ||||||
|  | 					Key::Up => { pb.hist_up(); }, | ||||||
|  | 					Key::Down => { pb.hist_down(); }, | ||||||
|  |  | ||||||
|  | 					Key::Ctrl('d') | | ||||||
|  | 					Key::Ctrl('c') => { break 'outer; }, | ||||||
|  | 					_ => {} | ||||||
|  | 				}; | ||||||
| 			}; | 			}; | ||||||
|  |  | ||||||
| 		// Display parsed string | 			pb.write_prompt(&mut context, &mut stdout)?; | ||||||
| 		output.push(&format!( | 		} | ||||||
| 			" [t]=>[n] {left} = {}\n\n", |  | ||||||
| 			g.display(context) |  | ||||||
| 		)); |  | ||||||
|  |  | ||||||
| 		// Evaluate expression |  | ||||||
| 		let g_evaluated = evaluate::evaluate(context, &g); |  | ||||||
| 		let Ok(g_evaluated) = g_evaluated else { |  | ||||||
| 			let Err((l, e)) = g_evaluated else { unreachable!() }; |  | ||||||
| 			return Err(( |  | ||||||
| 				LineLocation{ pos: l.pos + starting_right, len: l.len}, |  | ||||||
| 				e |  | ||||||
| 			)); |  | ||||||
| 		}; |  | ||||||
|  |  | ||||||
| 		context.push_variable(left.to_string(), g_evaluated).unwrap(); |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return Ok(output); | 	FormattedText::newline(&mut stdout)?; | ||||||
|  | 	return Ok(()); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | |||||||
| @ -1,9 +1,9 @@ | |||||||
| use std::collections::VecDeque; | use std::collections::VecDeque; | ||||||
| use std::io::Write; | use std::io::Write; | ||||||
| use termion::raw::RawTerminal; | use termion::raw::RawTerminal; | ||||||
| use crate::formattedtext; | use daisycalc::FormattedText; | ||||||
| use crate::parser::substitute_cursor; | use daisycalc::parser::substitute_cursor; | ||||||
| use crate::context::Context; | use daisycalc::Context; | ||||||
| 
 | 
 | ||||||
| const PROMPT_STR: &str = "==> "; | const PROMPT_STR: &str = "==> "; | ||||||
| 
 | 
 | ||||||
| @ -55,8 +55,8 @@ impl PromptBuffer { | |||||||
| 	
 | 	
 | ||||||
| 		write!( | 		write!( | ||||||
| 			stdout, "\r{}{PROMPT_STR}{}{}", | 			stdout, "\r{}{PROMPT_STR}{}{}", | ||||||
| 			formattedtext::format_map('p', context).unwrap(), | 			FormattedText::format_map('p', context).unwrap(), | ||||||
| 			formattedtext::format_map('n', context).unwrap(), | 			FormattedText::format_map('n', context).unwrap(), | ||||||
| 			s | 			s | ||||||
| 		)?; | 		)?; | ||||||
| 
 | 
 | ||||||
| @ -1,7 +1,7 @@ | |||||||
| // Many of these have been borrowed from insect. | // Many of these have been borrowed from insect. | ||||||
| use crate::parser; | use daisycalc::parser; | ||||||
| use crate::evaluate::evaluate; | use daisycalc::evaluate; | ||||||
| use crate::context::Context; | use daisycalc::Context; | ||||||
|  |  | ||||||
| fn eval_to_str(s: &str) -> Result<String, ()> { | fn eval_to_str(s: &str) -> Result<String, ()> { | ||||||
| 	let g = match parser::parse_no_context(&String::from(s)) { | 	let g = match parser::parse_no_context(&String::from(s)) { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user