JavaScript (ES6)
Use Proxy to catch roman numerals.
Testable in Firefox (latest) on JSFiddle.
Not testable in Chrome (with Traceur) since Proxy implementation is broken.
// Convert roman numeral to integer.
// Borrowed from http://codegolf.stackexchange.com/a/20702/10920
var toDecimal = (s) => {
var n = 0;
var X = {
M: 1e3, CM: 900, D: 500, CD: 400, C: 100, XC: 90,
L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1
};
s.replace(/[MDLV]|C[MD]?|X[CL]?|I[XV]?/g, (d) => n += X[d]);
return n;
};
// Whether a string is a valid roman numeral.
var isRoman = (s) => s.match(/^[MDCLXVI]+$/);
// Profixy global environment to catch roman numerals.
// Since it uses `this`, global definitions are still accessible.
var romanEnv = new Proxy(this, {
get: (target, name) => isRoman(name) ? toDecimal(name) : target[name],
set: (target, name, value) => isRoman(name) ? undefined : (target[name] = value),
has: (target, name) => isRoman(name) || name in target,
hasOwn: (target, name) => name in target
});
Usage:
with (romanEnv) {
var i = MIV + XLVIII;
console.log(i); // 1052
}