Last active
May 16, 2016 08:54
-
-
Save gdejohn/8ac17b075b01009b9d8b584133304be7 to your computer and use it in GitHub Desktop.
Ceylon solution to the expression problem.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"The datatype." | |
shared interface Expression { | |
"The initial operation." | |
shared formal Integer evaluate(); | |
} | |
"The initial case of the datatype." | |
shared interface Literal satisfies Expression { | |
"The value of the literal expression." | |
shared formal Integer literal; | |
evaluate() => literal; | |
} | |
"The new case of the datatype." | |
shared interface Addition satisfies Expression { | |
"Left operand." | |
shared formal Expression left; | |
"Right operand." | |
shared formal Expression right; | |
evaluate() => left.evaluate() + right.evaluate(); | |
} | |
"Extension of the datatype." | |
shared interface Show satisfies Expression { | |
"The new operation." | |
shared formal String show(); | |
} | |
"Implementation of new operation for literals." | |
shared interface LiteralShow satisfies Literal & Show { | |
show() => literal.string; | |
} | |
"Implementation of new operation for addition." | |
shared interface AdditionShow satisfies Addition & Show { | |
"Covariant refinement of inherited attribute." | |
shared actual formal Show left; | |
see(`value left`) | |
shared actual formal Show right; | |
show() => "(``left.show()`` + ``right.show()``)"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment