Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
Privacy, please!
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarWitch7 committed Mar 18, 2024
1 parent b218f46 commit 0f2d961
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 207 deletions.
83 changes: 40 additions & 43 deletions Moth/AST/ASTGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static ScriptAST ProcessScript(ParseContext context)
var imports = new List<string>();
var funcs = new List<FuncDefNode>();
var globals = new List<FieldDefNode>();
var classes = new List<ClassNode>();
var classes = new List<StructNode>();

if (context.Current?.Type == TokenType.Namespace)
{
Expand All @@ -39,12 +39,11 @@ public static ScriptAST ProcessScript(ParseContext context)
context.MoveNext();
imports.Add(ProcessNamespace(context));
break;
case TokenType.Public:
case TokenType.Private:
default:
StatementNode result = ProcessDefinition(context, attributes);
attributes = new List<AttributeNode>();

if (result is ClassNode @class)
if (result is StructNode @class)
{
classes.Add(@class);
}
Expand All @@ -62,8 +61,6 @@ public static ScriptAST ProcessScript(ParseContext context)
}

break;
default:
throw new UnexpectedTokenException(context.Current.Value);
}
}

Expand Down Expand Up @@ -97,21 +94,16 @@ public static string ProcessNamespace(ParseContext context)
throw new UnexpectedTokenException(context.Current.Value);
}

public static ClassNode ProcessClass(ParseContext context, PrivacyType privacy, bool isForeign, bool isStruct = false)
public static StructNode ProcessStruct(ParseContext context, PrivacyType privacy, bool isForeign)
{
if ((isStruct && context.Current?.Type != TokenType.Struct) || (!isStruct && context.Current?.Type != TokenType.Class))
{
throw new UnexpectedTokenException(context.Current.Value, TokenType.Class);
}

if (context.MoveNext()?.Type == TokenType.Name)
{
string name = context.Current.Value.Text.ToString();
context.MoveNext();

if (!isStruct && !isForeign && context.Current?.Type == TokenType.OpeningGenericBracket)
if (!isForeign && context.Current?.Type == TokenType.LesserThan)
{
var @params = new List<GenericParameterNode>();
var @params = new List<TemplateParameterNode>();
context.MoveNext();

while (context.Current != null)
Expand All @@ -124,15 +116,15 @@ public static ClassNode ProcessClass(ParseContext context, PrivacyType privacy,
}
else
{
return context.Current?.Type == TokenType.ClosingGenericBracket
return context.Current?.Type == TokenType.GreaterThan
? context.MoveNext()?.Type == TokenType.OpeningCurlyBraces
? (ClassNode)new GenericClassNode(name, privacy, @params, ProcessScope(context, true))
? (StructNode)new TemplateNode(name, privacy, @params, ProcessScope(context, true))
: throw new UnexpectedTokenException(context.Current.Value, TokenType.OpeningCurlyBraces)
: throw new UnexpectedTokenException(context.Current.Value);
}
}

throw new UnexpectedTokenException(context.Current.Value, TokenType.ClosingGenericBracket);
throw new UnexpectedTokenException(context.Current.Value, TokenType.GreaterThan);
}
else
{
Expand All @@ -141,7 +133,7 @@ public static ClassNode ProcessClass(ParseContext context, PrivacyType privacy,
if (context.Current?.Type == TokenType.Semicolon)
{
context.MoveNext();
return new ClassNode(name, privacy, null, isStruct);
return new StructNode(name, privacy, null);
}
else
{
Expand All @@ -150,7 +142,7 @@ public static ClassNode ProcessClass(ParseContext context, PrivacyType privacy,
}
else
{
return new ClassNode(name, privacy, ProcessScope(context, true), isStruct);
return new StructNode(name, privacy, ProcessScope(context, true));
}
}
}
Expand All @@ -160,7 +152,7 @@ public static ClassNode ProcessClass(ParseContext context, PrivacyType privacy,
}
}

private static GenericParameterNode ProcessGenericParam(ParseContext context)
private static TemplateParameterNode ProcessGenericParam(ParseContext context)
{
if (context.Current?.Type != TokenType.Name)
{
Expand All @@ -171,11 +163,11 @@ private static GenericParameterNode ProcessGenericParam(ParseContext context)

if (context.MoveNext()?.Type != TokenType.TypeRef)
{
return new GenericParameterNode(name);
return new TemplateParameterNode(name);
}

TypeRefNode typeRef = ProcessTypeRef(context);
return new ConstGenericParameterNode(name, typeRef);
return new ConstTemplateParameterNode(name, typeRef);
}

public static ScopeNode ProcessScope(ParseContext context, bool isClassRoot = false)
Expand Down Expand Up @@ -203,14 +195,11 @@ public static ScopeNode ProcessScope(ParseContext context, bool isClassRoot = fa
case TokenType.AttributeMarker:
attributes.Add(ProcessAttribute(context));
break;
case TokenType.Public:
case TokenType.Private:
default:
StatementNode newDef = ProcessDefinition(context, attributes);
attributes = new List<AttributeNode>();
statements.Add(newDef);
break;
default:
throw new UnexpectedTokenException(context.Current.Value);
}
}
}
Expand Down Expand Up @@ -325,20 +314,20 @@ public static AttributeNode ProcessAttribute(ParseContext context)

public static StatementNode ProcessDefinition(ParseContext context, List<AttributeNode>? attributes = null)
{
PrivacyType privacy = context.Current?.Type == TokenType.Public
? PrivacyType.Public
: context.Current?.Type == TokenType.Private
? PrivacyType.Private
: throw new UnexpectedTokenException(context.Current.Value);
PrivacyType privacy = PrivacyType.Private;
bool isForeign = false;
bool isStatic = false;

context.MoveNext();

while (context.Current?.Type == TokenType.Foreign || context.Current?.Type == TokenType.Static)
while (context.Current?.Type == TokenType.Public
|| context.Current?.Type == TokenType.Foreign
|| context.Current?.Type == TokenType.Static)
{
switch (context.Current?.Type)
{
case TokenType.Public:
if (privacy != PrivacyType.Private) throw new UnexpectedTokenException(context.Current.Value);
privacy = PrivacyType.Public;
break;
case TokenType.Foreign:
if (isForeign) throw new UnexpectedTokenException(context.Current.Value);
isForeign = true;
Expand All @@ -356,11 +345,7 @@ public static StatementNode ProcessDefinition(ParseContext context, List<Attribu

if (context.Current?.Type == TokenType.Struct)
{
return ProcessClass(context, privacy, isForeign, true);
}
else if (context.Current?.Type == TokenType.Class)
{
return ProcessClass(context, privacy, isForeign);
return ProcessStruct(context, privacy, isForeign);
}
else if (context.Current?.Type == TokenType.Function)
{
Expand Down Expand Up @@ -604,27 +589,39 @@ public static TypeRefNode ProcessTypeRef(ParseContext context)
var genericParams = new List<ExpressionNode>();
uint pointerDepth = 0;

if (context.MoveNext()?.Type == TokenType.OpeningGenericBracket)
if (context.MoveNext()?.Type == TokenType.LesserThan)
{
context.MoveNext();

while (context.Current?.Type != TokenType.ClosingGenericBracket)
while (context.Current?.Type != TokenType.GreaterThan)
{
if (context.Current?.Type is TokenType.TypeRef
or TokenType.TemplateTypeRef)
{
genericParams.Add(ProcessTypeRef(context));
}
else
else if (context.Current?.Type == TokenType.OpeningParentheses)
{
context.MoveNext();
genericParams.Add(ProcessExpression(context));

if (context.Current?.Type != TokenType.ClosingParentheses)
{
throw new UnexpectedTokenException(context.Current.Value, TokenType.ClosingParentheses);
}

context.MoveNext();
}
else
{
throw new UnexpectedTokenException(context.Current.Value);
}

if (context.Current?.Type == TokenType.Comma)
{
context.MoveNext();
}
else if (context.Current?.Type != TokenType.ClosingGenericBracket)
else if (context.Current?.Type != TokenType.GreaterThan)
{
throw new UnexpectedTokenException(context.Current.Value);
}
Expand Down
8 changes: 0 additions & 8 deletions Moth/AST/Node/ConstGenericParameterNode.cs

This file was deleted.

8 changes: 8 additions & 0 deletions Moth/AST/Node/ConstTemplateParameterNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Moth.AST.Node;

public class ConstTemplateParameterNode : TemplateParameterNode
{
public TypeRefNode TypeRef { get; set; }

public ConstTemplateParameterNode(string name, TypeRefNode typeRef) : base(name) => TypeRef = typeRef;
}
9 changes: 0 additions & 9 deletions Moth/AST/Node/GenericClassNode.cs

This file was deleted.

8 changes: 0 additions & 8 deletions Moth/AST/Node/GenericParameterNode.cs

This file was deleted.

6 changes: 2 additions & 4 deletions Moth/AST/Node/ClassNode.cs → Moth/AST/Node/StructNode.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
namespace Moth.AST.Node;

public class ClassNode : DefinitionNode
public class StructNode : DefinitionNode
{
public string Name { get; set; }
public PrivacyType Privacy { get; set; }
public ScopeNode? Scope { get; set; }
public bool IsStruct { get; set; }

public ClassNode(string name, PrivacyType privacy, ScopeNode? scope, bool isStruct) : base(new List<AttributeNode>())
public StructNode(string name, PrivacyType privacy, ScopeNode? scope) : base(new List<AttributeNode>())
{
Name = name;
Privacy = privacy;
Scope = scope;
IsStruct = isStruct;
}
}
9 changes: 9 additions & 0 deletions Moth/AST/Node/TemplateNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Moth.AST.Node;

public class TemplateNode : StructNode
{
public List<TemplateParameterNode> Params { get; set; }

public TemplateNode(string name, PrivacyType privacy, List<TemplateParameterNode> @params, ScopeNode scope)
: base(name, privacy, scope) => Params = @params;
}
8 changes: 8 additions & 0 deletions Moth/AST/Node/TemplateParameterNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Moth.AST.Node;

public class TemplateParameterNode : ASTNode
{
public string Name { get; set; }

public TemplateParameterNode(string name) => Name = name;
}
4 changes: 2 additions & 2 deletions Moth/AST/ScriptAST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ public class ScriptAST : ASTNode
{
public string Namespace { get; }
public List<string> Imports { get; }
public List<ClassNode> ClassNodes { get; }
public List<StructNode> ClassNodes { get; }
public List<FuncDefNode> GlobalFunctions { get; }
public List<FieldDefNode> GlobalVariables { get; }

public ScriptAST(string @namespace,
List<string> imports,
List<ClassNode> classNodes,
List<StructNode> classNodes,
List<FuncDefNode> globalFuncs,
List<FieldDefNode> globalVariables)
{
Expand Down
2 changes: 1 addition & 1 deletion Moth/LLVM/Data/Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Struct Build(LLVMCompiler compiler, IReadOnlyList<ExpressionNode> args)
}
}

var classNode = new ClassNode($"{Name}{Template.ArgsToSig(args)}", Privacy, Contents, true);
var classNode = new StructNode($"{Name}{Template.ArgsToSig(args)}", Privacy, Contents);
@struct = new Struct(Parent,
classNode.Name,
compiler.Context.CreateNamedStruct(classNode.Name),
Expand Down
Loading

0 comments on commit 0f2d961

Please sign in to comment.