summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2020-05-15 16:44:36 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2020-10-12 16:20:51 +0900
commit1bcf6d9f3cacf6a5c54ee0c74d0dad0f0e9ed436 (patch)
tree50807553ee909e0956b84cfd05d40bbdfb5ef320
parent97e3c3a5a339696827bd5c936c12df3241f3a81c (diff)
downloadmruby-1bcf6d9f3cacf6a5c54ee0c74d0dad0f0e9ed436.tar.gz
mruby-1bcf6d9f3cacf6a5c54ee0c74d0dad0f0e9ed436.zip
Create `MRB_OPSYM()` macro to refer symbols corresponding operators.
For example, `MRB_OPSYM(add)` refers a symbol for `+`.
-rw-r--r--Rakefile35
-rw-r--r--include/mruby/presym.h5
-rw-r--r--src/symbol.c2
3 files changed, 41 insertions, 1 deletions
diff --git a/Rakefile b/Rakefile
index 8c7533b43..2aa96fc1a 100644
--- a/Rakefile
+++ b/Rakefile
@@ -146,11 +146,46 @@ file presym_inc => presym_file do
presyms = File.readlines(presym_file, chomp: true)
rm_f presym_inc
File.open(presym_inc, "w") do |f|
+ op_table = {
+ "!" => "not",
+ "!=" => "neq",
+ "!~" => "nmatch",
+ "%" => "mod",
+ "&" => "and",
+ "&&" => "andand",
+ "*" => "mul",
+ "**" => "pow",
+ "+" => "add",
+ "+@" => "plus",
+ "-" => "sub",
+ "-@" => "minus",
+ "/" => "div",
+ "<" => "lt",
+ "<=" => "le",
+ "<<" => "lshift",
+ "<=>" => "cmp",
+ "==" => "eq",
+ "===" => "eqq",
+ "=~" => "match",
+ ">" => "gt",
+ ">=" => "ge",
+ ">>" => "rshift",
+ "[]" => "aref",
+ "[]=" => "aset",
+ "^" => "xor",
+ "`" => "tick",
+ "|" => "or",
+ "||" => "oror",
+ "~" => "neg",
+ }
f.print "/* MRB_PRESYM_CSYM(sym, num) - symbol which is valid C id name */\n"
+ f.print "/* MRB_PRESYM_OPSYM(op, sym, num) - symbol which is an operator id */\n"
f.print "/* MRB_PRESYM_SYM(sym, num) - symbol which is not valid C id */\n\n"
presyms.each.with_index do |sym,i|
if /\A\w+\Z/ =~ sym
f.print "MRB_PRESYM_CSYM(#{sym}, #{i+1})\n"
+ elsif op_table.key?(sym)
+ f.print "MRB_PRESYM_OPSYM(#{sym}, #{op_table[sym]}, #{i+1})\n"
else
f.print "MRB_PRESYM_SYM(#{sym}, #{i+1})\n"
end
diff --git a/include/mruby/presym.h b/include/mruby/presym.h
index ff3cc7ac5..bd3918c52 100644
--- a/include/mruby/presym.h
+++ b/include/mruby/presym.h
@@ -9,14 +9,17 @@
#undef MRB_PRESYM_MAX
#define MRB_PRESYM_CSYM(sym, num) MRB_PRESYM__##sym = (num<<1),
-#define MRB_PRESYM_SYM(sym, num)
+#define MRB_PRESYM_OPSYM(op, sym, num) MRB_PRESYM_op_##sym = (num<<1),
+#define MRB_PRESYM_SYM(sym, num)
enum mruby_presym {
#include <../build/presym.inc>
};
#undef MRB_PRESYM_CSYM
+#undef MRB_PRESYM_OPSYM
#undef MRB_PRESYM_SYM
#define MRB_SYM(sym) MRB_PRESYM__##sym
+#define MRB_OPSYM(sym) MRB_PRESYM_op_##sym
#endif /* MRUBY_PRESYM_H */
diff --git a/src/symbol.c b/src/symbol.c
index 35c3c3015..5f30ccb39 100644
--- a/src/symbol.c
+++ b/src/symbol.c
@@ -14,8 +14,10 @@
#undef MRB_PRESYM_MAX
#undef MRB_PRESYM_CSYM
+#undef MRB_PRESYM_OPSYM
#undef MRB_PRESYM_SYM
#define MRB_PRESYM_CSYM(sym, num) #sym,
+#define MRB_PRESYM_OPSYM(op, sym, num) #op,
#define MRB_PRESYM_SYM(sym, num) #sym,
static const char *presym_table[] = {