Skip to content

Commit

Permalink
fixed set local variable issues
Browse files Browse the repository at this point in the history
  • Loading branch information
NaveenDanj committed Dec 26, 2022
1 parent fbd407f commit 9183b04
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
Binary file modified lib/RuntimUtil/__pycache__/var_util.cpython-310.pyc
Binary file not shown.
22 changes: 16 additions & 6 deletions lib/RuntimUtil/var_util.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
19 changes: 18 additions & 1 deletion test/t1.pr
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,30 @@
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 : ') )
var int x2 = int( input('Enter number 2 : ') )
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

0 comments on commit 9183b04

Please sign in to comment.