|
16 | 16 | # IMPORTS |
17 | 17 | ########################################################## |
18 | 18 |
|
| 19 | +import re |
19 | 20 | from strings_with_arrows import * |
20 | 21 | from mpmath import mpf, mpc, mp # type: ignore |
21 | 22 | from colorama import just_fix_windows_console # type: ignore |
@@ -228,6 +229,8 @@ def make_tokens(self): |
228 | 229 | while self.current_char is not None: |
229 | 230 | if self.current_char in ' \t': |
230 | 231 | self.advance() |
| 232 | + elif self.current_char == '#': |
| 233 | + self.skip_comment() |
231 | 234 | elif self.current_char in ';\n': |
232 | 235 | tokens.append(Token(TT_NEWLINE, pos_start=self.pos)) |
233 | 236 | self.advance() |
@@ -415,6 +418,14 @@ def make_greater_than(self): |
415 | 418 |
|
416 | 419 | return Token(tok_type, pos_start=pos_start, pos_end=self.pos) |
417 | 420 |
|
| 421 | + def skip_comment(self): |
| 422 | + self.advance() |
| 423 | + |
| 424 | + while self.current_char != '\n': |
| 425 | + self.advance() |
| 426 | + |
| 427 | + self.advance() |
| 428 | + |
418 | 429 | ########################################################## |
419 | 430 | # NODES |
420 | 431 | ########################################################## |
@@ -2404,6 +2415,62 @@ def execute_cos(self, exec_ctx): |
2404 | 2415 | execute_cos.positional_arg_names = ["theta"] # type: ignore |
2405 | 2416 | execute_cos.optional_arg_names = {} # type: ignore |
2406 | 2417 |
|
| 2418 | + def execute_exec(self, exec_ctx): |
| 2419 | + code_or_filename = exec_ctx.symbol_table.get('code_or_filename') |
| 2420 | + |
| 2421 | + if not isinstance(code_or_filename, String): |
| 2422 | + return RTResult.failure(RTError( |
| 2423 | + self.pos_start, self.pos_end, |
| 2424 | + "Argument code_or_filename must be a String.", |
| 2425 | + exec_ctx |
| 2426 | + )) |
| 2427 | + |
| 2428 | + code_or_filename = code_or_filename.value |
| 2429 | + is_filename = re.match('((^([a-z]|[A-Z]):(?=\\(?![\0-\37<>:"/\\|?*])|\/(?![\0-\37<>:"/\\|?*])|$)|^\\(?=[\\\/][^\0-\37<>:"/\\|?*]+)|^(?=(\\|\/)$)|^\.(?=(\\|\/)$)|^\.\.(?=(\\|\/)$)|^(?=(\\|\/)[^\0-\37<>:"/\\|?*]+)|^\.(?=(\\|\/)[^\0-\37<>:"/\\|?*]+)|^\.\.(?=(\\|\/)[^\0-\37<>:"/\\|?*]+))((\\|\/)[^\0-\37<>:"/\\|?*]+|(\\|\/)$)*()$)|(^\/$|(^(?=\/)|^\.|^\.\.)(\/(?=[^/\0])[^/\0]+)*\/?$)', code_or_filename) |
| 2430 | + filename = code_or_filename if is_filename else '<code>' |
| 2431 | + |
| 2432 | + if is_filename: |
| 2433 | + try: |
| 2434 | + with open(filename, 'r') as file: |
| 2435 | + code = file.read() |
| 2436 | + except Exception as e: |
| 2437 | + return RTResult().failure(RTError( |
| 2438 | + self.pos_start, self.pos_end, |
| 2439 | + f'Failed to open file "{filename}" because of the following exception:\n{e}', |
| 2440 | + exec_ctx |
| 2441 | + )) |
| 2442 | + else: |
| 2443 | + code = code_or_filename |
| 2444 | + |
| 2445 | + _, error = run(filename, code) |
| 2446 | + |
| 2447 | + if error: |
| 2448 | + return RTResult().failure(RTError( |
| 2449 | + self.pos_start, self.pos_end, |
| 2450 | + f'Failed to run "{filename}" because of the following exception:\n{error}', |
| 2451 | + exec_ctx |
| 2452 | + )) |
| 2453 | + |
| 2454 | + return RTResult().success(NullType()) |
| 2455 | + execute_run.positional_arg_names = ["code_or_filename"] # type: ignore |
| 2456 | + execute_run.optional_arg_names = {} # type: ignore |
| 2457 | + |
| 2458 | + def execute_length(self, exec_ctx): |
| 2459 | + iterable = exec_ctx.symbol_table.get('iterable') |
| 2460 | + |
| 2461 | + if not isinstance(iterable, (List, String)): |
| 2462 | + return RTResult().failure(RTError( |
| 2463 | + self.pos_start, self.pos_end, |
| 2464 | + "Argument iterable must be a List or a String.", |
| 2465 | + exec_ctx |
| 2466 | + )) |
| 2467 | + |
| 2468 | + return RTResult().success(Integer( |
| 2469 | + len(iterable.elements if isinstance(iterable, List) else iterable.value) |
| 2470 | + )) |
| 2471 | + execute_length.positional_arg_names = ["iterable"] # type: ignore |
| 2472 | + execute_length.optional_arg_names = {} # type: ignore |
| 2473 | + |
2407 | 2474 | ########################################################## |
2408 | 2475 | # CONTEXT |
2409 | 2476 | ########################################################## |
@@ -2750,6 +2817,8 @@ def visit_BreakNode(self, node, context): |
2750 | 2817 | global_symbol_table.set('type', BuiltInFunction('type')) |
2751 | 2818 | global_symbol_table.set('sin', BuiltInFunction('sin')) |
2752 | 2819 | global_symbol_table.set('cos', BuiltInFunction('cos')) |
| 2820 | +global_symbol_table.set('exec', BuiltInFunction('exec')) |
| 2821 | +global_symbol_table.set('length', BuiltInFunction('length')) |
2753 | 2822 | global_symbol_table.set('pi', Decimal(3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802758995)) |
2754 | 2823 | global_symbol_table.set('e', Decimal(2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408914993488416750924476146066808226480016847741185374234544243710753907774499206955170276183860626133138458300075204493382656029760673711320070932870912744374704723069697720931014169283681902551510865746377211125238978442505695369677078544996996794686445490598793163688923009879312773617821542499922957635148220826989519366803318252886939849646510582093923982948879332036250944311730123819706841614039701983767932068328237646480429531180232878250981945581530175671736133206981125099618188159304169035159888851934580727386673858942287922849989208680582574927961048419844436346324496848756023362482704197862320900216099023530436994184914631409343173814364054625315209618369088870701676839642437814059271456354906130310720851038375051011574770417189861068739696552126715468895703503540212340784981933432106817012100562788023519303322474501585390473041995777709350366041699732972508868769664035557071622684471625607988265178713419512466520103059212366771943252786753985589448969709640975459185695638023637016211204774272283648961342251644507818244235294863637214174023889344124796357437026375529444833799801612549227850925778256209262264832627793338656648162772516401910590049164499828931505660472580277863186415519565324425869829469593080191529872117255634754639644791014590409058629849679128740687050489585867174798546677575732056812884592054133405392200011378630094556068816674001698420558040336379537645203040243225661352783695117788386387443966253224985065499588623428189970773327617178392803494650143455889707194258639877275471096295374152111513683506275260232648472870392076431005958411661205452970302364725492966693811513732275364509888903136020572481765851180630364428123149655070475102544650117272115551948668508003685322818315219600373562527944951582841882947876108526398144)) |
2755 | 2824 |
|
|
0 commit comments