Skip to content

Commit

Permalink
rexx: backport recent changes from stepA to steps 2-9
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Oct 8, 2024
1 parent 1b63fd7 commit 84e287b
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 298 deletions.
58 changes: 30 additions & 28 deletions impls/rexx/step2_eval.rexx
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,29 @@ exit
read: procedure expose values. err /* read(str) */
return read_str(arg(1))

eval_ast: procedure expose values. env. err /* eval_ast(ast) */
eval: procedure expose values. env. err /* eval(ast) */
ast = arg(1)

-- call lineout , ("EVAL: " || print(ast))

type = obj_type(ast)
val = obj_val(ast)
astval = obj_val(ast)
select
when type == "symb" then do
varname = val
varname = astval
if env.varname == "" then do
err = "'" || varname || "' not found"
return "ERR"
end
return env.varname
end
when type == "list" then do
res = ""
do i=1 to words(val)
element = eval(word(val, i))
if element == "ERR" then return "ERR"
if i > 1 then
res = res || " " || element
else
res = element
end
return new_list(res)
when type == "list" & words(astval) > 0 then do
-- proceed after this select statement
end
when type == "vect" then do
res = ""
do i=1 to words(val)
element = eval(word(val, i))
do i=1 to words(astval)
element = eval(word(astval, i), env_idx)
if element == "ERR" then return "ERR"
if i > 1 then
res = res || " " || element
Expand All @@ -48,8 +42,8 @@ eval_ast: procedure expose values. env. err /* eval_ast(ast) */
end
when type == "hash" then do
res = ""
do i=1 to words(val)
element = eval(word(val, i))
do i=1 to words(astval)
element = eval(word(astval, i), env_idx)
if element == "ERR" then return "ERR"
if i > 1 then
res = res || " " || element
Expand All @@ -62,16 +56,24 @@ eval_ast: procedure expose values. env. err /* eval_ast(ast) */
return ast
end

eval: procedure expose values. env. err /* eval(ast) */
ast = arg(1)
if \list?(ast) then return eval_ast(ast)
astval = obj_val(ast)
if words(astval) == 0 then return ast
lst_obj = eval_ast(ast)
if lst_obj == "ERR" then return "ERR"
lst = obj_val(lst_obj)
f = word(lst, 1)
call_args = subword(lst, 2)
-- ast is a non-empty list

a0 = word(astval, 1)
f = eval(a0, env_idx)
if f == "ERR" then return "ERR"

-- Evaluate the arguments and store them to lst.
lst = ""
do i=2 to words(astval)
element = eval(word(astval, i), env_idx)
if element == "ERR" then return "ERR"
if i > 2 then
lst = lst || " " || element
else
lst = element
end

call_args = lst
call_list = ""
do i=1 to words(call_args)
element = '"' || word(call_args, i) || '"'
Expand Down
63 changes: 33 additions & 30 deletions impls/rexx/step3_env.rexx
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,25 @@ exit
read: procedure expose values. err /* read(str) */
return read_str(arg(1))

eval_ast: procedure expose values. env. err /* eval_ast(ast, env_idx) */
eval: procedure expose values. env. err /* eval(ast) */
ast = arg(1)
env_idx = arg(2)

debug_eval = obj_type(env_get(env_idx, "DEBUG-EVAL"))
if debug_eval <> "ERR" & debug_eval <> "nill" & debug_eval <> "fals" then,
call lineout , ("EVAL: " || print(ast))

type = obj_type(ast)
val = obj_val(ast)
astval = obj_val(ast)
select
when type == "symb" then return env_get(env_idx, val)
when type == "list" then do
res = ""
do i=1 to words(val)
element = eval(word(val, i), env_idx)
if element == "ERR" then return "ERR"
if i > 1 then
res = res || " " || element
else
res = element
end
return new_list(res)
when type == "symb" then return env_get(env_idx, astval)
when type == "list" & words(astval) > 0 then do
-- proceed after this select statement
end
when type == "vect" then do
res = ""
do i=1 to words(val)
element = eval(word(val, i), env_idx)
do i=1 to words(astval)
element = eval(word(astval, i), env_idx)
if element == "ERR" then return "ERR"
if i > 1 then
res = res || " " || element
Expand All @@ -43,8 +39,8 @@ eval_ast: procedure expose values. env. err /* eval_ast(ast, env_idx) */
end
when type == "hash" then do
res = ""
do i=1 to words(val)
element = eval(word(val, i), env_idx)
do i=1 to words(astval)
element = eval(word(astval, i), env_idx)
if element == "ERR" then return "ERR"
if i > 1 then
res = res || " " || element
Expand All @@ -57,13 +53,10 @@ eval_ast: procedure expose values. env. err /* eval_ast(ast, env_idx) */
return ast
end

eval: procedure expose values. env. err /* eval(ast) */
ast = arg(1)
env_idx = arg(2)
if \list?(ast) then return eval_ast(ast, env_idx)
astval = obj_val(ast)
if words(astval) == 0 then return ast
a0sym = obj_val(word(astval, 1))
-- ast is a non-empty list

a0 = word(astval, 1)
a0sym = obj_val(a0)
select
when a0sym == "def!" then do
a1sym = obj_val(word(astval, 2))
Expand All @@ -83,11 +76,21 @@ eval: procedure expose values. env. err /* eval(ast) */
return eval(word(astval, 3), letenv_idx)
end
otherwise
lst_obj = eval_ast(ast, env_idx)
if lst_obj == "ERR" then return "ERR"
lst = obj_val(lst_obj)
f = word(lst, 1)
call_args = subword(lst, 2)
f = eval(a0, env_idx)
if f == "ERR" then return "ERR"

-- Evaluate the arguments and store them to lst.
lst = ""
do i=2 to words(astval)
element = eval(word(astval, i), env_idx)
if element == "ERR" then return "ERR"
if i > 2 then
lst = lst || " " || element
else
lst = element
end

call_args = lst
call_list = ""
do i=1 to words(call_args)
element = '"' || word(call_args, i) || '"'
Expand Down
65 changes: 34 additions & 31 deletions impls/rexx/step4_if_fn_do.rexx
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,25 @@ exit
read: procedure expose values. err /* read(str) */
return read_str(arg(1))

eval_ast: procedure expose values. env. err /* eval_ast(ast, env_idx) */
eval: procedure expose values. env. err /* eval(ast) */
ast = arg(1)
env_idx = arg(2)

debug_eval = obj_type(env_get(env_idx, "DEBUG-EVAL"))
if debug_eval <> "ERR" & debug_eval <> "nill" & debug_eval <> "fals" then,
call lineout , ("EVAL: " || print(ast))

type = obj_type(ast)
val = obj_val(ast)
astval = obj_val(ast)
select
when type == "symb" then return env_get(env_idx, val)
when type == "list" then do
res = ""
do i=1 to words(val)
element = eval(word(val, i), env_idx)
if element == "ERR" then return "ERR"
if i > 1 then
res = res || " " || element
else
res = element
end
return new_list(res)
when type == "symb" then return env_get(env_idx, astval)
when type == "list" & words(astval) > 0 then do
-- proceed after this select statement
end
when type == "vect" then do
res = ""
do i=1 to words(val)
element = eval(word(val, i), env_idx)
do i=1 to words(astval)
element = eval(word(astval, i), env_idx)
if element == "ERR" then return "ERR"
if i > 1 then
res = res || " " || element
Expand All @@ -44,8 +40,8 @@ eval_ast: procedure expose values. env. err /* eval_ast(ast, env_idx) */
end
when type == "hash" then do
res = ""
do i=1 to words(val)
element = eval(word(val, i), env_idx)
do i=1 to words(astval)
element = eval(word(astval, i), env_idx)
if element == "ERR" then return "ERR"
if i > 1 then
res = res || " " || element
Expand All @@ -58,13 +54,10 @@ eval_ast: procedure expose values. env. err /* eval_ast(ast, env_idx) */
return ast
end

eval: procedure expose values. env. err /* eval(ast) */
ast = arg(1)
env_idx = arg(2)
if \list?(ast) then return eval_ast(ast, env_idx)
astval = obj_val(ast)
if words(astval) == 0 then return ast
a0sym = obj_val(word(astval, 1))
-- ast is a non-empty list

a0 = word(astval, 1)
a0sym = obj_val(a0)
select
when a0sym == "def!" then do
a1sym = obj_val(word(astval, 2))
Expand Down Expand Up @@ -103,13 +96,23 @@ eval: procedure expose values. env. err /* eval(ast) */
end
when a0sym == "fn*" then return new_func(word(astval, 3), env_idx, word(astval, 2))
otherwise
lst_obj = eval_ast(ast, env_idx)
if lst_obj == "ERR" then return "ERR"
lst = obj_val(lst_obj)
f = word(lst, 1)
f = eval(a0, env_idx)
if f == "ERR" then return "ERR"

-- Evaluate the arguments and store them to lst.
lst = ""
do i=2 to words(astval)
element = eval(word(astval, i), env_idx)
if element == "ERR" then return "ERR"
if i > 2 then
lst = lst || " " || element
else
lst = element
end

select
when nativefn?(f) then do
call_args = subword(lst, 2)
call_args = lst
call_list = ""
do i=1 to words(call_args)
element = '"' || word(call_args, i) || '"'
Expand All @@ -123,7 +126,7 @@ eval: procedure expose values. env. err /* eval(ast) */
return res
end
when func?(f) then do
call_args = new_list(subword(lst, 2))
call_args = new_list(lst)
return eval(func_body_ast(f), new_env(func_env_idx(f), func_binds(f), call_args))
end
otherwise
Expand Down
Loading

0 comments on commit 84e287b

Please sign in to comment.