Skip to content

Commit 96e099a

Browse files
Added tests for OOP.
Added tests for OOP. Performed basic refactoring. Cleaned up unused code.
1 parent 5a5864c commit 96e099a

File tree

18 files changed

+309
-272
lines changed

18 files changed

+309
-272
lines changed

src/lib/ast/expression.rs

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ pub enum Expression {
2020
IntegerLiteral(i64),
2121
StringLiteral(String),
2222
ParenthesizedExpression(BoxExpression),
23-
// ThisExpression,
24-
// SuperExpression,
2523
UnaryExpression {
2624
operator: Operator,
2725
argument: BoxExpression,

src/lib/interpreter/environment/default_global_scope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::interpreter::environment::native_functions::get_native_functions_list;
22
use crate::interpreter::runtime_values::objects::runtime_object::RuntimeObject;
33
use crate::interpreter::runtime_values::PrimitiveValue;
4+
use crate::interpreter::utils::consts::OBJECT;
45
use crate::interpreter::variables_containers::{GlobalScope, VariablesMap};
56
use crate::utils::cell_ref::{gc_box_from, GcBox};
6-
use crate::interpreter::utils::consts::{OBJECT};
77

88
pub fn get_object_class() -> GcBox<RuntimeObject> {
99
return RuntimeObject::new_gc(VariablesMap::new(), None, OBJECT.into());

src/lib/interpreter/environment/expression_evaluation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use std::ops::{Add, Div, Mul, Rem, Sub};
22
use std::ops::Deref;
33

4-
use gc::GcCellRefMut;
5-
64
use crate::ast::expression::{BoxExpression, Expression, MemberIndexer};
75
use crate::ast::operator::Operator;
86
use crate::ast::structs::CallExpression;
97
use crate::errors::{Descriptor, ErrorT, EvilangError, ResultWithError, RuntimeError};
108
use crate::interpreter::environment::Environment;
11-
use crate::interpreter::runtime_values::{GcBoxOfPrimitiveValueExt, PrimitiveValue, ref_to_value::{DerefOfRefToValue, RefToValue}};
9+
use crate::interpreter::runtime_values::{GcBoxOfPrimitiveValueExt, PrimitiveValue, ref_to_value::RefToValue};
1210
use crate::interpreter::runtime_values::functions::ifunction::IFunction;
1311
use crate::interpreter::runtime_values::functions::types::FunctionParameters;
1412
use crate::interpreter::runtime_values::objects::runtime_object::RuntimeObject;
@@ -85,9 +83,11 @@ impl Environment {
8583
RefToValue::new_object_property_ref(object_val, name)
8684
}
8785
Expression::NewObjectExpression(call_expr) => self.eval_new_object_expression(call_expr)?,
86+
/*
8887
expr => {
8988
return Err(ErrorT::UnimplementedExpressionTypeForInterpreter(expr.clone()).into());
9089
}
90+
*/
9191
});
9292
}
9393

src/lib/interpreter/environment/mod.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ use gc::{Finalize, Trace};
55

66
use crate::ast::expression::{Expression, IdentifierT};
77
use crate::ast::statement::{BoxStatement, Statement, StatementList};
8-
use crate::ast::structs::ClassDeclaration;
9-
use crate::errors::{Descriptor, ErrorT, ResultWithError, RuntimeError};
8+
use crate::errors::ResultWithError;
109
use crate::interpreter::environment::default_global_scope::get_default_global_scope;
1110
use crate::interpreter::environment::statement_result::{handle_unrolling, handle_unrolling_in_loop, StatementExecution, StatementMetaGeneration, UnrollingReason};
12-
use crate::interpreter::runtime_values::{PrimitiveValue, ref_to_value::RefToValue};
13-
use crate::interpreter::runtime_values::objects::runtime_object::RuntimeObject;
14-
use crate::interpreter::utils::consts::{OBJECT, SUPER};
11+
use crate::interpreter::runtime_values::PrimitiveValue;
1512
use crate::interpreter::utils::consume_or_clone::ConsumeOrCloneOf;
16-
use crate::interpreter::utils::get_object_superclass;
1713
use crate::interpreter::variables_containers::{GlobalScope, map::{delegate_ivariables_map, IVariablesMap, IVariablesMapConstMembers, IVariablesMapDelegator}, VariableScope, VariablesMap};
1814
use crate::parser::parse;
19-
use crate::utils::cell_ref::{gc_box_from, gc_cell_clone, GcBox};
15+
use crate::utils::cell_ref::{gc_cell_clone, GcBox};
2016

2117
pub mod statement_result;
2218
pub mod expression_evaluation;
@@ -93,20 +89,20 @@ impl Environment {
9389
for decl in decls.iter() {
9490
self.hoist_identifier(&decl.identifier)?;
9591
}
96-
},
92+
}
9793
Statement::FunctionDeclarationStatement(fdecl) => {
9894
self.declare(
9995
&fdecl.name,
100-
PrimitiveValue::new_closure(self, fdecl.clone()).into()
96+
PrimitiveValue::new_closure(self, fdecl.clone()).into(),
10197
)?;
102-
},
98+
}
10399
Statement::ClassDeclarationStatement(cdecl) => {
104100
let class = PrimitiveValue::new_class_by_eval(self, cdecl)?;
105101
self.declare(
106102
&cdecl.name,
107-
class.into()
103+
class.into(),
108104
)?;
109-
},
105+
}
110106
_ => {}
111107
}
112108
return Ok(StatementMetaGeneration::NormalGeneration);
@@ -215,13 +211,13 @@ impl Environment {
215211
};
216212
Ok(StatementExecution::Unrolling(UnrollingReason::ReturningValue(res)))
217213
}
218-
Statement::ClassDeclarationStatement(decl) => {
214+
Statement::ClassDeclarationStatement(..) => {
219215
// Class declaration has already been hoisted
220216
Ok(StatementExecution::NormalFlow)
221-
},
222-
stmt => {
223-
Err(ErrorT::UnimplementedStatementTypeForInterpreter(stmt.clone()).into())
224217
}
218+
/*stmt => {
219+
Err(ErrorT::UnimplementedStatementTypeForInterpreter(stmt.clone()).into())
220+
}*/
225221
};
226222
}
227223

src/lib/interpreter/environment/native_functions/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use std::collections::HashMap;
22

33
use crate::ast::expression::IdentifierT;
4-
use crate::errors::{EvilangError, ResultWithError, RuntimeError};
5-
use crate::interpreter::utils::consts::{OBJECT, INSTANCE_OF_};
4+
use crate::errors::{ResultWithError, RuntimeError};
65
use crate::interpreter::environment::Environment;
76
use crate::interpreter::runtime_values::functions::native_function::{NativeFunction, NativeFunctionFn};
87
use crate::interpreter::runtime_values::functions::types::{FunctionParameters, FunctionReturnValue};
98
use crate::interpreter::runtime_values::objects::runtime_object::RuntimeObject;
109
use crate::interpreter::runtime_values::PrimitiveValue;
10+
use crate::interpreter::utils::consts::INSTANCE_OF_;
1111
use crate::interpreter::utils::get_object_superclass;
12-
use crate::interpreter::variables_containers::map::IVariablesMapConstMembers;
1312
use crate::utils::cell_ref::gc_cell_clone;
1413

1514
pub fn push_res_stack(env: &mut Environment, params: FunctionParameters) -> ResultWithError<FunctionReturnValue> {

src/lib/interpreter/runtime_values/objects/runtime_object.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use std::ops::Deref;
2+
23
use gc::{Finalize, Trace};
4+
35
use crate::ast::expression::IdentifierT;
46
use crate::ast::structs::CallExpression;
57
use crate::errors::{Descriptor, ResultWithError, RuntimeError};
68
use crate::interpreter::environment::Environment;
79
use crate::interpreter::runtime_values::functions::ifunction::IFunction;
810
use crate::interpreter::runtime_values::functions::types::{FunctionParameters, FunctionReturnValue};
911
use crate::interpreter::runtime_values::PrimitiveValue;
10-
use crate::interpreter::runtime_values::ref_to_value::{RefToValue};
11-
12+
use crate::interpreter::runtime_values::ref_to_value::RefToValue;
1213
use crate::interpreter::utils::consts::INSTANCE_OF_;
1314
use crate::interpreter::utils::consume_or_clone::ConsumeOrCloneOf;
1415
use crate::interpreter::variables_containers::map::IVariablesMapConstMembers;
@@ -51,7 +52,7 @@ impl RuntimeObject {
5152
);
5253
}
5354

54-
pub fn call_method_on_object_with_args(this: GcBox<RuntimeObject>, env: &mut Environment, method_name: &IdentifierT, call_expr: &CallExpression)->ResultWithError<FunctionReturnValue> {
55+
pub fn call_method_on_object_with_args(this: GcBox<RuntimeObject>, env: &mut Environment, method_name: &IdentifierT, call_expr: &CallExpression) -> ResultWithError<FunctionReturnValue> {
5556
let Some(method_prop_box) = this.borrow().get_actual(method_name) else {
5657
return Err(RuntimeError::ExpectedFunction(Descriptor::Expression((*call_expr.callee).clone())).into());
5758
};

src/lib/interpreter/runtime_values/ref_to_value/deref_of_ref_to_value.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::ops::Deref;
2+
23
use gc::GcCellRef;
4+
35
use crate::interpreter::runtime_values::PrimitiveValue;
46

57
#[derive(Debug)]

src/lib/interpreter/runtime_values/ref_to_value/mod.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::interpreter::variables_containers::map::{IVariablesMapConstMembers, I
1313
use crate::utils::cell_ref::{gc_box_from, GcBox};
1414

1515
pub mod deref_of_ref_to_value;
16-
pub mod ref_to_object_property;
1716

1817
#[derive(Debug, PartialEq)]
1918
pub enum RefToValue {
@@ -89,15 +88,7 @@ impl RefToValue {
8988
RefToValue::ObjectProperty { snapshot: None, .. } => DerefOfRefToValue::Value(PrimitiveValue::Null),
9089
};
9190
}
92-
93-
// #[inline(always)]
94-
// pub fn try_borrow_mut(&self) -> ResultWithError<GcCellRefMut<PrimitiveValue>> {
95-
// return match self {
96-
// RefToValue::RValue(_) => Err(ErrorT::ExpectedLhsExpression.into()),
97-
// RefToValue::LValue(v) => Ok(v.deref().borrow_mut()),
98-
// };
99-
// }
100-
91+
10192
delegate! {
10293
to match self {
10394
RefToValue::RValue(v) => v,

src/lib/interpreter/runtime_values/ref_to_value/ref_to_object_property.rs

-48
This file was deleted.

src/lib/interpreter/utils/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
pub mod consts;
2-
pub mod consume_or_clone;
3-
41
use crate::errors::{EvilangError, ResultWithError, RuntimeError};
5-
use crate::interpreter::utils::consts::{OBJECT, INSTANCE_OF_};
62
use crate::interpreter::environment::Environment;
73
use crate::interpreter::runtime_values::PrimitiveValue;
4+
use crate::interpreter::utils::consts::OBJECT;
85
use crate::interpreter::variables_containers::map::IVariablesMapConstMembers;
96

7+
pub mod consts;
8+
pub mod consume_or_clone;
9+
1010
pub fn get_object_superclass(env: &mut Environment) -> ResultWithError<PrimitiveValue> {
1111
let object_class_name = OBJECT.to_string();
1212
Ok(env

src/lib/interpreter/variables_containers/map.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use gc::{Finalize, Trace};
77
use crate::ast::expression::IdentifierT;
88
use crate::errors::{ErrorT, ResultWithError};
99
use crate::interpreter::runtime_values::{GcBoxOfPrimitiveValueExt, PrimitiveValue};
10-
use crate::interpreter::runtime_values::ref_to_value::RefToValue;
1110
use crate::utils::cell_ref::{gc_box_from, gc_cell_clone, GcBox};
1211

1312
pub trait IVariablesMapConstMembers {

src/lib/interpreter/variables_containers/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use std::ops::DerefMut;
2-
31
use gc::{Finalize, Trace};
42

53
pub use map::VariablesMap;
64
pub use scope::VariableScope;
75

86
use crate::ast::expression::IdentifierT;
97
use crate::errors::ResultWithError;
10-
use crate::interpreter::runtime_values::{GcBoxOfPrimitiveValueExt, PrimitiveValue};
11-
use crate::interpreter::runtime_values::ref_to_value::RefToValue;
8+
use crate::interpreter::runtime_values::PrimitiveValue;
129
use crate::interpreter::variables_containers::map::{delegate_ivariables_map, IVariablesMapConstMembers, IVariablesMapDelegator};
1310
use crate::utils::cell_ref::{gc_box_from, GcBox};
1411

src/lib/interpreter/variables_containers/scope.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use gc::{Finalize, Trace};
44
use crate::ast::expression::IdentifierT;
55
use crate::errors::ResultWithError;
66
use crate::interpreter::runtime_values::PrimitiveValue;
7-
use crate::interpreter::runtime_values::ref_to_value::RefToValue;
87
use crate::interpreter::variables_containers::map::{IVariablesMapConstMembers, IVariablesMapDelegator, VariablesMap};
98
use crate::interpreter::variables_containers::map::IVariablesMap;
109
use crate::utils::cell_ref::{gc_box_from, gc_cell_clone, GcBox};
@@ -19,7 +18,7 @@ pub trait IGenericVariablesScope<T: IGenericVariablesScope<T> + 'static>: Trace
1918
return self_vars;
2019
}
2120
let mut parent_opt = self.get_parent();
22-
while let Some(mut parent) = parent_opt {
21+
while let Some(parent) = parent_opt {
2322
let v_borrow = parent.borrow();
2423
let v_vars = v_borrow.get_variables();
2524
if v_vars.borrow().contains_key(name) {

src/lib/parser.rs

-4
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,6 @@ impl Parser {
600600
}
601601
return match token_type {
602602
TokenType::OpenParen => self.parenthesized_expression(),
603-
// TokenType::Keyword(Keyword::This) => {
604-
// self.eat(TokenType::Keyword(Keyword::This))?;
605-
// Ok(Expression::ThisExpression)
606-
// }
607603
TokenType::Keyword(Keyword::New) => self.new_expression(),
608604
TokenType::Keyword(Keyword::Fn) => self.function_expression(),
609605
TokenType::Keyword(Keyword::Class) => self.class_declaration_expression(),

src/lib/tokenizer/matchers.rs

-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ pub(super) fn get_token_matchers() -> Vec<(Matcher, Option<TokenType>)> {
7272
(starts_with_matcher("]"), Some(TokenType::CloseSquareBracket)),
7373
(starts_with_matcher(","), Some(TokenType::Comma)),
7474
(starts_with_matcher("."), Some(TokenType::Dot)),
75-
// (starts_with_matcher("::"), Some(TokenType::DoubleColon)),
7675
(starts_with_matcher("->"), Some(TokenType::Arrow)),
7776
//
7877
(one_of_many(["==", "!="]), Some(TokenType::EqualityOperator)),
@@ -103,8 +102,6 @@ pub(super) fn get_token_matchers() -> Vec<(Matcher, Option<TokenType>)> {
103102
(keyword_matcher("class"), Some(TokenType::Keyword(Keyword::Class))),
104103
(keyword_matcher("extends"), Some(TokenType::Keyword(Keyword::Extends))),
105104
(keyword_matcher("new"), Some(TokenType::Keyword(Keyword::New))),
106-
// (keyword_matcher("super"), Some(TokenType::Keyword(Keyword::Super))),
107-
// (keyword_matcher("this"), Some(TokenType::Keyword(Keyword::This))),
108105
(keyword_matcher("break"), Some(TokenType::Keyword(Keyword::Break))),
109106
(keyword_matcher("continue"), Some(TokenType::Keyword(Keyword::Continue))),
110107
//

src/lib/tokenizer/token.rs

-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ pub enum Keyword {
1515
Class,
1616
Extends,
1717
New,
18-
// Super,
19-
// This,
2018
Break,
2119
Continue,
2220
}
@@ -47,7 +45,6 @@ pub enum TokenType {
4745
Comma,
4846
Dot,
4947
Arrow,
50-
// DoubleColon,
5148
Keyword(Keyword),
5249
}
5350

src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ fn main() {
3737
let mut env = Environment::new();
3838
let program = r#"
3939
class SuperClass {
40-
4140
}
4241
SuperClass.x = 12;
4342
class Point extends SuperClass {

0 commit comments

Comments
 (0)