Skip to content

Commit

Permalink
Improving the Plato grammar, tools, and error recovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdiggins committed Mar 11, 2024
1 parent b0b8b46 commit c9fba58
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 110 deletions.
17 changes: 3 additions & 14 deletions Plato.AST/AstNodeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,11 @@ public static AstMethodDeclaration ToAst(this CstMethodDeclaration md)
md,
ToAst(md.Identifier.Node),
ToAst(md.TypeAnnotation.Node),
md.FunctionParameterList.Node.FunctionParameter.Nodes.Select(ToAst).ToList(),
md.FunctionParameterList.Node?.FunctionParameter.Nodes.Select(ToAst).ToList()
?? Enumerable.Empty<AstParameterDeclaration>(),
ToAst(md.FunctionBody.Node));
}

public static AstMemberDeclaration ToAst(this CstMemberDeclaration memberDeclaration)
{
if (memberDeclaration.MethodDeclaration.Present)
return ToAst(memberDeclaration.MethodDeclaration.Node);
if (memberDeclaration.FieldDeclaration.Present)
return ToAst(memberDeclaration.FieldDeclaration.Node);

throw new Exception($"Unrecognized member type {memberDeclaration.Text}");
}

public static AstParameterDeclaration ToAst(this CstLambdaParameter lp, int index)
{
return new AstParameterDeclaration(lp,
Expand Down Expand Up @@ -593,8 +584,6 @@ public static AstNode ToAst(this CstNode node)
throw new NotImplementedException();
case CstMemberAccess cstMemberAccess:
throw new NotImplementedException();
case CstMemberDeclaration cstMemberDeclaration:
throw new NotImplementedException();
case CstMethodDeclaration cstMethodDeclaration:
throw new NotImplementedException();
case CstOperator cstOperator:
Expand Down Expand Up @@ -799,7 +788,7 @@ public static string ToAst(this CstQualifiedIdentifier cstQualifiedIdentifier)
=> cstQualifiedIdentifier.Text;

public static string ToAst(this CstIdentifier cstIdentifier)
=> cstIdentifier.Text;
=> cstIdentifier?.Text ?? "";

public static IEnumerable<AstVarDef> CreateVarDefs(this CstVarDecl cstVarDecl)
{
Expand Down
7 changes: 4 additions & 3 deletions PlatoCompiler/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public void WriteReifiedTypes()
public IType GetExpressionType(Expression expr)
=> ExpressionTypes.TryGetValue(expr, out var r) ? r : null;

// TODO: I have to make these more like Parser Errors
public void LogResolutionErrors(IEnumerable<SymbolFactory.ResolutionError> resolutionErrors)
{
foreach (var error in resolutionErrors)
Expand All @@ -265,9 +266,9 @@ public void LogResolutionErrors(IEnumerable<SymbolFactory.ResolutionError> resol
Log($"Symbol resolution error: {error.Message}");
if (pos != null)
{
Log(pos.Node.Range.Begin.Input.File);
Log(pos.Node.Range.Begin.CurrentLine);
Log(pos.Node.Range.Begin.Indicator);
Log(pos.Node.Range.End.Input.File);
Log(pos.Node.Range.Begin?.CurrentLine);
Log(pos.Node.Range.Begin?.Indicator);
Log(pos.Node.Range.End.CurrentLine);
Log(pos.Node.Range.End.Indicator);
}
Expand Down
2 changes: 1 addition & 1 deletion PlatoStandardLibrary/std.angles.library.plato
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library Angles
{
Radians(x : Number): Angle
Radians(x: Number): Angle
=> x;

Degrees(x: Number): Angle
Expand Down
7 changes: 1 addition & 6 deletions PlatoStandardLibrary/std.easing.library.plato
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ library Easings
// https://github.com/ai/easings.net/blob/master/src/easings/easingsFunctions.ts
// This is an extremely good example of why Plato matters as a language.
// When we look at the source code of the two other implementations.

BlendEaseFunc(p: Numerical, easeIn: Function1, easeOut: Function1): Number
=> p < 0.5
? 0.5 * easeIn(p * 2)
: 0.5 * easeOut(p * 2 - 1) + 0.5;

InvertEaseFunc(p: Numerical, easeIn: Function1): Numerical
=> 1 - easeIn(1 - p);
Expand Down Expand Up @@ -137,5 +132,5 @@ library Easings
? a :
(x <= a)
? b * (a.Pow2 - x.Pow2) / a).Sqrt.FromOne
: b + (b.FromOne / a.FromOne * (a.FromOne.Pow2 - x.MinusOne.Pow2).Sqrt;
: b + (b.FromOne / a.FromOne * (a.FromOne.Pow2 - x.MinusOne.Pow2).Sqrt);
}
18 changes: 7 additions & 11 deletions PlatoWinFormsEditor/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public void OnLogMessage(string logMsg)
LogBuilder.AppendLine(logMsg);
}

public void ApplyStyle(string kind, ParserRange range)
{

}

public void ApplyStyle(string kind, int start, int length)
{
var style = Styling.Styles[kind];
Expand All @@ -115,24 +120,15 @@ public void ApplyStyles()
{
foreach (var node in Tokenizer.ParserNodes)
{
if (Styling.Styles.ContainsKey(node.Name))
if (node.IsEnd && Styling.Styles.ContainsKey(node.Name))
{
ApplyStyle(node.Name, node.Start, node.Length);
}
}

foreach (var error in Parser.ParserErrors)
{
var pos1 = error.ParentState.Position;
var pos2 = error.State.Position;
var cnt = pos2 - pos1;
if (cnt > 5)
{
cnt = 5;
pos1 = pos2 - cnt;
}

ApplyStyle("ERROR", pos1, cnt);
ApplyStyle("ERROR", error.Range);
}

// TODO: set the properties.
Expand Down
Loading

0 comments on commit c9fba58

Please sign in to comment.