Created
April 15, 2024 12:38
-
-
Save thelissimus/00304902ac7973e18027f59c49fb7e08 to your computer and use it in GitHub Desktop.
LInt implemented in Prolog.
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
exp(E) --> int(E). | |
exp(E) --> binop(E). | |
int(I) --> [I], { integer(I) }. | |
binop(add(A, B)) --> "+", exp(A), exp(B). | |
binop(sub(A, B)) --> "-", exp(A), exp(B). | |
binop(mul(A, B)) --> "*", exp(A), exp(B). | |
binop(div(A, B)) --> "/", exp(A), exp(B). | |
eval(int(I), I). | |
eval(add(A, B), Result) :- eval(A, AVal), eval(B, BVal), Result is AVal + BVal. | |
eval(sub(A, B), Result) :- eval(A, AVal), eval(B, BVal), Result is AVal - BVal. | |
eval(mul(A, B), Result) :- eval(A, AVal), eval(B, BVal), Result is AVal * BVal. | |
eval(div(A, B), Result) :- eval(A, AVal), eval(B, BVal), BVal =\= 0, Result is AVal / BVal. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dot feels more logical than semicolons for ending the line🤔