Mixed Languages


Summary

This article shows an example of a program written using a collection of languages weaved together in the context of Micro Java.  The embedded/weaved languages include XOCL, PHP and Lisp which have all been described in previous documents.  The important feature of this example is that each of the languages were defined independently without knowledge of each other.


Example


parserImport Languages::MicroJava;
parserImport XOCL;
parserImport Parser::BNF;

import LispOps;

context Root
@Operation count(S:Seq(Element)):Integer
S->size
end

context Root

@Java

class X {

int x ;

public X(int x) {
this.x = x;
}

public int test() {
Vector ints = this.descending(x);
Vector ascending = this.reverse(ints);
Vector facts = this.mapFact(ascending);
this.printCollection(facts);
}

with JOCL {
@Operation mapFact(s:Seq(Integer)):Integer
s->collect(n | self.fact(n))
end
}

with JOCL {
@Operation reverse(S:Seq(Element)):Seq(Element)
S->reverse
end
}

with JLisp {
(let ((Fact
(lambda (fact)
(lambda fact (n)
(if (eql n 0)
1
(mult n (fact (sub n 1))))))))

(Y Fact))
}

with JLisp {
(let ((descending
(lambda(descending)
(lambda descending(n)
(if (eql n 0)
()
`(,n . ,(descending (sub n 1))))))))
(Y descending))
}

with JPHP {
function printCollection($collection) {
for($i=0;$i<count($collection);$i++) {
echo $collection[$i]
}
}
}

}
end

A transcript of use:

[1] XMF> X(10).test();
1
2
6
24
120
720
5040
40320
362880
3628800
null
[1] XMF>