# Code Romanization

The challenge is to make any Roman numerals valid code in your chosen language.

They should not appear inside of strings or anything similar, but work just like any other tokens, literals such as (Arabic) numbers, characters or strings; or variable/method/function identifiers, etc.

For instance, in Java, the following would have to compile and run just as if i was initialized to 42:

int i = XLII;


The actual parsing of the numerals is secondary, so you can use a library if you want to, but this is a popularity contest, so creativity is encouraged.

You cannot use any language that actually uses roman numerals, if there is such a thing.

Good luck.

-
So, we need to write an extension to the language, thereby creating a new one? –  Kendall Frey Feb 13 '14 at 15:48
I'll complain if I want to, because the languages I use are not extensible like that, so I can't even participate. –  Kendall Frey Feb 13 '14 at 16:15
@KendallFrey The source would have to compile and run. For Java, you could write a "compiler" that edits the source, then programmatically compiles. One way of such compiling is through running a Process –  Quincunx Feb 13 '14 at 19:04
@Bakuriu and your comment is boring too. This is a popularity contest, so you should try to come up with something fun. I think there are some nice answers here that are more imaginative (than compiling a scripting language). –  daniero Feb 13 '14 at 21:19
@Quincunx an old blog post about someone doing that... but rather closer to the compiler than a separate thing. iam.unibe.ch/~akuhn/blog/2008/roman-numerals-in-your-java –  MichaelT Feb 16 '14 at 1:25

# C

There are only so many Roman numerals, since 4000 and higher have no standard notation, and the preprocessor is a wonderful decompression tool, especially if you have no problems with the fact that the code has undefined behaviour.

enum{_
#define a_
#define d_
#define g_
#define j_
#define a(x)c(x)b
#define b(x)c(x)a
#define c(x)x,
#define d(x)f(x)e
#define e(x)f(x)d
#define f(x)m(a(x)(x##I)(x##II)(x##III)(x##IV)(x##V)(x##VI)(x##VII)(x##VIII)(x##IX))
#define g(x)i(x)h
#define h(x)i(x)g
#define i(x)m(d(x)(x##X)(x##XX)(x##XXX)(x##XL)(x##L)(x##LX)(x##LXX)(x##LXXX)(x##XC))
#define j(x)l(x)k
#define k(x)l(x)j
#define l(x)m(g(x)(x##C)(x##CC)(x##CCC)(x##CD)(x##D)(x##DC)(x##DCC)(x##DCCC)(x##CM))
#define m(x)n(x)
#define n(...)__VA_ARGS__##_
m(j()(M)(MM)(MMM))
};


This defines all Roman numerals from I to MMMCMXCIX as enumeration constants, plus _ (which can be replaced by anything you like) as zero.

-
Absolutely brilliant, propose adding it to the next C release (C2X?) as <roman.h>! With %r and %R printf formats! –  Yimin Rong Feb 13 '14 at 21:40
I thought i know how to use the preprocessor, until now :| Could you update your answer with a minimal usage example of this enum? –  klingt.net Feb 13 '14 at 22:59
@YiminRong And scanf too :) @klingt.net I'm not sure what sort of example you're looking for. A fairly simple one would be int main() { return MMMCMXCIX - M - M - M - CM - XC - IX; } –  hvd Feb 13 '14 at 23:21
I like the idea, but why did you opt to go with undefined behavior when defined behavior was fairly simple? Not judging, just curious? –  CasaDeRobison Feb 14 '14 at 7:19
@CasaDeRobison For fun, mainly. I like it because it is one of the very few cases of undefined behaviour at preprocessing time that isn't flagged as an error by current compilers, and likely won't be in the future. I don't ever write anything like that in code that's meant to be useful, but code posted here isn't meant to be useful, so what better occasion to try it? –  hvd Feb 14 '14 at 7:44

## Ruby

def Object.const_missing(const)
i = const.to_s
if i =~ /^[IVXLCDM]+$/ #http://codegolf.stackexchange.com/questions/16254/convert-arbitrary-roman-numeral-input-to-integer-output %w[IV,4 IX,9 XL,40 XC,90 CD,400 CM,900 I,1 V,5 X,10 L,50 C,100 D,500 M,1000].map{|s|k,v=s.split ?,;i.gsub!(k,?++v)} eval(i) else super end end  Any (uppercase) Roman numerals will now be parsed like their decimal equivalents. The only issue is that they're still assignable: you can do X = 9, but not 10 = 9. I don't think there's a way to fix that. - This is perfect. The assignment thing is not an issue; You can use assignments to do all sorts of stupid things. – daniero Feb 13 '14 at 16:10 # Lua setmetatable(_G, { __index = function(_, k) if k:match"^[IVXLCDM]*$" then
local n = 0
for _, v in ipairs{{IV = 4}, {IX = 9}, {I = 1}, {V = 5}, {XL = 40}, {X = 10}, {XC = 900}, {CD = 400}, {CM = 900}, {C = 100}, {D = 500}, {M = 1000}} do
local p, q = next(v)
local r
k, r = k:gsub(p, "")
n = n + r * q
end
return n
end
end
})


Simply a fallback __index for the global table. The actual conversion using gsub turned out much prettier than I imagined it to be.

-

# 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 }  - # C & C++ (Updated Answer) As observed in a comment, my original solution had two problems: 1. Optional parameters are only available in C99 and later standards of the language family. 2. Trailing comma in enum definition is also specific to C99 and later. Since I wanted my code to be as generic as possible to work on older platforms, I decided to take another stab at it. It is longer than it was before, but it works on compilers and preprocessors set to C89/C90 compatibility mode. All macros are passed an appropriate number of arguments in the source code, though sometimes those macros "expand" into nothing. Visual C++ 2013 (aka version 12) emits warnings about missing parameters, but neither mcpp (an open source preprocessor that claims high compliance with the standard) nor gcc 4.8.1 (with -std=iso9899:1990 -pedantic-errors switches) emit warnings or errors for those macro invocations with an effectively empty argument list. After reviewing the relevant standard (ANSI/ISO 9899-1990, 6.8.3, Macro Replacement), I think there is sufficient ambiguity that this should not be considered non-standard. "The number of arguments in an invocation of a function-like macro shall agree with the number of parameters in the macro definition...". It does not seem to preclude an empty argument list as long as the needed parentheses (and commas in the case of multiple parameters) are in place to invoke the macro As for the trailing comma problem, that is resolved by adding an extra identifier to the enumeration (in my case, MMMM which seems as reasonable as anything for the identifier to follow 3999 even if it doesn't obey the accepted rules of Roman numeral sequencing exactly). A slightly cleaner solution would involve moving the enum and supporting macros to a separate header file as was implied in a comment elsewhere, and using undef of the macro names immediately after they were used so as to avoid polluting the namespace. Better macro names should undoubtedly be chosen as well, but this is adequate for the task at hand. My updated solution, followed by my original solution: #define _0(i,v,x) #define _1(i,v,x) i #define _2(i,v,x) i##i #define _3(i,v,x) i##i##i #define _4(i,v,x) i##v #define _5(i,v,x) v #define _6(i,v,x) v##i #define _7(i,v,x) v##i##i #define _8(i,v,x) v##i##i##i #define _9(i,v,x) i##x #define k(p,s) p##s, #define j(p,s) k(p,s) #define i(p) j(p,_0(I,V,X)) j(p,_1(I,V,X)) j(p,_2(I,V,X)) j(p,_3(I,V,X)) j(p,_4(I,V,X)) j(p,_5(I,V,X)) j(p,_6(I,V,X)) j(p,_7(I,V,X)) j(p,_8(I,V,X)) j(p,_9(I,V,X)) #define h(p,s) i(p##s) #define g(p,s) h(p,s) #define f(p) g(p,_0(X,L,C)) g(p,_1(X,L,C)) g(p,_2(X,L,C)) g(p,_3(X,L,C)) g(p,_4(X,L,C)) g(p,_5(X,L,C)) g(p,_6(X,L,C)) g(p,_7(X,L,C)) g(p,_8(X,L,C)) g(p,_9(X,L,C)) #define e(p,s) f(p##s) #define d(p,s) e(p,s) #define c(p) d(p,_0(C,D,M)) d(p,_1(C,D,M)) d(p,_2(C,D,M)) d(p,_3(C,D,M)) d(p,_4(C,D,M)) d(p,_5(C,D,M)) d(p,_6(C,D,M)) d(p,_7(C,D,M)) d(p,_8(C,D,M)) d(p,_9(C,D,M)) #define b(p) c(p) #define a() b(_0(M,N,O)) b(_1(M,N,O)) b(_2(M,N,O)) b(_3(M,N,O)) enum { _ a() MMMM }; #include <stdio.h> int main(int argc, char** argv) { printf("%d", MMMCMXCIX * MMMCMXCIX); return 0; }  The original answer (which received the first six upvotes, so if no one ever upvotes this again, you shouldn't think my updated solution got the upvotes): In the same spirit as an earlier answer, but done in a way that should be portable using only defined behavior (though different environments don't always agree on some aspects of the preprocessor). Treats some parameters as optional, ignores others, it should work on preprocessors that don't support the __VA_ARGS__ macro, including C++, it uses indirect macros to ensure parameters are expanded before token pasting, and finally it is shorter and I think easier to read (though it is still tricky and probably not easy to read, just easier): #define g(_,__) _, _##I, _##II, _##III, _##IV, _##V, _##VI, _##VII, _##VIII, _##IX, #define f(_,__) g(_,) #define e(_,__) f(_,) f(_##X,) f(_##XX,) f(_##XXX,) f(_##XL,) f(_##L,) f(_##LX,) f(_##LXX,) f(_##LXXX,) f(_##XC,) #define d(_,__) e(_,) #define c(_,__) d(_,) d(_##C,) d(_##CC,) d(_##CCC,) d(_##CD,) d(_##D,) d(_##DC,) d(_##DCC,) d(_##DCCC,) d(_##CM,) #define b(_,__) c(_,) #define a b(,) b(M,) b(MM,) b(MMM,) enum { _ a };  - +1, but note that the use of empty macro arguments, and the comma at the end of an enumerator list, are both new features of C99 and C++11, and both C99 and C++11 do support __VA_ARGS__. – hvd Feb 14 '14 at 17:08 Dang if you're not right! I guess I've been seeing this used as extensions all this time. Ah well. :) – CasaDeRobison Feb 15 '14 at 5:25 # Smalltalk (Smalltalk/X) (87/101 chars) of course we could easily modify the parser's tokenizer (as it is part of the class library, and as such open for modification, and always present), but a challenge is to affect only evaluations in a given context, so that the rest of the system works as usual. ### Version 1: define a number of variables in the evaluation namespace. So this will affect interactive doIts (aka evals): (1 to:3999) do:[:n | Workspace workspaceVariables at:(n romanPrintString asUppercase) put:n]  then I can do (in a doIt, but not in compiled code):  |a b| a := MMXIV. b := V. a + b  -> 2019 Notice: the 101 chars includes whitespace; actually it can be done with 87 chars. Also notice, when define in the global Smalltalk namespace, I'd see those constants also in compiled code. ### Version 2: Use a methodWrapper hook, which allows for any existing code to be wrapped without recompiling. The following wraps the Parser's tokenizer to look for a roman identifier to be scanned and makes it an integer. The tricky part is to dynamically detect if the calling context is from the roman empire or not. This is done using a query signal (which is technically a proceedable exception): define the query: InRomanScope := QuerySignal new defaultAnswer:false.  So we can ask at any time ("InRomanScope query") to get false by default. Then wrap the scanner's checkIdentifier method: MessageTracer wrapMethod:(Scanner compiledMethodAt:#checkForKeyword:) onEntry:nil onExit:[:ctx :ret | InRomanScope query ifTrue:[ |scanner string token n| scanner := ctx receiver. string := ctx argAt:1. (n := Integer readFromRomanString:string onError:nil) notNil ifTrue:[ scanner "/ hack; there is no setter for those two instVarNamed:'tokenType' put:#Integer; instVarNamed:'tokenValue' put:n. true ] ifFalse:[ ret ]. ] ifFalse:[ ret ] ].  Now the scanner works as usual, unless we are in the roman empire: InRomanScope answer:true do:[ (Parser evaluate:'MMDXXV') printCR. ].  -> 2525 we can even compile code: Compiler compile:' inTheYear2525 ^ MMDXXV ' forClass:Integer.  nice try; but this fails with a syntax error (which is exactly what we want). However, in the roman empire, we CAN compile: InRomanScope answer:true do:[ Compiler compile:' inTheYear2525 ^ MMDXXV ' forClass:Integer. ]  and now, we can ask any integer (sending that message) from inside and outside of Rome: (1000 factorial) inTheYear2525  -> 2525 - Nice to see Smalltalk! – Yimin Rong Feb 13 '14 at 21:48 # Postscript I tried to follow the C one but I didn't understand it. So I did it this way: /strcat{ 2 copy length exch length add string % 1 2 3 3 2 roll % 2 3 1 2 copy 0 exch putinterval % 2 3 1 2 copy length % 2 3 1 3 n(1) 5 4 roll putinterval % 3 1 pop }def /S{ dup length string cvs } def [ /u {/ /I /II /III /IV /V /VI /VII /VIII /IX} /t {/ /X /XX /XXX /XL /L /LX /LXX /LXXX /XC} /h {/ /C /CC /CCC /CD /D /DC /DCC /DCCC /CM} /m {/ /M /MM /MMM /MMMM} >>begin [0 [ [m]{ % M* S [h]{ % M* H* S [t]{ % M* H* T* S [u]{ % M* H* T* U* S 4 copy strcat strcat strcat % M* H* T* U* M*H*T*U* 5 1 roll % M*H*T*U* M* H* T* U* pop % (M*H*T*U*)* M* H* T* }forall pop % (M*H*T*U*)** M* H* }forall pop % (M*H*T*U*)*** M* }forall pop % (M*H*T*U*)**** }forall ] {exch dup 1 add}forall pop>>end begin  Postscript doesn't have enum but we can construct a dictionary with sequential integer values and fold them into an array. This reduces the problem to generating all the strings in sequence, which is done by concatenating in 4 nested loops. - ## D using D's compile time function evaluation import std.algorithm; string numerals(){ auto s = cartesianProduct(["","I","II","III","IV","V","VI","VII","VIII","IX"], ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"], ["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"], ["","M","MM","MMM"]); s.popFront();//skip first char[] result="enum "; int i=1; foreach(p;s){ result~=p[3]~p[2]~p[1]~p[0]~"="~i++~","; } result[$-1]=';';//replace last , with a ;
return result.idup;
}
mixin(numerals());

-

# Common Lisp

The following is a rather lengthy explanation of how I made a macro that you can use like this:

(roman-progn
(+ XIV XXVIII))


When a macro is called in Common Lisp, it basically acts like a function, only that the arguments are received before they are evaluated. Actually, since in Common Lisp code is just data, what we receive is a (nested) list representing an unparsed syntax tree that we can do whatever we want with, and it's done in compile-time.

### Helper functions

The first step of the plan is to take this tree and scan it for anything that looks like Roman Numerals. This being Lisp and all, let's try to do it somewhat functionally: We need a function that will do a deep traversal of a tree and return every object for which a provided function searchp returns true. This one is even (semi) tail-recursive.

(defun deep-find-all (searchp tree &optional found)
(if (null tree)
found
(let* ((item (car tree))
(new-found (if (consp item)
(deep-find-all searchp item found)
(if (funcall searchp item)
(cons item found)
found))))

(deep-find-all searchp (cdr tree) new-found))))


Then some code for parsing the roman numerals, courtesy of Rosetta Code:

(defun mapcn (chars nums string)
(loop as char across string as i = (position char chars) collect (and i (nth i nums))))

(defun parse-roman (R)
(loop with nums = (mapcn "IVXLCDM" '(1 5 10 50 100 500 1000) R)
as (A B) on nums if A sum (if (and B (< A B)) (- A) A)))


### The actual macro

We take the syntax tree (body), search it with our deep-find-all procedure and somehow make the roman numerals that we find, available.

(defmacro roman-progn (&body body)
(let* ((symbols (deep-find-all (lambda (sym)
(and
(symbolp sym)
(loop for c across (string sym)
always (find c "IVXLCDM")))) body))
(parsed-romans (mapcar (lambda (sym)
(list sym (parse-roman (string sym)))) symbols)))

(if parsed-romans
(let (,@parsed-romans)
,@body)
(progn
,@body))))


So, what is 1 + 2 + 3 + (4 * (5 + 6)) + 7 ?

(roman-progn
(+ I II III (* IV (+ V VI)) VII))
=> 57


And to see what actually happened when the macro was invoked:

(macroexpand-1 +)
=> (LET ((VII 7) (VI 6) (V 5) (IV 4) (III 3) (II 2) (I 1))
(+ I II III (* IV (+ V VI)) VII))

-

# J - 88 char

This only goes up to MMMCMXCIX = 3999, as with the other solutions.

(}.,;L:1{''&;@;:&.>'M MM MMM';(<3,~2933774030998#:~27#3)({' '&,)&.>CDMXLCIVX)=:}.i.4e3


Breaking it down (recall J is usually read from right to left, unless superseded by parentheses):

• 2933774030998#:~27#3 - Expand ~2.9 trillion into base 3, giving us a strategic array of numbers in the range 0 to 2, namely 1,0,1,1,0,1,1,1,0,1,2,0,2,0,2,1,0,2,1,1,0,2,1,1,1,0,1. The 27 specifies how many place values to take it to.
• <3,~ - Add 3 to the end of the array, and put this in a box for later.
• (...)({' '&,)&.>CDMXLCIVX - For each (&.>) of the strings 'CDM', 'XLC', and 'IVX', add a space to the front (' '&,), and then use the above boxed array to index the string. This builds strings like 'I II III IV V VI VII VIII IX', and puts each new string in a box.
• 'M MM MMM'; add a box containing 'M MM MMM' to the front.
• ''&;@;:&.> - Under each box (&.>), split up the string by words (;:) and then add an empty string to the wordlist (''&;).
• { - Take the Cartesian product. Now we have a 4-dimensional array of wordlists.
• ;L:1 - Turn each wordlist into a name. (L: is a rare sight in J, and L:1 even more so!)
• }., - Turn the array into a 1D list (,), and remove the boxed empty string at the front (}.) because it would be an invalid name.
• }.i.4e3 - The integers from 0 to 4000, exclusive.
• Finally we put everything together with a global assignment =:. J allows you to have a boxed list of names on the LHS, as a form of multiple assignment, so this works out fine.

Now the J namespace is full of variables representing Roman numerals.

   XCIX
99
MMCDLXXVIII
2478
V * LXIII   NB. 5*63
315
#4!:1]0     NB. How many variables are now defined in the J namespace?
3999

-

{-# LANGUAGE TemplateHaskell #-}
import Data.Char (toLower)
import Text.Numeral.Roman

$( mapM (\n -> funD (mkName . map toLower . toRoman$ n)
[ clause [] (normalB [| n |]) []]) \$ [1..1000 :: Int] )

main = print xlii


Haskell reserves identifiers starting with upper case letters for constructors, so I used lower-case.

-

## Python

The Idea is simple as the other answers. But Just to be neat and not to pollute the global namespace, a context manager is used. This also imposes the restriction, that you need to declare before hand, the extent of Roman numerical you are planning to use.

Note Just to keep it simple, and not to reinvent the wheel, I have utilized the Roman python package

Implementation

class Roman(object):
memo = [0]
def __init__(self, hi):
import rome
if hi <= len(RomanNumericals.memo):
self.romans = Roman.memo[0:hi]
else:
Roman.memo += [str(rome.Roman(i))
for i in range(len(Roman.memo),
hi+1)]
self.romans = Roman.memo
def __enter__(self):
from copy import copy
self.saved_globals = copy(globals())
globals().update((v,k) for k,v in enumerate(self.romans[1:], 1))
def __exit__(self,*args ):
globals().clear()
globals().update(self.saved_globals)


Demo

with Roman(5):
with Roman(10):
print X
print V
print X

10
5

Traceback (most recent call last):
File "<pyshell#311>", line 5, in <module>
print X
NameError: name 'X' is not defined

-

# Rebol

Rebol []

roman-dialect: func [
{Dialect which allows Roman numerals as numbers (integer!)}
block [block!]
/local word w m1 m2 when-in-rome rule word-rule block-rule
][
when-in-rome: does [
if roman? w: to-string word [change/part m1 roman-to-integer w m2]
]

word-rule:  [m1: copy word word! m2: (when-in-rome)]
block-rule: [m1: any-block! (parse m1/1 rule)]
rule:       [any [block-rule | word-rule | skip]]

parse block rule
do block
]

; couple of helper functions used in above parse rules

roman-to-integer: func [roman /local r eval] [
r: [IV 4 IX 9 XL 40 XC 90 CD 400 CM 900 I 1 V 5 X 10 L 50 C 100 D 500 M 1000]
eval: join "0" copy roman
foreach [k v] r [replace/all eval k join " + " v]
do replace/all to-block eval '+ '+
]

roman?: func [s /local roman-chars] [
roman-char: charset "IVXLCDM"
parse/case s [some roman-char]
]


### Example

roman-dialect [
m: X + V
print m
print M + m  ; doesn't confuse the variable m with Roman Numeral M
if now/year = MMXIV [print "Yes it is 2014"]
for n I V I [
print [rejoin [n "+" X "="] n + X]
]
]


Output:

15

1015

Yes it is 2014

1+10= 11

2+10= 12

3+10= 13

4+10= 14

5+10= 15

Disclaimer: I'm sure there are other (and probably better!) ways of doing this in Rebol as well.

PS. My roman-to-integer function is a transliteration of histocrat's nice Ruby algorithm for converting Roman Numeral string into a number. Returned with thanks! +1

-

Python

This is possibly the simplest solution using Python:

ns = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
ls = 'M CM D CD C XC L XL X IX V IV I'.split()
for N in range(1, 4000):
r=''
p=N
for n,l in zip(ns,ls):
while p>=n:
r+=l
p-=n
exec('%s=%d'%(r,N))

i, j = XIX, MCXIV
print i, j

-
Better to use globals()[var] = value than exec(). –  Ramchandra Apte Feb 15 '14 at 15:21