Skip to content

Commit

Permalink
Add operators to EC# parser/printer: <=>, |>, ?|> (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertie committed Jul 24, 2020
1 parent ad37baa commit 5203967
Show file tree
Hide file tree
Showing 10 changed files with 964 additions and 893 deletions.
1 change: 1 addition & 0 deletions Core/Loyc.Syntax/CodeSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public partial class CodeSymbols
public static readonly Symbol LT = GSymbol.Get("'<"); //!< "<" Less-than operator
public static readonly Symbol LE = GSymbol.Get("'<="); //!< "<=" Less-than-or-equal-to operator
public static readonly Symbol Matches = GSymbol.Get("'=~"); //!< "=~" Pattern match test operator
public static readonly Symbol Compare = GSymbol.Get("'<=>"); //!< "<=>" Three-way comparison a.k.a. shaceship operator
public static readonly Symbol Shr = GSymbol.Get("'>>"); //!< ">>" Right-shift operator
public static readonly Symbol Shl = GSymbol.Get("'<<"); //!< "<<" Left-shift operator
public static readonly Symbol Not = GSymbol.Get("'!"); //!< "!" Logical 'not' operator
Expand Down
8 changes: 6 additions & 2 deletions Main/Ecs/EcsCodeSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public class EcsCodeSymbols : Loyc.Syntax.CodeSymbols
public static new readonly Symbol Async = GSymbol.Get("#async"); //!< [#async] Task Foo(); <=> async Task Foo();
// async is a normal contextual attribute so it needs no special parser support.
public static new readonly Symbol Await = GSymbol.Get("await"); //!< await(x); <=> await x; (TENTATIVE: should this be changed to #await?)
public static new readonly Symbol InitializerAssignment = GSymbol.Get("'[]="); //!< @`[]=`(0, 1, x) <=> [0, 1]=x
// (TENTATIVE, and only supported in 'new' initializer blocks)
public static new readonly Symbol InitializerAssignment = GSymbol.Get("'[]="); //!< @`'[]=`(0, 1, x) <=> [0, 1]=x
// (TENTATIVE, and only supported in 'new' initializer blocks)

// Proposed: https://github.com/dotnet/csharplang/issues/74
public static readonly Symbol ForwardPipeArrow = GSymbol.Get("'|>"); //!< @`'|>`(a, b) <=> a |> b
public static readonly Symbol NullForwardPipeArrow = GSymbol.Get("'?|>"); //!< @`'?|>`(a, b) <=> a ?|> b

public static new readonly Symbol TriviaCsRawText = GSymbol.Get("%C#RawText"); //!< "%C#RawText" - `%C#RawText`("stuff") - Raw text that is only printed by the C# printer (not printers for other languages)
public static new readonly Symbol TriviaCsPPRawText = GSymbol.Get("%C#PPRawText"); //!< "%C#PPRawText" - `%C#PPRawText`("#stuff") - Raw text that is guaranteed to be preceded by a newline and is only printed by the C# printer
Expand Down
4 changes: 3 additions & 1 deletion Main/Ecs/EcsPrecedence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ public static class EcsPrecedence
public static readonly Precedence Primary = new Precedence(100); // x.y x::y x=:y x->y f(x) x(->y) a[x] x++ x-- typeof() checked() unchecked() new
public static readonly Precedence NullDot = new Precedence(99); // ?.
public static readonly Precedence Prefix = new Precedence(91, 90, 90, 91); // + - ! ~ ++x --x (T)x
public static readonly Precedence Power = new Precedence(86, 85);// **
public static readonly Precedence Power = new Precedence(85); // ** (tentatively left-associative)
public static readonly Precedence Range = new Precedence(80); // ..
public static readonly Precedence Forward = new Precedence(78); // ==> x
public static readonly Precedence Switch = new Precedence(75); // expr switch { ... }
public static readonly Precedence Multiply = new Precedence(70); // *, /, %
public static readonly Precedence Add = new Precedence(60); // +, -, ~
public static readonly Precedence Shift = new Precedence(56, 56, 56, 70); // >> << (for printing purposes, immiscible with * / + -)
public static readonly Precedence Backtick = new Precedence(46, 72, 45, 73); // `custom operator` (immiscible with * / + - << >> ..)
public static readonly Precedence Compare3Way= new Precedence(42); // <=>
public static readonly Precedence Compare = new Precedence(40); // < > <= >=
public static readonly Precedence IsAsUsing = new Precedence(40, 99, 40, 40); // is as using
public static readonly new Precedence Equals = new Precedence(38); // == != in
Expand All @@ -72,6 +73,7 @@ public static class EcsPrecedence
public static readonly Precedence And = new Precedence(22); // &&
public static readonly Precedence Or = new Precedence(20); // || ^^
public static readonly Precedence OrIfNull = new Precedence(16); // ??
public static readonly Precedence ForwardPipeArrow = new Precedence(13); // |> ?|>
public static readonly Precedence IfElse = new Precedence(11, 10, 10, 11); // x ? y : z
public static readonly Precedence Assign = new Precedence(26, 0, 0, 1); // = *= /= %= += -= <<= >>= &= ^= |= ??= ~=
public static readonly Precedence Lambda = new Precedence(85, -1, -2, -1); // =>
Expand Down
9 changes: 6 additions & 3 deletions Main/Ecs/Parser/EcsLexerGrammar.les
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,22 @@ namespace Loyc.Ecs.Parser
@[private] token At @{ '@' { _type = TT.At; _value = S.AtSign; } };

@[k(3), private] token Operator @{
( "..." { _type = TT.DotDot; _value = S.DotDotDot; }
/ "..<" { _type = TT.DotDot; _value = S.DotDot; }
( "..." { _type = TT.DotDot; _value = S.DotDotDot; }
/ "..<" { _type = TT.DotDot; _value = S.DotDot; }
/ ".." { _type = TT.DotDot; _value = S.DotDot; }
/ "." { _type = TT.Dot; _value = S.Dot; }
| ">>=" { _type = TT.CompoundSet; _value = S.ShrAssign; }
/ ">=" { _type = TT.LEGE; _value = S.GE; }
/ ">" { _type = TT.GT; _value = S.GT; }
/ "<=>" { _type = TT.Compare; _value = S.Compare; }
/ "<<=" { _type = TT.CompoundSet; _value = S.ShlAssign; }
/ "<=" { _type = TT.LEGE; _value = S.LE; }
/ "<" { _type = TT.LT; _value = S.LT; }
| "&&" { _type = TT.And; _value = S.And; }
/ "&=" { _type = TT.CompoundSet; _value = S.AndBitsAssign; }
/ "&" { _type = TT.AndBits; _value = S.AndBits; }
| "||" { _type = TT.OrXor; _value = S.Or; }
| "|>" { _type = TT.PipeArrow; _value = S.ForwardPipeArrow; }
/ "||" { _type = TT.OrXor; _value = S.Or; }
/ "|=" { _type = TT.CompoundSet; _value = S.OrBitsAssign; }
/ "|" { _type = TT.OrBits; _value = S.OrBits; }
| "^^" { _type = TT.OrXor; _value = S.Xor; }
Expand Down Expand Up @@ -236,6 +238,7 @@ namespace Loyc.Ecs.Parser
| "??=" { _type = TT.CompoundSet; _value = S.NullCoalesceAssign; }
/ "??" { _type = TT.NullCoalesce; _value = S.NullCoalesce; }
/ "?." { _type = TT.NullDot; _value = S.NullDot; }
/ "?|>" { _type = TT.PipeArrow; _value = S.NullForwardPipeArrow; }
/ "?" { _type = TT.QuestionMark; _value = S.QuestionMark; }
| "$" { _type = TT.Substitute; _value = S.Substitute; }
| "\\" { _type = TT.Backslash; _value = S.Backslash; }
Expand Down
Loading

0 comments on commit 5203967

Please sign in to comment.