It works
This commit is contained in:
@@ -27,5 +27,13 @@ export func main(args: []cstring): i64 {
|
|||||||
|
|
||||||
y("proxy")
|
y("proxy")
|
||||||
|
|
||||||
|
func(){ c::puts("anon") }()
|
||||||
|
|
||||||
|
let z: func()
|
||||||
|
|
||||||
|
z = func() { c::puts("anon variable") }
|
||||||
|
|
||||||
|
z()
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public static class QBEGenerator
|
|||||||
private static DefinitionTable _definitionTable = null!;
|
private static DefinitionTable _definitionTable = null!;
|
||||||
|
|
||||||
private static StringBuilder _builder = new();
|
private static StringBuilder _builder = new();
|
||||||
private static List<string> _strings = [];
|
private static List<CStringLiteral> _cStringLiterals = [];
|
||||||
private static Stack<string> _breakLabels = [];
|
private static Stack<string> _breakLabels = [];
|
||||||
private static Stack<string> _continueLabels = [];
|
private static Stack<string> _continueLabels = [];
|
||||||
private static Queue<(AnonymousFuncNode Func, string Name)> _anonymousFunctions = [];
|
private static Queue<(AnonymousFuncNode Func, string Name)> _anonymousFunctions = [];
|
||||||
@@ -26,6 +26,7 @@ public static class QBEGenerator
|
|||||||
private static int _variableIndex;
|
private static int _variableIndex;
|
||||||
private static int _labelIndex;
|
private static int _labelIndex;
|
||||||
private static int _anonymousFuncIndex;
|
private static int _anonymousFuncIndex;
|
||||||
|
private static int _cStringLiteralIndex;
|
||||||
private static bool _codeIsReachable = true;
|
private static bool _codeIsReachable = true;
|
||||||
|
|
||||||
public static string Emit(CompilationUnit compilationUnit, DefinitionTable definitionTable)
|
public static string Emit(CompilationUnit compilationUnit, DefinitionTable definitionTable)
|
||||||
@@ -34,7 +35,7 @@ public static class QBEGenerator
|
|||||||
_definitionTable = definitionTable;
|
_definitionTable = definitionTable;
|
||||||
|
|
||||||
_builder = new StringBuilder();
|
_builder = new StringBuilder();
|
||||||
_strings = [];
|
_cStringLiterals = [];
|
||||||
_breakLabels = [];
|
_breakLabels = [];
|
||||||
_continueLabels = [];
|
_continueLabels = [];
|
||||||
_anonymousFunctions = [];
|
_anonymousFunctions = [];
|
||||||
@@ -43,6 +44,7 @@ public static class QBEGenerator
|
|||||||
_variableIndex = 0;
|
_variableIndex = 0;
|
||||||
_labelIndex = 0;
|
_labelIndex = 0;
|
||||||
_anonymousFuncIndex = 0;
|
_anonymousFuncIndex = 0;
|
||||||
|
_cStringLiteralIndex = 0;
|
||||||
_codeIsReachable = true;
|
_codeIsReachable = true;
|
||||||
|
|
||||||
foreach (var structDef in _definitionTable.GetStructs())
|
foreach (var structDef in _definitionTable.GetStructs())
|
||||||
@@ -63,9 +65,9 @@ public static class QBEGenerator
|
|||||||
_builder.AppendLine();
|
_builder.AppendLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < _strings.Count; i++)
|
foreach (var cStringLiteral in _cStringLiterals)
|
||||||
{
|
{
|
||||||
_builder.AppendLine($"data $string{i + 1} = {{ b \"{_strings[i]}\", b 0 }}");
|
_builder.AppendLine($"data {cStringLiteral.Name} = {{ b \"{cStringLiteral.Value}\", b 0 }}");
|
||||||
}
|
}
|
||||||
|
|
||||||
return _builder.ToString();
|
return _builder.ToString();
|
||||||
@@ -81,6 +83,11 @@ public static class QBEGenerator
|
|||||||
return $"@l{++_labelIndex}";
|
return $"@l{++_labelIndex}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string CStringName()
|
||||||
|
{
|
||||||
|
return $"$cstring{++_cStringLiteralIndex}";
|
||||||
|
}
|
||||||
|
|
||||||
private static string FuncName(IFuncSignature funcDef)
|
private static string FuncName(IFuncSignature funcDef)
|
||||||
{
|
{
|
||||||
return funcDef switch
|
return funcDef switch
|
||||||
@@ -1077,8 +1084,9 @@ public static class QBEGenerator
|
|||||||
switch (literal.Kind)
|
switch (literal.Kind)
|
||||||
{
|
{
|
||||||
case LiteralKind.String:
|
case LiteralKind.String:
|
||||||
_strings.Add(literal.Literal);
|
var cStringLiteral = new CStringLiteral(literal.Literal, CStringName());
|
||||||
return new Ident($"$string{_strings.Count}", literal.Type, IdentKind.Literal);
|
_cStringLiterals.Add(cStringLiteral);
|
||||||
|
return new Ident(cStringLiteral.Name, literal.Type, IdentKind.Literal);
|
||||||
case LiteralKind.Bool:
|
case LiteralKind.Bool:
|
||||||
return new Ident(bool.Parse(literal.Literal) ? "1" : "0", literal.Type, IdentKind.Literal);
|
return new Ident(bool.Parse(literal.Literal) ? "1" : "0", literal.Type, IdentKind.Literal);
|
||||||
default:
|
default:
|
||||||
@@ -1319,6 +1327,12 @@ public static class QBEGenerator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal class CStringLiteral(string value, string name)
|
||||||
|
{
|
||||||
|
public string Value { get; } = value;
|
||||||
|
public string Name { get; } = name;
|
||||||
|
}
|
||||||
|
|
||||||
internal class Variable(string name, Ident ident)
|
internal class Variable(string name, Ident ident)
|
||||||
{
|
{
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
|
|||||||
Reference in New Issue
Block a user