Assembler API

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Assembler API

steven
I am having some difficulty with the API for the assembler.  The book suggests an API that would look similar to this for a C program:

int has_more_commands()
void advance()

I have created the functions as specified.  Unfortunately as I am not passing the address of the current_command and the input_file_buffer that contains the data, these functions have to be in main.  Otherwise the current_command variable will not be in scope.

For a procedural language such as C, would it not make more sense to pass *current_command and *input_file_buffer to each function like so:

int has_more_commands(char *current_command, char *input_file_buffer)
void advance(char *current_command, char *input_file_buffer)

This would make the functions be portable to another file and eliminate the need for global variables.

Is there another way to solve this problem in a procedural language without changing the API specified?

Thank you so much for your help.
Reply | Threaded
Open this post in threaded view
|

Re: Assembler API

Jacek Dabrowski
steven wrote
I have created the functions as specified.  Unfortunately as I am not passing the address of the current_command and the input_file_buffer that contains the data, these functions have to be in main.  Otherwise the current_command variable will not be in scope.
Why don't you define both pointers in your parser.c file? No function outside the parser module needs direct access to the input buffer or the command string. You can read the input in parser initialization.
Reply | Threaded
Open this post in threaded view
|

Re: Assembler API

ysh443
Hi
like steve, i have the same question.
in order to stick to the API suggested functions, the Initializer and advance and other functions
should have the possibility to access the variables that hols the currect_parsed command or file_position

how can i do it?
one option is to denige these variables as global (to the parser.c or to all the program files)
is there any other way?


regards
Reply | Threaded
Open this post in threaded view
|

Re: Assembler API

cadet1620
Administrator
In a non-object oriented language like C you need to use global variables.

parser.c will declare things like the current line buffer as
    static char current_line[MAX_LINE_LEN];
so that other source files won't accidentally modify them. Functions in parser.c are allowed to access the line buffer.

You should have a parser.h file that declares all the functions, data types, and constants that are the public interface to the parser code.

In an object oriented language like C++ these globals become static member variables in the class definition and are automatically hidden from functions that are not class methods.

--Mark