2023-03-27 10:54:36 -07:00
|
|
|
pub mod parser;
|
2023-06-07 14:38:22 -07:00
|
|
|
pub mod command;
|
2023-04-01 13:50:52 -07:00
|
|
|
pub mod quantity;
|
2023-06-11 13:53:45 -07:00
|
|
|
pub mod evaluate;
|
2023-06-14 14:36:58 -07:00
|
|
|
pub mod context;
|
2023-08-03 22:13:38 -07:00
|
|
|
pub mod errors;
|
|
|
|
pub mod formattedtext;
|
2023-03-18 22:16:26 -07:00
|
|
|
|
2023-08-04 22:40:27 -07:00
|
|
|
use crate::parser::substitute;
|
|
|
|
use crate::errors::DaisyError;
|
|
|
|
use crate::formattedtext::FormattedText;
|
|
|
|
use crate::context::Context;
|
|
|
|
use crate::parser::LineLocation;
|
|
|
|
|
2023-08-05 10:05:37 -07:00
|
|
|
|
|
|
|
// Run main script for target system
|
|
|
|
mod entrypoint;
|
|
|
|
use crate::entrypoint::main_e;
|
|
|
|
|
2023-08-04 22:40:27 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2023-03-18 22:16:26 -07:00
|
|
|
fn main() -> Result<(), std::io::Error> {
|
2023-04-07 20:36:55 -07:00
|
|
|
return main_e();
|
2023-03-27 10:54:36 -07:00
|
|
|
}
|
|
|
|
|
2023-08-04 22:40:27 -07:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn do_string(
|
2023-08-16 22:33:38 -07:00
|
|
|
context: &mut Context,
|
|
|
|
s: &String
|
2023-08-04 22:40:27 -07:00
|
|
|
) -> Result<FormattedText, FormattedText> {
|
|
|
|
|
|
|
|
let r: (LineLocation, DaisyError);
|
|
|
|
if command::is_command(s) {
|
2023-08-16 22:33:38 -07:00
|
|
|
return Ok(command::do_command(context, s));
|
2023-08-04 22:40:27 -07:00
|
|
|
} else if s.contains("=") {
|
2023-08-16 22:33:38 -07:00
|
|
|
let x = do_assignment(context, s);
|
2023-08-04 22:40:27 -07:00
|
|
|
match x {
|
|
|
|
Ok(t) => { return Ok(t) },
|
|
|
|
Err(t) => { r = t }
|
|
|
|
};
|
|
|
|
} else {
|
2023-08-16 22:33:38 -07:00
|
|
|
let x = do_expression(context, s);
|
2023-08-04 22:40:27 -07:00
|
|
|
match x {
|
|
|
|
Ok((t, e)) => { context.push_hist(e); return Ok(t) },
|
|
|
|
Err(t) => { r = t }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let (l, e) = r;
|
2023-08-04 22:47:27 -07:00
|
|
|
let mut t = FormattedText::new("".to_string());
|
|
|
|
if l.zero() {
|
|
|
|
t.push(&format!(
|
|
|
|
"\n {}\n\n",
|
|
|
|
e.text().to_string(),
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
t.push(&format!(
|
2023-08-04 22:40:27 -07:00
|
|
|
concat!(
|
|
|
|
"{}[e]{}[n]\n",
|
2023-08-04 22:47:27 -07:00
|
|
|
" {}\n\n"
|
2023-08-04 22:40:27 -07:00
|
|
|
),
|
|
|
|
" ".repeat(l.pos + 4),
|
|
|
|
"^".repeat(l.len),
|
|
|
|
e.text().to_string(),
|
2023-08-04 22:47:27 -07:00
|
|
|
));
|
|
|
|
}
|
2023-08-04 22:40:27 -07:00
|
|
|
|
|
|
|
return Err(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a simple evaluation string.
|
|
|
|
// Returns a FormattedText with output that should be printed.
|
|
|
|
#[inline(always)]
|
|
|
|
fn do_expression(
|
2023-08-16 22:33:38 -07:00
|
|
|
context: &mut Context,
|
|
|
|
s: &String
|
2023-08-04 22:40:27 -07:00
|
|
|
) -> Result<(FormattedText, parser::Expression), (LineLocation, DaisyError)> {
|
|
|
|
|
|
|
|
let mut output = FormattedText::new("".to_string());
|
|
|
|
|
2023-08-16 22:33:38 -07:00
|
|
|
let g = parser::parse(context, &s)?;
|
|
|
|
let g_evaluated = evaluate::evaluate(context, &g)?;
|
2023-08-04 22:40:27 -07:00
|
|
|
|
|
|
|
// Display parsed string
|
|
|
|
output.push(&format!(
|
|
|
|
" [s]=>[n] {}\n\n",
|
2023-08-17 10:10:38 -07:00
|
|
|
g.display(context)
|
2023-08-04 22:40:27 -07:00
|
|
|
));
|
|
|
|
|
|
|
|
// Display result
|
|
|
|
output.push(&format!(
|
|
|
|
" [r]=[n] {}\n\n",
|
2023-08-17 10:10:38 -07:00
|
|
|
g_evaluated.display_outer(context),
|
2023-08-04 22:40:27 -07:00
|
|
|
));
|
|
|
|
|
|
|
|
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(
|
2023-08-16 22:33:38 -07:00
|
|
|
context: &mut Context,
|
|
|
|
s: &String
|
2023-08-04 22:40:27 -07:00
|
|
|
) -> 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());
|
|
|
|
|
|
|
|
|
2023-08-16 22:33:38 -07:00
|
|
|
let left = substitute(context, &parts[0].trim().to_string());
|
|
|
|
let right = substitute(context, &parts[1].trim().to_string());
|
2023-08-04 22:40:27 -07:00
|
|
|
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
|
2023-08-16 22:33:38 -07:00
|
|
|
let g = parser::parse(context, &right);
|
2023-08-04 22:40:27 -07:00
|
|
|
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",
|
2023-08-17 10:10:38 -07:00
|
|
|
g.display(context)
|
2023-08-04 22:40:27 -07:00
|
|
|
));
|
|
|
|
|
|
|
|
// Evaluate expression with shadow variables
|
|
|
|
for a in &args { context.add_shadow(a.to_string(), None);}
|
2023-08-16 22:33:38 -07:00
|
|
|
let g_evaluated = evaluate::evaluate(context, &g);
|
2023-08-04 22:40:27 -07:00
|
|
|
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
|
2023-08-16 22:33:38 -07:00
|
|
|
let g = parser::parse(context, &right);
|
2023-08-04 22:40:27 -07:00
|
|
|
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",
|
2023-08-17 10:10:38 -07:00
|
|
|
g.display(context)
|
2023-08-04 22:40:27 -07:00
|
|
|
));
|
|
|
|
|
|
|
|
// Evaluate expression
|
2023-08-16 22:33:38 -07:00
|
|
|
let g_evaluated = evaluate::evaluate(context, &g);
|
2023-08-04 22:40:27 -07:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-07 20:36:55 -07:00
|
|
|
|