A Data Manipulation Library for TAP
Originally published on: Sat, 24 Oct 2009 16:03:51 +0000
I've addded the file tap_data.c to the TAP family. ( Please see the kick-off post http://www.mailsend-online.com/blog?p=45 .)
TAP Data exposes the void function tap_data_init(). When called, the function adds fifteen new commands to the initialized TAP interpreter.
The new commands consist of integer math operations, logical and / or / not, basic string manipulation, and simple C-style string comparison.
I have also added the following public functions to tap_core.c/.h
// global tap error message.
extern char _tap_emsg[1025];
// display an error message
void tap_error_msg(void);
// check for the appropriate number of arguments and
// issue an error message if the criteria isn't met.
int tap_arg_check(int argc,int minargs,int maxargs);
// Return the current line as a string.
// Do not modify this string!!!
char *tap_current_line(void) ;
// Return the current line number.
int tap_current_line_num(void);
I started using the tap_error_msg() method internally in the TAP Core and added the other utility routines to check argument counts uniformly. I exposed two introspective functions ( tap_current_line() and tap_current_line_num() ) in this update.
I believe the combination of the TAP Core library and the TAP Data library causes the initialized interpreter to be categorized as Turing-complete.
Here is an updated interpreter host that includes both TAP core and TAP data. script that exercises the new commands:
tap_test2.c
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include <stdio.h>
#include <stdlib.h>
#include "tap_core.h"
#include "tap_data.h"
// New commands.
// Print all of the specified arguments
// on the console.
static void do_println(int argc,char **argv);
int main(int argc,char **argv) {
int i;
if(argc<2) {
fprintf(stderr,"Syntax:\n\ttap_test2 script_filename\n");
return 1;
}
i=tap_init();
if(!i) {
printf("Init error!");
return 1;
}
tap_register_cmd("println",do_println);
// load the basic data manipulation extensions
tap_data_init();
i=tap_load_file(argv[1]);
if(!i) {
printf("load error");
return 1;
}
tap_run(0);
return 0;
}
static void do_println(int argc,char **argv) {
int i;
for(i=0;i<argc;i++) {
printf("%s",argv[i]);
}
printf("\n");
}
Here's a script that will exercise the new TAP commands:
test_2.txt
println "Starting..."
println ""
set $a 2
set $b 3
add $a $b
println "add $a $b => " $retval
sub $a $b
println "sub $a $b => " $retval
mul $a $b
println "mul $a $b => " $retval
mod $a $b
println "mod $a $b => " $retval
println ""
or 0 0
println "or 0 0 => " $retval
or 0 1
println "or 0 1 => " $retval
or 1 0
println "or 1 0 => " $retval
or 1 1
println "or 1 1 => " $retval
println ""
and 0 0
println "and 0 0 => " $retval
and 0 1
println "and 0 1 => " $retval
and 1 0
println "and 1 0 => " $retval
and 1 1
println "and 1 1 => " $retval
println ""
not 1
println "not 1 => " $retval
not 0
println "not 0 => " $retval
println ""
substr "Hello, world!" 7
println 'substr "Hello, world!" 7 => ' $retval
substr "Hello, world!" 7 4
println 'substr "Hello, world!" 7 4 => ' $retval
length "Hello, world!"
println 'length "Hello, world!" => ' $retval
index "Testing" "ing"
println 'index "Testing" "ing" => ' $retval
index "Testing" "ung"
println 'index "Testing" "ung" => ' $retval
ord "A"
println 'ord "A" => ' $retval
ord "a"
println 'ord "a" => ' $retval
chr 97
println "chr 97 => " $retval
strcmp "Yes" "Yes"
println 'strcmp "Yes" "Yes" => ' $retval
strcmp "Yes" "YES"
println 'strcmp "Yes" "YES" => ' $retval
strcmp "YES" "Yes"
println 'strcmp "YES" "Yes" => ' $retval
stricmp "Yes" "YES"
println 'stricmp "Yes" "YES" => ' $retval
The output from the above should read:
Starting...
add $a $b => 5
sub $a $b => -1
mul $a $b => 6
mod $a $b => 2
or 0 0 => 0
or 0 1 => 1
or 1 0 => 1
or 1 1 => 1
and 0 0 => 0
and 0 1 => 0
and 1 0 => 0
and 1 1 => 1
not 1 => 0
not 0 => 1
substr "Hello, world!" 7 => world!
substr "Hello, world!" 7 4 => worl
length "Hello, world!" => 13
index "Testing" "ing" => 4
index "Testing" "ung" => -1
ord "A" => 65
ord "a" => 97
chr 97 => a
strcmp "Yes" "Yes" => 0
strcmp "Yes" "YES" => 1
strcmp "YES" "Yes" => -1
stricmp "Yes" "YES" => 0
The source, executable sample file, and sample script for the TAP Data with the updated TAP Core can be downloaded in a single archive at:
http://www.mailsend-online.com/wp/tapdata.zip
Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless
Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.
Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks