Skip to content

Commit

Permalink
refactor: Convert to new ast
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 22, 2024
1 parent e37fd17 commit c6bb808
Show file tree
Hide file tree
Showing 80 changed files with 419 additions and 824 deletions.
450 changes: 0 additions & 450 deletions NewSource/.idea/.idea.Backlang/.idea/workspace.xml

This file was deleted.

6 changes: 0 additions & 6 deletions NewSource/.idea/.idea.BacklangSdk/.idea/vcs.xml

This file was deleted.

9 changes: 0 additions & 9 deletions NewSource/Socordia.CodeAnalysis/AST/AstNode.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
using MrKWatkins.Ast;
using Socordia.CodeAnalysis.Parsing;

namespace Socordia.CodeAnalysis.AST;

public class AstNode : PropertyNode<AstNode>
{
public AstNode WithRange(Token keywordToken, Token iteratorPrev)
{
throw new NotImplementedException();
}

public AstNode WithRange(Token token)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Socordia.CodeAnalysis.AST.Declarations;

public class ClassDeclaration : Declaration
{
public ClassDeclaration(string name, List<AstNode> inheritances, List<AstNode> members)
{
Properties.Set(nameof(Name), name);
Properties.Set(nameof(Inheritances), inheritances);
Children.Add(members);
}

public string Name => Properties.GetOrThrow<string>(nameof(Name));
public List<AstNode> Inheritances => Properties.GetOrThrow<List<AstNode>>(nameof(Inheritances));
}
13 changes: 13 additions & 0 deletions NewSource/Socordia.CodeAnalysis/AST/Expressions/Call.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Socordia.CodeAnalysis.AST.Expressions;

public class Call : AstNode
{
public Call(AstNode callee, List<AstNode> arguments)
{
Children.Add(callee);
Children.Add(arguments);
}

public AstNode Callee => Children[0];
public IEnumerable<AstNode> Arguments => Children.Skip(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Socordia.CodeAnalysis.AST.Expressions;

public class CollectionExpression : AstNode
{
public CollectionExpression(List<AstNode> elements)
{
Children.Add(elements);
}
}
15 changes: 11 additions & 4 deletions NewSource/Socordia.CodeAnalysis/AST/Expressions/UnaryOperator.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
using Loyc;
namespace Socordia.CodeAnalysis.AST.Expressions;

namespace Socordia.CodeAnalysis.AST.Expressions;
public enum UnaryOperatorKind
{
Prefix,
Suffix
}

public class UnaryOperator : AstNode
{
public UnaryOperator(Symbol op, AstNode operand)
public UnaryOperator(string op, AstNode operand, UnaryOperatorKind kind)
{
Properties.Set(nameof(Op), op);
Properties.Set(nameof(Kind), kind);

Children.Add(operand);
}

public Symbol Op => Properties.GetOrThrow<Symbol>(nameof(Op));
public string Op => Properties.GetOrThrow<string>(nameof(Op));
public UnaryOperatorKind Kind => Properties.GetOrThrow<UnaryOperatorKind>(nameof(Kind));
public AstNode Operand => Children.First;
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Socordia.CodeAnalysis.AST.Statements.Loops;

public class BreakStatement : AstNode
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Socordia.CodeAnalysis.AST.Statements.Loops;

public class ContinueStatement : AstNode
{

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Socordia.CodeAnalysis.AST.Statements;
namespace Socordia.CodeAnalysis.AST.Statements.Loops;

public class DoWhileStatement : AstNode
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Socordia.CodeAnalysis.AST.Statements.Loops;

public class ForStatement : AstNode
{
public ForStatement(AstNode varExpr, AstNode type, AstNode arr, Block body)
{
Children.Add(varExpr);
Children.Add(type);
Children.Add(arr);
Children.Add(body);
}

public AstNode VarExpr => Children[0];
public AstNode Type => Children[1];
public AstNode Arr => Children[2];
public Block Body => (Block) Children[3];
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Socordia.CodeAnalysis.AST.Statements;
namespace Socordia.CodeAnalysis.AST.Statements.Loops;

public class WhileStatement : AstNode
{
Expand Down
11 changes: 11 additions & 0 deletions NewSource/Socordia.CodeAnalysis/AST/TypeAlias.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Socordia.CodeAnalysis.AST;

public class TypeAlias : AstNode
{
public TypeAlias(AstNode expr)
{
Children.Add(expr);
}

public AstNode Expression => Children[0];
}
11 changes: 11 additions & 0 deletions NewSource/Socordia.CodeAnalysis/AST/TypeNames/GenericTypeName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Socordia.CodeAnalysis.AST.TypeNames;

public class GenericTypeName : SimpleTypeName
{
public GenericTypeName(string name, List<TypeName> genericArguments) : base(name)
{
Properties.Set(nameof(GenericArguments), genericArguments);
}

public List<TypeName> GenericArguments => Properties.GetOrThrow<List<TypeName>>(nameof(GenericArguments));
}
7 changes: 7 additions & 0 deletions NewSource/Socordia.CodeAnalysis/AST/TypeNames/PointerKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Socordia.CodeAnalysis.AST.TypeNames;

public enum PointerKind
{
Transient,
Reference
}
37 changes: 37 additions & 0 deletions NewSource/Socordia.CodeAnalysis/AST/TypeNames/PointerTypeName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Text;
using Socordia.CodeAnalysis.Parsing;

namespace Socordia.CodeAnalysis.AST.TypeNames;

public class PointerTypeName : TypeName
{
public PointerTypeName(TypeName type, PointerKind kind)
{
Children.Add(type);
Properties.Set(nameof(Kind), kind);
}

public TypeName Type => (TypeName)Children[0];
public PointerKind Kind => Properties.GetOrThrow<PointerKind>(nameof(Kind));

public override string ToString()
{
var builder = new StringBuilder();

switch (Kind)
{
case PointerKind.Transient:
builder.Append('*');
break;
case PointerKind.Reference:
builder.Append('&');
break;
default:
throw new ArgumentOutOfRangeException();
}

builder.Append(Type);

return builder.ToString();
}
}
13 changes: 13 additions & 0 deletions NewSource/Socordia.CodeAnalysis/AST/TypeNames/SimpleTypeName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Socordia.CodeAnalysis.AST.TypeNames;

public class SimpleTypeName : TypeName
{
public SimpleTypeName(string name)
{
Properties.Set(nameof(Name), name);
}

public string Name => Properties.GetOrThrow<string>(nameof(Name));

public override string ToString() => Name;
}
6 changes: 6 additions & 0 deletions NewSource/Socordia.CodeAnalysis/AST/TypeNames/TypeName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Socordia.CodeAnalysis.AST.TypeNames;

public abstract class TypeName : AstNode
{

}
15 changes: 6 additions & 9 deletions NewSource/Socordia.CodeAnalysis/Parsing/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Loyc;
using Loyc.Syntax;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Expressions;
using Socordia.CodeAnalysis.Core;
using Socordia.CodeAnalysis.Core.Attributes;

Expand Down Expand Up @@ -47,7 +48,7 @@ public static int GetBinaryOperatorPrecedence(TokenType kind)
return BinaryOperators.GetValueOrDefault(kind);
}

public static AstNode Parse(Parser parser, ParsePoints parsePoints = null, int parentPrecedence = 0)
public static AstNode Parse(Parser parser, ParsePointCollection parsePoints = null, int parentPrecedence = 0)
{
AstNode left = null;
var preUnaryOperatorPrecedence = GetPreUnaryOperatorPrecedence(parser.Iterator.Current.Type);
Expand All @@ -60,8 +61,7 @@ public static AstNode Parse(Parser parser, ParsePoints parsePoints = null, int p

var operand = Parse(parser, parsePoints, preUnaryOperatorPrecedence + 1);

left = SyntaxTree.Unary(GSymbol.Get($"'{operatorToken.Text}"), operand)
.WithRange(operatorToken.Start, operand.Range.EndIndex).WithStyle(NodeStyle.PrefixNotation);
left = SyntaxTree.Unary(operatorToken.Text, operand, UnaryOperatorKind.Prefix);
}
}
else
Expand All @@ -77,8 +77,7 @@ public static AstNode Parse(Parser parser, ParsePoints parsePoints = null, int p
{
var unaryOperatorToken = parser.Iterator.NextToken();

left = SyntaxTree.Unary(GSymbol.Get($"'suf{unaryOperatorToken.Text}"), left)
.WithRange(left.Range.StartIndex, unaryOperatorToken.End).WithStyle(NodeStyle.Operator);
left = SyntaxTree.Unary(unaryOperatorToken.Text, left, UnaryOperatorKind.Suffix);
}
}
}
Expand All @@ -94,8 +93,7 @@ public static AstNode Parse(Parser parser, ParsePoints parsePoints = null, int p
var operatorToken = parser.Iterator.NextToken();
var right = Parse(parser, parsePoints, precedence);

left = SyntaxTree.Binary(GSymbol.Get($"'{operatorToken.Text}"), left, right)
.WithRange(left.Range.StartIndex, right.Range.StartIndex);
left = SyntaxTree.Binary(GSymbol.Get($"'{operatorToken.Text}"), left, right);

// parsing postunary for: Hello::new()? = false;
var postUnaryOperatorPrecedence = GetPostUnaryOperatorPrecedence(parser.Iterator.Current.Type);
Expand All @@ -106,8 +104,7 @@ public static AstNode Parse(Parser parser, ParsePoints parsePoints = null, int p
{
var unaryOperatorToken = parser.Iterator.NextToken();

left = SyntaxTree.Unary(GSymbol.Get($"'suf{unaryOperatorToken.Text}"), left)
.WithRange(left.Range.StartIndex, unaryOperatorToken.End).WithStyle(NodeStyle.Operator);
left = SyntaxTree.Unary(unaryOperatorToken.Text, left, UnaryOperatorKind.Suffix);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions NewSource/Socordia.CodeAnalysis/Parsing/LNodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Loyc.Syntax;
using Socordia.CodeAnalysis.Parsing.ParsePoints;

namespace Socordia.CodeAnalysis.Parsing;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ public static Annotation Parse(TokenIterator iterator, Parser parser)
{
var atToken = iterator.Match(TokenType.At);

//ToDo: allow support for annotations without arguments and parens
var call = Expression.Parse(parser);
var name = iterator.Match(TokenType.Identifier);
var args = new List<AstNode>();

return new Annotation(call).WithRange(atToken, iterator.Prev);
if (iterator.IsMatch(TokenType.OpenParen))
{
iterator.NextToken();

args = Expression.ParseList(parser, TokenType.CloseParen);
}

return new Annotation(name.Text, args);
}

public static bool TryParse(Parser parser, out List<Annotation> node)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Loyc.Syntax;
using Socordia.CodeAnalysis.Core;

namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations;
namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations;

/*
public sealed class BitFieldDeclaration : IParsePoint
{
public static LNode Parse(TokenIterator iterator, Parser parser)
Expand All @@ -16,4 +14,5 @@ public static LNode Parse(TokenIterator iterator, Parser parser)
return SyntaxTree.Bitfield(nameToken, members).WithRange(keywordToken, iterator.Prev);
}
}
}
*/
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Loyc.Syntax;
using Socordia.CodeAnalysis.Core;

namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations;
namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations;

/*
public sealed class BitFieldMemberDeclaration : IParsePoint
{
public static LNode Parse(TokenIterator iterator, Parser parser)
Expand Down Expand Up @@ -30,4 +28,5 @@ public static LNode Parse(TokenIterator iterator, Parser parser)
return SyntaxTree.Factory.Tuple(SyntaxTree.Factory.Id(nameToken.Text).WithRange(nameToken), value);
}
}
}
*/
Loading

0 comments on commit c6bb808

Please sign in to comment.