C compiler with embedded metalanguage.
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

47 righe
1.1 KiB

@(import("class.mc"))
@{
//creation of the object class
Object = initClass();
Object.type = "object";
type_t = { type_Nil: 0, type_Int: 1, type_Pair: 2};
printf(x) {
switch (x.type) {
case type_t.type_Int: {
print(x.values.value);
return;
}
case type_t.type_Pair: {
print("(");
printf(x.values.a);
print(", ");
printf(x.values.d);
print(")");
return;
}
}
}
Object.functions.printf = printf;
// from object
Nil = classCopy(Object);
Nil.type = type_t.type_Nil;
Int = classCopy(Object);
Int.values.value = "null";
Int.type = type_t.type_Int;
Pair = classCopy(Object);
Pair.values.a = "null";
Pair.values.d = "null";
Pair.type = type_t.type_Pair;
// function to simplify the writting of example
newNil(x) { new(Nil, {}); }
newInt(x) { new(Int, {value: x}); }
newPair(x, y) { new(Pair, { a: x, d: y }); }
// example
l = newPair(newInt(1), newPair(newInt(2), newPair(newInt(3), newNil())))
l.functions.printf(l);
nil;
}