-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script mode support & comment support
- Loading branch information
Showing
9 changed files
with
170 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::compiler::compile; | ||
use crate::computer::compute; | ||
use crate::public::number::Number; | ||
use crate::public::ast::ASTNode; | ||
|
||
pub fn attempt( | ||
input: String, | ||
build_in_funcs: &HashMap<&str, fn(f64) -> f64>, | ||
variables: &mut HashMap<String, Number>, | ||
goto_statements: &mut HashMap<String, ASTNode>, | ||
) -> Result<Number, ()> { | ||
let root_node = compile::compile(input)?; | ||
let result_num = compute::compute( | ||
root_node, | ||
build_in_funcs, | ||
variables, | ||
goto_statements, | ||
)?; | ||
|
||
Ok(result_num) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod attempt; | ||
pub mod repl; | ||
pub mod run_script; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::collections::HashMap; | ||
use std::io::{self, Write}; | ||
|
||
use super::attempt::attempt; | ||
use crate::public::number::Number; | ||
use crate::public::ast::ASTNode; | ||
|
||
pub fn repl( | ||
build_in_funcs: &HashMap<&str, fn(f64) -> f64>, | ||
variables: &mut HashMap<String, Number>, | ||
goto_statements: &mut HashMap<String, ASTNode>, | ||
) -> ! { | ||
// print program name and version | ||
println!("Calculator.rs v1.2.0"); | ||
|
||
loop { | ||
print!("> "); | ||
io::stdout().flush().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin() | ||
.read_line(&mut input) | ||
.unwrap(); | ||
|
||
let result = attempt( | ||
input, | ||
build_in_funcs, | ||
variables, | ||
goto_statements, | ||
); | ||
|
||
if let Ok(num) = result { | ||
println!("= {}", num); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::collections::HashMap; | ||
use std::fs::File; | ||
use std::io::{self, BufRead}; | ||
|
||
use crate::exec::attempt::attempt; | ||
use crate::public::number::Number; | ||
use crate::public::ast::ASTNode; | ||
|
||
type FileBuf = io::BufReader<File>; | ||
fn read_lines(path: String) -> io::Result<io::Lines<FileBuf>> { | ||
let file = File::open(path)?; | ||
Ok(io::BufReader::new(file).lines()) | ||
} | ||
|
||
pub fn run_script( | ||
path: String, | ||
build_in_funcs: &HashMap<&str, fn(f64) -> f64>, | ||
variables: &mut HashMap<String, Number>, | ||
goto_statements: &mut HashMap<String, ASTNode>, | ||
) { | ||
let mut script_lines = | ||
if let Ok(lines) = read_lines(path) { | ||
lines | ||
} else { | ||
println!("Invalid file."); | ||
return | ||
}; | ||
|
||
// cache last compute result, | ||
// when the loop ends, | ||
// print the final result. | ||
let mut last_result = Number::NotANumber; | ||
loop { | ||
match script_lines.next() { | ||
Some(item) => { | ||
let mut script_line = | ||
if let Ok(line) = item { | ||
line | ||
} else { | ||
String::new() | ||
}; | ||
script_line.push('\r'); | ||
|
||
last_result = | ||
if let Ok(res_number) = attempt( | ||
script_line, | ||
build_in_funcs, | ||
variables, | ||
goto_statements | ||
) { | ||
res_number | ||
} else { | ||
Number::NotANumber | ||
}; | ||
}, | ||
None => { | ||
println!("{}", last_result); | ||
break; | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,43 @@ | ||
mod public; | ||
mod compiler; | ||
mod computer; | ||
mod exec; | ||
|
||
use std::env; | ||
use std::collections::HashMap; | ||
use std::io::{self, Write}; | ||
|
||
use public::ast::ASTNode; | ||
use public::number::Number; | ||
use public::build_in; | ||
use compiler::compile; | ||
use computer::compute; | ||
use exec::repl::repl; | ||
use exec::run_script::run_script; | ||
|
||
fn attempt( | ||
input: String, | ||
build_in_funcs: &HashMap<&str, fn(f64) -> f64>, | ||
variables: &mut HashMap<String, Number>, | ||
goto_statements: &mut HashMap<String, ASTNode> | ||
) -> Result<Number, ()> { | ||
let root_node = compile::compile(input)?; | ||
let result_num = compute::compute( | ||
root_node, | ||
build_in_funcs, | ||
variables, | ||
goto_statements, | ||
)?; | ||
|
||
Ok(result_num) | ||
} | ||
|
||
fn main() -> ! { | ||
fn main() { | ||
let build_in_inst = build_in::BuildIn::init(); | ||
let build_in_funcs = build_in::build_in_funcs(&build_in_inst); | ||
let mut variables = build_in::variables(&build_in_inst); | ||
let mut goto_statements = HashMap::<String, ASTNode>::new(); | ||
|
||
// print program name and version | ||
println!("Calculator.rs v1.1.1"); | ||
|
||
loop { | ||
print!("> "); | ||
io::stdout().flush().unwrap(); | ||
|
||
let mut input = String::new(); | ||
std::io::stdin() | ||
.read_line(&mut input) | ||
.unwrap(); | ||
|
||
let result = attempt( | ||
input, | ||
&build_in_funcs, | ||
&mut variables, | ||
&mut goto_statements, | ||
); | ||
|
||
if let Ok(num) = result { | ||
println!("= {}", num); | ||
let args: Vec<String> = env::args().collect(); | ||
match args.len() { | ||
1 => { | ||
// REPL mode | ||
repl( | ||
&build_in_funcs, | ||
&mut variables, | ||
&mut goto_statements, | ||
); | ||
}, | ||
2 => { | ||
// script mode | ||
run_script( | ||
args[1].to_owned(), | ||
&build_in_funcs, | ||
&mut variables, | ||
&mut goto_statements, | ||
); | ||
}, | ||
_ => { | ||
println!("Too many args."); | ||
} | ||
} | ||
} |