I have found a program that reads "tokens" and reads either a string or a number . It uses postfix and a stack to evaluate simple commands.
For example:
/ x 100 def / y 200 def xy add 300 should be returned. The first line defines a variable named "x" and sets it to 100. To do so, "/ x" and "100" on the reader stack and when it is called "DIF" operator, which asks it to create a token with the symbol named "x" and its value is 100. The stack is empty, and next time "x" will be pushed, the interpreter should change it automatically with its value. This is where my problem is.
This is my interpreter:
while (r.hasMoreTokens ()) {token T = RNXToken (); If (! T. Ascambol ()) {operands.push (t); } And if (env.contains (T.T. T. simbol ())) {token tmp = nv.Tet (ttssonim.ball); Operands.push (tmp); } And if (T.T. T. Symbol) Starts ("/") {operands.push (t); } And if (TTA symbol (.) Equals ("DEF") {execute_def (); } And if (TAT isymbol (.) Equals ("add")) {execute_add (); } And if (T.T.T. Asimbol (.) Equals ("sub")} {execute_sub (); } And if (TGS Sombol (.) Equals ("MUL")) {execute_mul (); } And if (tttti simbol (.) Equal ("ach")) {execute_exch (); } And if (TATS is limited). Equal ("dump")) {execute_dup (); } And if (tttti simbol (.) Equal ("pop")) {execute_pop (); } And if (TTA symbol (.) Equals ("Paccha")) {execute_pstack (); } And if (TGS simbol (.) Equal ("move") {execute_moveto (); } And if (t.getsimbol (.) Equals ("linoteau")) {execute_lineto (g); } And if (tatty symbol (.) Equals ("arc")) {execute_arc (g); } And if (TGS Sambobal (.) Equals ("leave")) {execute_quit (); } And {System.out.println ("Invalid symbol:" + T); }} Once the variables are defined correctly, I can not enter the first and if and change the value. Because I can not do this, I never push anything on the stack and ends with an empty stack error, here contains () and get () < / Code> env (environment): are included in public boolean (string key) {Elem TMP = top; (For the IIFI = 0; I & lt; size; i ++) (if (tmp.key == key) {back true;} Else {tmp = tmp.next;}} description is false;} public token (String key) {Elem tmp = top; int counter = 0; boolean found = false; for (int i = 0; i & lt; size; i ++) {if (tmp.key == key) { Found = true; break;} Else {tmp = tmp.next;} Counter ++;} if (found == true) {tmp = top; for (int i = 0; i I linked to the environment to keep track of symbols Elements of I am using anemed environment in a nested class:
Private Static Class Elam {Private String Key; Private Token Value; Private Elm Next; Personal Eelam (String Key, Token Value, Allem Next) {this.key = key; This.value = value; this.next = next; In Java there are objects instead of string primitives. When You say:
int i = 5; I store the value "5"
When you say:
string s = "string"; s stores the value of the reference to "string".
The comparison of "string" will return false, even if you have the same values while printing whether you are there. The reason for this is that the computer compares the context of memory with "string" in the context of memory "string". Similar values, but different references
In addition, you are setting several different values in your code as "T". First of all against , and try setting a precomputed t value.
Comments
Post a Comment