-
Notifications
You must be signed in to change notification settings - Fork 202
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,6 +170,7 @@ noParamAnonEventHandler : functionBody; | |
expr : primitive # PrimitiveExpr | ||
| LPAREN unnamedTupleBody RPAREN # UnnamedTupleExpr | ||
| LPAREN namedTupleBody RPAREN # NamedTupleExpr | ||
| LSEQ seqElems RSEQ # SequenceLiteralExpr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| LPAREN expr RPAREN # ParenExpr | ||
| expr DOT field=iden # NamedTupleAccessExpr | ||
| expr DOT field=int # TupleAccessExpr | ||
|
@@ -212,6 +213,10 @@ floatLiteral : pre=IntLiteral? DOT post=IntLiteral # DecimalFloat | |
| FLOAT LPAREN base=IntLiteral COMMA exp=IntLiteral RPAREN # ExpFloat | ||
; | ||
|
||
seqElems : | ||
| elems+=primitive (COMMA elems+=primitive)* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
; | ||
|
||
unnamedTupleBody : fields+=rvalue COMMA | ||
| fields+=rvalue (COMMA fields+=rvalue)+ | ||
; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Antlr4.Runtime; | ||
using Plang.Compiler.TypeChecker.Types; | ||
|
||
namespace Plang.Compiler.TypeChecker.AST.Expressions | ||
{ | ||
public class SequenceLiteralExpr: IPExpr | ||
{ | ||
public SequenceLiteralExpr(ParserRuleContext sourceLocation, IReadOnlyList<IPExpr> sequenceElements) | ||
{ | ||
SourceLocation = sourceLocation; | ||
SequenceElements = sequenceElements; | ||
|
||
if (sequenceElements.Count > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
Type = new SequenceType(sequenceElements[0].Type); | ||
} | ||
else { | ||
Type = new SequenceType(PrimitiveType.Any); | ||
} | ||
} | ||
|
||
public IReadOnlyList<IPExpr> SequenceElements { get; } | ||
|
||
public ParserRuleContext SourceLocation { get; } | ||
|
||
public PLanguageType Type { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,23 @@ public override IPExpr VisitTupleAccessExpr(PParser.TupleAccessExprContext conte | |
return new TupleAccessExpr(context, subExpr, fieldNo, tuple.Types[fieldNo]); | ||
} | ||
|
||
public override IPExpr VisitSequenceLiteralExpr(PParser.SequenceLiteralExprContext context) { | ||
var elems = context.seqElems()._elems.Select(Visit).ToArray(); | ||
|
||
// check whether all elements have the same type | ||
if (elems.Count() > 0) { | ||
var firstElementType = elems[0].Type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
for (int i = 1; i < elems.Count(); i++) { | ||
var currElementType = elems[i].Type; | ||
if (!currElementType.Equals(firstElementType)) { | ||
throw handler.TypeMismatch(context.seqElems()._elems[i], currElementType, firstElementType); | ||
} | ||
} | ||
} | ||
|
||
return new SequenceLiteralExpr(context, elems); | ||
} | ||
|
||
public override IPExpr VisitSeqAccessExpr(PParser.SeqAccessExprContext context) | ||
{ | ||
var seqOrMap = Visit(context.seq); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
machine Main { | ||
var s1: seq[int]; | ||
var s2: seq[any]; | ||
var s3: seq[bool]; | ||
var a: int; | ||
|
||
start state S | ||
{ | ||
entry | ||
{ | ||
s1 = {| 1 , 2 , 3 |}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test for a negative int; I suspect it will fail. |
||
assert (s1[0] == 1); | ||
assert (s1[1] == 2); | ||
assert (s1[2] == 3); | ||
|
||
s2 = {| true, false, true |}; | ||
assert (s2[0] as bool); | ||
assert (!(s2[1] as bool)); | ||
assert (s2[2] as bool); | ||
|
||
s1 = {| a, a, a |}; | ||
assert (s1[0] == 0); | ||
assert (s1[1] == 0); | ||
assert (s1[2] == 0); | ||
|
||
s3 = {||} as seq[bool]; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
machine Main { | ||
var s1 : seq[bool]; | ||
|
||
start state S | ||
{ | ||
entry | ||
{ | ||
s1 = {| true, 1, 2 |}; //error: "got type int, expected bool" | ||
raise halt; | ||
} | ||
} | ||
} |
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 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.
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.
Not sure if this needs to be addressed. The generated code needs to a pass a List to the constructor for PSeq.