port libemb.py to perl

This commit is contained in:
Yuichi Nishiwaki 2014-05-19 08:40:22 +09:00
parent d05adcb023
commit 52c175a4fe
3 changed files with 65 additions and 77 deletions

63
etc/libemb.pl Executable file
View File

@ -0,0 +1,63 @@
#!/usr/bin/perl
@files = (
'piclib/built-in.scm',
'piclib/srfi/1.scm',
'piclib/srfi/26.scm',
'piclib/srfi/95.scm'
);
print <<EOL;
#include "picrin.h"
#include "picrin/error.h"
EOL
foreach my $file (@files) {
my $var = &escape_v($file);
print "const char *$var =\n";
open IN, $file;
while (<IN>) {
chomp;
s/\\/\\\\/g;
s/"/\\"/g;
print "\"$_\"\n";
}
print ";\n\n";
}
close IN;
print <<EOL;
void
pic_load_piclib(pic_state *pic)
{
pic_try {
EOL
foreach my $file (@files) {
my $var = &escape_v($file);
print " pic_load_cstr(pic, $var);\n";
}
print <<EOL;
}
pic_catch {
/* error! */
fputs("fatal error: failure in loading built-in.scm\\n", stderr);
fputs(pic_errmsg(pic), stderr);
abort();
}
#if DEBUG
puts("successfully loaded stdlib");
#endif
}
EOL
sub escape_v {
my ($_) = @_;
s/\.scm$//g;
s/[\/-]/_/g;
$_;
}

View File

@ -1,74 +0,0 @@
import re
import os
from collections import OrderedDict
outfn = 'src/load_piclib.c'
libs = [
'piclib/built-in.scm',
'piclib/srfi/1.scm',
'piclib/srfi/26.scm',
'piclib/srfi/95.scm',
]
if os.path.exists(outfn):
os.remove(outfn)
piclibs = OrderedDict()
for lib in libs:
vname = lib[:-4]
vname = re.sub(r'/', r'_', vname)
vname = re.sub(r'-', r'_', vname)
piclibs[lib] = vname
def escape_scm(infn, outfn, vname):
with open(outfn, 'a') as output:
output.write('const char *{} =\n'.format(vname))
with open(infn, 'r') as input:
for line in input:
output.write('"')
line = line.strip('\n')
line = re.sub(r'\\', r'\\\\', line)
line = re.sub(r'"', r'\"', line)
output.write(line)
output.write('\\n"\n')
output.write(';\n\n')
piclib_load_head = """
#include "picrin.h"
#include "picrin/error.h"
"""
piclib_load_tail = """
void
pic_load_piclib(pic_state *pic)
{{
pic_try {{
{}
}}
pic_catch {{
/* error! */
fputs("fatal error: failure in loading built-in.scm\\n", stderr);
fputs(pic_errmsg(pic), stderr);
abort();
}}
#if DEBUG
puts("successfully loaded stdlib");
#endif
}}
"""
def gen_piclib_load_c(outfn):
with open(outfn, 'a') as f:
f.write(piclib_load_head)
loads = ""
for infn,vname in piclibs.items():
escape_scm(infn, outfn, vname)
loads += "pic_load_cstr(pic, {});\n ".format(vname)
with open(outfn, 'a') as f:
f.write('\n\n')
f.write(piclib_load_tail.format(loads))
gen_piclib_load_c(outfn)

View File

@ -7,12 +7,11 @@ set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${PROJECT_SOURCE
set(XFILE_SOURCES extlib/xfile/xfile.c)
# piclib
set(Python_ADDITIONAL_VERSIONS 2.7)
find_package(PYTHON REQUIRED)
find_package(Perl REQUIRED)
set(PICLIB_SOURCES ${PROJECT_SOURCE_DIR}/src/load_piclib.c)
add_custom_command(
OUTPUT ${PICLIB_SOURCES}
COMMAND ${PYTHON_EXECUTABLE} etc/libemb.py
COMMAND ${PERL_EXECUTABLE} etc/libemb.pl > ${PROJECT_SOURCE_DIR}/src/load_piclib.c
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)