Skip to content

Commit

Permalink
todos added && cursor bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
BHznJNs committed Jun 3, 2023
1 parent 860864d commit 7f97b6b
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 59 deletions.
1 change: 1 addition & 0 deletions src/compiler/analyzer/resolvers/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub fn resolve(tokens: &mut TokenVec) -> Result<ExpressionNode, ()> {
}
}
_ => {
// todo
println!("Invalid expression: unexpected ASTNodeType: {}.", node);
return Err(());
}
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/analyzer/resolvers/symbol_priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ fn get_priority(symbol_node: &ASTNode) -> Result<i8, ()> {
if let ASTNode::SymbolLiteral(symbol) = symbol_node {
let symbol_index = *symbol as usize;
if symbol_index >= PRIORITY.len() {
// todo
println!("AnalyzerError: invalid symbol: `{}`.", symbol);
return Err(());
}
Ok(PRIORITY[symbol_index])
} else {
// todo
println!("Analyzer error from 'get_priority'.");
return Err(());
}
Expand Down
2 changes: 2 additions & 0 deletions src/computer/resolvers/composer/array_reading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub fn assign(
let char_str = &target.as_ref().borrow();
str.replace_range(index_value..index_value + 1, char_str);
} else {
// todo: replace with range_error
println!("Invalid array reading.");
return Err(());
}
Expand All @@ -95,6 +96,7 @@ pub fn resolve(
let slice = &str[index_value..index_value + 1];
Ok(Value::create(slice.to_string()))
} else {
// todo: replace with range_error
println!("Invalid indexing.");
Err(())
}
Expand Down
1 change: 1 addition & 0 deletions src/computer/resolvers/operate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn operate(val1: Value, val2: Value, operator: Symbols) -> Result<Value, ()>
Symbols::LessThanEqual => Value::Boolean(num1 <= num2),
Symbols::MoreThanEqual => Value::Boolean(num1 >= num2),
_ => {
// todo
println!("Unexpected symbol: '{}' at function 'operate'.", operator);
return Err(());
}
Expand Down
6 changes: 5 additions & 1 deletion src/computer/resolvers/statement.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io::stdout;
use std::rc::Rc;

use crate::computer::resolvers::expression;
Expand All @@ -7,6 +8,7 @@ use crate::public::compile_time::keywords::Keyword;
use crate::public::error::{import_error, syntax_error};
use crate::public::run_time::scope::Scope;
use crate::public::value::value::{Value, VoidSign};
use crate::utils::output::print_line;

use super::sequence;

Expand All @@ -23,7 +25,9 @@ pub fn resolve(statement_node: Rc<StatementNode>, scope: &mut Scope) -> Result<V
} else {
Value::Void(VoidSign::Empty)
};
println!("{}", output_value);

print_line(&mut stdout(), output_value);
// println!("{}", output_value);
Value::Void(VoidSign::Empty)
}
Keyword::For => {
Expand Down
10 changes: 7 additions & 3 deletions src/exec/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::public::error::import_error;
use crate::public::run_time::scope::Scope;
use crate::public::value::value::Value;
use crate::utils::line_editor::{LineEditor, Signal};
use crate::utils::output::print_line;

const PROMPT: &'static str = "> ";

Expand Down Expand Up @@ -47,6 +48,7 @@ pub fn repl(scope: &mut Scope, calc_env: Env) -> io::Result<()> {
enable_raw_mode()?;

let mut rl = LineEditor::new(PROMPT);
let mut stdout = io::stdout();
loop {
support_keyboard_enhancement::resolve()?;

Expand All @@ -63,7 +65,7 @@ pub fn repl(scope: &mut Scope, calc_env: Env) -> io::Result<()> {
result = attempt(&line_content, scope);
let elapsed_time = now.elapsed();
let elapsed_second = elapsed_time.as_secs_f64();
println!("Executed in: {}s.", elapsed_second);
print_line(&mut stdout, format!("Executed in: {}s.", elapsed_second));
} else {
result = attempt(&line_content, scope);
}
Expand All @@ -72,9 +74,11 @@ pub fn repl(scope: &mut Scope, calc_env: Env) -> io::Result<()> {
if let Value::Void(_) = val {
continue;
} else if let Value::String(_) = val {
println!("= {}", val.str_format());
print!("= ");
print_line(&mut stdout, val.str_format());
} else {
println!("= {}", val);
print!("= ");
print_line(&mut stdout, val);
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/public/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::fmt::Display;

use crossterm::style::{StyledContent, Stylize};

use crate::utils::output::print_line__;

use super::value::value::ValueType;

type ErrorResult = Result<(), ()>;
Expand Down Expand Up @@ -33,42 +35,42 @@ pub fn type_error(param: Option<&str>, expected: Vec<ValueType>, found: ValueTyp
if let Some(name) = param {
print!(" for \"{}\"", name);
}
println!(": expected {}, found {}.", join(expected), found);
print_line__(format!(": expected {}, found {}.", join(expected), found));
Err(())
}

const RANGE_ERROR_NAME: &'static str = " RangeError ";
pub fn range_error<T: Display>(param: &str, expected: T, found: usize) -> ErrorResult {
print!("{} for \"{}\"", error_name_output(RANGE_ERROR_NAME), param);
println!(": expected {}, found {}.", expected, found);
print_line__(format!(": expected {}, found {}.", expected, found));
Err(())
}

const SYNTAX_ERROR_NAME: &'static str = " SyntaxError ";
pub fn syntax_error(msg: &str) -> ErrorResult {
println!("{}: {}.", error_name_output(SYNTAX_ERROR_NAME), msg);
print_line__(format!("{}: {}.\r", error_name_output(SYNTAX_ERROR_NAME), msg));
Err(())
}

const ASSIGNMENT_ERROR_NAME: &'static str = " SyntaxError ";
pub fn assignment_error(msg: &str) -> ErrorResult {
println!("{}: {}.", error_name_output(ASSIGNMENT_ERROR_NAME), msg);
print_line__(format!("{}: {}.", error_name_output(ASSIGNMENT_ERROR_NAME), msg));
Err(())
}

const REFERENCE_ERROR_NAME: &'static str = " ReferenceError ";
pub fn reference_error(var_name: &str) -> ErrorResult {
println!(
print_line__(format!(
"{}: variable `{}` is not defined.",
error_name_output(REFERENCE_ERROR_NAME),
var_name
);
));
Err(())
}

const IMPORT_ERROR_NAME: &'static str = " ImportError ";
pub fn import_error(msg: &str) -> ErrorResult {
println!("{}: {}.", error_name_output(IMPORT_ERROR_NAME), msg);
print_line__(format!("{}: {}.", error_name_output(IMPORT_ERROR_NAME), msg));
Err(())
}

Expand All @@ -95,11 +97,11 @@ impl fmt::Display for InternalComponent {

const INTERNAL_ERROR_NAME: &'static str = " InternalError ";
pub fn internal_error(from: InternalComponent, msg: &str) -> ErrorResult {
println!(
print_line__(format!(
"{} from {}: {}.",
error_name_output(INTERNAL_ERROR_NAME),
from,
msg
);
));
Err(())
}
1 change: 1 addition & 0 deletions src/public/std/utils/get_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub fn get_val(val_name: &str, scope: &mut Scope) -> Result<Value, ()> {
match option_value {
Some(val) => Ok(val.clone()),
None => {
// todo: replace with syntax_error
println!("Input for function is missing.");
Err(())
}
Expand Down
1 change: 1 addition & 0 deletions src/public/std/utils/str_to_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub fn str_to_num<T: FromStr>(str: Ref<String>) -> Result<T, ()> {
match str.parse::<T>() {
Ok(val) => Ok(val),
Err(_) => {
// todo: replace with type_error
println!("Invalid string coverting to number.");
return Err(());
}
Expand Down
20 changes: 13 additions & 7 deletions src/public/value/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cell::RefCell, collections::VecDeque, rc::Rc};
use std::{cell::RefCell, collections::VecDeque, rc::Rc, io};

use crate::public::value::oop::object;
use crate::{public::value::oop::object, utils::output::print_line};

use super::value::Value;

Expand All @@ -9,26 +9,32 @@ pub type ArrayLiteral = VecDeque<Value>;
pub fn display(arr: Rc<RefCell<ArrayLiteral>>, level: usize) {
const LINE_COUNT: i8 = 5;
let mut index = 0;
let mut stdout = io::stdout();

print!("[");
let iterator = &*(arr.as_ref().borrow());
for element in iterator {
// print indent
if index % LINE_COUNT == 0 {
print!("\n{}", " ".repeat(level));
print_line(&mut stdout, "");
print!("{}", " ".repeat(level));
}

// print elements
match element {
Value::String(_) => print!("{}", element.str_format()),
Value::Array(arr) => display(arr.clone(), level + 1),
Value::Object(obj) => object::display(obj.clone(), level + 1),
Value::String(_) =>
print!("{}", element.str_format()),
Value::Array(arr) =>
display(arr.clone(), level + 1),
Value::Object(obj) =>
object::display(obj.clone(), level + 1),
_ => print!("{}", element),
}

print!(", ");
index += 1;
}

print!("\n{}]", " ".repeat(level - 1))
print_line(&mut stdout, "");
print!("{}]", " ".repeat(level - 1))
}
4 changes: 3 additions & 1 deletion src/public/value/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::f64::INFINITY;
use std::fmt;
use std::ops::{Add, Div, Mul, Sub};

use crate::utils::output::print_line__;

#[derive(PartialOrd, Clone, Copy)]
pub enum Number {
NotANumber,
Expand Down Expand Up @@ -164,7 +166,7 @@ impl Div for Number {

if let (Number::Float(num1__), Number::Float(num2__)) = (num1, num2) {
if num2__ == 0.0 {
println!("The dividend should not to be ZERO!");
print_line__("The dividend should not to be ZERO!");
let inf = if num1__ >= 0.0 { INFINITY } else { -INFINITY };
return Number::Float(inf);
}
Expand Down
15 changes: 10 additions & 5 deletions src/public/value/oop/class.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use std::{fmt, io};
use std::rc::Rc;

use crossterm::style::Stylize;
Expand All @@ -10,6 +10,7 @@ use crate::public::error::type_error;
use crate::public::value::array::ArrayLiteral;
use crate::public::value::function::Function;
use crate::public::value::value::{Value, ValueType};
use crate::utils::output::print_line;

use super::object::Object;
use super::utils::data_storage::DataStoragePattern;
Expand Down Expand Up @@ -43,6 +44,7 @@ impl Class {
match result_target_method {
Ok(target_method) => Ok(target_method),
Err(err_msg) => {
// todo: replace with reference_error
println!("{}", err_msg);
Err(())
}
Expand Down Expand Up @@ -112,9 +114,12 @@ impl Class {
const CLASS_METHOD_DISP_STR: &'static str = "<Class-Method>";
impl fmt::Display for Class {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
println!("{{");
let mut stdout = io::stdout();

print_line(&mut stdout, '{');
for prop in &self.properties {
println!(" {},", prop.identi);
// todo: display property indentifier and type
print_line(&mut stdout, format!(" {},", prop.identi));
}

let class_method_disp = if unsafe { ENV_OPTION.support_ansi } {
Expand All @@ -126,14 +131,14 @@ impl fmt::Display for Class {
DataStoragePattern::List => {
let list = self.method_list.as_ref().unwrap();
for method in list {
println!(" {}: {},", method.0, class_method_disp);
print_line(&mut stdout, format!(" {}: {},", method.0, class_method_disp));
}
}
DataStoragePattern::Map => {
let map = self.method_map.as_ref().unwrap();

for (key, _) in map {
println!(" {}: {},", key, class_method_disp);
print_line(&mut stdout, format!(" {}: {},", key, class_method_disp));
}
}
}
Expand Down
25 changes: 17 additions & 8 deletions src/public/value/oop/object.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::{Stdout, self};
use std::rc::Rc;

use crate::public::value::array;
use crate::utils::output::print_line;

use super::super::value::Value;
use super::utils::data_storage::DataStoragePattern;
Expand All @@ -18,38 +20,43 @@ pub struct Object {
}

pub fn display(obj: Rc<RefCell<Object>>, level: usize) {
fn display_item(key: &String, value: &Rc<RefCell<Value>>, level: usize) {
fn display_item(stdout: &mut Stdout, key: &String, value: &Rc<RefCell<Value>>, level: usize) {
let value_ref = value.as_ref().borrow();

// print indent and key
print!("{}{}: ", " ".repeat(level), key);

// print value
match value_ref.unwrap() {
Value::String(_) => print!("{}", value_ref.str_format()),
Value::Array(arr) => array::display(arr, level + 1),
Value::Object(obj) => display(obj, level + 1),
Value::String(_) =>
print!("{}", value_ref.str_format()),
Value::Array(arr) =>
array::display(arr, level + 1),
Value::Object(obj) =>
display(obj, level + 1),
_ => print!("{}", value_ref),
}

// next line
println!();
print_line(stdout, "");
}

let obj_ref = obj.as_ref().borrow();
println!("{{");
let mut stdout = io::stdout();

print_line(&mut stdout, '{');
match obj_ref.storage_pattern {
DataStoragePattern::List => {
let list = obj_ref.data_list.as_ref().unwrap();
for (k, v) in list {
display_item(k, v, level);
display_item(&mut stdout, k, v, level);
}
}
DataStoragePattern::Map => {
let map = obj_ref.data_map.as_ref().unwrap();

for (k, v) in map {
display_item(k, v, level);
display_item(&mut stdout, k, v, level);
}
}
}
Expand All @@ -75,6 +82,7 @@ impl Object {
Ok(Value::Function(target_method.clone()))
}
_ => {
// todo: replace with reference_error
println!("Property '{}' in object does not exist.", prop_name);
Err(())
}
Expand All @@ -97,6 +105,7 @@ impl Object {
Ok(())
}
Err(err_msg) => {
// todo: replace with reference_error
println!("{}", err_msg);
Err(())
}
Expand Down
Loading

0 comments on commit 7f97b6b

Please sign in to comment.