Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/PPublic

Add sequence literals#901

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
anmolsahoo25 wants to merge2 commits intop-org:master
base:master
Choose a base branch
Loading
fromanmolsahoo25:feature-seq-literals

Conversation

@anmolsahoo25
Copy link

@anmolsahoo25anmolsahoo25 commentedAug 10, 2025
edited
Loading

Wrote up a small PR to add sequence literals with the syntax{| elems+ |}, which enables assigning sequence literals to seq[T] variables. Added tests and C# code generation. This is now valid syntax:

var s1 : seq[int]...s1 = {| 1,2,3 |}

Not sure if this feature is on the roadmap for the P team right now, feel free to close if required. Was just having fun exploring the P compiler.

Are there any tests for the Java backend? I haven't added the code generation for the Java backend, but could do that and add a few tests, if they exist.

This commit adds sequence literals with the syntax `{| elems+ |}`,which enables assigning sequence literals to seq[T] variables.
Copy link
Contributor

CopilotAI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for sequence literals to the P language with the new syntax{| elems+ |}, enabling developers to assign sequence literals directly toseq[T] variables.

  • Add parser support for sequence literal syntax using{| and|} delimiters
  • Implement type checking and AST representation for sequence literal expressions
  • Include C# code generation for the new sequence literal feature

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
FileDescription
SequenceLiteralExpr.pTest file demonstrating sequence literal usage and validation
PParser.g4Grammar rules for sequence literal expressions and elements
PLexer.g4Lexical tokens for sequence literal delimiters
SequenceLiteralExpr.csAST node implementation for sequence literal expressions
ExprVisitor.csType checker visitor method for sequence literals
DeclarationStubVisitor.csStub visitor method for sequence literals
IRTransformer.csIR transformation logic for sequence literals
IrToPseudoP.csDebug output formatting for sequence literals
CSharpCodeGenerator.csC# code generation for sequence literal expressions

{
SourceLocation=sourceLocation;
SequenceElements=sequenceElements;
Type=newSequenceType(sequenceElements[0].Type);

Choose a reason for hiding this comment

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

This code will throw an IndexOutOfRangeException if sequenceElements is empty. The grammar allows empty sequences, but this constructor assumes at least one element exists.

Suggested change
Type=newSequenceType(sequenceElements[0].Type);
if(sequenceElements.Count>0)
{
Type=newSequenceType(sequenceElements[0].Type);
}
else
{
// Assign a default type for empty sequences. Adjust as needed for your type system.
Type=newSequenceType(PrimitiveType.Any);
}

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

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

Good catch. Addressed as shown.

|FLOATLPAREN base=IntLiteralCOMMA exp=IntLiteralRPAREN # ExpFloat
;

seqElems : elems+=primitive

Choose a reason for hiding this comment

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

The grammar rule only allows 'primitive' expressions in sequence literals, but the test file and implementation suggest it should support any expression (like variables). This should be 'expr' instead of 'primitive' to match the test usage of variable 'a'.

Copilot uses AI. Check for mistakes.
Copy link
Author

@anmolsahoo25anmolsahoo25Aug 11, 2025
edited
Loading

Choose a reason for hiding this comment

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

current code supports primitives and idents. Do we want to support the full range of expressions in the literals?

break;

caseSequenceLiteralExprsequenceLiteralExpr:
context.Write(output,$"new{GetCSharpType(sequenceLiteralExpr.Type)}(new List<IPValue>");

Choose a reason for hiding this comment

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

The generated C# code creates a List but should use the specific element type. For a seq[int], this should generate List or the appropriate typed collection, not List.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

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

Not sure if this needs to be addressed. The generated code needs to a pass a List to the constructor for PSeq.

Copy link
Contributor

@lewisbrulewisbru left a comment

Choose a reason for hiding this comment

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

While i like the general idea behind this PR, a couple of things to note:

  1. It conflicts both syntactically and terminologically with how sequences are defined in parameterized tests in the P3.0 branch. They don't need to be the same but they should be aligned or made much more distinct. For instance, parameterized test "sequences" can be of heterogeneous types; should these sequence literals support this?
  2. For P2.x, support should be added to the Java code generator.
  3. This will need additional work to support the PVerifier and PEx backends in P3.0.

expr : primitive # PrimitiveExpr
|LPAREN unnamedTupleBodyRPAREN # UnnamedTupleExpr
|LPAREN namedTupleBodyRPAREN # NamedTupleExpr
|LSEQ seqElemsRSEQ # SequenceLiteralExpr
Copy link
Contributor

Choose a reason for hiding this comment

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

This syntax conflicts with the other definition of sequence literals in the P3.0 branch (from the parameterized test work:https://github.com/p-org/P/blame/2febcebeb4bf16f333362280f2db01dd621a333f/Src/PCompiler/CompilerCore/Parser/PParser.g4#L281

We should either come up with a common representation and handling of sequences in code and parameterized tests.

;

seqElems :
| elems+=primitive (COMMA elems+=primitive)*
Copy link
Contributor

Choose a reason for hiding this comment

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

A trickiness with this use of primitive is that it doesn't handle negative numbers: negative numbers are not primitives but are unary operator expressions.

{
entry
{
s1= {|1 ,2 ,3 |};
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test for a negative int; I suspect it will fail.

SourceLocation=sourceLocation;
SequenceElements=sequenceElements;

if(sequenceElements.Count>0){
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we determine a more general type (like seq or seq) if the sequence elements are heterogeneous?


// check whether all elements have the same type
if(elems.Count()>0){
varfirstElementType=elems[0].Type;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we handle a more general type (like seq or seq) if the sequence elements are heterogeneous?

@anmolsahoo25
Copy link
Author

Thanks for the feedback! :) Will take a pass over the comments today and tomorrow and come up with some revisions.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@lewisbrulewisbrulewisbru left review comments

Copilot code reviewCopilotCopilot left review comments

@aman-goelaman-goelAwaiting requested review from aman-goel

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@anmolsahoo25@lewisbru

[8]ページ先頭

©2009-2025 Movatter.jp