mirror of
https://github.com/sdiehl/write-you-a-haskell.git
synced 2024-09-11 11:46:57 +03:00
31 lines
682 B
C
31 lines
682 B
C
typedef struct T {
|
|
enum { ADD, MUL, DIV, SUB, NUM } tag;
|
|
union {
|
|
struct {
|
|
struct T *left, *right;
|
|
} node;
|
|
int value;
|
|
};
|
|
} Expr;
|
|
|
|
int eval(Expr t)
|
|
{
|
|
switch (t.tag) {
|
|
case ADD:
|
|
return eval(*t.node.left) + eval(*t.node.right);
|
|
break;
|
|
case MUL:
|
|
return eval(*t.node.left) * eval(*t.node.right);
|
|
break;
|
|
case DIV:
|
|
return eval(*t.node.left) / eval(*t.node.right);
|
|
break;
|
|
case SUB:
|
|
return eval(*t.node.left) - eval(*t.node.right);
|
|
break;
|
|
case NUM:
|
|
return t.value;
|
|
break;
|
|
}
|
|
}
|