Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: condition #6

Merged
merged 14 commits into from
Feb 20, 2023
Prev Previous commit
Next Next commit
fix: prefix unary expression
  • Loading branch information
echosoar committed Feb 17, 2023
commit 61c737b717b8c30dde91fb4632a0acef3c4fb2d2
6 changes: 4 additions & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl AST{
let mut default = None;
let mut clauses: Vec<CaseClause> = vec![];
loop {
if self.token == Token::EOF {
if self.token == Token::EOF || self.token == Token::RightBrace {
break;
}
let mut is_default = false;
Expand All @@ -251,6 +251,7 @@ impl AST{
// TODO: throw new error
} else {
is_default = true;
self.next();
}
} else {
self.check_token_and_next(Token::Case);
Expand Down Expand Up @@ -296,8 +297,9 @@ impl AST{
initializer = self.parse_variable_statement();
} else if self.token != Token::Semicolon {
initializer = Statement::Expression(ExpressionStatement { expression: self.parse_expression() })
self.check_token_and_next(Token::Semicolon);
}
// TODO: self.check_token_and_next(Token::Semicolon);
println!("xxx");
let codition = self.parse_expression();
self.check_token_and_next(Token::Semicolon);
let incrementor = self.parse_expression();
Expand Down
24 changes: 23 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{rc::{Rc, Weak}, cell::RefCell, ops::Index};

use crate::{ast::Program, ast_node::{Statement, Declaration, ObjectLiteral, AssignExpression, CallContext, ArrayLiteral, ClassType, ForStatement, VariableFlag, PostfixUnaryExpression, IdentifierLiteral}, ast_node::{Expression, CallExpression, Keywords, BinaryExpression}, value::{Value, ValueInfo, CallStatementOptions}, scope::{Scope, get_value_and_scope}, ast_token::Token, builtins::{object::{Object, Property, create_object}, function::{create_function, get_function_this}, global::{new_global_this, get_global_object}, array::create_array}};
use crate::{ast::Program, ast_node::{Statement, Declaration, ObjectLiteral, AssignExpression, CallContext, ArrayLiteral, ClassType, ForStatement, VariableFlag, PostfixUnaryExpression, IdentifierLiteral, PrefixUnaryExpression}, ast_node::{Expression, CallExpression, Keywords, BinaryExpression}, value::{Value, ValueInfo, CallStatementOptions}, scope::{Scope, get_value_and_scope}, ast_token::Token, builtins::{object::{Object, Property, create_object}, function::{create_function, get_function_this}, global::{new_global_this, get_global_object}, array::create_array}};

use super::ast::AST;
pub struct Context {
Expand Down Expand Up @@ -169,6 +169,9 @@ impl Context {
Expression::Binary(binary) => {
ValueInfo { value: self.execute_binary_expression(binary), name: None, reference: None }
},
Expression::PrefixUnary(expr) => {
ValueInfo { value: self.execute_prefix_unary_expression(expr), name: None, reference: None }
},
Expression::PostfixUnary(expr) => {
ValueInfo { value: self.execute_postfix_unary_expression(expr), name: None, reference: None }
},
Expand Down Expand Up @@ -394,6 +397,25 @@ impl Context {
}
}

// 执行 ++i --i
fn execute_prefix_unary_expression(&mut self, expression: &PrefixUnaryExpression) -> Value {
let mut operand_info = self.execute_expression_info(&expression.operand);
let mut new_value = operand_info.value.to_number().unwrap();
match &expression.operator {
Token::Increment => {
new_value = new_value + 1f64;
},
Token::Decrement => {
new_value = new_value - 1f64;
},
_ => {}
}
println!("new_value2: {:?}", new_value);
let value = Value::Number(new_value);
operand_info.set_value(value.clone());
value
}

// 执行 i++ i--
fn execute_postfix_unary_expression(&mut self, expression: &PostfixUnaryExpression) -> Value {
let mut operand_info = self.execute_expression_info(&expression.operand);
Expand Down
14 changes: 14 additions & 0 deletions tests/confitional_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ fn run_switch_case() {
assert_eq!(result , Value::Number(2f64));
}

#[test]
fn run_for() {
let mut jsi = JSI::new();
let result = jsi.run(String::from("\
let a = [];
let i;
// for(i = 0; i < 3; i++) {
// a.push(++i);
// }
// a.join(':')"));
assert_eq!(result , Value::String(String::from("1:3")));
}


#[test]
fn run_for_break_continue_label() {
let mut jsi = JSI::new();
Expand Down