TXL solution to TIL Chairmarks #4.2: Collecting statement statistics.

-- JamesCordy - 28 Oct 2005

File "TILstats.Txl"

% Gather statement statistics for a Tiny Imperative Language program
% Jim Cordy, October 2005

% Gathers and outputs statistics on the number of each kind of statement
% in a program.  Replaces the program itself with an empty one.

% Begin with the TIL base grammar
include "TIL.Grm"

% Compute and output statement kind statistics, replace program with an empty one.
% There are many different ways to do this - this naive way is simple and
% obvioulsy correct, but exposes TXL's need for generics.  
% Another less clear solution could use polymorphism to avoid the repetition.

function main
    replace [program]
        Program [program]

    % Count each kind of statement we're interested in
    % by extracting all of each kind from the program

    construct Statements [statement*]
        _ [^ Program]
    construct StatementCount [number]
        _ [length Statements] [putp "Total: %"]

    construct Declarations [declaration*]
        _ [^ Program]
    construct DeclarationsCount [number]
        _ [length Declarations] [putp "Declarations: %"]

    construct Assignments [assignment_statement*]
        _ [^ Program]
    construct AssignmentsCount [number]
        _ [length Assignments] [putp "Assignments: %"]

    construct Ifs [if_statement*]
        _ [^ Program]
    construct IfCount [number]
        _ [length Ifs] [putp "Ifs: %"]

    construct Whiles [while_statement*]
        _ [^ Program]
    construct WhileCount [number]
        _ [length Whiles] [putp "Whiles: %"]

    construct Fors [for_statement*]
        _ [^ Program]
    construct ForCount [number]
        _ [length Fors] [putp "Fors: %"]

    construct Reads [read_statement*]
        _ [^ Program]
    construct ReadCount [number]
        _ [length Reads] [putp "Reads: %"]

    construct Writes [write_statement*]
        _ [^ Program]
    construct WriteCount [number]
        _ [length Writes] [putp "Writes: %"]

    by
        % nothing
end function

Example run:

<linux> cat factors.til
// Factor an input number
var n;
write "Input n please";
read n;
write "The factors of n are";
var f;
f := 2;
while n != 1 do
    while (n / f) * f = n do
        write f;
        n := n / f;
    end
    f := f + 1;
end
<linux> txl factors.til TILstats.Txl
TXL v10.4a (15.6.05) (c)1988-2005 Queen's University at Kingston
Compiling TILstats.Txl ... 
Parsing factors.til ...
Transforming ...
Total: 11
Declarations: 2
Assignments: 3
Ifs: 0
Whiles: 2
Fors: 0
Reads: 1
Writes: 3
<linux> 

Revision: r1.1 - 28 Oct 2005 - 03:43 - JamesCordy
Sts > StsBench > TILChairmarks > StatementStatisticsUsingTXL
Copyright © 1999-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback