RosettaCodeData/Task/Tree-datastructures/M2000-Interpreter/tree-datastructures.m2000

89 lines
1.6 KiB
Plaintext

module Tree_datastructures (a$){
terminal=(,)
document b$, all$=a$
report a$
nl$={
}
tree=@MakeTree(a$)
traverseTree(tree)
report b$
all$=b$
clear b$
TraverseTreeIndent(tree)
report b$
all$=b$
print b$=a$
all$=(b$=a$)+nl$
clipboard all$
end
sub TraverseTree(t as array, Level=1)
b$=Level+" "+t#val$(0)+nl$
local i
for i=1 to len(t)-1
if t#val(i) is terminal else TraverseTree(t#val(i), Level+1)
next
end sub
sub TraverseTreeIndent(t as array, Level=0)
b$=string$("....", Level) +t#val$(0)+nl$
local i
for i=1 to len(t)-1
if t#val(i) is terminal else TraverseTreeIndent(t#val(i), Level+1)
next
end sub
function MakeTree(a$)
local a$(), c$
a$()=piece$(a$, nl$)
local child=terminal, tree=(a$(0),terminal)
local Level=2, i, j
flush
for i=1 to len(a$())-1
if len(a$(i))=0 then continue
for j=1 to len(a$(i)) div 2
if left$(a$(i), j*4)<>string$(".", j*4) then
c$=mid$(a$(i), (j-1)*4+1)
exit for
end if
next j
while j<level
level--
read tree
end while
if j=level then
if tree is terminal then
Return tree, 1:=(c$, terminal)
else
child=(c$, terminal)
Append tree, (child,)
end if
else
level++
push tree
tree=child
Return tree, 1:=(c$, terminal)
end if
next
while not empty
read tree
end while
=tree
end function
}
Tree_datastructures {Rosetta Code
....rocks
........code
........comparison
........wiki
....mocks
........trolling
}
Tree_datastructures {Rosetta Code
....rocks
........code
........comparison
....................ok
....ok1
........wiki
....mocks
........trolling
}