From 9183b04cd7c082d5281aa1f95bda0927c3ed93b0 Mon Sep 17 00:00:00 2001 From: Naveen Hettiwaththa Date: Mon, 26 Dec 2022 12:03:07 +0530 Subject: [PATCH] fixed set local variable issues --- .../__pycache__/var_util.cpython-310.pyc | Bin 1833 -> 1936 bytes lib/RuntimUtil/var_util.py | 22 +++++++++++++----- test/t1.pr | 19 ++++++++++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/RuntimUtil/__pycache__/var_util.cpython-310.pyc b/lib/RuntimUtil/__pycache__/var_util.cpython-310.pyc index ee6f9427a4bd048bb69ee2fd160abb0844a1e4d7..0abe4c82b228976aab576551d4a5c774c64e70f2 100644 GIT binary patch delta 819 zcmY+CPfrs;7{+I2cV}n+;-8`|g4AMy3z5V`IW_dCCt|{_#?;bTU}3x9bV`)0A)&^L ziLu$ECyyLF@EJV&1^Ny28yFMyeOnUi9)9!A^US=@%w*5Qk2B$65V!=sAC2?Q6K@#K z)2GALg+`Y*IVF-vmfBs>6i^$|d`p^+v?Pbom9`WxdeV_DjK1`?NaTOVUJ)M-R{6*} zYOD6emm#I1Xs`iYEWWZ2<{elt{5HqRw0LQhP02p9;=S?U^pUo6qe9-8xuNZ2svc?~ zMQ%uEV4Tsc461WX)P{B@tFjqTTu>fs_fp~0JG7TWr1rF*o6@b23K`G?Lp_Hz-hiG_ z%_Xl8X&*3^=1kkC*hKnul9T;li(JON25TJnpM$aQz!?YsbEe~?U{g78Q-zPKJiv){ z+*bFNPuOy#LKJ5ZSI}{*{p?i6J4qHxbrt0RaSafKN?=_=%pz_eZURQZZme6q{&r8R zB->RM8XQqUmttQZ_(gKIN|X3FP7z(DZ++rQ(@&QJQLqt!sd-qRMA!tyt$# z9qovYk20MN>g1Y-+xu}xw-WgRq#gs9RIR|U83n~Bet&0t)zS4d*}`2X{Pzx;9b}aF&EnVz`Bw4b$~I}p^h*nI@SrsnyzgzC4Z5R@+W8OmtW2SLie1H zVaa{tAEE8uh!+3OTqPP#!h*XH?UTngtVGV26_rqr>fyHN$e(_%Epv5E=xJaEBlWIYRknk@CBQCZUDsO1xNaSdMJJ9291W^eOJS zp5J+}isuY&LIx5%kUWA2GyXRc6dQP!TteWSL6YzMQ zXGpW`J_Txn+7(RiGCLjBs~r!v_lkk-4fSg>;Rp|m!mz2N`yJeO55u~eK2sY-(;?8z WY0r$~|E0>XE}KjOF0d9Y%Jd%t7Jw1} diff --git a/lib/RuntimUtil/var_util.py b/lib/RuntimUtil/var_util.py index 7eebf71..e09c08a 100644 --- a/lib/RuntimUtil/var_util.py +++ b/lib/RuntimUtil/var_util.py @@ -1,4 +1,4 @@ -from lib.RuntimUtil.Mem import var , sample_data_types , local , scope , mem +from lib.RuntimUtil.Mem import var , sample_data_types , local , scope , mem , param from lib.util.lex_helper import remove_unwanted_whitespaces @@ -46,7 +46,7 @@ def handle_var_statement(statement): def handle_set_var_statement(statement): st = remove_unwanted_whitespaces( statement.raw_statement.strip() ) - val = eval(st.split("=")[1]) + val = eval(st.split("=" , 1)[1]) var_name = st.split(' ')[1] var_name = get_substring(var_name , "var('" , "')") var_instance = None @@ -56,17 +56,27 @@ def handle_set_var_statement(statement): if var_name not in mem : raise Exception('Variable name ' , var_name , ' undefined') else: - var_instance = local[var_name] - if var_name not in local : + if var_name in local : + var_instance = local[var_name] + elif var_name in param: + var_instance = param[var_name] + else: raise Exception('Variable name ' , var_name , ' undefined') - + + if type(val) != type(sample_data_types[ var_instance['datatype'] ]): raise Exception('Invalid data type') if scope['func_name'] == 'global' : mem[var_name]['value'] = val else: - local[var_name]['value'] = val + + if var_name in local: + local[var_name]['value'] = val + elif var_name in param: + param[var_name]['value'] = val + else: + raise Exception('Variable name ' , var_name , ' undefined') return statement.next diff --git a/test/t1.pr b/test/t1.pr index 34f2ec8..ac76f5b 100644 --- a/test/t1.pr +++ b/test/t1.pr @@ -5,6 +5,17 @@ call sum ->( var('n1') , var('n1') ) endfunction + function factorial (int n) -> + var int total = 1 + + while var('n') > 0 + set var('total') = var('total') * var('n') + set var('n') = var('n') - 1 + endwhile + + print 'Factorial is : ' + str( var('total') ) + + endfunction function InputNumbers () -> var int x1 = int( input('Enter number 1 : ') ) @@ -12,6 +23,12 @@ call sum ->( var('x1') , var('x2') ) endfunction - call InputNumbers ->() + function InputNumber () -> + var int x1 = int( input('Enter number 1 : ') ) + call factorial ->( var('x1') ) + endfunction + + ~ call InputNumbers ->() + call InputNumber ->() @end \ No newline at end of file