Here is a link to the "Pierce Book" or Benjamin Pierce's draft transcript of (the first part of) "Types and Programming Languages".
On PDF page 28 you'll see an OCaml implementation of eval1
. Which I assumed is like a recursive single-step evaluator?
See the directory Dots' first tutorial / project D
code. As you see I have a fully functioning grammar at least of the syntax. Now I'm wondering if we convert the pegged ParseTree into some other internal format? Or do we try to operate on ParseTree's themselves and then finally pretty print a result?
For example, one evaluation rule is E-BetaBoolT: if true then s:Term else t:Term |-> s
, where here |->
is the evalutation relation.
So if for example I decided to convert true to a bool and throw away the pegged ParseTree, then it seems like it would 1) harder to implement because you have to write a 1-1 correspondence of classes with your grammar rules at least. And 2) it's going to be full of bugs and error prone, thus making your math kernel buggy which can't happen as is enforced in practice.
On the other hand manipulating someone else's ParseTree seems tacky. Though, it seems like an efficient enough structure. So I'm wondering, what would be the advantages of converting to a second type or layer of software? Would the ParseTree method be significantly slower?
NumbersAndBooleans:
Term <- IfThenElse(Term) / Successor(Value) /
Predecessor(Value) / IsZero(Value) / Value
IfThenElse(T) < "if" T "then" T "else" T
Successor(V) < "succ" V
Predecessor(V) < "pred" V
IsZero(V) < "iszero" V
Value <- True / False / Zero
True <- "true"
False <- "false"
Zero <- "0"
There is my grammar, and I am able to produce parse trees from it. But I don't know what to do with the parse trees to "evaluate" them.
How I'm currently implementing if-then-else evaluator:
// Inside class IfThenElse that inherits from Term abstract class
override Value evaluate() const {
if (if_cond.evaluate().is_true)
{
// Pierce book: if true then t2 else t3 -> t2 (E-BOOLBETAT)
return then_part.evaluate();
}
else {
// Pierce book: if false then t2 else t3 -> t3 (E-BOOLBETAF)
return else_part.evaluate();
}
}
if b then t else t ≡ t
is not something that would appear in such a straightforward manner in an interpreter. $\endgroup$