@@ -25,16 +25,16 @@ public void interpreter(String[] args) {
2525 Executor executor = new Stack .Executor (Mode .Debug , true );
2626 // REPL Execution
2727 while (!executor .exitmode ) {
28- String code = "" ;
28+ StringBuilder code = new StringBuilder () ;
2929 while (!executor .exitmode ) {
3030 String enter = input (br , "> " );
31- code += enter + "\n " ;
32- if (enter .equals ( "" )) {
31+ code . append ( enter ). append ( "\n " ) ;
32+ if (enter .isEmpty ( )) {
3333 break ;
3434 }
3535 }
3636
37- executor .evaluateProgram (code );
37+ executor .evaluateProgram (code . toString () );
3838 }
3939
4040 try {
@@ -206,7 +206,7 @@ public static enum Mode {
206206 }
207207
208208 public static class Executor {
209- private List <Type > stack ; // Data stack
209+ protected List <Type > stack ; // Data stack
210210 private Map <String , Type > memory ; // Variable's memory
211211 private Mode mode ; // Execution mode
212212 public boolean exitmode = false ;
@@ -390,43 +390,7 @@ public void evaluateProgram(String code) { // evaluate string as program
390390
391391 // execute string as commands
392392 public void executeCommand (String command ) {
393- // Commands of calculation
394-
395- // addition
396- if (command .equals ("add" )) {
397- double b = this .popStack ().getNumber ();
398- double a = this .popStack ().getNumber ();
399- this .stack .add (new Type (Type .TypeEnum .NUMBER , a + b ));
400- }
401-
402- // Subtraction
403- else if (command .equals ("sub" )) {
404- double b = this .popStack ().getNumber ();
405- double a = this .popStack ().getNumber ();
406- this .stack .add (new Type (Type .TypeEnum .NUMBER , a - b ));
407- }
408-
409- // Pop in stack
410- else if (command .equals ("pop" )) {
411- this .popStack ();
412- }
413-
414- // Exit Stack
415- else if (command .equals ("exit" )) {
416- double code = this .popStack ().getNumber ();
417- this .exitmode = true ;
418- this .exitcode = (int ) code ;
419- if (this .interpreterMode == false ) {
420- System .exit (exitcode );
421- }
422- }
423-
424- // TODO Other commands...
425-
426- // When other string inputted
427- else {
428- this .stack .add (new Type (Type .TypeEnum .STRING , command ));
429- }
393+ new Functions (this , command );
430394 }
431395
432396 public Type popStack () { // Pop stack's top value
0 commit comments