@@ -2,6 +2,7 @@ use crate::ast::AST;
22
33use std:: { collections:: HashMap , path:: PathBuf } ;
44use crate :: utils;
5+ use crate :: packages:: get_package;
56
67pub fn eval ( expr : AST , context : & mut HashMap < String , AST > ) -> Result < AST , String > {
78 let mut depth = 0 ;
@@ -22,6 +23,10 @@ pub fn eval(expr: AST, context: &mut HashMap<String, AST>) -> Result<AST, String
2223 }
2324
2425 for expr in body {
26+ if let AST :: Return { value, line : _ } = expr {
27+ return eval ( * value. clone ( ) , & mut new_context) ;
28+ }
29+
2530 eval ( expr. clone ( ) , & mut new_context) ?;
2631 }
2732 } else {
@@ -93,39 +98,50 @@ pub fn eval(expr: AST, context: &mut HashMap<String, AST>) -> Result<AST, String
9398 let path: PathBuf ;
9499
95100 if args. len ( ) > 2 {
96- path = std:: path:: Path :: new ( & args[ 2 ] ) . parent ( ) . unwrap ( ) . join ( file. unwrap ( ) . replace ( "\" " , "" ) ) ;
101+ path = std:: path:: Path :: new ( & args[ 2 ] ) . parent ( ) . unwrap ( ) . join ( file. clone ( ) . unwrap ( ) . replace ( "\" " , "" ) ) ;
97102 } else {
98- path = file. unwrap ( ) . replace ( "\" " , "" ) . into ( ) ;
103+ path = file. clone ( ) . unwrap ( ) . replace ( "\" " , "" ) . into ( ) ;
99104 }
100105
101- match std:: fs:: read_to_string ( & path) {
102- Ok ( file) => {
103- let mut new_context = context. clone ( ) ;
104-
105- match crate :: parser:: parse ( & file, & mut new_context) {
106- Ok ( _) => {
107- let mut properties = crate :: utils:: create_context ( ) ;
108-
109- for ( name, value) in new_context {
110- properties. insert ( name, value) ;
106+ if path. ends_with ( ".modu" ) {
107+ match std:: fs:: read_to_string ( & path) {
108+ Ok ( file) => {
109+ let mut new_context = context. clone ( ) ;
110+
111+ match crate :: parser:: parse ( & file, & mut new_context) {
112+ Ok ( _) => {
113+ let mut properties = crate :: utils:: create_context ( ) ;
114+
115+ for ( name, value) in new_context {
116+ properties. insert ( name, value) ;
117+ }
118+
119+ context. insert ( as_. unwrap ( ) , AST :: Object { properties, line } ) ;
120+ }
121+
122+ Err ( e) => {
123+ return Err ( e. 0 ) ;
111124 }
112-
113- context. insert ( as_. unwrap ( ) , AST :: Object { properties, line } ) ;
114- }
115-
116- Err ( e) => {
117- return Err ( e. 0 ) ;
118125 }
119126 }
127+
128+ Err ( e) => {
129+ dbg ! ( path) ;
130+
131+ return Err ( e. to_string ( ) ) ;
132+ }
120133 }
134+ } else {
135+ let package = get_package ( & file. clone ( ) . unwrap ( ) . replace ( "\" " , "" ) ) ;
121136
122- Err ( e) => {
123- dbg ! ( path) ;
124-
125- return Err ( e. to_string ( ) ) ;
137+ if let Some ( package) = package {
138+ if let AST :: Object { properties, line } = package {
139+ context. insert ( as_. unwrap ( ) , AST :: Object { properties, line } ) ;
140+ }
141+ } else {
142+ return Err ( format ! ( "Package {} not found" , file. unwrap( ) . replace( "\" " , "" ) ) ) ;
126143 }
127144 }
128-
129145 }
130146
131147 AST :: PropertyCall { object, property, args, line } => {
@@ -154,8 +170,16 @@ pub fn eval(expr: AST, context: &mut HashMap<String, AST>) -> Result<AST, String
154170 }
155171 }
156172
173+ AST :: InternalFunction { name, args : f_args, call_fn } => {
174+ if args. len ( ) == f_args. len ( ) {
175+ return call_fn ( args, context) ;
176+ } else {
177+ return Err ( format ! ( "{} takes {} arguments" , name, f_args. len( ) ) ) ;
178+ }
179+ }
180+
157181 _ => {
158- return Err ( format ! ( "{} of {} is not a function" , property. as_ref( ) . unwrap( ) , name) ) ;
182+ return Err ( format ! ( "{} on object {} is not a function" , property. as_ref( ) . unwrap( ) , name) ) ;
159183 }
160184 }
161185 }
@@ -390,9 +414,7 @@ pub fn eval(expr: AST, context: &mut HashMap<String, AST>) -> Result<AST, String
390414 }
391415 }
392416
393- unsafe {
394- depth -= 1 ;
395- }
417+ depth -= 1 ;
396418
397419 Ok ( AST :: Null )
398420}
0 commit comments