Category:TXR

From Rosetta Code
Revision as of 23:00, 26 October 2011 by 192.139.122.42 (talk) (Ported to Windows natively.)
This page is a stub. It needs more information! You can help Rosetta Code by filling it in!
Language
TXR
This programming language may be used to instruct a computer to perform a task.
Official website
See Also:


Listed below are all of the tasks on Rosetta Code which have been solved using TXR.

TXR is a new text extraction language implemented in C, running on Linux and Cygwin (and possibly other POSIX platforms) as well as Windows.

The source of a TXR query is literal text except for directives and variables preceded by the @ character.

Computation evolves by textual pattern matching with implicit backtracking. Non-pattern matching activities are embedded into a pattern matching paradigm. For instance, the line

Four score and seven years ago,

is a TXR directive which matches a line of that exact text, or else fails. The following is also a directive:

<lang txr>@(bind foo "abc")</lang>

which succeeds if foo has no prior binding, or already contains "abc", but fails if foo has a binding to something other than "abc".

The success of a directive means that computation proceeds to the next directive (and, if this is a pattern match, the input position advances). Failure means that the enclosing query fails, triggering back-tracking behaviors and possibly failure of the entire query.

Simple Query

Here is a very basic hello-world-type TXR query that re-implements the "free" utility:

<lang txr>#!/usr/bin/txr -f @(next "/proc/meminfo") @(skip) MemTotal:@/ +/@TOTAL kB MemFree:@/ +/@FREE kB Buffers:@/ +/@BUFS kB Cached:@/ +/@CACHED kB @(skip) SwapTotal:@/ +/@SWTOT kB SwapFree:@/ +/@SWFRE kB @(next `!echo $(( @TOTAL - @FREE ))`) @USED @(next `!echo $(( @USED - @BUFS - @CACHED ))`) @RUSED @(next `!echo $(( @FREE + @BUFS + @CACHED ))`) @RFREE @(next `!echo $(( @SWTOT - @SWFRE ))`) @SWUSE @(output)

              TOTAL         USED         FREE      BUFFERS       CACHED

Mem: @{TOTAL -12} @{USED -12} @{FREE -12} @{BUFS -12} @{CACHED -12} +/- buffers/cache: @{RUSED -12} @{RFREE -12} Swap: @{SWTOT -12} @{SWUSE -12} @{SWFRE -12} @(end)</lang>

Sample run:

$ ./meminfo.txr
               TOTAL         USED         FREE      BUFFERS       CACHED
Mem:          769280       647752       121528       160108       286844
+/- buffers/cache:         200800       568480
Swap:        1048568        18200      1030368

Arithmetic is not implemented in TXR as of version 035. The above example simply continues the pattern matching across invocations of echo to borrow the shell to do math. The command

@(next `!echo $(( @TOTAL - @FREE ))`)
@USED

means, "Next, please switch to scanning the output of this echo command with some variables substituted in. Then capture entire first line of this command into the USED variable."

Complex Query

Here is a TXR query for computing the complete dependencies of a C source file (including system and compiler headers) on a typical GNU/Linux system, demonstrating features like parallel clauses, recursion and exception handling.

<lang txr>@(define process_file (dir file already_visited visited_out)) @ (local this_file next_file header_dir next_dir next_dir2 visited_out_next) @ (bind this_file `@dir@file`) @ (none) @ (bind already_visited this_file) @ (end) @ (merge already_visited already_visited this_file) @ (next this_file) @ (collect) @ (cases)

  1. include "@*header_dir/@next_file"

@ (bind next_dir `@dir@header_dir/`) @ (or)

  1. include "@next_file"

@ (bind next_dir dir) @ (or)

  1. @/ */include <@*header_dir/@next_file>

@ (bind next_dir `@sys_includes@header_dir/`) @ (bind next_dir2 `@gcc_includes@header_dir/`) @ (or)

  1. @/ */include <@next_file>

@ (bind next_dir sys_includes) @ (bind next_dir2 gcc_includes) @ (or)

  1. @/ */include_next <@*header_dir/@next_file>

@ (bind next_dir `@gcc_includes@header_dir/`) @ (or)

  1. @/ */include_next <@next_file>

@ (bind next_dir gcc_includes) @ (end) @ (try) @ (process_file next_dir next_file already_visited visited_out_next) @ (merge already_visited already_visited visited_out_next) @ (catch file_error) @ (try) @ (process_file next_dir2 next_file already_visited visited_out_next) @ (merge already_visited already_visited visited_out_next) @ (catch file_error) @ (end) @ (end) @ (end) @ (bind visited_out this_file) @ (try) @ (flatten visited_out_next) @ (merge visited_out visited_out visited_out_next) @ (catch) @ (end) @(end) @(next :args) @(cases) @*dir/@*file.@suffix @ (bind directory `@dir/`) @(or) @*file.@suffix @ (bind directory "") @(end) @(next `!gcc -print-search-dirs`) install: @gcc_install @(bind gcc_includes `@{gcc_install}include/`) @(bind sys_includes "/usr/include/") @(process_file directory `@file.@suffix` nil list_out) @(output) @(rep) @list_out@(first)@file.o:@(end) @(end)</lang>

Sample run:

$ txr dep.txr match.c
match.o: /usr/include/stdio.h /usr/include/features.h /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h /usr/include/gnu/stubs-32.h /usr/lib/gcc/i586-redhat-linux/4.4.1/include/stddef.h /usr/include/bits/types.h /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h /usr/lib/gcc/i586-redhat-linux/4.4.1/include/stdarg.h /usr/include/bits/wchar.h /usr/include/wctype.h /usr/include/endian.h /usr/include/bits/endian.h /usr/include/bits/byteswap.h /usr/include/xlocale.h /usr/include/bits/wchar2.h /usr/include/bits/wchar-ldbl.h /usr/include/gconv.h /usr/include/bits/stdio-lock.h /usr/include/bits/libc-lock.h /usr/include/pthread.h /usr/include/sched.h /usr/include/time.h /usr/include/bits/time.h /usr/include/bits/sched.h /usr/include/signal.h /usr/include/bits/signum.h /usr/include/bits/siginfo.h /usr/include/bits/sigaction.h /usr/include/bits/sigcontext.h /usr/include/asm/sigcontext.h /usr/include/linux/types.h /usr/include/linux/posix_types.h /usr/include/linux/stddef.h /usr/include/asm/posix_types.h /usr/include/asm/types.h /usr/include/asm-generic/int-ll64.h /usr/include/bits/sigstack.h /usr/include/sys/ucontext.h /usr/include/bits/pthreadtypes.h /usr/include/bits/sigthread.h /usr/include/bits/setjmp.h /usr/include/bits/libio-ldbl.h /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h /usr/include/getopt.h /usr/include/ctype.h /usr/include/bits/stdio.h /usr/include/bits/stdio2.h /usr/include/bits/stdio-ldbl.h /usr/include/stdlib.h /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h /usr/include/alloca.h /usr/include/bits/stdlib.h /usr/include/bits/stdlib-ldbl.h /usr/include/string.h /usr/include/bits/string.h /usr/include/bits/string2.h /usr/include/bits/string3.h /usr/include/assert.h /usr/include/errno.h /usr/include/bits/errno.h /usr/include/linux/errno.h /usr/include/asm/errno.h /usr/include/asm-generic/errno.h /usr/include/asm-generic/errno-base.h /usr/include/dirent.h /usr/include/bits/dirent.h /usr/include/bits/posix1_lim.h /usr/include/bits/local_lim.h /usr/include/linux/limits.h /usr/include/setjmp.h config.h lib.h gc.h unwind.h regex.h /usr/include/limits.h /usr/lib/gcc/i586-redhat-linux/4.4.1/include/limits.h /usr/lib/gcc/i586-redhat-linux/4.4.1/include/syslimits.h /usr/include/bits/posix2_lim.h /usr/include/bits/xopen_lim.h stream.h parser.h txr.h utf8.h filter.h match.h

Subcategories

This category has the following 3 subcategories, out of 3 total.

Pages in category "TXR"

The following 160 pages are in this category, out of 160 total.