java - Implementing functions in an interpreted programming language -
Before I write about your question, just a little background information. I am writing a toy programming language in Java because I am fascinated with compilers / interpreters and likes. I have found the basics in this small language, it is on the pattern of:
5, 6 -> Add 5; C print will store in C # 11 in C This is very basic, but this is a beginning since I am only 16 years old, I can not read books about technical things They are very meager / soft for me, I like to read articles on the Internet, or small tutorials posting on HN (for example, writing plans in C). Either way, I'm really confused about how to implement a function in a language, eg
# Only integers, since it is easier than many data types, FUNC (a, b) - & gt; The way I can implement the (5, 6) function, will actually be hack-y and a mess of spaghetti will change in the mess. I want to know the 'proper' method of applying the work in a programming language. A little bit on the language: I have not applied AST because I have not learned them yet, I wrote a lixar for language which works well, the parser is very simple and just above the top, left to right ( Forgetting the technical term for this, the recurring descent parser?).
Sorry if this is a bad question, ambiguous, anything like this. Ever never posted anything on the stack overflow, and I wrote some code in an attempt to implement the function but removed it because it was not working very well (it was a few days ago), and since I want to have a definite plan of implementation, and I believe it will work.
Thank you!
I suggest you start with expression evaluation. It is very easy to use 1 or 2 stack (whether you output or immediately output the RPN output).
I have used the shunning-yard algorithm for mathematical expression ().
For functions, you will need to define a structure to capture information such as variables, local variable names, and some representation of functions such as the execution of the function code , Which can be executed.
If you use the call stack, you can keep the local arguments on the stack. You will then need to "compile" the executable representation so that they can use stack offsets instead of the variable names. Alternatively, you can use hash-tables as a stack namespace stack. You will then need to look up the variable from top to bottom in each hash table until the variable is found. In these ways, local variables will obscure the global variable with the same name (which is what you want).
With the Shanting-yard algorithm, you will need to follow some book-to-answer brackets, with your example
print whatever (5 , 6) print is probably considered as a statement type that executes the following expression and then prints the result. So you will see this expression as many different tokens.
Whatever can be found (5, 6) After the left colonel To evaluate the code of the function, then call your function-call-handling function and return the result to Print and use it as a result of evaluating the entire printing. / P> to be a function name, if It was defined earlier but if you want to allow functions as first class citizens , then it can not be a function call even if you do not see the parents. (You probably want to print the code for the function.) () an identifier, the function call is clearly beginning. Arrange to recapitulate each comma-delimited expression, and use these results as the argument of the function. With the call-stack method, just press two results. Define two variables with the namespace stack, and press the hash
Comments
Post a Comment