tmp...
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using System.Security.Principal;
|
||||
using System.Text;
|
||||
using Microsoft.VisualBasic;
|
||||
|
||||
namespace Compiler;
|
||||
|
||||
@@ -36,8 +34,24 @@ public class Generator
|
||||
writer.WriteLine($$"""
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
return {{entryPoint}}();
|
||||
struct nub_dynamic_array_52747d8c11b7118c args = (struct nub_dynamic_array_52747d8c11b7118c){0};
|
||||
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
auto nubstring = nub_core_string_from_cstr(argv[i]);
|
||||
da_append(&args, nubstring);
|
||||
}
|
||||
|
||||
int returnValue = {{entryPoint}}(args);
|
||||
|
||||
for (int i = 0; i < args.count; ++i)
|
||||
{
|
||||
nub_core_string_rc_dec(args.items[i]);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
""");
|
||||
|
||||
writer.WriteLine();
|
||||
@@ -104,50 +118,6 @@ public class Generator
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
""");
|
||||
|
||||
while (referencedTypes.Count != 0)
|
||||
{
|
||||
var type = referencedTypes.ElementAt(0);
|
||||
EmitTypeDefinitionIfNotEmitted(type);
|
||||
referencedTypes.Remove(type);
|
||||
}
|
||||
|
||||
foreach (var (name, value) in referencedStringLiterals)
|
||||
{
|
||||
writer.WriteLine
|
||||
(
|
||||
$$"""
|
||||
static struct nub_core_string {{name}} = {
|
||||
.data = "{{value}}",
|
||||
.length = {{value.Length}},
|
||||
.ref = 0,
|
||||
.flags = FLAG_STRING_LITERAL
|
||||
};
|
||||
|
||||
"""
|
||||
);
|
||||
}
|
||||
|
||||
var header = writer.ToString();
|
||||
|
||||
return $"{header}\n{declarations}\n{implementations}";
|
||||
}
|
||||
|
||||
private void EmitTypeDefinitionIfNotEmitted(NubType type)
|
||||
{
|
||||
if (emittedTypes.Contains(type))
|
||||
return;
|
||||
|
||||
emittedTypes.Add(type);
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case NubTypeString:
|
||||
{
|
||||
writer.WriteLine
|
||||
(
|
||||
"""
|
||||
#define FLAG_STRING_LITERAL 1
|
||||
#define STRING_DEBUG 1
|
||||
|
||||
@@ -276,9 +246,69 @@ public class Generator
|
||||
return result;
|
||||
}
|
||||
|
||||
#define da_append(xs, x) \
|
||||
do { \
|
||||
if ((xs)->count >= (xs)->capacity) { \
|
||||
if ((xs)->capacity == 0) (xs)->capacity = 256; \
|
||||
else (xs)->capacity *= 2; \
|
||||
(xs)->items = realloc((xs)->items, (xs)->capacity*sizeof(*(xs)->items)); \
|
||||
} \
|
||||
(xs)->items[(xs)->count++] = (x); \
|
||||
} while (0)
|
||||
""");
|
||||
|
||||
while (referencedTypes.Count != 0)
|
||||
{
|
||||
var type = referencedTypes.ElementAt(0);
|
||||
EmitTypeDefinitionIfNotEmitted(type);
|
||||
referencedTypes.Remove(type);
|
||||
}
|
||||
|
||||
foreach (var (name, value) in referencedStringLiterals)
|
||||
{
|
||||
writer.WriteLine
|
||||
(
|
||||
$$"""
|
||||
static struct nub_core_string {{name}} = {
|
||||
.data = "{{value}}",
|
||||
.length = {{value.Length}},
|
||||
.ref = 0,
|
||||
.flags = FLAG_STRING_LITERAL
|
||||
};
|
||||
|
||||
"""
|
||||
);
|
||||
}
|
||||
|
||||
var header = writer.ToString();
|
||||
|
||||
return $"{header}\n{declarations}\n{implementations}";
|
||||
}
|
||||
|
||||
private void EmitTypeDefinitionIfNotEmitted(NubType type)
|
||||
{
|
||||
if (emittedTypes.Contains(type))
|
||||
return;
|
||||
|
||||
emittedTypes.Add(type);
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case NubTypeArray arrayType:
|
||||
{
|
||||
var name = NameMangler.Mangle("dynamic", "array", arrayType);
|
||||
|
||||
writer.WriteLine
|
||||
(
|
||||
$$"""
|
||||
struct {{name}}
|
||||
{
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
{{CType(arrayType.ElementType)}} *items;
|
||||
};
|
||||
"""
|
||||
);
|
||||
break;
|
||||
}
|
||||
case NubTypeStruct structType:
|
||||
@@ -563,6 +593,8 @@ public class Generator
|
||||
TypedNodeExpressionStructMemberAccess expression => EmitExpressionMemberAccess(expression),
|
||||
TypedNodeExpressionStringLength expression => EmitExpressionStringLength(expression),
|
||||
TypedNodeExpressionStringPointer expression => EmitExpressionStringPointer(expression),
|
||||
TypedNodeExpressionArrayCount expression => EmitExpressionArrayCount(expression),
|
||||
TypedNodeExpressionArrayPointer expression => EmitExpressionArrayPointer(expression),
|
||||
TypedNodeExpressionLocalIdent expression => expression.Name,
|
||||
TypedNodeExpressionGlobalIdent expression => EmitNodeExpressionGlobalIdent(expression),
|
||||
TypedNodeExpressionFuncCall expression => EmitExpressionFuncCall(expression),
|
||||
@@ -712,6 +744,18 @@ public class Generator
|
||||
return $"{target}->data";
|
||||
}
|
||||
|
||||
private string EmitExpressionArrayCount(TypedNodeExpressionArrayCount expression)
|
||||
{
|
||||
var target = EmitExpression(expression.Target);
|
||||
return $"{target}.count";
|
||||
}
|
||||
|
||||
private string EmitExpressionArrayPointer(TypedNodeExpressionArrayPointer expression)
|
||||
{
|
||||
var target = EmitExpression(expression.Target);
|
||||
return $"{target}.items";
|
||||
}
|
||||
|
||||
private string EmitNodeExpressionGlobalIdent(TypedNodeExpressionGlobalIdent expression)
|
||||
{
|
||||
if (!moduleGraph.TryResolveIdentifier(expression.Module, expression.Name, true, out var info))
|
||||
@@ -747,6 +791,7 @@ public class Generator
|
||||
NubTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),
|
||||
NubTypeString type => "struct nub_core_string" + (varName != null ? $" *{varName}" : "*"),
|
||||
NubTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})",
|
||||
NubTypeArray type => $"struct {NameMangler.Mangle("dynamic", "array", type)}" + (varName != null ? $" {varName}" : ""),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -211,6 +211,7 @@ public class ModuleGraph
|
||||
NodeTypeUInt type => NubTypeUInt.Get(type.Width),
|
||||
NodeTypeString => NubTypeString.Instance,
|
||||
NodeTypeVoid => NubTypeVoid.Instance,
|
||||
NodeTypeArray type => NubTypeArray.Get(ResolveType(type.ElementType, currentModule)),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -256,6 +256,28 @@ public class NubTypeFunc : NubType
|
||||
private record Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType);
|
||||
}
|
||||
|
||||
public class NubTypeArray : NubType
|
||||
{
|
||||
private static readonly Dictionary<NubType, NubTypeArray> Cache = new();
|
||||
|
||||
public static NubTypeArray Get(NubType to)
|
||||
{
|
||||
if (!Cache.TryGetValue(to, out var ptr))
|
||||
Cache[to] = ptr = new NubTypeArray(to);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
public NubType ElementType { get; }
|
||||
|
||||
private NubTypeArray(NubType elementType)
|
||||
{
|
||||
ElementType = elementType;
|
||||
}
|
||||
|
||||
public override string ToString() => $"[]{ElementType}";
|
||||
}
|
||||
|
||||
public class TypeEncoder
|
||||
{
|
||||
public static string Encode(NubType type)
|
||||
@@ -355,6 +377,12 @@ public class TypeEncoder
|
||||
sb.Append(')');
|
||||
break;
|
||||
|
||||
case NubTypeArray a:
|
||||
sb.Append("A(");
|
||||
EncodeType(sb, a.ElementType);
|
||||
sb.Append(')');
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotSupportedException(type.GetType().Name);
|
||||
}
|
||||
@@ -396,6 +424,7 @@ public class TypeDecoder
|
||||
'F' => DecodeFunc(),
|
||||
'T' => DecodeStruct(),
|
||||
'E' => DecodeEnum(),
|
||||
'A' => DecodeArray(),
|
||||
_ => throw new Exception($"'{start}' is not a valid start to a type")
|
||||
};
|
||||
}
|
||||
@@ -531,6 +560,14 @@ public class TypeDecoder
|
||||
throw new Exception($"Expected 'V' or 'N'");
|
||||
}
|
||||
|
||||
private NubTypeArray DecodeArray()
|
||||
{
|
||||
Expect('(');
|
||||
var elementType = DecodeType();
|
||||
Expect(')');
|
||||
return NubTypeArray.Get(elementType);
|
||||
}
|
||||
|
||||
private bool TryPeek(out char c)
|
||||
{
|
||||
if (index >= encoded.Length)
|
||||
|
||||
@@ -524,6 +524,13 @@ public class Parser
|
||||
return new NodeTypeAnonymousStruct(TokensFrom(startIndex), fields);
|
||||
}
|
||||
|
||||
if (TryExpectSymbol(Symbol.OpenSquare))
|
||||
{
|
||||
ExpectSymbol(Symbol.CloseSquare);
|
||||
var elementType = ParseType();
|
||||
return new NodeTypeArray(TokensFrom(startIndex), elementType);
|
||||
}
|
||||
|
||||
if (TryExpectIdent(out var ident))
|
||||
{
|
||||
switch (ident.Ident)
|
||||
@@ -1035,6 +1042,11 @@ public class NodeTypeAnonymousStruct(List<Token> tokens, List<NodeTypeAnonymousS
|
||||
}
|
||||
}
|
||||
|
||||
public class NodeTypeArray(List<Token> tokens, NodeType elementType) : NodeType(tokens)
|
||||
{
|
||||
public NodeType ElementType { get; } = elementType;
|
||||
}
|
||||
|
||||
public class NodeTypePointer(List<Token> tokens, NodeType to) : NodeType(tokens)
|
||||
{
|
||||
public NodeType To { get; } = to;
|
||||
|
||||
@@ -144,9 +144,15 @@ if (!compileLib)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (entryPointType.Parameters.Any())
|
||||
if (entryPointType.Parameters.Count != 1)
|
||||
{
|
||||
DiagnosticFormatter.Print(Diagnostic.Error($"Entrypoint must not take any parameters").Build(), Console.Error);
|
||||
DiagnosticFormatter.Print(Diagnostic.Error($"Entrypoint must take exaxtly one parameter").Build(), Console.Error);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (entryPointType.Parameters[0] is not NubTypeArray { ElementType: NubTypeString })
|
||||
{
|
||||
DiagnosticFormatter.Print(Diagnostic.Error($"First parameter of entrypoint must be a string array").Build(), Console.Error);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -203,6 +203,16 @@ public class Tokenizer
|
||||
Consume();
|
||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseCurly);
|
||||
}
|
||||
case '[':
|
||||
{
|
||||
Consume();
|
||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.OpenSquare);
|
||||
}
|
||||
case ']':
|
||||
{
|
||||
Consume();
|
||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseSquare);
|
||||
}
|
||||
case '(':
|
||||
{
|
||||
Consume();
|
||||
@@ -497,6 +507,8 @@ public enum Symbol
|
||||
CloseCurly,
|
||||
OpenParen,
|
||||
CloseParen,
|
||||
OpenSquare,
|
||||
CloseSquare,
|
||||
Comma,
|
||||
Period,
|
||||
Colon,
|
||||
@@ -566,6 +578,8 @@ public static class TokenExtensions
|
||||
Symbol.CloseCurly => "}",
|
||||
Symbol.OpenParen => "(",
|
||||
Symbol.CloseParen => ")",
|
||||
Symbol.OpenSquare => "[",
|
||||
Symbol.CloseSquare => "]",
|
||||
Symbol.Comma => ",",
|
||||
Symbol.Period => ".",
|
||||
Symbol.Colon => ":",
|
||||
|
||||
@@ -478,7 +478,19 @@ public class TypeChecker
|
||||
case "ptr":
|
||||
return new TypedNodeExpressionStringPointer(expression.Tokens, NubTypePointer.Get(NubTypeUInt.Get(8)), target);
|
||||
default:
|
||||
throw BasicError($"'{expression.Name.Ident}' is not a member of type string", expression.Name);
|
||||
throw BasicError($"'{expression.Name.Ident}' is not a member of type {stringType}", expression.Name);
|
||||
}
|
||||
}
|
||||
case NubTypeArray arrayType:
|
||||
{
|
||||
switch (expression.Name.Ident)
|
||||
{
|
||||
case "count":
|
||||
return new TypedNodeExpressionArrayCount(expression.Tokens, NubTypeUInt.Get(64), target);
|
||||
case "ptr":
|
||||
return new TypedNodeExpressionArrayPointer(expression.Tokens, NubTypePointer.Get(arrayType.ElementType), target);
|
||||
default:
|
||||
throw BasicError($"'{expression.Name.Ident}' is not a member of type {arrayType}", expression.Name);
|
||||
}
|
||||
}
|
||||
case NubTypeStruct structType:
|
||||
@@ -676,6 +688,7 @@ public class TypeChecker
|
||||
NodeTypeUInt type => NubTypeUInt.Get(type.Width),
|
||||
NodeTypeString => NubTypeString.Instance,
|
||||
NodeTypeVoid => NubTypeVoid.Instance,
|
||||
NodeTypeArray type => NubTypeArray.Get(ResolveType(type.ElementType)),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
@@ -965,6 +978,16 @@ public class TypedNodeExpressionStringPointer(List<Token> tokens, NubType type,
|
||||
public TypedNodeExpression Target { get; } = target;
|
||||
}
|
||||
|
||||
public class TypedNodeExpressionArrayCount(List<Token> tokens, NubType type, TypedNodeExpression target) : TypedNodeExpression(tokens, type)
|
||||
{
|
||||
public TypedNodeExpression Target { get; } = target;
|
||||
}
|
||||
|
||||
public class TypedNodeExpressionArrayPointer(List<Token> tokens, NubType type, TypedNodeExpression target) : TypedNodeExpression(tokens, type)
|
||||
{
|
||||
public TypedNodeExpression Target { get; } = target;
|
||||
}
|
||||
|
||||
public class TypedNodeExpressionFuncCall(List<Token> tokens, NubType type, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeExpression(tokens, type)
|
||||
{
|
||||
public TypedNodeExpression Target { get; } = target;
|
||||
|
||||
@@ -10,25 +10,12 @@ enum Message {
|
||||
Say: string
|
||||
}
|
||||
|
||||
func main(): i32 {
|
||||
core::println("Hello, world!")
|
||||
core::println("Hello" + "World")
|
||||
|
||||
let message = getMessage()
|
||||
|
||||
let newStr = new string("cstring".ptr)
|
||||
|
||||
core::println(newStr)
|
||||
|
||||
match message {
|
||||
Quit {
|
||||
core::println("quit")
|
||||
func main(args: []string): i32 {
|
||||
let i = 0
|
||||
while i < args.count {
|
||||
// core::print(args[i])
|
||||
i = i + 1
|
||||
}
|
||||
Say msg {
|
||||
core::println(msg)
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user