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

Commit

Permalink
Merge pull request #20 from StellarWitch7/dev
Browse files Browse the repository at this point in the history
Quick update
  • Loading branch information
StellarWitch7 authored Jun 21, 2024
2 parents dcea4cb + 763cd65 commit cf72342
Show file tree
Hide file tree
Showing 89 changed files with 1,099 additions and 9,640 deletions.
432 changes: 215 additions & 217 deletions Moth.Compiler/Program.cs

Large diffs are not rendered by default.

16 changes: 2 additions & 14 deletions Moth.Luna/Builders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public static string BuildFromProject(ProjectSource source)
new ProcessStartInfo(source.Build.Command, source.Build.Args)
{
WorkingDirectory = source.Dir,
RedirectStandardOutput = true,
RedirectStandardError = true
}
);

Expand All @@ -50,18 +48,8 @@ public static string BuildFromProject(ProjectSource source)

build.WaitForExit();

lock (ConsoleLock)
{
Program.Logger.WriteUnsigned(build.StandardOutput.ReadToEnd());

if (build.ExitCode != 0)
{
Program.Logger.Error(build.StandardError.ReadToEnd());
throw new Exception(
$"{source.Build.Command} finished with exit code {build.ExitCode}"
);
}
}
if (build.ExitCode != 0)
throw new Exception($"{source.Build.Command} finished with exit code {build.ExitCode}");

return Path.Combine(source.Dir, project.Out, project.FullOutputName);
}
Expand Down
1 change: 1 addition & 0 deletions Moth.Luna/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ var file in Directory.GetFiles(project.Root, "*.moth", SearchOption.AllDirectori

var mothc = Moth.Compiler.Program.Main(args.ToString().Split(' '));
Environment.CurrentDirectory = oldDir;
Logger.MakeSubLogger("mothc").ExitCode(mothc);

if (mothc != 0)
throw new Exception($"mothc finished with exit code {mothc}");
Expand Down
139 changes: 71 additions & 68 deletions Moth/AST/ASTGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ public static class ASTGenerator
public static ScriptAST ProcessScript(ParseContext context)
{
NamespaceNode @namespace;
var imports = new List<NamespaceNode>();
var types = new List<TypeNode>();
var enums = new List<EnumNode>();
var funcs = new List<FuncDefNode>();
var globals = new List<GlobalVarNode>();
var traits = new List<TraitNode>();
var implements = new List<ImplementNode>();
var contents = new List<IStatementNode>();

if (context.Current?.Type == TokenType.Namespace)
{
Expand All @@ -40,23 +34,37 @@ public static ScriptAST ProcessScript(ParseContext context)

while (context.Current != null)
{
switch (context.Current?.Type)
if (context.Current?.Type == TokenType.Import)
{
case TokenType.AttributeMarker:
attributes.Add(ProcessAttribute(context));
break;
case TokenType.Import:
context.MoveNext();
imports.Add(ProcessNamespace(context));
context.MoveNext();
contents.Add(new ImportNode(ProcessNamespace(context)));

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

context.MoveNext();
}
else
break;
}

while (context.Current != null)
{
switch (context.Current?.Type)
{
case TokenType.BlockComment:
case TokenType.Comment:
contents.Add(
new CommentNode(
context.Current.Value.Text.ToString(),
context.Current?.Type == TokenType.BlockComment
)
);
context.MoveNext();
break;
case TokenType.AttributeMarker:
attributes.Add(ProcessAttribute(context));
break;
case TokenType.Implement:
context.MoveNext();
var trait = ProcessTypeRef(context);
Expand Down Expand Up @@ -102,42 +110,17 @@ public static ScriptAST ProcessScript(ParseContext context)
throw new Exception($"{error} cannot be unimplemented.");
}

implements.Add(new ImplementNode(type, trait, scope));
contents.Add(new ImplementNode(type, trait, scope));
break;
default:
StatementNode result = ProcessDefinition(context, attributes);
var result = ProcessDefinition(context, attributes);
attributes = new List<AttributeNode>();

if (result is TypeNode typeNode)
{
types.Add(typeNode);
}
else if (result is EnumNode enumNode)
{
enums.Add(enumNode);
}
else if (result is TraitNode traitNode)
{
traits.Add(traitNode);
}
else if (result is FuncDefNode funcDefNode)
{
funcs.Add(funcDefNode);
}
else if (result is GlobalVarNode globalVarNode)
{
globals.Add(globalVarNode);
}
else
{
throw new NotImplementedException();
}

contents.Add(result);
break;
}
}

return new ScriptAST(@namespace, imports, types, enums, traits, funcs, globals, implements);
return new ScriptAST(@namespace, contents);
}

public static NamespaceNode ProcessNamespace(ParseContext context)
Expand Down Expand Up @@ -343,7 +326,7 @@ private static TemplateParameterNode ProcessTemplateParam(ParseContext context)

public static ScopeNode ProcessScope(ParseContext context, bool isClassRoot = false)
{
var statements = new List<StatementNode>();
var statements = new List<IStatementNode>();

if (context.Current?.Type != TokenType.OpeningCurlyBraces)
{
Expand All @@ -363,11 +346,21 @@ public static ScopeNode ProcessScope(ParseContext context, bool isClassRoot = fa
case TokenType.ClosingCurlyBraces:
context.MoveNext();
return new ScopeNode(statements);
case TokenType.BlockComment:
case TokenType.Comment:
statements.Add(
new CommentNode(
context.Current.Value.Text.ToString(),
context.Current?.Type == TokenType.BlockComment
)
);
context.MoveNext();
break;
case TokenType.AttributeMarker:
attributes.Add(ProcessAttribute(context));
break;
default:
StatementNode newDef = ProcessDefinition(context, attributes);
IStatementNode newDef = ProcessDefinition(context, attributes);
attributes = new List<AttributeNode>();
statements.Add(newDef);
break;
Expand All @@ -380,6 +373,16 @@ public static ScopeNode ProcessScope(ParseContext context, bool isClassRoot = fa
{
switch (context.Current?.Type)
{
case TokenType.BlockComment:
case TokenType.Comment:
statements.Add(
new CommentNode(
context.Current.Value.Text.ToString(),
context.Current?.Type == TokenType.BlockComment
)
);
context.MoveNext();
break;
case TokenType.Return:
context.MoveNext();
statements.Add(new ReturnNode(ProcessExpression(context, true)));
Expand Down Expand Up @@ -429,7 +432,7 @@ public static ScopeNode ProcessScope(ParseContext context, bool isClassRoot = fa
throw new UnexpectedTokenException(context.Current.Value);
}

private static ExpressionNode ProcessIncrementDecrement(ParseContext context)
private static IExpressionNode ProcessIncrementDecrement(ParseContext context)
{
var type = (TokenType)(context.Current?.Type);

Expand All @@ -442,9 +445,9 @@ private static ExpressionNode ProcessIncrementDecrement(ParseContext context)
: new DecrementVarNode(value);
}

public static StatementNode ProcessWhile(ParseContext context)
public static IStatementNode ProcessWhile(ParseContext context)
{
ExpressionNode condition = ProcessExpression(context);
IExpressionNode condition = ProcessExpression(context);

if (context.Current?.Type != TokenType.OpeningCurlyBraces)
{
Expand Down Expand Up @@ -473,7 +476,7 @@ public static AttributeNode ProcessAttribute(ParseContext context)
}
else
{
return new AttributeNode(name, new List<ExpressionNode>());
return new AttributeNode(name, new List<IExpressionNode>());
}
}
else
Expand All @@ -489,7 +492,7 @@ public static AttributeNode ProcessAttribute(ParseContext context)
throw new NotImplementedException();
}

public static StatementNode ProcessDefinition(
public static DefinitionNode ProcessDefinition(
ParseContext context,
List<AttributeNode>? attributes
)
Expand Down Expand Up @@ -782,7 +785,7 @@ out bool isVariadic

public static IfNode ProcessIf(ParseContext context)
{
ExpressionNode condition = ProcessExpression(context);
IExpressionNode condition = ProcessExpression(context);

if (context.Current?.Type != TokenType.OpeningCurlyBraces)
{
Expand All @@ -802,7 +805,7 @@ public static IfNode ProcessIf(ParseContext context)
{
context.MoveNext();
return new ScopeNode(
new List<StatementNode>
new List<IStatementNode>
{
ProcessIf(context) //TODO: this does not work in compilation
}
Expand All @@ -819,9 +822,9 @@ public static IfNode ProcessIf(ParseContext context)
}
}

public static List<ExpressionNode> ProcessArgs(ParseContext context, TokenType terminator)
public static List<IExpressionNode> ProcessArgs(ParseContext context, TokenType terminator)
{
var args = new List<ExpressionNode>();
var args = new List<IExpressionNode>();

while (context.Current != null)
{
Expand Down Expand Up @@ -899,7 +902,7 @@ public static TypeRefNode ProcessTypeRef(ParseContext context)
if (context.MoveNext()?.Type == TokenType.Name)
{
string retTypeName = context.Current.Value.Text.ToString();
var genericParams = new List<ExpressionNode>();
var genericParams = new List<IExpressionNode>();
uint pointerDepth = 0;
bool isRef = false;

Expand Down Expand Up @@ -1069,9 +1072,9 @@ public static TypeRefNode ProcessTypeRef(ParseContext context)
}
}

public static ExpressionNode ProcessExpression(ParseContext context, bool nullAllowed = false)
public static IExpressionNode ProcessExpression(ParseContext context, bool nullAllowed = false)
{
Stack<ExpressionNode> stack = new Stack<ExpressionNode>();
Stack<IExpressionNode> stack = new Stack<IExpressionNode>();

while (context.Current != null)
{
Expand Down Expand Up @@ -1252,7 +1255,7 @@ out bool isVariadic
{
string name = context.Current.Value.Text.ToString();

if (context.MoveNext()?.Type == TokenType.InferAssign)
if (context.MoveNext()?.Type == TokenType.Assign)
{
context.MoveNext();
stack.Push(new InferredLocalDefNode(name, ProcessExpression(context)));
Expand All @@ -1271,23 +1274,23 @@ out bool isVariadic
break;
case TokenType.If:
context.MoveNext();
ExpressionNode condition = ProcessExpression(context);
IExpressionNode condition = ProcessExpression(context);

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

context.MoveNext();
ExpressionNode then = ProcessExpression(context);
IExpressionNode then = ProcessExpression(context);

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

context.MoveNext();
ExpressionNode @else = ProcessExpression(context);
IExpressionNode @else = ProcessExpression(context);

stack.Push(new InlineIfNode(condition, then, @else));
break;
Expand Down Expand Up @@ -1452,7 +1455,7 @@ out bool isVariadic
if (stack.Count > 0 && stack.Peek() is not BinaryOperationNode)
throw new UnexpectedTokenException(context.Current.Value);

stack.Push(new ThisNode());
stack.Push(new SelfNode());
context.MoveNext();
break;
default:
Expand Down Expand Up @@ -1516,7 +1519,7 @@ public static LiteralArrayNode ProcessLiteralArray(
TypeRefNode elementType
)
{
var elements = new List<ExpressionNode>();
var elements = new List<IExpressionNode>();

if (context.Current?.Type != TokenType.OpeningSquareBrackets)
{
Expand Down
7 changes: 2 additions & 5 deletions Moth/AST/ASTNode.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
namespace Moth.AST;

public abstract partial class ASTNode
public interface IASTNode
{
public virtual string GetSource()
{
throw new NotImplementedException();
}
string GetSource();
}
Loading

0 comments on commit cf72342

Please sign in to comment.