This commit is contained in:
2026-02-08 01:22:24 +01:00
parent 26d365cf4f
commit e77c7028b9
2 changed files with 41 additions and 100 deletions

View File

@@ -14,6 +14,12 @@ public sealed class Generator(List<NodeDefinition> nodes)
private string Emit() private string Emit()
{ {
writer.WriteLine(""" writer.WriteLine("""
#include <float.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
struct string { struct string {
const char *data; const char *data;
int length; int length;
@@ -59,6 +65,8 @@ public sealed class Generator(List<NodeDefinition> nodes)
case NodeStatementReturn statement: case NodeStatementReturn statement:
EmitStatementReturn(statement); EmitStatementReturn(statement);
break; break;
default:
throw new ArgumentOutOfRangeException(nameof(node), node, null);
} }
} }
@@ -91,155 +99,88 @@ public sealed class Generator(List<NodeDefinition> nodes)
{ {
return node switch return node switch
{ {
NodeExpressionBoolLiteral expression => EmitExpressionBoolLiteral(expression), NodeExpressionBoolLiteral expression => expression.Value.Value ? "true" : "false",
NodeExpressionIntLiteral expression => EmitExpressionIntLiteral(expression), NodeExpressionIntLiteral expression => expression.Value.Value.ToString(),
NodeExpressionStringLiteral expression => EmitExpressionStringLiteral(expression), NodeExpressionStringLiteral expression => $"(struct string){{ \"{expression.Value.Value}\", {expression.Value.Value.Length} }}",
NodeExpressionIdent expression => EmitExpressionIdent(expression), NodeExpressionIdent expression => expression.Value.Ident,
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
}; };
} }
private string EmitExpressionBoolLiteral(NodeExpressionBoolLiteral expression) private static string CType(NodeType node, string? varName = null)
{ {
return expression.Value.Value ? "1" : "0"; return node switch
}
private string EmitExpressionIntLiteral(NodeExpressionIntLiteral expression)
{ {
return expression.Value.Value.ToString(); NodeTypeVoid => "void" + (varName != null ? $" {varName}" : ""),
} NodeTypeBool => "bool" + (varName != null ? $" {varName}" : ""),
NodeTypeCustom type => $"struct {type}" + (varName != null ? $" {varName}" : ""),
private string EmitExpressionStringLiteral(NodeExpressionStringLiteral expression) NodeTypeSInt type => $"int{type.Width}_t" + (varName != null ? $" {varName}" : ""),
{ NodeTypeUInt type => $"uint{type.Width}_t" + (varName != null ? $" {varName}" : ""),
return $"(struct string){{ \"{expression.Value.Value}\", {expression.Value.Value.Length} }}"; NodeTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),
} NodeTypeString => "struct string" + (varName != null ? $" {varName}" : ""),
NodeTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})",
private string EmitExpressionIdent(NodeExpressionIdent expression) _ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
{
return expression.Value.Ident;
}
private static string CType(NodeType type, string? variableName = null)
{
return type switch
{
NodeTypeVoid => "void" + (variableName != null ? $" {variableName}" : ""),
NodeTypeBool => "bool" + (variableName != null ? $" {variableName}" : ""),
NodeTypeSInt intType => CTypeSInt(intType, variableName),
NodeTypeUInt intType => CTypeUInt(intType, variableName),
NodeTypePointer ptr => CType(ptr.To) + "*" + (variableName != null ? $" {variableName}" : ""),
NodeTypeString => "struct string" + (variableName != null ? $" {variableName}" : ""),
NodeTypeFunc fn => CTypeFunc(fn, variableName),
}; };
} }
private static string CTypeSInt(NodeTypeSInt intType, string? varName)
{
var cType = intType.Width switch
{
8 => "char",
16 => "short",
32 => "int",
64 => "long long",
};
return cType + (varName != null ? $" {varName}" : "");
}
private static string CTypeUInt(NodeTypeUInt intType, string? varName)
{
var cType = intType.Width switch
{
8 => "unsigned char",
16 => "unsigned short",
32 => "unsigned int",
64 => "unsigned long long",
};
return cType + (varName != null ? $" {varName}" : "");
}
private static string CTypeFunc(NodeTypeFunc fn, string? varName)
{
var returnType = CType(fn.ReturnType);
var parameters = string.Join(", ", fn.Parameters.Select(p => CType(p)));
if (string.IsNullOrEmpty(parameters))
{
parameters = "void";
}
if (varName != null)
{
return $"{returnType} (*{varName})({parameters})";
}
return $"{returnType} (*)({parameters})";
}
} }
internal class IndentedTextWriter internal class IndentedTextWriter
{ {
private readonly StringBuilder _builder = new(); private readonly StringBuilder builder = new();
private int _indentLevel; private int indentLevel;
public IDisposable Indent() public IDisposable Indent()
{ {
_indentLevel++; indentLevel++;
return new IndentScope(this); return new IndentScope(this);
} }
public void WriteLine(string text) public void WriteLine(string text)
{ {
WriteIndent(); WriteIndent();
_builder.AppendLine(text); builder.AppendLine(text);
} }
public void Write(string text) public void Write(string text)
{ {
WriteIndent(); WriteIndent();
_builder.Append(text); builder.Append(text);
} }
public void WriteLine() public void WriteLine()
{ {
_builder.AppendLine(); builder.AppendLine();
} }
public override string ToString() public override string ToString()
{ {
return _builder.ToString(); return builder.ToString();
} }
private void WriteIndent() private void WriteIndent()
{ {
if (_builder.Length > 0) if (builder.Length > 0)
{ {
var lastChar = _builder[^1]; var lastChar = builder[^1];
if (lastChar != '\n' && lastChar != '\r') if (lastChar != '\n' && lastChar != '\r')
return; return;
} }
for (var i = 0; i < _indentLevel; i++) for (var i = 0; i < indentLevel; i++)
{ {
_builder.Append(" "); builder.Append(" ");
} }
} }
private class IndentScope : IDisposable private class IndentScope(IndentedTextWriter writer) : IDisposable
{ {
private readonly IndentedTextWriter _writer; private bool disposed;
private bool _disposed;
public IndentScope(IndentedTextWriter writer)
{
_writer = writer;
}
public void Dispose() public void Dispose()
{ {
if (_disposed) return; if (disposed) return;
_writer._indentLevel--; writer.indentLevel--;
_disposed = true; disposed = true;
} }
} }
} }

View File

@@ -6,7 +6,7 @@ const string contents = """
return 69 return 69
} }
func do_something(text: string): func(i32 u32): void { func do_something(text: string): void {
} }
"""; """;