Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for loop support and other improvements #240

Merged
merged 5 commits into from
Jun 15, 2024
Merged

Conversation

0xF6
Copy link
Member

@0xF6 0xF6 commented Jun 15, 2024

Summary by CodeRabbit

  • New Features

    • Introduced support for parsing and handling for loop statements.
    • Added a new class ForStatementSyntax for representing for loops in the syntax tree.
  • Enhancements

    • Improved IfStatementSyntax to support nullable ElseStatement.
    • Enhanced LocalVariableDeclaration to implement IAdvancedPositionAware.
  • Bug Fixes

    • Corrected handling of post-increment assignments in expressions.
  • Tests

    • Added ForFeatureTest to validate the parsing of for statement syntax.
    • Introduced GenericDocumentFeature tests for various struct definitions and methods, including Fibonacci calculation.

@0xF6 0xF6 added the area-compiler Area of compiler staff label Jun 15, 2024
@0xF6 0xF6 requested a review from code-maid June 15, 2024 02:50
@0xF6 0xF6 self-assigned this Jun 15, 2024
Copy link
Contributor

coderabbitai bot commented Jun 15, 2024

Walkthrough

The update introduces significant additions and changes to the Vein syntax handling, particularly focusing on adding support for for loops, enhancing variable declarations, and improving position awareness in syntax trees. It also includes IL generator updates to handle the new for loop syntax and additional tests to verify these features, with overall improvements to syntax parsing and error handling.

Changes

Files Summary
lib/ast/syntax/Statement.cs Modified declarationStatement, introduced ValidateSemicolon, added for_statement, and updated embedded_statement
lib/.../ast/ForStatementSyntax.cs Introduced ForStatementSyntax class with properties for loop variables and statement body
lib/.../ast/ForeachStatementSyntax.cs Changed constructor signature for handling variables and statements
lib/.../ast/IfStatementSyntax.cs Made ElseStatement property nullable in IfStatementSyntax
lib/.../ast/LocalVariableDeclaration.cs Implement IAdvancedPositionAware instead of IPositionAware for LocalVariableDeclaration
lib/ast/vein.ast.csproj Enabled implicit usings and nullable reference types
runtime/.../generators/cycles.cs Added EmitForStatement method to ILGenerator for compiling for loops
runtime/.../generators/logic.cs Added handling of ForStatementSyntax in the EmitStatement method of ILGenerator
runtime/.../generators/operators.cs Added post-increment assignment (ExpressionType.PostIncrementAssign) logic in EmitUnary
test/vc_test/Features/ForFeatureTest.cs Introduced tests for parsing for statements in ForFeatureTest class
test/vc_test/Features/GenericDocumentFeature.cs Added multiple syntax tests in GenericDocumentFeature class for various struct definitions and methods

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Parser
    participant ILGenerator
    participant SyntaxTree
    User->>Parser: Parse code with `for` loop
    Parser->>SyntaxTree: Create ForStatementSyntax
    SyntaxTree-->>Parser: Return ForStatementSyntax Node
    Parser->>ILGenerator: Emit ForStatementSyntax
    ILGenerator->>ILGenerator: Generate IL for loop variable
    ILGenerator->>ILGenerator: Generate IL for loop condition
    ILGenerator->>ILGenerator: Generate IL for loop iteration
    ILGenerator->>User: Return compiled code
Loading

Poem

Amidst the code, a loop does glide,
A for loop's journey, side by side,
Semicolons check, no errors to hide,
Expressions parsed, in perfect stride.
Variables declare, they now confide,
Syntax trees bloom, with loops inside. 🌸🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fde1ac3 and 2518083.

Files selected for processing (11)
  • lib/ast/syntax/Statement.cs (3 hunks)
  • lib/ast/syntax/ast/ForStatementSyntax.cs (1 hunks)
  • lib/ast/syntax/ast/ForeachStatementSyntax.cs (1 hunks)
  • lib/ast/syntax/ast/IfStatementSyntax.cs (2 hunks)
  • lib/ast/syntax/ast/LocalVariableDeclaration.cs (1 hunks)
  • lib/ast/vein.ast.csproj (1 hunks)
  • runtime/ishtar.generator/generators/cycles.cs (1 hunks)
  • runtime/ishtar.generator/generators/logic.cs (1 hunks)
  • runtime/ishtar.generator/generators/operators.cs (1 hunks)
  • test/vc_test/Features/ForFeatureTest.cs (1 hunks)
  • test/vc_test/Features/GenericDocumentFeature.cs (1 hunks)
Additional context used
GitHub Check: Codacy Static Code Analysis
runtime/ishtar.generator/generators/operators.cs

[warning] 243-243: runtime/ishtar.generator/generators/operators.cs#L243
Remove this commented out code.

Additional comments not posted (10)
test/vc_test/Features/ForFeatureTest.cs (1)

3-15: This is a good start for testing basic for loop functionality. Consider adding more tests to cover edge cases such as nested loops, and loops with different conditionals and increments.

lib/ast/syntax/ast/LocalVariableDeclaration.cs (1)

5-5: The implementation of IAdvancedPositionAware in LocalVariableDeclaration is appropriate. Ensure that the rest of the syntax handling system is updated to utilize these advanced position capabilities effectively.

lib/ast/syntax/ast/IfStatementSyntax.cs (1)

Line range hint 6-17: The addition of nullable ElseStatement increases flexibility and aligns with modern C# practices. Ensure that all usages of ElseStatement properly handle the possibility of it being null.

lib/ast/syntax/ast/ForeachStatementSyntax.cs (1)

6-24: The direct initialization of properties in the constructor of ForeachStatementSyntax enhances readability and consistency. Verify that these properties are intended to be immutable, as they are set only once and do not change.

lib/ast/vein.ast.csproj (1)

7-8: Enabling implicit usings and nullable reference types are beneficial for reducing boilerplate and improving type safety.

lib/ast/syntax/ast/ForStatementSyntax.cs (1)

5-28: The ForStatementSyntax class is well-designed to encapsulate the components of a for loop. Using optional types for loop components enhances flexibility and robustness.

runtime/ishtar.generator/generators/logic.cs (1)

70-71: The integration of ForStatementSyntax handling in the EmitStatement method correctly extends the IL generator's functionality to support for loops.

test/vc_test/Features/GenericDocumentFeature.cs (1)

18-20: Consider adding more comprehensive assertions in the for loop test to ensure loop behavior is as expected.

lib/ast/syntax/Statement.cs (2)

34-36: The modification to allow optional semicolons is a significant change. Ensure that this flexibility does not introduce parsing ambiguities in other parts of the language.


56-66: The new for_statement parser looks well-implemented. However, ensure that all optional components (loopVariable, exp, loopCounter) are handled correctly in all scenarios.

Comment on lines +32 to +58
public static void EmitForStatement(this ILGenerator gen, ForStatementSyntax @for)
{
var ctx = gen.ConsumeFromMetadata<GeneratorContext>("context");
if (@for.LoopVariable is not null)
gen.EmitLocalVariable(@for.LoopVariable);
var start = gen.DefineLabel("for-start");
gen.UseLabel(start);
gen.EmitStatement(@for.Statement);

if (@for.LoopCounter is not null)
gen.EmitExpression(@for.LoopCounter);
if (@for.LoopContact is not null)
{
var expType = @for.LoopContact.DetermineType(ctx);

if (expType.TypeCode is not VeinTypeCode.TYPE_BOOLEAN)
{
ctx.LogError($"Cannot implicitly convert type '{expType}' to 'Boolean'", @for.LoopContact);
return; // TODO implicit boolean
}

gen.EmitExpression(@for.LoopContact);
gen.Emit(OpCodes.JMP_T, start);
}
else
gen.Emit(OpCodes.JMP, start);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EmitForStatement method effectively handles the components of a for loop. Consider adding support for implicit boolean conversion to handle non-boolean types in the loop condition more gracefully.

}
}
public test1(): void {
fail null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line attempts to fail with a null value, which might not be valid syntax or logic. Please confirm if this is intended or an error.

Comment on lines +26 to +27
public test1(): void {
let b = "asdads" + "gfdfg";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate method name test1 may cause compilation issues or unexpected behavior. Consider renaming one of the methods.

Comment on lines +34 to +42
public static Fib(n: i32): i32
{
if (n < 2)
{
return n;
}
auto a = Fib(n - 1);
auto b = Fib(n - 2)
return a + b;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recursive Fibonacci function could be optimized for better performance, possibly using memoization to avoid redundant calculations.

+ private static Dictionary<int, int> memo = new Dictionary<int, int>();
- auto a = Fib(n - 1);
- auto b = Fib(n - 2)
+ auto a = memo.ContainsKey(n - 1) ? memo[n - 1] : Fib(n - 1);
+ auto b = memo.ContainsKey(n - 2) ? memo[n - 2] : Fib(n - 2);

Committable suggestion was skipped due to low confidence.

Comment on lines +38 to +43
protected internal virtual T ValidateSemicolon<T>(IOption<char> semi, T other) where T : BaseSyntax
{
if (semi.IsDefined)
return other;
var poz = other.Transform.pos;
throw new VeinParseException("Semicolon required", poz, other);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception thrown for missing semicolons might benefit from a more detailed error message, including suggestions or examples.

- throw new VeinParseException("Semicolon required", poz, other);
+ throw new VeinParseException("Semicolon required after declarations. For example, 'let x = 5;'", poz, other);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
protected internal virtual T ValidateSemicolon<T>(IOption<char> semi, T other) where T : BaseSyntax
{
if (semi.IsDefined)
return other;
var poz = other.Transform.pos;
throw new VeinParseException("Semicolon required", poz, other);
throw new VeinParseException("Semicolon required after declarations. For example, 'let x = 5;'", poz, other);

@@ -238,6 +238,14 @@
gen.Emit(OpCodes.LDC_I4_0);
gen.Emit(OpCodes.EQL_NQ);
}
else if (node.OperatorType is ExpressionType.PostIncrementAssign)
{
// todo work only in for cycle, in other cases, it works like ++i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the commented code or replace it with a more descriptive comment explaining why the current implementation is specific to for loops.

- // todo work only in for cycle, in other cases, it works like ++i
+ // Currently, PostIncrementAssign is optimized for use within for loops. In other contexts, it behaves like pre-increment (++i).
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// todo work only in for cycle, in other cases, it works like ++i
// Currently, PostIncrementAssign is optimized for use within for loops. In other contexts, it behaves like pre-increment (++i).
Tools
GitHub Check: Codacy Static Code Analysis

[warning] 243-243: runtime/ishtar.generator/generators/operators.cs#L243
Remove this commented out code.

Comment on lines +241 to +248
else if (node.OperatorType is ExpressionType.PostIncrementAssign)
{
// todo work only in for cycle, in other cases, it works like ++i
var addOne =
new BinaryExpressionSyntax(node.Operand, new Int32LiteralExpressionSyntax(1), ExpressionType.Add);
var operand_assign = new BinaryExpressionSyntax(node.Operand, addOne);
gen.EmitAssignExpression(operand_assign);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation for PostIncrementAssign is specific to for loops. Consider generalizing it or ensuring it behaves correctly in all contexts.

- // todo work only in for cycle, in other cases, it works like ++i
+ // Ensure correct behavior for PostIncrementAssign in all contexts.

Committable suggestion was skipped due to low confidence.

Tools
GitHub Check: Codacy Static Code Analysis

[warning] 243-243: runtime/ishtar.generator/generators/operators.cs#L243
Remove this commented out code.

@0xF6 0xF6 merged commit cb579d0 into master Jun 15, 2024
6 of 8 checks passed
@0xF6 0xF6 deleted the improvements/compiler-features branch June 15, 2024 02:59
@vein-lang vein-lang locked and limited conversation to collaborators Jun 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-compiler Area of compiler staff
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant