tool/prelude.c.tmpl: use RubyVM::CEscape
Do not repeat yourself.
This commit is contained in:
parent
b0eb5aa344
commit
acd8ee8dbc
Notes:
git
2020-08-11 16:51:33 +09:00
@ -5,24 +5,12 @@
|
|||||||
# Since $(BASERUBY) may be older than Ruby 1.9,
|
# Since $(BASERUBY) may be older than Ruby 1.9,
|
||||||
# Ruby 1.9 feature should not be used.
|
# Ruby 1.9 feature should not be used.
|
||||||
|
|
||||||
|
require_relative '../tool/ruby_vm/helpers/c_escape'
|
||||||
|
|
||||||
class Prelude
|
class Prelude
|
||||||
|
include RubyVM::CEscape
|
||||||
LINE_LIMIT = 509 # by C89
|
LINE_LIMIT = 509 # by C89
|
||||||
|
|
||||||
C_ESC = {
|
|
||||||
"/*" => "/\\*",
|
|
||||||
"*/" => "*\\/",
|
|
||||||
"\\" => "\\\\",
|
|
||||||
'"' => '\"',
|
|
||||||
"\n" => '\n',
|
|
||||||
}
|
|
||||||
|
|
||||||
0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\%03o" % ch }
|
|
||||||
0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\%03o" % ch }
|
|
||||||
C_ESC_PAT = Regexp.union(*C_ESC.keys)
|
|
||||||
|
|
||||||
def c_esc(str)
|
|
||||||
str.gsub(C_ESC_PAT) { C_ESC[$&] }
|
|
||||||
end
|
|
||||||
def prelude_base(filename)
|
def prelude_base(filename)
|
||||||
filename.chomp(".rb")
|
filename.chomp(".rb")
|
||||||
end
|
end
|
||||||
@ -97,7 +85,7 @@ Prelude.new(output, ARGV, vpath).instance_eval do
|
|||||||
% preludes.each {|i, prelude, lines, sub|
|
% preludes.each {|i, prelude, lines, sub|
|
||||||
|
|
||||||
% name = prelude_name(*prelude)
|
% name = prelude_name(*prelude)
|
||||||
static const char prelude_name<%=i%><%=%>[] = "<%=c_esc(name)%>";
|
static const char prelude_name<%=i%><%=%>[] = <%=rstring2cstr(name)%>;
|
||||||
static const struct {
|
static const struct {
|
||||||
% size = beg = 0
|
% size = beg = 0
|
||||||
% lines.each_with_index {|(line, comment), n|
|
% lines.each_with_index {|(line, comment), n|
|
||||||
@ -114,15 +102,15 @@ static const struct {
|
|||||||
% end
|
% end
|
||||||
} prelude_code<%=i%><%=%> = {
|
} prelude_code<%=i%><%=%> = {
|
||||||
% size = 0
|
% size = 0
|
||||||
#line 1 "<%=c_esc(prelude)%>"
|
#line 1 <%=rstring2cstr(prelude)%>
|
||||||
% lines.each_with_index {|(line, comment), n|
|
% lines.each_with_index {|(line, comment), n|
|
||||||
% if size + line.size >= Prelude::LINE_LIMIT
|
% if size + line.size >= Prelude::LINE_LIMIT
|
||||||
% size = 0
|
% size = 0
|
||||||
,
|
,
|
||||||
#line <%=n+1%> "<%=c_esc(prelude)%>"
|
#line <%=n+1%> <%=rstring2cstr(prelude)%>
|
||||||
% end
|
% end
|
||||||
% size += line.size
|
% size += line.size
|
||||||
"<%=c_esc(line)%>"<%if comment%>/* <%=c_esc(comment)%> */<%end%>
|
<%=rstring2cstr(line)%><%if comment%><%=commentify(comment)%><%end%>
|
||||||
% }
|
% }
|
||||||
#line <%=_erbout.count("\n")+2%> "<%=@output%>"
|
#line <%=_erbout.count("\n")+2%> "<%=@output%>"
|
||||||
};
|
};
|
||||||
|
@ -17,7 +17,7 @@ module RubyVM::CEscape
|
|||||||
|
|
||||||
# generate comment, with escaps.
|
# generate comment, with escaps.
|
||||||
def commentify str
|
def commentify str
|
||||||
return "/* #{str.strip.b.gsub '*/', '*\\/'} */"
|
return "/* #{str.b.gsub '*/', '*\\/'} */"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Mimic gensym of CL.
|
# Mimic gensym of CL.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user