These routines traverse (walk) the Abstract Syntax Tree (AST) created by the parser. In this case, a string corresponding to the translation of the AST into an equivalent Slim program is returned by the top most call (Walk) made in expr.slm. Other walker modules might directly interpret the AST, or partially evaluate it, or translate it into a different target language.
import custwalk
The routines in custwalk.slm, if any, will appear as annotations of nodes in the AST (the first elements of the lists corresponding to the nodes) and will be called by Walk.
export Object_id_for, Runcall, Walk, Walk_list
These routines are exported for use by the parser and AST builder, as well as custbld.slm.
func Walk(T) -> Entity
if T(1) in {Runcall, Walk_list}
return T(1)(tl(T))
else
return eval(T(1), tl(T))
end
This routine is a general routine that calls specific routines to walk the AST represented by T. The first element of T is the annotation of the root node and represents the specific routine (belonging either to this module, or to custwalk.slm) that is to be used to walk the node. If the specific routine is Runcall or Walk_list, the children of the node (the remaining elements of T) is passed as a single list. If it is any other routine, the children are passed as individual parameters.
In other words, if T = [Walk_list x y z] then the call is Walk_list([x y z]) whereas if T = [Object_id_for `integer' `123'] the call is Object_id_for(`integer', `123').
func Runcall(lst) -> Entity
return lst(1) + as_string(Walk_list(rest(lst)))
The first parameter of this call is the name of the Slim routine to be called at run-time. In the case of the expression language this will be a routine belonging to identifier.slm or number.slm. The subsequent parameters are ASTs which represent expressions which form the run-time parameters of the call. These ASTs are walked (turned into strings) and collected into a parameter list by as_string. For example:
Runcall([`plus' [Object_id_for `number' `1'] [Object_id_for `number' 2]])
will result in the string `plus(new_number(`1'), new_number(`2'))'
func Walk_list(lst) -> list of Entity
return [all Walk(i) for i in lst]
This function takes a list of ASTs as parameter and returns a list of walked (translated) ASTs as result.
func Object_id_for(cn, iv) -> Entity
return `new_'+cn+`(\`'+iv+`\')
This function returns a string that represents a run-time call to a constructor of an object of class cn. The constructor is assumed to take a single string as a parameter.
func as_string(lst) -> String
return `()' when lst = []
result := `('
for e in all_but_last(lst)
result +:= mkstr(e)+`, '
loop
result +:= mkstr(last(lst))+`)'
This function takes a list of strings and turns it into a comma delimited, bracketed string. For example, [`x' `y' `z'] becomes `(x, y, z)'.