Preprocess text with embedded perl code

Create my first project at Github:

This script can be downloaded from

https://github.com/agraja/text2perl/blob/master/text2perl.pl

#!/usr/bin/perl

# Tiny preprocessor:
# Example: Paste this example in a file
# Pass it as argument to this script
# some text /* for(1..5) { */
# this line will be printed 5 times
# /* } */
open(P,”|$^X”);
print P map {
if(/^\/\*(.*)/){
$1
}else{
s/([\\\’])/\\$1/g;
“print ‘$_’;”
}
}map{
s/\*\//\r/g;
split(/(\/\*[^\r]*\r)/)
}<>;
close(P);

Advertisements

Lambda Calculus in Perl

#!/usr/bin/perl
# lambda expression = anonymous subroutine
# all subroutines defined in this file are anonymous
my $apply = sub { # lift
  my ($func,@args)=@_;
  sub {$func->(@args,@_)} # bind
};
my $identity = sub { # wrap
  my $value=shift;
  sub {$value}
};
my $self_apply = sub {
  my $func=shift;
  $apply->($func,$func);
};
my $do_pipe = sub {
  my $out=shift;
  for(@_){$out=$_->($out);}
  $out
};
my $say    = sub {print shift->(),”\n”};
my $prefix = sub {$identity->(“prefix ” . shift->())};
my $suffix = sub {$identity->(shift->() . ” suffix”)};
print $identity->(‘raja’)->(),”\n”;
print $apply->($prefix)->($identity->(‘xyz’))->(),”\n”;
my $v=$identity->(‘123’);
my $p=$apply->($suffix)->($v);
my $s=$apply->($prefix)->($p);
$say->($s);
$do_pipe->(
  ‘456’,
  $identity,
  $suffix,
  $prefix,
  $say
);
$self_apply->(
  sub {
    my ($decr,$in)=@_;
    if(!$in){return;}
    print “$in\n”;
    $decr->($decr,$in-1);
  }
)->(7);

Identity Monad in Perl

#!/usr/bin/perl

sub wrap_value {
my $value=shift;
return sub {
return $value;
}
}

sub bind_func {
my ($value,$func)=@_;
return $func->($value);
}

sub lift_func {
my $func=shift;
return sub {
my $value=shift;
&bind_func($value,$func);
}
}

sub prefix {
my $value=shift;
return &wrap_value(“prefix ” . $value->());
}

sub suffix {
my $value=shift;
return &wrap_value($value->() . ” suffix”);
}

print &wrap_value(‘raja’)->(),”\n”;

print &bind_func(&wrap_value(‘raja’),\&prefix)->(),”\n”;

print &lift_func(\&prefix)->(&wrap_value(‘xyz’))->(),”\n”;

my $v=&wrap_value(‘123’);
my $p=&lift_func(\&suffix)->($v);
my $s=&lift_func(\&prefix)->($p);

print $s->(),”\n”;

Perl Script to Indent Verilog Files

Here’s a perl script to indent verilog files.

Download here. vindent.pdf

Add your own list of keywords to indent any source code

For example, this script could be used to indent C/C++/Java source codes.

This script uses two spaces for each indent.  This could be changed as required.

#!/usr/bin/perl
# Filename    : vindent.pl
# Author      : A.G.Raja
# Website     : https://agraja.wordpress.com
#
# Usage       : ./vindent.pl verilogfile.v
#             : ./vindent.pl *.*v*
# Help        : ./vindent.pl

# Verilog File indenter

# Space for each indent
my $Space = ”  “;

# Block begin.  For indenting C/C++/Java program use “{”
my $Open = “begin”;
# Block end.  For indenting C/C++/Java program use “}”
my $Close = “end”;

# Block statements.
# Note: Each Open keyword must have corresponding Close keyword
my @Openkeywords = qw{ module program package interface class task function };
my @Closekeywords = qw{ endmodule endprogram endpackage endinterface endclass endtask endfunction };

sub indent {
my $indent = 0;
my($infile) = @_;
open(FILE, “$infile”) or die “Couldn’t open file \”$infile\”\n”;
open(TFILE, “>temp”) or die “Couldn’t create temp file\n”;

while($line = <FILE>) {
chomp $line;
$line =~ s/^\s*//;
print TFILE “$line \n”;
}
close TFILE;
close FILE;
open(TFILE, “<temp”);
open(FILE, “>$infile”);

while($line = <TFILE>) {
foreach $Closekey (@Closekeywords) {
if($line =~ /^$Closekey\s/) { $indent–; }
}
if (($line =~ /.*\s+$Close\s/ ) || ($line =~ /^$Close\s/))
{ $indent–; }
for($i=0;$i<$indent;$i++) { print FILE $Space; }
print FILE $line;
foreach $Openkey (@Openkeywords) {
if($line =~ /^$Openkey\s/) { $indent++; }
}
if (($line =~ /.*\s+$Open\s/ )|| ($line =~ /^$Open\s/))
{ $indent++; }
}
close FILE; close TFILE;
print “Indented File \”$infile\”\n”;
}

my $argnum;
if($ARGV[0] eq “”) {
print “Usage : vindent.pl verilogfile.v\n”;
print ”        vindent.pl *.v\n”;
print ”        vindent.pl *.*v*\n”;
print “Help  :\n”;
print ”        vindent.pl\n”;
}
foreach $argnum (0 .. $#ARGV) {
my $ifile = $ARGV[$argnum];
&indent($ifile);
}
unlink “temp”;
Download here. vindent.pdf

Perl script to display comments

Download here. c_help.pdf

 

#!/usr/bin/perl

# Filename: chelp.pl

# Author : A. G. Raja

# Website : agraja.wordpress.com

# License : GPL

# pass c file as argument to this script

# this script displays all the comments

use strict;

use warnings;

open(FILE,$ARGV[0]) or die “File not Found”;

my $line;

my $start=0;

while ( $line = <FILE> ) {

if($line =~ /\/\//)

{

$line=~ s/(.*)\/\/(.*)/$2/;

print $line;

}

if($line =~ /\/\*/)

{

$start=1;

$line=~ s/(.*)\/\*(.*)/$2/;

}

if($line =~ /\*\//)

{

$line=~ s/(.*)\*\//$1/;

print $line;

$start=0;

}

if($start) { print $line; }

}

close FILE;

 

Tips:

Use a shell script to redirect the output

of the above script to another program.

Example

#!/bin/bash

# Filename: help.sh

# Author : A.G.Raja

# Website : agraja.wordpress.com

# License : GPL

# rename chelp.pl to chelp

# Install chelp and add to path

chelp $1 | less

Demo:

 

Below is a C file

 

// Filename : demo.c

// Author : cfile.pl

// License : GPL

#include <stdio.h>

#define print(x) printf(“str=%s\n”,x);

int main(int argc, char **argv)

{

/* this is a multi-

line comment */

char *str=”File name:”;// this will print file name

print(argv[0])

return 0;

}

 

Type at the terminal:

[raja@agraja ]$ chelp demo.c

 

The following will be printed to stdout

 

Filename : demo.c

Author : cfile.pl

License : GPL

this is a multi-

line comment

this will print file name

Download here. c_help.pdf

Perl script to add Header to files

The following script will add text to the beginning of files.

Donwload here. perlheader.pdf

This is done in three steps

1) This script copies the contents of a file to local variable by slurping.

2) The necessary text that needs to be added at the beginning of the file is added.

3) Then the original contents of the file are added using the local variable.

 

 

#!/usr/bin/perl

# Filename: header.pl

# Author : A.G.Raja

# Website : agraja.wordpress.com

# License : GPL

# pass files as arguments to this script

# this script will add the header text

use strict;

use warnings;

sub addheader{

my($infile,$header)=@_;

my $text = do { local( @ARGV, $/ ) = $infile ; <> } ;

open(FILE,”>$infile”);

print FILE $header;

print FILE $text;

close FILE

}

my $argnum;

foreach $argnum (0 .. $#ARGV){

my $infile = $ARGV[$argnum];

# Edit line below to change header text

my $header = “Header line 1nHeader line 2nHeader line 3n”;

&addheader($infile,$header);

}

# chmod +x header.pl

# ./header.pl *.txt

 

Donwload here. perlheader.pdf