representing functions with arguments in an abstract syntax tree

melkorainur@yahoo.com (Melkor Ainur)
27 Dec 2003 14:18:44 -0500

          From comp.compilers

Related articles
representing functions with arguments in an abstract syntax tree melkorainur@yahoo.com (2003-12-27)
Re: representing functions with arguments in an abstract syntax tree torek@torek.net (Chris Torek) (2004-01-02)
Re: representing functions with arguments in an abstract syntax tree malcolm@55bank.freeserve.co.uk (Malcolm) (2004-01-02)
Re: representing functions with arguments in an abstract syntax tree cfc@world.std.com (Chris F Clark) (2004-01-02)
Re: representing functions with arguments in an abstract syntax tree jacob@jacob.remcomp.fr (jacob navia) (2004-01-02)
Re: representing functions with arguments in an abstract syntax tree witness@t-online.de (Uli Kusterer) (2004-01-02)
| List of all articles for this month |
From: melkorainur@yahoo.com (Melkor Ainur)
Newsgroups: comp.compilers,comp.lang.c
Date: 27 Dec 2003 14:18:44 -0500
Organization: http://groups.google.com
Keywords: code, question
Posted-Date: 27 Dec 2003 14:18:44 EST

Hello,


I'm attempting to build an interpreter for a pascal-like language.
Currently, I don't generate any assembly. Instead, I just build an
abstract syntax tree representing what I've parsed from a user's
program and use a C backend to evaluate the tree. I'm using Lex, Yacc
and C. Now, there are some functions that are built into this language
and others which are not. The problem I'm having is that I haven't
found a way to represent functions and handle generating their
arguments.


For example, an example code block in this language looks like:


if (cpu_temp > 100) then
begin
    TurnOffWireless(interface1, interface2);
end


where both cpu_temp and TurnOffWireless are builtin functions.
currently, once I've parsed above, I build a datastructure looking
like below:


if_then_block_node->expression = expression_node
if_then_block_node->statements = function_node
expression_node->body.left = function_node
expression_node->body.right = number_node


where function_node looks like
node {
    fptr a;
    int numargs;
    node *arglist;
} function_node;


and arglist looks like
node {
    node *next;
    enum argtype_t argtype;
    union {
          int num;
          char *str;
          void* ptr;
    } body;
}




currently, my function_node just stores a function pointer (declared
as
typedef (void *)(*fptr)(void*) ) which i fill in with a C function for
the builtin. for example, cpu_temp(), calls a C function that uses a
system call to get the cpu temperature. when it comes to
TurnOffWireless(...), then I'm a bit stuck because of the need to
generate arguments for the builtin function. To be specific, how do
(or is it even possible) I write a generic function pointer that can
represent all my different functions. some that have multiple
promotable-arguments (chars, ints) and pointers. and then, how do I
Pass these functions their arguments? I currently suspect I can't do
that within C and that I might need to generate architecture specific
assembly to store the arguments on the stack and then call the builtin
function. Perhaps there is a GCC or compiler specific convention for
doing this?


That said, I'd suspect that other people have done or explored this
type of work. Googling for this didn't get me any real answers so I'm
hoping someone on these groups might point me towards some examples or
further reading that I could look at. I would think lanaguages like
perl/python/etc might be using similar concepts but trawling around
blindly in the source code was difficult. Anyone know how these
languages handle functions?


Last question, are there open-source blocks of code that I could reuse
for the purpose of this type of projects. IE: A reasonable parser that
generates a syntax tree/datastructure which I could then evaluate
using my choice of backend.


Any and all help/suggestions/recommendations/education are welcome.


Thanks,
Melkor


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.