-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
WalkthroughThe update introduces significant additions and changes to the Vein syntax handling, particularly focusing on adding support for Changes
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
Poem
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this 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
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 basicfor
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 ofIAdvancedPositionAware
inLocalVariableDeclaration
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 nullableElseStatement
increases flexibility and aligns with modern C# practices. Ensure that all usages ofElseStatement
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 ofForeachStatementSyntax
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
: TheForStatementSyntax
class is well-designed to encapsulate the components of afor
loop. Using optional types for loop components enhances flexibility and robustness.runtime/ishtar.generator/generators/logic.cs (1)
70-71
: The integration ofForStatementSyntax
handling in theEmitStatement
method correctly extends the IL generator's functionality to supportfor
loops.test/vc_test/Features/GenericDocumentFeature.cs (1)
18-20
: Consider adding more comprehensive assertions in thefor
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 newfor_statement
parser looks well-implemented. However, ensure that all optional components (loopVariable, exp, loopCounter) are handled correctly in all scenarios.
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); | ||
} |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
public test1(): void { | ||
let b = "asdads" + "gfdfg"; |
There was a problem hiding this comment.
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.
public static Fib(n: i32): i32 | ||
{ | ||
if (n < 2) | ||
{ | ||
return n; | ||
} | ||
auto a = Fib(n - 1); | ||
auto b = Fib(n - 2) | ||
return a + b; |
There was a problem hiding this comment.
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.
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); |
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
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.
// 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.
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); | ||
} |
There was a problem hiding this comment.
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.
Summary by CodeRabbit
New Features
for
loop statements.ForStatementSyntax
for representingfor
loops in the syntax tree.Enhancements
IfStatementSyntax
to support nullableElseStatement
.LocalVariableDeclaration
to implementIAdvancedPositionAware
.Bug Fixes
Tests
ForFeatureTest
to validate the parsing offor
statement syntax.GenericDocumentFeature
tests for various struct definitions and methods, including Fibonacci calculation.