@@ -5,7 +5,7 @@ def evaluate(ast, env={}):
55 "*" , "/" , "-" , "+" , "let" , "print" , "scan" , "if" , "elif" , "else" , "else" ,"while" ,
66 "list" , "append" , "index" , ">" , "<" , ">=" , "<=" , "==" , "!=" ,
77 "true" , "false" , "&" , "|" , "sqrt" , "pow" , "mod" , "abs" ,
8- "len" , "reverse" , "concat" , "strlen" , "substr" , "fn" , "call"
8+ "len" , "reverse" , "concat" , "strlen" , "substr" , "fn" , "call" , "native"
99 ]
1010
1111 E = lambda x : evaluate (x , env )
@@ -183,7 +183,10 @@ def evaluate(ast, env={}):
183183
184184 if token == "fn" : #fn define
185185 fnname = ast [idx + 1 ]
186- fnvars = ast [idx + 2 ].split ("|" )# the format wil be like fn sayhi x|y|z code
186+ if "|" in ast [idx + 2 ]:
187+ fnvars = ast [idx + 2 ].split ("|" )# the format wil be like fn sayhi x|y|z code
188+ else :
189+ fnvars = ast [idx + 2 ]
187190 fnbody = ast [idx + 3 :]
188191
189192 env [fnname ] = [fnvars , fnbody ]
@@ -201,6 +204,17 @@ def evaluate(ast, env={}):
201204 result = evaluate (expr , localenv )
202205 return result
203206
207+ if token == "native" :
208+ code = evaluate (ast [1 ], env )
209+ if not isinstance (code , str ):
210+ return code
211+ # Try expression first
212+ try :
213+ return eval (code , env ) # expression
214+ except SyntaxError :
215+ exec (code , env ) # statement/block
216+ return None
217+
204218 # ---- Base case: literals ----
205219 elif isinstance (ast , (int , float )):
206220 return ast
0 commit comments