amritrout / ArcaneScript

ArcaneScript is a custom programming language developed to explore language design and interpretation. This project includes the development of both the ArcaneScript language and its online Runner.
https://arcanescript.netlify.app/
6 stars 3 forks source link

Add else if support in ArcaneScript #7

Open thecloudcode opened 1 month ago

thecloudcode commented 1 month ago

Description: This pull request addresses the issue of missing else-if functionality in ArcaneScript's grammar and its IfElse class implementation. The changes made enhance the script's conditional control structures to support nested conditions with else-if, improving the script's flexibility and aligning it with common programming syntax.

Closes #6

Changes Made:

  1. Grammar Update: Modified ArcaneGrammar7.grm to add support for else-if conditions.

    • Added new grammar rules for else-if and else-if-else scenarios.
    • New rules:
      | if '(' <Expression> ')' start <Statements> end else if '(' <Expression> ')' start <Statements> end
      | if '(' <Expression> ')' start <Statements> end else if '(' <Expression> ')' start <Statements> end else start <Statements> end
  2. IfElse.java Update:

    • Updated the @ProcessRule annotation in IfElse.java to include new else-if grammar rules.
    • Added new fields elseIfCondition and elseIfStatements to handle else-if conditions.
    • Enhanced the execute() method to evaluate else-if conditions.
    • Revised the constructor to handle reduction sizes (7, 11, 15, or 19) to correctly parse and process if, if-else, if-else-if, and if-else-if-else structures.

Code Changes Summary:

@ProcessRule(rule={
    "<Statement> ::= if ( <Expression> ) start <Statements> end",
    "<Statement> ::= if ( <Expression> ) start <Statements> end else start <Statements> end",
    "<Statement> ::= if ( <Expression> ) start <Statements> end else if ( <Expression> ) start <Statements> end",
    "<Statement> ::= if ( <Expression> ) start <Statements> end else if ( <Expression> ) start <Statements> end else start <Statements> end"
})

Testing Instructions:

  1. Update ArcaneGrammar7.grm with the new rules.
  2. Regenerate the parser if required.
  3. Replace the IfElse.java file with the updated version provided in this PR.
  4. Build the project.
  5. Run the following code to validate the changes:

    a = true;
    if(a==true){
       a = false;
    } else if(a==false){
       a = true;
    }

    Expected Output:

    Execution complete!

    image

Checklist:


amritrout commented 1 month ago

image image

After running some tests, it seems that, aside from simple print statements, nothing else is working. This issue has been occurring since your last PR, so I’ll be reverting it until the problems are fixed.