Some cleanup
This commit is contained in:
@@ -2,17 +2,17 @@ using NubLang.Ast;
|
||||
|
||||
namespace NubLang.Generation;
|
||||
|
||||
public class CGenerator
|
||||
public class Generator
|
||||
{
|
||||
private readonly List<DefinitionNode> _definitions;
|
||||
private readonly HashSet<NubStructType> _structTypes;
|
||||
private readonly CWriter _writer;
|
||||
private readonly IndentedTextWriter _writer;
|
||||
|
||||
public CGenerator(List<DefinitionNode> definitions, HashSet<NubStructType> structTypes)
|
||||
public Generator(List<DefinitionNode> definitions, HashSet<NubStructType> structTypes)
|
||||
{
|
||||
_definitions = definitions;
|
||||
_structTypes = structTypes;
|
||||
_writer = new CWriter();
|
||||
_writer = new IndentedTextWriter();
|
||||
}
|
||||
|
||||
private static string MapType(NubType nubType)
|
||||
@@ -114,7 +114,6 @@ public class CGenerator
|
||||
""");
|
||||
_writer.WriteLine();
|
||||
|
||||
_writer.WriteLine("// Struct definitions");
|
||||
foreach (var structType in _structTypes)
|
||||
{
|
||||
_writer.WriteLine("typedef struct");
|
||||
@@ -131,9 +130,11 @@ public class CGenerator
|
||||
_writer.WriteLine();
|
||||
}
|
||||
|
||||
_writer.WriteLine("// Function declarations");
|
||||
var appendNewLine = false;
|
||||
|
||||
foreach (var funcNode in _definitions.OfType<FuncNode>())
|
||||
{
|
||||
appendNewLine = true;
|
||||
var parameters = funcNode.Signature.Parameters.Count != 0
|
||||
? string.Join(", ", funcNode.Signature.Parameters.Select(x => $"{MapType(x.Type)} {x.Name}"))
|
||||
: "void";
|
||||
@@ -142,12 +143,13 @@ public class CGenerator
|
||||
_writer.WriteLine($"{MapType(funcNode.Signature.ReturnType)} {name}({parameters});");
|
||||
}
|
||||
|
||||
_writer.WriteLine();
|
||||
if (appendNewLine)
|
||||
{
|
||||
_writer.WriteLine();
|
||||
}
|
||||
|
||||
_writer.WriteLine("// Struct function implementations");
|
||||
foreach (var structNode in _definitions.OfType<StructNode>())
|
||||
{
|
||||
_writer.WriteLine($"// {structNode.Module}::{structNode.Name}");
|
||||
foreach (var structFuncNode in structNode.Functions)
|
||||
{
|
||||
var parameters = structFuncNode.Signature.Parameters.Count != 0
|
||||
@@ -157,12 +159,10 @@ public class CGenerator
|
||||
var name = StructFuncName(structNode.Module, structNode.Name, structFuncNode.Name);
|
||||
_writer.WriteLine($"{MapType(structFuncNode.Signature.ReturnType)} {name}({parameters})");
|
||||
EmitBlock(structFuncNode.Body);
|
||||
_writer.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
_writer.WriteLine();
|
||||
|
||||
_writer.WriteLine("// Function implementations");
|
||||
foreach (var funcNode in _definitions.OfType<FuncNode>())
|
||||
{
|
||||
if (funcNode.Body == null) continue;
|
||||
@@ -229,7 +229,9 @@ public class CGenerator
|
||||
|
||||
private void EmitAssignment(AssignmentNode assignmentNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var target = EmitExpression(assignmentNode.Target);
|
||||
var value = EmitExpression(assignmentNode.Value);
|
||||
_writer.WriteLine($"{target} = {value};");
|
||||
}
|
||||
|
||||
private void EmitBreak(BreakNode breakNode)
|
||||
@@ -314,7 +316,7 @@ public class CGenerator
|
||||
|
||||
private string EmitExpression(ExpressionNode expressionNode)
|
||||
{
|
||||
return expressionNode switch
|
||||
var expr = expressionNode switch
|
||||
{
|
||||
ArrayIndexAccessNode arrayIndexAccessNode => EmitArrayIndexAccess(arrayIndexAccessNode),
|
||||
ArrayInitializerNode arrayInitializerNode => EmitArrayInitializer(arrayInitializerNode),
|
||||
@@ -342,21 +344,53 @@ public class CGenerator
|
||||
UnaryExpressionNode unaryExpressionNode => EmitUnaryExpression(unaryExpressionNode),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(expressionNode))
|
||||
};
|
||||
|
||||
return $"({expr})";
|
||||
}
|
||||
|
||||
private string EmitArrayIndexAccess(ArrayIndexAccessNode arrayIndexAccessNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var array = EmitExpression(arrayIndexAccessNode.Target);
|
||||
var index = EmitExpression(arrayIndexAccessNode.Index);
|
||||
return $"(({MapType(arrayIndexAccessNode.Type)}*){array})[{index}]";
|
||||
}
|
||||
|
||||
private string EmitArrayInitializer(ArrayInitializerNode arrayInitializerNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var capacity = EmitExpression(arrayInitializerNode.Capacity);
|
||||
var elementType = MapType(arrayInitializerNode.ElementType);
|
||||
return $"({elementType}[{capacity}]){{0}}";
|
||||
}
|
||||
|
||||
private string EmitBinaryExpression(BinaryExpressionNode binaryExpressionNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var left = EmitExpression(binaryExpressionNode.Left);
|
||||
var right = EmitExpression(binaryExpressionNode.Right);
|
||||
|
||||
var op = binaryExpressionNode.Operator switch
|
||||
{
|
||||
BinaryOperator.Plus => "+",
|
||||
BinaryOperator.Minus => "-",
|
||||
BinaryOperator.Multiply => "*",
|
||||
BinaryOperator.Divide => "/",
|
||||
BinaryOperator.Modulo => "%",
|
||||
BinaryOperator.Equal => "==",
|
||||
BinaryOperator.NotEqual => "!=",
|
||||
BinaryOperator.LessThan => "<",
|
||||
BinaryOperator.LessThanOrEqual => "<=",
|
||||
BinaryOperator.GreaterThan => ">",
|
||||
BinaryOperator.GreaterThanOrEqual => ">=",
|
||||
BinaryOperator.LogicalAnd => "&&",
|
||||
BinaryOperator.LogicalOr => "||",
|
||||
BinaryOperator.BitwiseAnd => "&",
|
||||
BinaryOperator.BitwiseOr => "|",
|
||||
BinaryOperator.BitwiseXor => "^",
|
||||
BinaryOperator.LeftShift => "<<",
|
||||
BinaryOperator.RightShift => ">>",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
|
||||
return $"{left} {op} {right}";
|
||||
}
|
||||
|
||||
private string EmitBoolLiteral(BoolLiteralNode boolLiteralNode)
|
||||
@@ -366,12 +400,14 @@ public class CGenerator
|
||||
|
||||
private string EmitConvertFloat(ConvertFloatNode convertFloatNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var value = EmitExpression(convertFloatNode.Value);
|
||||
return $"({MapType(convertFloatNode.Type)}){value}";
|
||||
}
|
||||
|
||||
private string EmitConvertInt(ConvertIntNode convertIntNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var value = EmitExpression(convertIntNode.Value);
|
||||
return $"({MapType(convertIntNode.Type)}){value}";
|
||||
}
|
||||
|
||||
private string EmitCStringLiteral(CStringLiteralNode cStringLiteralNode)
|
||||
@@ -381,22 +417,36 @@ public class CGenerator
|
||||
|
||||
private string EmitDereference(DereferenceNode dereferenceNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var pointer = EmitExpression(dereferenceNode.Target);
|
||||
return $"*({MapType(dereferenceNode.Type)}*){pointer}";
|
||||
}
|
||||
|
||||
private string EmitFloat32Literal(Float32LiteralNode float32LiteralNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var str = float32LiteralNode.Value.ToString("G9", System.Globalization.CultureInfo.InvariantCulture);
|
||||
if (!str.Contains('.') && !str.Contains('e') && !str.Contains('E'))
|
||||
{
|
||||
str += ".0";
|
||||
}
|
||||
|
||||
return str + "f";
|
||||
}
|
||||
|
||||
private string EmitFloat64Literal(Float64LiteralNode float64LiteralNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var str = float64LiteralNode.Value.ToString("G17", System.Globalization.CultureInfo.InvariantCulture);
|
||||
if (!str.Contains('.') && !str.Contains('e') && !str.Contains('E'))
|
||||
{
|
||||
str += ".0";
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
private string EmitFloatToIntBuiltin(FloatToIntBuiltinNode floatToIntBuiltinNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var value = EmitExpression(floatToIntBuiltinNode.Value);
|
||||
return $"({MapType(floatToIntBuiltinNode.Type)}){value}";
|
||||
}
|
||||
|
||||
private string EmitFuncCall(FuncCallNode funcCallNode)
|
||||
@@ -426,22 +476,23 @@ public class CGenerator
|
||||
|
||||
private string EmitAddressOf(AddressOfNode addressOfNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var value = EmitExpression(addressOfNode.LValue);
|
||||
return $"(uintptr_t)&{value}";
|
||||
}
|
||||
|
||||
private string EmitLValueIdentifier(LValueIdentifierNode lValueIdentifierNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return lValueIdentifierNode.Name;
|
||||
}
|
||||
|
||||
private string EmitRValueIdentifier(RValueIdentifierNode rValueIdentifierNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return rValueIdentifierNode.Name;
|
||||
}
|
||||
|
||||
private string EmitSizeBuiltin(SizeBuiltinNode sizeBuiltinNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return $"sizeof({MapType(sizeBuiltinNode.TargetType)})";
|
||||
}
|
||||
|
||||
private string EmitStringLiteral(StringLiteralNode stringLiteralNode)
|
||||
@@ -451,7 +502,8 @@ public class CGenerator
|
||||
|
||||
private string EmitStructFieldAccess(StructFieldAccessNode structFieldAccessNode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var structExpr = EmitExpression(structFieldAccessNode.Target);
|
||||
return $"{structExpr}.{structFieldAccessNode.Field}";
|
||||
}
|
||||
|
||||
private string EmitStructFuncCall(StructFuncCallNode structFuncCallNode)
|
||||
@@ -474,7 +526,7 @@ public class CGenerator
|
||||
? "0"
|
||||
: string.Join(", ", initValues);
|
||||
|
||||
return $"({MapType(structInitializerNode.Type)}){{ {initString} }}";
|
||||
return $"({MapType(structInitializerNode.Type)}){{{initString}}}";
|
||||
}
|
||||
|
||||
private string EmitUIntLiteral(UIntLiteralNode uIntLiteralNode)
|
||||
@@ -2,7 +2,7 @@ using System.Text;
|
||||
|
||||
namespace NubLang.Generation;
|
||||
|
||||
internal class CWriter
|
||||
internal class IndentedTextWriter
|
||||
{
|
||||
private readonly StringBuilder _builder = new();
|
||||
private int _indentLevel;
|
||||
@@ -52,10 +52,10 @@ internal class CWriter
|
||||
|
||||
private class IndentScope : IDisposable
|
||||
{
|
||||
private readonly CWriter _writer;
|
||||
private readonly IndentedTextWriter _writer;
|
||||
private bool _disposed;
|
||||
|
||||
public IndentScope(CWriter writer)
|
||||
public IndentScope(IndentedTextWriter writer)
|
||||
{
|
||||
_writer = writer;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,39 +0,0 @@
|
||||
using System.Text;
|
||||
|
||||
namespace NubLang.Generation;
|
||||
|
||||
internal class QBEWriter
|
||||
{
|
||||
private readonly StringBuilder _builder = new();
|
||||
|
||||
public void Indented(string value)
|
||||
{
|
||||
_builder.Append('\t');
|
||||
_builder.AppendLine(value);
|
||||
}
|
||||
|
||||
public void Comment(string comment)
|
||||
{
|
||||
_builder.AppendLine("# " + comment);
|
||||
}
|
||||
|
||||
public void WriteLine(string text)
|
||||
{
|
||||
_builder.AppendLine(text);
|
||||
}
|
||||
|
||||
public void Write(string text)
|
||||
{
|
||||
_builder.Append(text);
|
||||
}
|
||||
|
||||
public void NewLine()
|
||||
{
|
||||
_builder.AppendLine();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return _builder.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user