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

Commit

Permalink
WE ARE ONE
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarWitch7 committed Nov 8, 2023
1 parent 0c87af0 commit 6b01014
Show file tree
Hide file tree
Showing 20 changed files with 425 additions and 449 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@ csharp_style_implicit_object_creation_when_type_is_apparent = false

# IDE0160: Convert to block scoped namespace
csharp_style_namespace_declarations = file_scoped

# IDE0003: Remove qualification
dotnet_style_qualification_for_property = true

# IDE0003: Remove qualification
dotnet_style_qualification_for_event = true

# IDE0003: Remove qualification
dotnet_style_qualification_for_field = true

# IDE0003: Remove qualification
dotnet_style_qualification_for_method = true
4 changes: 2 additions & 2 deletions Moth-Lang.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33627.172
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CLI", "CLI\CLI.csproj", "{C6BB9E5B-143C-495A-90BA-623C4E640CFE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moth.CLI", "Moth.CLI\Moth.CLI.csproj", "{C6BB9E5B-143C-495A-90BA-623C4E640CFE}"
ProjectSection(ProjectDependencies) = postProject
{45A62957-88A8-419D-98D0-EFD5C901AA06} = {45A62957-88A8-419D-98D0-EFD5C901AA06}
EndProjectSection
Expand All @@ -15,7 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moth.Unit", "Moth.Unit\Moth.Unit.csproj", "{D2661EE9-F280-4797-94DF-D4FB93EB48B0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moth.Test", "Moth.Test\Moth.Test.csproj", "{D2661EE9-F280-4797-94DF-D4FB93EB48B0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions CLI/Program.cs → Moth.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ unsafe static void Main(string[] args)

Parser.Default.ParseArguments<Options>(args).WithParsed<Options>(options =>
{
var compiler = new CompilerContext(options.OutputFile);
var compiler = new LLVMCompiler(options.OutputFile);
var scripts = new List<ScriptAST>();

logger.WriteLine($"Building {options.OutputFile}...");
Expand Down Expand Up @@ -61,7 +61,7 @@ unsafe static void Main(string[] args)
logger.WriteLine($"Generating AST of \"{filePath}\"");
}

var scriptAST = TokenParser.ProcessScript(new ParseContext(tokens));
var scriptAST = ASTGenerator.ProcessScript(new ParseContext(tokens));
scripts.Add(scriptAST);

if (options.Verbose)
Expand Down Expand Up @@ -94,7 +94,7 @@ unsafe static void Main(string[] args)
try
{
logger.WriteLine("Compiling to LLVM IR...");
LLVMCompiler.Compile(compiler, scripts);
compiler.Compile(scripts);

if (options.Verbose)
{
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions Moth.Unit/Utils.cs → Moth.Test/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ public static LLVMModuleRef FullCompile(string code)
{
var tokens = Tokenizer.Tokenize(code);
var context = new ParseContext(tokens);
var ast = TokenParser.ProcessScript(context);
var compiler = new CompilerContext("fullcomp");
LLVMCompiler.Compile(compiler, new ScriptAST[] { ast });
var ast = ASTGenerator.ProcessScript(context);
var compiler = new LLVMCompiler("fullcomp", new ScriptAST[] { ast });
compiler.Module.Verify(LLVMVerifierFailureAction.LLVMPrintMessageAction);
return compiler.Module;
}
Expand Down
87 changes: 30 additions & 57 deletions Moth/AST/TokenParser.cs → Moth/AST/ASTGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Moth.AST; //TODO: allow calling functions on expressions

public static class TokenParser
public static class ASTGenerator
{
public static ScriptAST ProcessScript(ParseContext context)
{
Expand Down Expand Up @@ -61,13 +61,9 @@ public static ScriptAST ProcessScript(ParseContext context)
privacyType = PrivacyType.Private;
}

context.MoveNext();

if (context.Current?.Type == TokenType.Class)
if (context.MoveNext()?.Type == TokenType.Class)
{
context.MoveNext();

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

Check warning on line 68 in Moth/AST/ASTGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

Nullable value type may be null.
context.MoveNext();
Expand Down Expand Up @@ -132,11 +128,8 @@ public static ClassNode ProcessClass(ParseContext context, PrivacyType privacy,
}
else if (context.Current?.Type == TokenType.ClosingGenericBracket)
{
context.MoveNext();

if (context.Current?.Type == TokenType.OpeningCurlyBraces)
if (context.MoveNext()?.Type == TokenType.OpeningCurlyBraces)
{
context.MoveNext();
return new GenericClassNode(name, privacy, @params, ProcessScope(context, true));
}
else
Expand All @@ -156,7 +149,6 @@ public static ClassNode ProcessClass(ParseContext context, PrivacyType privacy,
{
if (context.Current?.Type == TokenType.OpeningCurlyBraces)
{
context.MoveNext();
return new ClassNode(name, privacy, ProcessScope(context, true));
}
else
Expand All @@ -174,9 +166,8 @@ private static GenericParameterNode ProcessGenericParam(ParseContext context)
}

string name = context.Current.Value.Text.ToString();
context.MoveNext();

if (context.Current?.Type != TokenType.TypeRef)
if (context.MoveNext()?.Type != TokenType.TypeRef)
{
return new GenericParameterNode(name);
}
Expand All @@ -189,6 +180,13 @@ public static ScopeNode ProcessScope(ParseContext context, bool isClassRoot = fa
{
List<StatementNode> statements = new List<StatementNode>();

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

context.MoveNext();

if (isClassRoot)
{
List<AttributeNode> attributes = new List<AttributeNode>();
Expand Down Expand Up @@ -225,7 +223,6 @@ public static ScopeNode ProcessScope(ParseContext context, bool isClassRoot = fa
context.MoveNext();
return new ScopeNode(statements);
case TokenType.OpeningCurlyBraces:
context.MoveNext();
statements.Add(ProcessScope(context));
break;
case TokenType.If:
Expand Down Expand Up @@ -272,9 +269,8 @@ private static ExpressionNode ProcessIncrementDecrement(ParseContext context)
{
TokenType type = (TokenType)(context.Current?.Type);
RefNode refNode;
context.MoveNext();

if (context.Current?.Type == TokenType.This || context.Current?.Type == TokenType.Name)
if (context.MoveNext()?.Type == TokenType.This || context.Current?.Type == TokenType.Name)
{
refNode = ProcessAccess(context);
}
Expand Down Expand Up @@ -302,7 +298,6 @@ public static StatementNode ProcessWhile(ParseContext context)
throw new UnexpectedTokenException(context.Current.Value, TokenType.OpeningCurlyBraces);
}

context.MoveNext();
var then = ProcessScope(context);
return new WhileNode(condition, then);
}
Expand All @@ -311,19 +306,14 @@ public static AttributeNode ProcessAttribute(ParseContext context)
{
if (context.Current?.Type == TokenType.AttributeMarker)
{
context.MoveNext();

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

if (context.Current?.Type == TokenType.OpeningParentheses)

if (context.MoveNext()?.Type == TokenType.OpeningParentheses)
{
context.MoveNext();
var args = ProcessArgs(context);

return new AttributeNode(name, args);
return new AttributeNode(name, ProcessArgs(context));
}
else
{
Expand Down Expand Up @@ -372,22 +362,18 @@ public static StatementNode ProcessDefinition(ParseContext context, List<Attribu
throw new UnexpectedTokenException(context.Current.Value);
}

context.MoveNext();

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

if (context.Current?.Type == TokenType.OpeningParentheses)
if (context.MoveNext()?.Type == TokenType.OpeningParentheses)
{
context.MoveNext();
var @params = ProcessParameterList(context, out bool isVariadic);
var retTypeRef = ProcessTypeRef(context);

if (privacy != PrivacyType.Foreign && context.Current?.Type == TokenType.OpeningCurlyBraces)
{
context.MoveNext();
return new FuncDefNode(name, privacy, retTypeRef, @params, ProcessScope(context), isVariadic, attributes);
}
else if (privacy == PrivacyType.Foreign && context.Current?.Type == TokenType.Semicolon)
Expand Down Expand Up @@ -479,8 +465,8 @@ public static ParameterNode ProcessParameter(ParseContext context, out bool isVa
}
else if (context.Current?.Type == TokenType.Variadic)
{
context.MoveNext();
isVariadic = true;
context.MoveNext();
return null;
}
else
Expand All @@ -500,7 +486,6 @@ public static IfNode ProcessIf(ParseContext context)
throw new UnexpectedTokenException(context.Current.Value, TokenType.OpeningCurlyBraces);
}

context.MoveNext();
var then = ProcessScope(context);
var @else = ProcessElse(context);
return new IfNode(condition, then, @else);
Expand All @@ -510,19 +495,16 @@ public static IfNode ProcessIf(ParseContext context)
{
if (context.Current?.Type == TokenType.Else)
{
context.MoveNext();

if (context.Current?.Type == TokenType.If)
if (context.MoveNext()?.Type == TokenType.If)
{
context.MoveNext(); //TODO: this does not work in compilation
context.MoveNext();
return new ScopeNode(new List<StatementNode>
{
ProcessIf(context)
ProcessIf(context) //TODO: this does not work in compilation
});
}
else
{
context.MoveNext();
return ProcessScope(context);
}
}
Expand Down Expand Up @@ -567,17 +549,15 @@ public static TypeRefNode ProcessTypeRef(ParseContext context)
|| context.Current?.Type == TokenType.GenericTypeRef) //TODO: handle this patheticness
{
var startToken = context.Current?.Type;
context.MoveNext();

if (context.Current?.Type == TokenType.Name)
if (context.MoveNext()?.Type == TokenType.Name)
{
string retTypeName = context.Current.Value.Text.ToString();
var genericParams = new List<ExpressionNode>();
uint pointerDepth = 0;
context.MoveNext();

if (startToken != TokenType.GenericTypeRef
&& context.Current?.Type == TokenType.OpeningGenericBracket)
if (context.MoveNext()?.Type == TokenType.OpeningGenericBracket
&& startToken != TokenType.GenericTypeRef)
{
context.MoveNext();

Expand All @@ -602,8 +582,6 @@ public static TypeRefNode ProcessTypeRef(ParseContext context)
throw new UnexpectedTokenException(context.Current.Value);
}
}

context.MoveNext();
}

while (context.Current?.Type == TokenType.Asterix)
Expand Down Expand Up @@ -694,14 +672,11 @@ public static ExpressionNode ProcessExpression(ParseContext context, ExpressionN
case TokenType.Period:
throw new NotImplementedException("Access operations on expressions are not currently supported."); //TODO
case TokenType.Local:
context.MoveNext();

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

if (context.Current?.Type == TokenType.InferAssign)
if (context.MoveNext()?.Type == TokenType.InferAssign)
{
context.MoveNext();
lastCreatedNode = new InferredLocalDefNode(name, ProcessExpression(context, null));
Expand Down Expand Up @@ -876,9 +851,8 @@ public static RefNode ProcessAccess(ParseContext context)
if (context.Current?.Type == TokenType.This)
{
newRefNode = new ThisNode();
context.MoveNext();

if (context.Current?.Type == TokenType.Period)
if (context.MoveNext()?.Type == TokenType.Period)
{
context.MoveNext();
}
Expand Down Expand Up @@ -908,9 +882,8 @@ public static RefNode ProcessAccess(ParseContext context)
{
string name = context.Current.Value.Text.ToString();
RefNode currentNode = newRefNode;
context.MoveNext();

switch (context.Current?.Type)
switch (context.MoveNext()?.Type)
{
case TokenType.OpeningParentheses:
{
Expand Down
31 changes: 5 additions & 26 deletions Moth/AST/ParseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ namespace Moth.AST;

public class ParseContext
{
public int Position { get; private set; }

public readonly int Length;

private readonly List<Token> _tokens;
public int Position { get; private set; }

public ParseContext(List<Token> tokens)
{
Expand All @@ -27,32 +29,9 @@ public Token? Current
}
}

public void MoveNext()
public Token? MoveNext()
{
Position++;
}

public void MoveAmount(int amount)
{
Position += amount;
}

public Token GetByIndex(int index)
{
return _tokens[index];
}

public Token[] Peek(int count)
{
if (Position + count <= Length)
{
var copied = new Token[count];
_tokens.CopyTo(Position, copied, 0, count);
return copied;
}
else
{
return Array.Empty<Token>();
}
return Current;
}
}
Loading

0 comments on commit 6b01014

Please sign in to comment.