55 lines
1.5 KiB
Text
55 lines
1.5 KiB
Text
-- demo\rosetta\Compiler\parse.exw
|
|
with javascript_semantics
|
|
include parse.e
|
|
|
|
procedure print_ast(object t)
|
|
if t == NULL then
|
|
printf(output_file,";\n")
|
|
else
|
|
integer ttype = t[1]
|
|
printf(output_file,tkNames[ttype])
|
|
if ttype=tk_Identifier then
|
|
printf(output_file," %s\n",t[2])
|
|
elsif ttype=tk_Integer then
|
|
printf(output_file," %d\n",t[2])
|
|
elsif ttype=tk_String then
|
|
printf(output_file," %s\n",enquote(t[2]))
|
|
else
|
|
printf(output_file,"\n")
|
|
print_ast(t[2])
|
|
print_ast(t[3])
|
|
end if
|
|
end if
|
|
end procedure
|
|
|
|
function ptree(object t)
|
|
if sequence(t) then
|
|
integer t1 = t[1]
|
|
t = deep_copy(t)
|
|
t[1] = tkNames[t1]
|
|
if not find(t1,{tk_Identifier,tk_String}) then
|
|
for i=2 to length(t) do
|
|
if t1=tk_Sequence and t[i]=NULL then
|
|
t[i] = "NULL"
|
|
else
|
|
t[i] = ptree(t[i])
|
|
end if
|
|
end for
|
|
end if
|
|
end if
|
|
return t
|
|
end function
|
|
|
|
procedure main(sequence cl)
|
|
open_files(cl)
|
|
toks = lex()
|
|
object t = parse()
|
|
print_ast(t)
|
|
pp(ptree(t),{pp_Nest,10,pp_Pause,0,pp_IntCh,false})
|
|
close_files()
|
|
end procedure
|
|
|
|
--main(command_line())
|
|
main({0,0,"test3.c"}) -- not parseable!
|
|
--main({0,0,"primes.c"}) -- as Algol, C, Python (apart from spacing)
|
|
--main({0,0,"count.c"}) -- as AWK ( "" )
|