ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I was reading through the thread and saw, and I don't understand:
Quote:
Originally Posted by Sergei Steshenko
... I have already described here in part my PerlPreProcessor - have a look at it. It can help you a lot with text substitution, it can cope with metadata, unlike C++ template it is stateful, i.e. one can pass data between templates - because it's all in Perl.
And no new language is invented - in your case it will be pure "C" + pure Perl. The only new entity is simple reserved comments - like
Code:
// PERL_BEGIN
// PERL_END
// PERL_ONE_LINER
.
Also, I just can't find a good explanation of extract_bracketed()'s return value.
Anyway, here is my current code, it first slurps the file, splices escaped newlines, writes it to a new file with the extension changes to ".c", runs cpp to extract the comments and write to a temp file, and then gets rid of the temp file.
The remarkable thing is that it worked perfectly the first try!!!
Using the browser's find function, I discovered "-fpreprocessed" may do the job. But the problem is that it doesn't splice escaped newlines.
EDIT: that might not be an issue, I can probably splice escaped newlines in Perl using s/\\\n//g.
EDIT2: I've tested the splicing trick, and it seems to work just as described in the CPP manual.
Now, how do you find all instances of "keyword { ... }" and process them with Text::Balanced?.
Item in bold is both smart and not. It is smart because you've quickly found what you need - using my prompt.
It is not because I am capable of giving such a prompt and you are apparently not at the moment. And it is because I still like reading full documentation - despite the browser "Find" function - the old fashioned pre-Internet way.
I have a very strict rule - not to bother with Perl code not having
Code:
use strict;
use warnings; # one can use -w switch on Perl invocation line instead/in addition to this pragma
I did that, along with a few changes from yesterday:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
# Iterate over all arguments
foreach $filename (@ARGV) {
# Check for things that would cause errors later on
if(!(-e $filename)) die "error: $filename does not exist\n";
if(!(-r $filename)) die "error: you do not have permission to read $filename\n";
if(-d $filename) die "error: $filename is a directory\n";
# Read file into string
open(FILE, "<$filename");
undef $/;
my $file = <FILE>;
close(FILE);
# Splice newlines escaped with a backslash
$file =~ s/\\\n//g;
# output filename variable
my $outfilename = $filename;
# Replace extension with ".c"
$outfilename =~ s/(.*)\..*/\1.c/; # TODO: be able to handle files with no dot in the name
# Write spliced string to new filename
open(FILE, ">$outfilename");
print FILE $file;
close(FILE);
# Remove comments from spliced file using the C preprocessor
system("cpp -fpreprocessed $outfilename -o $outfilename.temp") == 0 or die "error: cpp failed to remove comments\n";
# Take care of the temp file
system("rm $outfilename") == 0 or die "error: rm failed\n";
system("mv $outfilename.temp $outfilename") == 0 or die "error: mv failed\n";
# Read in processed file for actual OO conversion
open(FILE, "<$outfilename");
undef $/;
$file = <FILE>;
close(FILE);
# Find all class definitions and process them
my $offset = 0;
my $result = index($file, 'class', $offset);
while($result != -1) {
my @classdef = extract_bracketed(substr($file, $result, -1), '{}', '[^{]*');
# TODO: process class and turn it into C code
$offset = $result + 1;
$result = index($file, 'class', $offset);
}
}
It has some errors I don't understand:
Code:
\1 better written as $1 at ./test line 25.
Global symbol "$filename" requires explicit package name at ./test line 7.
Global symbol "$filename" requires explicit package name at ./test line 9.
syntax error at ./test line 9, near ") die"
Global symbol "$filename" requires explicit package name at ./test line 10.
syntax error at ./test line 10, near ") die"
Global symbol "$filename" requires explicit package name at ./test line 11.
syntax error at ./test line 11, near ") die"
Global symbol "$filename" requires explicit package name at ./test line 14.
Global symbol "$filename" requires explicit package name at ./test line 23.
Execution of ./test aborted due to compilation errors.
Quote:
Originally Posted by Sergei Steshenko
Code:
undef $/;
is a bad idea, though it works. It's a bad idea because it is a global setting change.
I don't understand how it works. I just copy-and-pasted it out of a google search. As you can tell, I am still very much a Perl newbie.
You are using global, bareword filehandles (e.g., FILE), but modern Perl supports (and encourages) lexical filehandles:
Code:
open my $file, '<', '/path/to/file.txt'
or die: "Can't open file.txt: $!";
while (<$file>) {
# whatever
}
close $file or die "Problem closing file.txt: $!";
Check out perldoc -f open and perldoc perlopentut for some discussion.
You are using global, bareword filehandles (e.g., FILE), but modern Perl supports (and encourages) lexical filehandles:
Code:
open my $file, '<', '/path/to/file.txt'
or die: "Can't open file.txt: $!";
while (<$file>) {
# whatever
}
close $file or die "Problem closing file.txt: $!";
Check out perldoc -f open and perldoc perlopentut for some discussion.
I fixed it now, but what about these errors?
Code:
\1 better written as $1 at ./test line 25.
syntax error at ./test line 9, near ") die"
syntax error at ./test line 10, near ") die"
syntax error at ./test line 11, near ") die"
Execution of ./test aborted due to compilation errors.
I was considering the design of this "new programming language", and thought that it would be easier if there would be a polymorphic struct-like type, but will not have the complications of methods and a dynamic dispatch table.
So I got the idea to make a "pstruct" (Polymorphic Structure) construct:
Code:
pstruct Shape2D {
int x;
int y;
}
// Shape3D inherits all of Shape2D's fields, and adds to it.
pstruct Shape3D Shape2D {
int z;
}
\1 better written as $1 at ./test line 25.
syntax error at ./test line 9, near ") die"
syntax error at ./test line 10, near ") die"
syntax error at ./test line 11, near ") die"
Execution of ./test aborted due to compilation errors.
I was considering the design of this "new programming language", and thought that it would be easier if there would be a polymorphic struct-like type, but will not have the complications of methods and a dynamic dispatch table.
So I got the idea to make a "pstruct" (Polymorphic Structure) construct:
Code:
pstruct Shape2D {
int x;
int y;
}
// Shape3D inherits all of Shape2D's fields, and adds to it.
pstruct Shape3D Shape2D {
int z;
}
It's interesting that someone else is trying to do such a thing, and I might check them out fur fun later.
Anyway, I am trying to figure out how to turn the above example into:
A string containing the pstruct's name
An array of the names of the pstruct's parents (they can have multiple inheritance)
An array of the pstruct's members.
I already have the code to turn those into valid C code, but I can't seem to fugure out how to get them from the original.
I am using a subroutine called "process_pstruct()", that takes a pstruct definition taken out of the input file, and returns it translated to C.
Here's how I see it would work:
Code:
pstruct Shape2D { // All code before the first "{" is the header
int x;
int y;
} // All code between the first "{" and the last "}" is the members
Then I split the header across matches of /\s+/, discard the first element (which is always the string "pstruct"), use the second one as the name, and the rest (if any) are the parents.
Next I split the members across /\s*;\s*/, and get the member array
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.