J
johan.tibell
I'm in the process of writing an interpreter for lambda calculus (i.e.
a small functional programming language) in C. I've previously written
one in Haskell so I understand at least some of the concepts involved
in writing an interpreter and I know how to define a grammar and use
lex/yacc to parse it. What I don't know is how to represent the
abstract syntax tree (AST) of my language in C. In Haskell I used
what's called an algebraic data type like so:
data Exp = Lam Id Exp
| Var Id
| App Exp Exp
| Lit Int
Legend:
Exp - expression (i.e. one of Lam, Var, App or Lit)
Lam - lambda abstraction (i.e. a function)
App - function application
Lit - integer literal
Id - variable name (i.e. a string)
How would I represent such a structure in C, perhaps with a union with
some sort of tag? I guess I would use a class hierarchy in an
object-oriented language but I really want to stay in C since I
consider this an educational experience for me (i.e. the goal is not to
write an interpreter as quickly as possible but rather learn how to do
it in an efficient way in C).
Once I have an AST I can begin applying transformations such as closure
and CPS (continuation-passing style) conversions to it and I think I
can achieve that on my own but right now finding a good AST
representation is holding me back.
P.S. For those who are interested my plan is to write an all C
interpreter for a strict functional language and use the Boehm garbage
collector.
Thanks in advance,
Johan Tibell
a small functional programming language) in C. I've previously written
one in Haskell so I understand at least some of the concepts involved
in writing an interpreter and I know how to define a grammar and use
lex/yacc to parse it. What I don't know is how to represent the
abstract syntax tree (AST) of my language in C. In Haskell I used
what's called an algebraic data type like so:
data Exp = Lam Id Exp
| Var Id
| App Exp Exp
| Lit Int
Legend:
Exp - expression (i.e. one of Lam, Var, App or Lit)
Lam - lambda abstraction (i.e. a function)
App - function application
Lit - integer literal
Id - variable name (i.e. a string)
How would I represent such a structure in C, perhaps with a union with
some sort of tag? I guess I would use a class hierarchy in an
object-oriented language but I really want to stay in C since I
consider this an educational experience for me (i.e. the goal is not to
write an interpreter as quickly as possible but rather learn how to do
it in an efficient way in C).
Once I have an AST I can begin applying transformations such as closure
and CPS (continuation-passing style) conversions to it and I think I
can achieve that on my own but right now finding a good AST
representation is holding me back.
P.S. For those who are interested my plan is to write an all C
interpreter for a strict functional language and use the Boehm garbage
collector.
Thanks in advance,
Johan Tibell