Compare commits
28 Commits
a5f73a84cc
...
llvm
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1923ed5a78 | ||
|
|
1dd14c8a77 | ||
|
|
17e754fc6e | ||
|
|
c3d64c4ea9 | ||
|
|
d3822bc9b4 | ||
|
|
36622755a9 | ||
|
|
47fef6bc9f | ||
|
|
7d49bf43b7 | ||
|
|
7ce451768d | ||
|
|
f231a45285 | ||
|
|
085f7a1a6a | ||
|
|
40d500fddd | ||
|
|
7c7624b1bc | ||
|
|
031b118a24 | ||
|
|
c764857561 | ||
|
|
4f724ddc0c | ||
|
|
bf4c8838c6 | ||
|
|
34a44f80a8 | ||
|
|
640bd8c573 | ||
|
|
560e6428ff | ||
|
|
27bc4da4fd | ||
|
|
d11df414ad | ||
|
|
3febaaea81 | ||
|
|
828e20ddb6 | ||
|
|
396ddf93a2 | ||
|
|
3f18aa4782 | ||
|
|
3505e2547a | ||
|
|
766ca9a6b9 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.vscode
|
||||||
@@ -24,9 +24,6 @@ def map_type(clang_type: Type):
|
|||||||
if not decl.is_definition():
|
if not decl.is_definition():
|
||||||
return "^void"
|
return "^void"
|
||||||
|
|
||||||
if pointee.kind == TypeKind.CHAR_S or pointee.kind == TypeKind.CHAR_U:
|
|
||||||
return "cstring"
|
|
||||||
|
|
||||||
if pointee.kind == TypeKind.FUNCTIONPROTO:
|
if pointee.kind == TypeKind.FUNCTIONPROTO:
|
||||||
arg_types = []
|
arg_types = []
|
||||||
|
|
||||||
@@ -109,10 +106,11 @@ if tu.diagnostics:
|
|||||||
if diag.severity >= clang.cindex.Diagnostic.Error:
|
if diag.severity >= clang.cindex.Diagnostic.Error:
|
||||||
print(f"Error: {diag.spelling}", file=sys.stderr)
|
print(f"Error: {diag.spelling}", file=sys.stderr)
|
||||||
|
|
||||||
print(f'module "{os.path.basename(filename).split(".")[0]}"')
|
print(f'module {os.path.basename(filename).split(".")[0]}')
|
||||||
print()
|
print()
|
||||||
|
|
||||||
seen_structs = []
|
seen_structs = []
|
||||||
|
seen_enums = []
|
||||||
|
|
||||||
for cursor in tu.cursor.walk_preorder():
|
for cursor in tu.cursor.walk_preorder():
|
||||||
if cursor.location.file and cursor.location.file.name != filename:
|
if cursor.location.file and cursor.location.file.name != filename:
|
||||||
@@ -154,6 +152,12 @@ for cursor in tu.cursor.walk_preorder():
|
|||||||
print("}")
|
print("}")
|
||||||
|
|
||||||
elif cursor.kind == CursorKind.ENUM_DECL:
|
elif cursor.kind == CursorKind.ENUM_DECL:
|
||||||
|
if cursor.get_usr() in seen_enums:
|
||||||
|
continue
|
||||||
|
|
||||||
|
seen_enums.append(cursor.get_usr())
|
||||||
|
|
||||||
|
if cursor.is_definition():
|
||||||
name = cursor.spelling
|
name = cursor.spelling
|
||||||
print(f"export enum {name} : u32")
|
print(f"export enum {name} : u32")
|
||||||
print("{")
|
print("{")
|
||||||
|
|||||||
@@ -1,32 +1,57 @@
|
|||||||
using System.Diagnostics;
|
using NubLang.Ast;
|
||||||
using NubLang.Ast;
|
|
||||||
using NubLang.Diagnostics;
|
using NubLang.Diagnostics;
|
||||||
using NubLang.Generation;
|
using NubLang.Generation;
|
||||||
|
using NubLang.Modules;
|
||||||
using NubLang.Syntax;
|
using NubLang.Syntax;
|
||||||
|
|
||||||
var diagnostics = new List<Diagnostic>();
|
var diagnostics = new List<Diagnostic>();
|
||||||
var syntaxTrees = new List<SyntaxTree>();
|
var syntaxTrees = new List<SyntaxTree>();
|
||||||
|
|
||||||
|
var tokenizer = new Tokenizer();
|
||||||
|
var parser = new Parser();
|
||||||
|
var generator = new LlvmSharpGenerator();
|
||||||
|
|
||||||
foreach (var file in args)
|
foreach (var file in args)
|
||||||
{
|
{
|
||||||
var tokenizer = new Tokenizer(file, File.ReadAllText(file));
|
var tokens = tokenizer.Tokenize(file, File.ReadAllText(file));
|
||||||
tokenizer.Tokenize();
|
|
||||||
diagnostics.AddRange(tokenizer.Diagnostics);
|
diagnostics.AddRange(tokenizer.Diagnostics);
|
||||||
|
|
||||||
var parser = new Parser();
|
var syntaxTree = parser.Parse(tokens);
|
||||||
var syntaxTree = parser.Parse(tokenizer.Tokens);
|
|
||||||
diagnostics.AddRange(parser.Diagnostics);
|
diagnostics.AddRange(parser.Diagnostics);
|
||||||
|
|
||||||
syntaxTrees.Add(syntaxTree);
|
syntaxTrees.Add(syntaxTree);
|
||||||
}
|
}
|
||||||
|
|
||||||
var modules = Module.Collect(syntaxTrees);
|
foreach (var diagnostic in diagnostics)
|
||||||
var compilationUnits = new List<CompilationUnit>();
|
{
|
||||||
|
Console.Error.WriteLine(diagnostic.FormatANSI());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (diagnostics.Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnostics.Clear();
|
||||||
|
|
||||||
|
ModuleRepository moduleRepository;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
moduleRepository = ModuleRepository.Create(syntaxTrees);
|
||||||
|
}
|
||||||
|
catch (CompileException e)
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine(e.Diagnostic.FormatANSI());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var compilationUnits = new List<List<TopLevelNode>>();
|
||||||
|
|
||||||
for (var i = 0; i < args.Length; i++)
|
for (var i = 0; i < args.Length; i++)
|
||||||
{
|
{
|
||||||
var typeChecker = new TypeChecker(syntaxTrees[i], modules);
|
var typeChecker = new TypeChecker();
|
||||||
var compilationUnit = typeChecker.Check();
|
var compilationUnit = typeChecker.Check(syntaxTrees[i], moduleRepository);
|
||||||
|
|
||||||
compilationUnits.Add(compilationUnit);
|
compilationUnits.Add(compilationUnit);
|
||||||
diagnostics.AddRange(typeChecker.Diagnostics);
|
diagnostics.AddRange(typeChecker.Diagnostics);
|
||||||
@@ -42,7 +67,7 @@ if (diagnostics.Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Erro
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var cPaths = new List<string>();
|
diagnostics.Clear();
|
||||||
|
|
||||||
Directory.CreateDirectory(".build");
|
Directory.CreateDirectory(".build");
|
||||||
|
|
||||||
@@ -51,41 +76,14 @@ for (var i = 0; i < args.Length; i++)
|
|||||||
var file = args[i];
|
var file = args[i];
|
||||||
var compilationUnit = compilationUnits[i];
|
var compilationUnit = compilationUnits[i];
|
||||||
|
|
||||||
var generator = new Generator(compilationUnit);
|
|
||||||
var directory = Path.GetDirectoryName(file);
|
var directory = Path.GetDirectoryName(file);
|
||||||
if (!string.IsNullOrWhiteSpace(directory))
|
if (!string.IsNullOrWhiteSpace(directory))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(Path.Combine(".build", directory));
|
Directory.CreateDirectory(Path.Combine(".build", directory));
|
||||||
}
|
}
|
||||||
|
|
||||||
var path = Path.Combine(".build", Path.ChangeExtension(file, "c"));
|
var path = Path.Combine(".build", Path.ChangeExtension(file, "ll"));
|
||||||
File.WriteAllText(path, generator.Emit());
|
generator.Emit(compilationUnit, moduleRepository, file, path);
|
||||||
cPaths.Add(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var objectPaths = new List<string>();
|
|
||||||
|
|
||||||
foreach (var cPath in cPaths)
|
|
||||||
{
|
|
||||||
var objectPath = Path.ChangeExtension(cPath, "o");
|
|
||||||
using var compileProcess = Process.Start("clang", [
|
|
||||||
"-ffreestanding", "-std=c23",
|
|
||||||
"-g", "-c",
|
|
||||||
"-o", objectPath,
|
|
||||||
cPath,
|
|
||||||
]);
|
|
||||||
|
|
||||||
compileProcess.WaitForExit();
|
|
||||||
|
|
||||||
if (compileProcess.ExitCode != 0)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine($"clang failed with exit code {compileProcess.ExitCode}");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
objectPaths.Add(objectPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.Out.WriteLine(string.Join(' ', objectPaths));
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -16,25 +16,26 @@ public static class AstExtensions
|
|||||||
|
|
||||||
return new Location
|
return new Location
|
||||||
{
|
{
|
||||||
Uri = node.Tokens.First().Span.FilePath,
|
Uri = node.Tokens.First().Span.SourcePath,
|
||||||
Range = new Range(node.Tokens.First().Span.Start.Line - 1, node.Tokens.First().Span.Start.Column - 1, node.Tokens.Last().Span.End.Line - 1, node.Tokens.Last().Span.End.Column - 1)
|
Range = new Range(node.Tokens.First().Span.StartLine - 1, node.Tokens.First().Span.StartColumn - 1, node.Tokens.Last().Span.EndLine - 1, node.Tokens.Last().Span.EndColumn - 1)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool ContainsPosition(this Node node, int line, int character)
|
public static Location ToLocation(this Token token)
|
||||||
{
|
{
|
||||||
if (node.Tokens.Count == 0)
|
return new Location
|
||||||
{
|
{
|
||||||
return false;
|
Uri = token.Span.SourcePath,
|
||||||
|
Range = new Range(token.Span.StartLine - 1, token.Span.StartColumn - 1, token.Span.EndLine - 1, token.Span.EndColumn - 1)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var start = node.Tokens.First().Span.Start;
|
public static bool ContainsPosition(this Token token, int line, int character)
|
||||||
var end = node.Tokens.Last().Span.End;
|
{
|
||||||
|
var startLine = token.Span.StartLine - 1;
|
||||||
var startLine = start.Line - 1;
|
var startChar = token.Span.StartColumn - 1;
|
||||||
var startChar = start.Column - 1;
|
var endLine = token.Span.EndLine - 1;
|
||||||
var endLine = end.Line - 1;
|
var endChar = token.Span.EndColumn - 1;
|
||||||
var endChar = end.Column - 1;
|
|
||||||
|
|
||||||
if (line < startLine || line > endLine) return false;
|
if (line < startLine || line > endLine) return false;
|
||||||
|
|
||||||
@@ -58,20 +59,56 @@ public static class AstExtensions
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FuncNode? FunctionAtPosition(this CompilationUnit compilationUnit, int line, int character)
|
public static bool ContainsPosition(this Node node, int line, int character)
|
||||||
|
{
|
||||||
|
if (node.Tokens.Count == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var span = node.Tokens.First().Span;
|
||||||
|
|
||||||
|
var startLine = span.StartLine - 1;
|
||||||
|
var startChar = span.StartColumn - 1;
|
||||||
|
var endLine = span.EndLine - 1;
|
||||||
|
var endChar = span.EndColumn - 1;
|
||||||
|
|
||||||
|
if (line < startLine || line > endLine) return false;
|
||||||
|
|
||||||
|
if (line > startLine && line < endLine) return true;
|
||||||
|
|
||||||
|
if (startLine == endLine)
|
||||||
|
{
|
||||||
|
return character >= startChar && character <= endChar;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line == startLine)
|
||||||
|
{
|
||||||
|
return character >= startChar;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line == endLine)
|
||||||
|
{
|
||||||
|
return character <= endChar;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FuncNode? FunctionAtPosition(this List<TopLevelNode> compilationUnit, int line, int character)
|
||||||
{
|
{
|
||||||
return compilationUnit
|
return compilationUnit
|
||||||
.Functions
|
.OfType<FuncNode>()
|
||||||
.FirstOrDefault(x => x.ContainsPosition(line, character));
|
.FirstOrDefault(x => x.ContainsPosition(line, character));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Node? DeepestNodeAtPosition(this CompilationUnit compilationUnit, int line, int character)
|
public static Node? DeepestNodeAtPosition(this List<TopLevelNode> compilationUnit, int line, int character)
|
||||||
{
|
{
|
||||||
return compilationUnit.Functions
|
return compilationUnit
|
||||||
.SelectMany(x => x.DescendantsAndSelf())
|
.SelectMany(x => x.DescendantsAndSelf())
|
||||||
.Where(n => n.ContainsPosition(line, character))
|
.Where(n => n.ContainsPosition(line, character))
|
||||||
.OrderBy(n => n.Tokens.First().Span.Start.Line)
|
.OrderBy(n => n.Tokens.First().Span.StartLine)
|
||||||
.ThenBy(n => n.Tokens.First().Span.Start.Column)
|
.ThenBy(n => n.Tokens.First().Span.StartColumn)
|
||||||
.LastOrDefault();
|
.LastOrDefault();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using NubLang.Ast;
|
using NubLang.Ast;
|
||||||
|
using NubLang.Modules;
|
||||||
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
|
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
|
||||||
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
|
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
|
||||||
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
|
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
|
||||||
@@ -29,13 +30,6 @@ internal class CompletionHandler(WorkspaceManager workspaceManager) : Completion
|
|||||||
Label = "module",
|
Label = "module",
|
||||||
InsertTextFormat = InsertTextFormat.Snippet,
|
InsertTextFormat = InsertTextFormat.Snippet,
|
||||||
InsertText = "module \"$0\"",
|
InsertText = "module \"$0\"",
|
||||||
},
|
|
||||||
new()
|
|
||||||
{
|
|
||||||
Kind = CompletionItemKind.Keyword,
|
|
||||||
Label = "import",
|
|
||||||
InsertTextFormat = InsertTextFormat.Snippet,
|
|
||||||
InsertText = "import \"$0\"",
|
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -112,41 +106,56 @@ internal class CompletionHandler(WorkspaceManager workspaceManager) : Completion
|
|||||||
private CompletionList HandleSync(CompletionParams request, CancellationToken cancellationToken)
|
private CompletionList HandleSync(CompletionParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var completions = new List<CompletionItem>();
|
var completions = new List<CompletionItem>();
|
||||||
var position = request.Position;
|
|
||||||
|
|
||||||
var uri = request.TextDocument.Uri;
|
var compilationUnit = workspaceManager.GetTopLevelNodes(request.TextDocument.Uri.GetFileSystemPath());
|
||||||
var compilationUnit = workspaceManager.GetCompilationUnit(uri);
|
|
||||||
if (compilationUnit != null)
|
var repository = workspaceManager.GetModuleRepository();
|
||||||
{
|
|
||||||
var function = compilationUnit.Functions.FirstOrDefault(x => x.Body != null && x.Body.ContainsPosition(position.Line, position.Character));
|
var function = compilationUnit
|
||||||
|
.OfType<FuncNode>()
|
||||||
|
.FirstOrDefault(x => x.Body != null && x.Body.ContainsPosition(request.Position.Line, request.Position.Character));
|
||||||
|
|
||||||
if (function != null)
|
if (function != null)
|
||||||
{
|
{
|
||||||
completions.AddRange(_statementSnippets);
|
completions.AddRange(_statementSnippets);
|
||||||
|
|
||||||
foreach (var prototype in compilationUnit.ImportedFunctions)
|
foreach (var module in repository.GetAll())
|
||||||
|
{
|
||||||
|
foreach (var prototype in module.FunctionPrototypes)
|
||||||
{
|
{
|
||||||
var parameterStrings = new List<string>();
|
var parameterStrings = new List<string>();
|
||||||
foreach (var (index, parameter) in prototype.Parameters.Index())
|
foreach (var (index, parameter) in prototype.Parameters.Index())
|
||||||
{
|
{
|
||||||
parameterStrings.AddRange($"${{{index + 1}:{parameter.Name}}}");
|
parameterStrings.AddRange($"${{{index + 1}:{parameter.NameToken.Value}}}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var isCurrentModule = false;
|
||||||
|
var moduleDecl = compilationUnit.OfType<ModuleNode>().FirstOrDefault();
|
||||||
|
if (moduleDecl != null)
|
||||||
|
{
|
||||||
|
if (moduleDecl.NameToken.Value == module.Name)
|
||||||
|
{
|
||||||
|
isCurrentModule = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
completions.Add(new CompletionItem
|
completions.Add(new CompletionItem
|
||||||
{
|
{
|
||||||
Kind = CompletionItemKind.Function,
|
Kind = CompletionItemKind.Function,
|
||||||
Label = $"{prototype.Module}::{prototype.Name}",
|
Label = isCurrentModule ? prototype.NameToken.Value : $"{module.Name}::{prototype.NameToken.Value}",
|
||||||
InsertTextFormat = InsertTextFormat.Snippet,
|
InsertTextFormat = InsertTextFormat.Snippet,
|
||||||
InsertText = $"{prototype.Module}::{prototype.Name}({string.Join(", ", parameterStrings)})",
|
InsertText = $"{(isCurrentModule ? "" : $"{module.Name}::")}{prototype.NameToken.Value}({string.Join(", ", parameterStrings)})",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var parameter in function.Prototype.Parameters)
|
foreach (var parameter in function.Prototype.Parameters)
|
||||||
{
|
{
|
||||||
completions.Add(new CompletionItem
|
completions.Add(new CompletionItem
|
||||||
{
|
{
|
||||||
Kind = CompletionItemKind.Variable,
|
Kind = CompletionItemKind.Variable,
|
||||||
Label = parameter.Name,
|
Label = parameter.NameToken.Value,
|
||||||
InsertText = parameter.Name,
|
InsertText = parameter.NameToken.Value,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,8 +168,8 @@ internal class CompletionHandler(WorkspaceManager workspaceManager) : Completion
|
|||||||
completions.Add(new CompletionItem
|
completions.Add(new CompletionItem
|
||||||
{
|
{
|
||||||
Kind = CompletionItemKind.Variable,
|
Kind = CompletionItemKind.Variable,
|
||||||
Label = variable.Name,
|
Label = variable.NameToken.Value,
|
||||||
InsertText = variable.Name,
|
InsertText = variable.NameToken.Value,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,7 +177,6 @@ internal class CompletionHandler(WorkspaceManager workspaceManager) : Completion
|
|||||||
{
|
{
|
||||||
completions.AddRange(_definitionSnippets);
|
completions.AddRange(_definitionSnippets);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return new CompletionList(completions, false);
|
return new CompletionList(completions, false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,47 +20,58 @@ internal class DefinitionHandler(WorkspaceManager workspaceManager) : Definition
|
|||||||
private LocationOrLocationLinks? HandleSync(DefinitionParams request, CancellationToken cancellationToken)
|
private LocationOrLocationLinks? HandleSync(DefinitionParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var uri = request.TextDocument.Uri;
|
var uri = request.TextDocument.Uri;
|
||||||
var compilationUnit = workspaceManager.GetCompilationUnit(uri);
|
var topLevelNodes = workspaceManager.GetTopLevelNodes(uri.GetFileSystemPath());
|
||||||
if (compilationUnit == null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var line = request.Position.Line;
|
var line = request.Position.Line;
|
||||||
var character = request.Position.Character;
|
var character = request.Position.Character;
|
||||||
|
|
||||||
var node = compilationUnit.DeepestNodeAtPosition(line, character);
|
var node = topLevelNodes.DeepestNodeAtPosition(line, character);
|
||||||
|
|
||||||
switch (node)
|
switch (node)
|
||||||
{
|
{
|
||||||
case VariableIdentifierNode variableIdentifierNode:
|
case VariableIdentifierNode variableIdentifierNode:
|
||||||
{
|
{
|
||||||
var function = compilationUnit.FunctionAtPosition(line, character);
|
var funcNode = topLevelNodes.FunctionAtPosition(line, character);
|
||||||
|
|
||||||
var parameter = function?.Prototype.Parameters.FirstOrDefault(x => x.Name == variableIdentifierNode.Name);
|
var parameter = funcNode?.Prototype.Parameters.FirstOrDefault(x => x.NameToken.Value == variableIdentifierNode.NameToken.Value);
|
||||||
if (parameter != null)
|
if (parameter != null)
|
||||||
{
|
{
|
||||||
return new LocationOrLocationLinks(parameter.ToLocation());
|
return new LocationOrLocationLinks(parameter.NameToken.ToLocation());
|
||||||
}
|
}
|
||||||
|
|
||||||
var variable = function?.Body?
|
var variable = funcNode?.Body?
|
||||||
.Descendants()
|
.Descendants()
|
||||||
.OfType<VariableDeclarationNode>()
|
.OfType<VariableDeclarationNode>()
|
||||||
.FirstOrDefault(x => x.Name == variableIdentifierNode.Name);
|
.FirstOrDefault(x => x.NameToken.Value == variableIdentifierNode.NameToken.Value);
|
||||||
|
|
||||||
if (variable != null)
|
if (variable != null)
|
||||||
{
|
{
|
||||||
return new LocationOrLocationLinks(variable.ToLocation());
|
return new LocationOrLocationLinks(variable.NameToken.ToLocation());
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case FuncIdentifierNode funcIdentifierNode:
|
case LocalFuncIdentifierNode localFuncIdentifierNode:
|
||||||
{
|
{
|
||||||
var prototype = compilationUnit.ImportedFunctions.FirstOrDefault(x => x.Module == funcIdentifierNode.Module && x.Name == funcIdentifierNode.Name);
|
var funcNode = topLevelNodes.OfType<FuncNode>().FirstOrDefault(x => x.NameToken.Value == localFuncIdentifierNode.NameToken.Value);
|
||||||
if (prototype != null)
|
if (funcNode != null)
|
||||||
{
|
{
|
||||||
return new LocationOrLocationLinks(prototype.ToLocation());
|
return new LocationOrLocationLinks(funcNode.NameToken.ToLocation());
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
case ModuleFuncIdentifierNode localFuncIdentifierNode:
|
||||||
|
{
|
||||||
|
var repository = workspaceManager.GetModuleRepository();
|
||||||
|
if (!repository.TryGet(localFuncIdentifierNode.ModuleToken, out var module))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (module.TryResolveFunc(localFuncIdentifierNode.NameToken, out var func, out _))
|
||||||
|
{
|
||||||
|
return new LocationOrLocationLinks(func.NameToken.ToLocation());
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public class DiagnosticsPublisher
|
|||||||
},
|
},
|
||||||
Message = $"{nubDiagnostic.Message}\n{(nubDiagnostic.Help == null ? "" : $"help: {nubDiagnostic.Help}")}",
|
Message = $"{nubDiagnostic.Message}\n{(nubDiagnostic.Help == null ? "" : $"help: {nubDiagnostic.Help}")}",
|
||||||
Range = nubDiagnostic.Span.HasValue
|
Range = nubDiagnostic.Span.HasValue
|
||||||
? new Range(nubDiagnostic.Span.Value.Start.Line - 1, nubDiagnostic.Span.Value.Start.Column - 1, nubDiagnostic.Span.Value.End.Line - 1, nubDiagnostic.Span.Value.End.Column - 1)
|
? new Range(nubDiagnostic.Span.Value.StartLine - 1, nubDiagnostic.Span.Value.StartColumn - 1, nubDiagnostic.Span.Value.EndLine - 1, nubDiagnostic.Span.Value.EndColumn - 1)
|
||||||
: new Range(),
|
: new Range(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using NubLang.Ast;
|
using NubLang.Ast;
|
||||||
|
using NubLang.Modules;
|
||||||
|
using NubLang.Types;
|
||||||
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
|
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
|
||||||
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
|
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
|
||||||
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
|
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
|
||||||
@@ -23,8 +25,16 @@ internal class HoverHandler(WorkspaceManager workspaceManager) : HoverHandlerBas
|
|||||||
|
|
||||||
private Hover? HandleSync(HoverParams request, CancellationToken cancellationToken)
|
private Hover? HandleSync(HoverParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var compilationUnit = workspaceManager.GetCompilationUnit(request.TextDocument.Uri);
|
var topLevelNodes = workspaceManager.GetTopLevelNodes(request.TextDocument.Uri.GetFileSystemPath());
|
||||||
if (compilationUnit == null)
|
|
||||||
|
var moduleDecl = topLevelNodes.OfType<ModuleNode>().FirstOrDefault();
|
||||||
|
if (moduleDecl == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var moduleRepository = workspaceManager.GetModuleRepository();
|
||||||
|
if (!moduleRepository.TryGet(moduleDecl.NameToken, out var module))
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -32,14 +42,14 @@ internal class HoverHandler(WorkspaceManager workspaceManager) : HoverHandlerBas
|
|||||||
var line = request.Position.Line;
|
var line = request.Position.Line;
|
||||||
var character = request.Position.Character;
|
var character = request.Position.Character;
|
||||||
|
|
||||||
var hoveredNode = compilationUnit.DeepestNodeAtPosition(line, character);
|
var hoveredNode = topLevelNodes.DeepestNodeAtPosition(line, character);
|
||||||
|
|
||||||
if (hoveredNode == null)
|
if (hoveredNode == null)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var message = CreateMessage(hoveredNode, compilationUnit);
|
var message = CreateMessage(hoveredNode, moduleRepository, module, line, character);
|
||||||
if (message == null)
|
if (message == null)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
@@ -55,17 +65,18 @@ internal class HoverHandler(WorkspaceManager workspaceManager) : HoverHandlerBas
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string? CreateMessage(Node hoveredNode, CompilationUnit compilationUnit)
|
private static string? CreateMessage(Node hoveredNode, ModuleRepository repository, ModuleRepository.Module currentModule, int line, int character)
|
||||||
{
|
{
|
||||||
return hoveredNode switch
|
return hoveredNode switch
|
||||||
{
|
{
|
||||||
FuncNode funcNode => CreateFuncPrototypeMessage(funcNode.Prototype),
|
FuncNode funcNode => CreateFuncPrototypeMessage(funcNode.Prototype),
|
||||||
FuncPrototypeNode funcPrototypeNode => CreateFuncPrototypeMessage(funcPrototypeNode),
|
FuncPrototypeNode funcPrototypeNode => CreateFuncPrototypeMessage(funcPrototypeNode),
|
||||||
FuncIdentifierNode funcIdentifierNode => CreateFuncIdentifierMessage(funcIdentifierNode, compilationUnit),
|
LocalFuncIdentifierNode funcIdentifierNode => CreateLocalFuncIdentifierMessage(funcIdentifierNode, currentModule),
|
||||||
FuncParameterNode funcParameterNode => CreateTypeNameMessage("Function parameter", funcParameterNode.Name, funcParameterNode.Type),
|
ModuleFuncIdentifierNode funcIdentifierNode => CreateModuleFuncIdentifierMessage(funcIdentifierNode, repository),
|
||||||
VariableIdentifierNode variableIdentifierNode => CreateTypeNameMessage("Variable", variableIdentifierNode.Name, variableIdentifierNode.Type),
|
FuncParameterNode funcParameterNode => CreateTypeNameMessage("Function parameter", funcParameterNode.NameToken.Value, funcParameterNode.Type),
|
||||||
VariableDeclarationNode variableDeclarationNode => CreateTypeNameMessage("Variable declaration", variableDeclarationNode.Name, variableDeclarationNode.Type),
|
VariableIdentifierNode variableIdentifierNode => CreateTypeNameMessage("Variable", variableIdentifierNode.NameToken.Value, variableIdentifierNode.Type),
|
||||||
StructFieldAccessNode structFieldAccessNode => CreateTypeNameMessage("Struct field", $"{structFieldAccessNode.Target.Type}.{structFieldAccessNode.Field}", structFieldAccessNode.Type),
|
VariableDeclarationNode variableDeclarationNode => CreateTypeNameMessage("Variable declaration", variableDeclarationNode.NameToken.Value, variableDeclarationNode.Type),
|
||||||
|
StructFieldAccessNode structFieldAccessNode => CreateTypeNameMessage("Struct field", $"{structFieldAccessNode.Target.Type}.{structFieldAccessNode.FieldToken.Value}", structFieldAccessNode.Type),
|
||||||
CStringLiteralNode cStringLiteralNode => CreateLiteralMessage(cStringLiteralNode.Type, '"' + cStringLiteralNode.Value + '"'),
|
CStringLiteralNode cStringLiteralNode => CreateLiteralMessage(cStringLiteralNode.Type, '"' + cStringLiteralNode.Value + '"'),
|
||||||
StringLiteralNode stringLiteralNode => CreateLiteralMessage(stringLiteralNode.Type, '"' + stringLiteralNode.Value + '"'),
|
StringLiteralNode stringLiteralNode => CreateLiteralMessage(stringLiteralNode.Type, '"' + stringLiteralNode.Value + '"'),
|
||||||
BoolLiteralNode boolLiteralNode => CreateLiteralMessage(boolLiteralNode.Type, boolLiteralNode.Value.ToString()),
|
BoolLiteralNode boolLiteralNode => CreateLiteralMessage(boolLiteralNode.Type, boolLiteralNode.Value.ToString()),
|
||||||
@@ -79,6 +90,7 @@ internal class HoverHandler(WorkspaceManager workspaceManager) : HoverHandlerBas
|
|||||||
U16LiteralNode u16LiteralNode => CreateLiteralMessage(u16LiteralNode.Type, u16LiteralNode.Value.ToString()),
|
U16LiteralNode u16LiteralNode => CreateLiteralMessage(u16LiteralNode.Type, u16LiteralNode.Value.ToString()),
|
||||||
U32LiteralNode u32LiteralNode => CreateLiteralMessage(u32LiteralNode.Type, u32LiteralNode.Value.ToString()),
|
U32LiteralNode u32LiteralNode => CreateLiteralMessage(u32LiteralNode.Type, u32LiteralNode.Value.ToString()),
|
||||||
U64LiteralNode u64LiteralNode => CreateLiteralMessage(u64LiteralNode.Type, u64LiteralNode.Value.ToString()),
|
U64LiteralNode u64LiteralNode => CreateLiteralMessage(u64LiteralNode.Type, u64LiteralNode.Value.ToString()),
|
||||||
|
StructInitializerNode structInitializerNode => CreateStructInitializerMessage(structInitializerNode, line, character),
|
||||||
// Expressions can have a generic fallback showing the resulting type
|
// Expressions can have a generic fallback showing the resulting type
|
||||||
ExpressionNode expressionNode => $"""
|
ExpressionNode expressionNode => $"""
|
||||||
**Expression** `{expressionNode.GetType().Name}`
|
**Expression** `{expressionNode.GetType().Name}`
|
||||||
@@ -91,6 +103,41 @@ internal class HoverHandler(WorkspaceManager workspaceManager) : HoverHandlerBas
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string CreateStructInitializerMessage(StructInitializerNode structInitializerNode, int line, int character)
|
||||||
|
{
|
||||||
|
var hoveredInitializerName = structInitializerNode
|
||||||
|
.Initializers
|
||||||
|
.Select(x => x.Key)
|
||||||
|
.FirstOrDefault(x => x.ContainsPosition(line, character));
|
||||||
|
|
||||||
|
var structType = (NubStructType)structInitializerNode.Type;
|
||||||
|
|
||||||
|
if (hoveredInitializerName != null)
|
||||||
|
{
|
||||||
|
var field = structType.Fields.FirstOrDefault(x => x.Name == hoveredInitializerName.Value);
|
||||||
|
if (field != null)
|
||||||
|
{
|
||||||
|
return $"""
|
||||||
|
**Field** in `{structType}`
|
||||||
|
```nub
|
||||||
|
{hoveredInitializerName.Value}: {field.Type}
|
||||||
|
```
|
||||||
|
""";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $"""
|
||||||
|
**Field** in `{structType}`
|
||||||
|
```nub
|
||||||
|
// Field not found
|
||||||
|
```
|
||||||
|
""";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $"**Struct initializer** `{structType}`";
|
||||||
|
}
|
||||||
|
|
||||||
private static string CreateLiteralMessage(NubType type, string value)
|
private static string CreateLiteralMessage(NubType type, string value)
|
||||||
{
|
{
|
||||||
return $"""
|
return $"""
|
||||||
@@ -111,13 +158,27 @@ internal class HoverHandler(WorkspaceManager workspaceManager) : HoverHandlerBas
|
|||||||
""";
|
""";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string CreateFuncIdentifierMessage(FuncIdentifierNode funcIdentifierNode, CompilationUnit compilationUnit)
|
private static string CreateLocalFuncIdentifierMessage(LocalFuncIdentifierNode funcIdentifierNode, ModuleRepository.Module currentModule)
|
||||||
{
|
{
|
||||||
var func = compilationUnit.ImportedFunctions.FirstOrDefault(x => x.Module == funcIdentifierNode.Module && x.Name == funcIdentifierNode.Name);
|
if (!currentModule.TryResolveFunc(funcIdentifierNode.NameToken, out var func, out _))
|
||||||
if (func == null)
|
|
||||||
{
|
{
|
||||||
return $"""
|
return $"""
|
||||||
**Function** `{funcIdentifierNode.Module}::{funcIdentifierNode.Name}`
|
**Function** `{funcIdentifierNode.NameToken.Value}`
|
||||||
|
```nub
|
||||||
|
// Declaration not found
|
||||||
|
```
|
||||||
|
""";
|
||||||
|
}
|
||||||
|
|
||||||
|
return CreateFuncPrototypeMessage(func);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string CreateModuleFuncIdentifierMessage(ModuleFuncIdentifierNode funcIdentifierNode, ModuleRepository repository)
|
||||||
|
{
|
||||||
|
if (!repository.TryGet(funcIdentifierNode.ModuleToken, out var module) || !module.TryResolveFunc(funcIdentifierNode.NameToken, out var func, out _))
|
||||||
|
{
|
||||||
|
return $"""
|
||||||
|
**Function** `{funcIdentifierNode.ModuleToken.Value}::{funcIdentifierNode.NameToken.Value}`
|
||||||
```nub
|
```nub
|
||||||
// Declaration not found
|
// Declaration not found
|
||||||
```
|
```
|
||||||
@@ -129,13 +190,13 @@ internal class HoverHandler(WorkspaceManager workspaceManager) : HoverHandlerBas
|
|||||||
|
|
||||||
private static string CreateFuncPrototypeMessage(FuncPrototypeNode funcPrototypeNode)
|
private static string CreateFuncPrototypeMessage(FuncPrototypeNode funcPrototypeNode)
|
||||||
{
|
{
|
||||||
var parameterText = string.Join(", ", funcPrototypeNode.Parameters.Select(x => $"{x.Name}: {x.Type}"));
|
var parameterText = string.Join(", ", funcPrototypeNode.Parameters.Select(x => $"{x.NameToken.Value}: {x.Type}"));
|
||||||
var externText = funcPrototypeNode.ExternSymbol != null ? $"extern \"{funcPrototypeNode.ExternSymbol}\" " : "";
|
var externText = funcPrototypeNode.ExternSymbolToken != null ? $"extern \"{funcPrototypeNode.ExternSymbolToken.Value}\" " : "";
|
||||||
|
|
||||||
return $"""
|
return $"""
|
||||||
**Function** `{funcPrototypeNode.Module}::{funcPrototypeNode.Name}`
|
**Function** `{funcPrototypeNode.NameToken.Value}`
|
||||||
```nub
|
```nub
|
||||||
{externText}func {funcPrototypeNode.Module}::{funcPrototypeNode.Name}({parameterText}): {funcPrototypeNode.ReturnType}
|
{externText}func {funcPrototypeNode.NameToken.Value}({parameterText}): {funcPrototypeNode.ReturnType}
|
||||||
```
|
```
|
||||||
""";
|
""";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,17 +18,7 @@ var server = await LanguageServer.From(options => options
|
|||||||
.WithHandler<HoverHandler>()
|
.WithHandler<HoverHandler>()
|
||||||
.WithHandler<CompletionHandler>()
|
.WithHandler<CompletionHandler>()
|
||||||
.WithHandler<DefinitionHandler>()
|
.WithHandler<DefinitionHandler>()
|
||||||
.OnInitialize((server, request, ct) =>
|
.WithHandler<SetRootPathCommandHandler>()
|
||||||
{
|
|
||||||
var workspaceManager = server.GetRequiredService<WorkspaceManager>();
|
|
||||||
|
|
||||||
if (request.RootPath != null)
|
|
||||||
{
|
|
||||||
workspaceManager.Init(request.RootPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Task.CompletedTask;
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
|
|
||||||
await server.WaitForExit;
|
await server.WaitForExit;
|
||||||
31
compiler/NubLang.LSP/SetRootPathCommandHandler.cs
Normal file
31
compiler/NubLang.LSP/SetRootPathCommandHandler.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using MediatR;
|
||||||
|
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
|
||||||
|
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
|
||||||
|
using OmniSharp.Extensions.LanguageServer.Protocol.Workspace;
|
||||||
|
|
||||||
|
namespace NubLang.LSP;
|
||||||
|
|
||||||
|
public class SetRootPathCommandHandler(WorkspaceManager workspaceManager) : ExecuteCommandHandlerBase
|
||||||
|
{
|
||||||
|
protected override ExecuteCommandRegistrationOptions CreateRegistrationOptions(ExecuteCommandCapability capability, ClientCapabilities clientCapabilities)
|
||||||
|
{
|
||||||
|
return new ExecuteCommandRegistrationOptions
|
||||||
|
{
|
||||||
|
Commands = new Container<string>("nub.setRootPath")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Task<Unit> Handle(ExecuteCommandParams request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (request is { Command: "nub.setRootPath", Arguments.Count: > 0 })
|
||||||
|
{
|
||||||
|
var newRoot = request.Arguments[0].ToString();
|
||||||
|
if (!string.IsNullOrEmpty(newRoot))
|
||||||
|
{
|
||||||
|
workspaceManager.SetRootPath(newRoot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Unit.Task;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,25 +15,25 @@ internal class TextDocumentSyncHandler(WorkspaceManager workspaceManager) : Text
|
|||||||
|
|
||||||
public override Task<Unit> Handle(DidOpenTextDocumentParams request, CancellationToken cancellationToken)
|
public override Task<Unit> Handle(DidOpenTextDocumentParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
workspaceManager.Update();
|
||||||
return Unit.Task;
|
return Unit.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<Unit> Handle(DidChangeTextDocumentParams request, CancellationToken cancellationToken)
|
public override Task<Unit> Handle(DidChangeTextDocumentParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
workspaceManager.Update();
|
||||||
return Unit.Task;
|
return Unit.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<Unit> Handle(DidSaveTextDocumentParams request, CancellationToken cancellationToken)
|
public override Task<Unit> Handle(DidSaveTextDocumentParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
workspaceManager.Update();
|
||||||
return Unit.Task;
|
return Unit.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<Unit> Handle(DidCloseTextDocumentParams request, CancellationToken cancellationToken)
|
public override Task<Unit> Handle(DidCloseTextDocumentParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
workspaceManager.Update();
|
||||||
return Unit.Task;
|
return Unit.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,74 +1,71 @@
|
|||||||
using NubLang.Ast;
|
using NubLang.Ast;
|
||||||
|
using NubLang.Diagnostics;
|
||||||
|
using NubLang.Modules;
|
||||||
using NubLang.Syntax;
|
using NubLang.Syntax;
|
||||||
using OmniSharp.Extensions.LanguageServer.Protocol;
|
|
||||||
|
|
||||||
namespace NubLang.LSP;
|
namespace NubLang.LSP;
|
||||||
|
|
||||||
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher)
|
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher)
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, SyntaxTree> _syntaxTrees = new();
|
private record Unit(SyntaxTree SyntaxTree, DateTimeOffset FileTimestamp, List<Diagnostic> Diagnostics);
|
||||||
private readonly Dictionary<string, CompilationUnit> _compilationUnits = new();
|
|
||||||
|
|
||||||
public void Init(string rootPath)
|
private readonly Tokenizer _tokenizer = new();
|
||||||
|
private readonly Parser _parser = new();
|
||||||
|
private readonly TypeChecker _typeChecker = new();
|
||||||
|
private string? _rootPath;
|
||||||
|
private readonly Dictionary<string, Unit> _units = [];
|
||||||
|
private readonly Dictionary<string, List<TopLevelNode>> _possiblyOutdatedTopLevelNodes = [];
|
||||||
|
private ModuleRepository _repository = new([]);
|
||||||
|
|
||||||
|
public void SetRootPath(string rootPath)
|
||||||
{
|
{
|
||||||
var files = Directory.GetFiles(rootPath, "*.nub", SearchOption.AllDirectories);
|
_units.Clear();
|
||||||
foreach (var path in files)
|
_possiblyOutdatedTopLevelNodes.Clear();
|
||||||
|
_repository = new ModuleRepository([]);
|
||||||
|
_rootPath = rootPath;
|
||||||
|
Update();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
{
|
{
|
||||||
var text = File.ReadAllText(path);
|
if (_rootPath == null) return;
|
||||||
var tokenizer = new Tokenizer(path, text);
|
var files = Directory.GetFiles(_rootPath, "*.nub", SearchOption.AllDirectories);
|
||||||
|
foreach (var file in files)
|
||||||
tokenizer.Tokenize();
|
|
||||||
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
|
|
||||||
|
|
||||||
var parser = new Parser();
|
|
||||||
var parseResult = parser.Parse(tokenizer.Tokens);
|
|
||||||
diagnosticsPublisher.Publish(path, parser.Diagnostics);
|
|
||||||
|
|
||||||
_syntaxTrees[path] = parseResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var (fsPath, syntaxTree) in _syntaxTrees)
|
|
||||||
{
|
{
|
||||||
var modules = Module.Collect(_syntaxTrees.Select(x => x.Value).ToList());
|
var lastUpdated = File.GetLastWriteTimeUtc(file);
|
||||||
|
var unit = _units.GetValueOrDefault(file);
|
||||||
var typeChecker = new TypeChecker(syntaxTree, modules);
|
if (unit == null || lastUpdated > unit.FileTimestamp)
|
||||||
var result = typeChecker.Check();
|
|
||||||
diagnosticsPublisher.Publish(fsPath, typeChecker.Diagnostics);
|
|
||||||
_compilationUnits[fsPath] = result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateFile(DocumentUri path)
|
|
||||||
{
|
{
|
||||||
var fsPath = path.GetFileSystemPath();
|
_units[file] = Update(file, lastUpdated);
|
||||||
|
}
|
||||||
var text = File.ReadAllText(fsPath);
|
|
||||||
var tokenizer = new Tokenizer(fsPath, text);
|
|
||||||
tokenizer.Tokenize();
|
|
||||||
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
|
|
||||||
|
|
||||||
var parser = new Parser();
|
|
||||||
var syntaxTree = parser.Parse(tokenizer.Tokens);
|
|
||||||
diagnosticsPublisher.Publish(path, parser.Diagnostics);
|
|
||||||
_syntaxTrees[fsPath] = syntaxTree;
|
|
||||||
|
|
||||||
var modules = Module.Collect(_syntaxTrees.Select(x => x.Value).ToList());
|
|
||||||
|
|
||||||
var typeChecker = new TypeChecker(syntaxTree, modules);
|
|
||||||
var result = typeChecker.Check();
|
|
||||||
diagnosticsPublisher.Publish(fsPath, typeChecker.Diagnostics);
|
|
||||||
_compilationUnits[fsPath] = result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveFile(DocumentUri path)
|
_repository = ModuleRepository.Create(_units.Select(x => x.Value.SyntaxTree).ToList());
|
||||||
|
|
||||||
|
foreach (var (file, unit) in _units)
|
||||||
{
|
{
|
||||||
var fsPath = path.GetFileSystemPath();
|
var topLevelNodes = _typeChecker.Check(unit.SyntaxTree, _repository);
|
||||||
_syntaxTrees.Remove(fsPath);
|
_possiblyOutdatedTopLevelNodes[file] = topLevelNodes;
|
||||||
_compilationUnits.Remove(fsPath);
|
diagnosticsPublisher.Publish(file, [..unit.Diagnostics, .._typeChecker.Diagnostics]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompilationUnit? GetCompilationUnit(DocumentUri path)
|
private Unit Update(string file, DateTimeOffset lastUpdated)
|
||||||
{
|
{
|
||||||
return _compilationUnits.GetValueOrDefault(path.GetFileSystemPath());
|
var text = File.ReadAllText(file);
|
||||||
|
var tokens = _tokenizer.Tokenize(file, text);
|
||||||
|
var syntaxTree = _parser.Parse(tokens);
|
||||||
|
|
||||||
|
return new Unit(syntaxTree, lastUpdated, [.._tokenizer.Diagnostics, .._parser.Diagnostics]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TopLevelNode> GetTopLevelNodes(string path)
|
||||||
|
{
|
||||||
|
return _possiblyOutdatedTopLevelNodes.GetValueOrDefault(path, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModuleRepository GetModuleRepository()
|
||||||
|
{
|
||||||
|
return _repository;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
namespace NubLang.Ast;
|
|
||||||
|
|
||||||
public sealed class CompilationUnit
|
|
||||||
{
|
|
||||||
public CompilationUnit(List<FuncNode> functions, List<NubStructType> importedStructTypes, List<FuncPrototypeNode> importedFunctions)
|
|
||||||
{
|
|
||||||
Functions = functions;
|
|
||||||
ImportedStructTypes = importedStructTypes;
|
|
||||||
ImportedFunctions = importedFunctions;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<FuncNode> Functions { get; }
|
|
||||||
public List<NubStructType> ImportedStructTypes { get; }
|
|
||||||
public List<FuncPrototypeNode> ImportedFunctions { get; }
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
using NubLang.Syntax;
|
using NubLang.Syntax;
|
||||||
|
using NubLang.Types;
|
||||||
|
|
||||||
namespace NubLang.Ast;
|
namespace NubLang.Ast;
|
||||||
|
|
||||||
public abstract record Node(List<Token> Tokens)
|
public abstract class Node(List<Token> tokens)
|
||||||
{
|
{
|
||||||
|
public List<Token> Tokens { get; } = tokens;
|
||||||
|
|
||||||
public abstract IEnumerable<Node> Children();
|
public abstract IEnumerable<Node> Children();
|
||||||
|
|
||||||
public IEnumerable<Node> Descendants()
|
public IEnumerable<Node> Descendants()
|
||||||
@@ -27,28 +30,54 @@ public abstract record Node(List<Token> Tokens)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Definitions
|
public abstract class TopLevelNode(List<Token> tokens) : Node(tokens);
|
||||||
|
|
||||||
public abstract record DefinitionNode(List<Token> Tokens, string Module, string Name) : Node(Tokens);
|
public class ModuleNode(List<Token> tokens, IdentifierToken nameToken) : TopLevelNode(tokens)
|
||||||
|
|
||||||
public record FuncParameterNode(List<Token> Tokens, string Name, NubType Type) : Node(Tokens)
|
|
||||||
{
|
{
|
||||||
|
public IdentifierToken NameToken { get; } = nameToken;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record FuncPrototypeNode(List<Token> Tokens, string Module, string Name, string? ExternSymbol, List<FuncParameterNode> Parameters, NubType ReturnType) : Node(Tokens)
|
#region Definitions
|
||||||
|
|
||||||
|
public abstract class DefinitionNode(List<Token> tokens, IdentifierToken nameToken) : TopLevelNode(tokens)
|
||||||
{
|
{
|
||||||
|
public IdentifierToken NameToken { get; } = nameToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class FuncParameterNode(List<Token> tokens, IdentifierToken nameToken, NubType type) : Node(tokens)
|
||||||
|
{
|
||||||
|
public IdentifierToken NameToken { get; } = nameToken;
|
||||||
|
public NubType Type { get; } = type;
|
||||||
|
|
||||||
|
public override IEnumerable<Node> Children()
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class FuncPrototypeNode(List<Token> tokens, IdentifierToken nameToken, StringLiteralToken? externSymbolToken, List<FuncParameterNode> parameters, NubType returnType) : Node(tokens)
|
||||||
|
{
|
||||||
|
public IdentifierToken NameToken { get; } = nameToken;
|
||||||
|
public StringLiteralToken? ExternSymbolToken { get; } = externSymbolToken;
|
||||||
|
public List<FuncParameterNode> Parameters { get; } = parameters;
|
||||||
|
public NubType ReturnType { get; } = returnType;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return Parameters;
|
return Parameters;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record FuncNode(List<Token> Tokens, FuncPrototypeNode Prototype, BlockNode? Body) : DefinitionNode(Tokens, Prototype.Module, Prototype.Name)
|
public class FuncNode(List<Token> tokens, FuncPrototypeNode prototype, BlockNode? body) : DefinitionNode(tokens, prototype.NameToken)
|
||||||
{
|
{
|
||||||
|
public FuncPrototypeNode Prototype { get; } = prototype;
|
||||||
|
public BlockNode? Body { get; } = body;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Prototype;
|
yield return Prototype;
|
||||||
@@ -59,40 +88,77 @@ public record FuncNode(List<Token> Tokens, FuncPrototypeNode Prototype, BlockNod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class StructFieldNode(List<Token> tokens, IdentifierToken nameToken, NubType type, ExpressionNode? value) : Node(tokens)
|
||||||
|
{
|
||||||
|
public IdentifierToken NameToken { get; } = nameToken;
|
||||||
|
public NubType Type { get; } = type;
|
||||||
|
public ExpressionNode? Value { get; } = value;
|
||||||
|
|
||||||
|
public override IEnumerable<Node> Children()
|
||||||
|
{
|
||||||
|
if (Value != null)
|
||||||
|
{
|
||||||
|
yield return Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StructNode(List<Token> tokens, IdentifierToken name, NubStructType structType, bool packed, List<StructFieldNode> fields) : DefinitionNode(tokens, name)
|
||||||
|
{
|
||||||
|
public NubStructType StructType { get; } = structType;
|
||||||
|
public bool Packed { get; } = packed;
|
||||||
|
public List<StructFieldNode> Fields { get; } = fields;
|
||||||
|
|
||||||
|
public override IEnumerable<Node> Children()
|
||||||
|
{
|
||||||
|
foreach (var field in Fields)
|
||||||
|
{
|
||||||
|
yield return field;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Statements
|
#region Statements
|
||||||
|
|
||||||
public abstract record StatementNode(List<Token> Tokens) : Node(Tokens);
|
public abstract class StatementNode(List<Token> tokens) : Node(tokens);
|
||||||
|
|
||||||
public abstract record TerminalStatementNode(List<Token> Tokens) : StatementNode(Tokens);
|
public class BlockNode(List<Token> tokens, List<StatementNode> statements) : StatementNode(tokens)
|
||||||
|
|
||||||
public record BlockNode(List<Token> Tokens, List<StatementNode> Statements) : StatementNode(Tokens)
|
|
||||||
{
|
{
|
||||||
|
public List<StatementNode> Statements { get; } = statements;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return Statements;
|
return Statements;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record StatementFuncCallNode(List<Token> Tokens, FuncCallNode FuncCall) : StatementNode(Tokens)
|
public class StatementFuncCallNode(List<Token> tokens, FuncCallNode funcCall) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
|
public FuncCallNode FuncCall { get; } = funcCall;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return FuncCall;
|
yield return FuncCall;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record ReturnNode(List<Token> Tokens, ExpressionNode? Value) : TerminalStatementNode(Tokens)
|
public class ReturnNode(List<Token> tokens, ExpressionNode? value) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
|
public ExpressionNode? Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
if (Value != null) yield return Value;
|
if (Value != null) yield return Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record AssignmentNode(List<Token> Tokens, LValueExpressionNode Target, ExpressionNode Value) : StatementNode(Tokens)
|
public class AssignmentNode(List<Token> tokens, ExpressionNode target, ExpressionNode value) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
|
public ExpressionNode Target { get; } = target;
|
||||||
|
public ExpressionNode Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Target;
|
yield return Target;
|
||||||
@@ -100,8 +166,12 @@ public record AssignmentNode(List<Token> Tokens, LValueExpressionNode Target, Ex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record IfNode(List<Token> Tokens, ExpressionNode Condition, BlockNode Body, Variant<IfNode, BlockNode>? Else) : StatementNode(Tokens)
|
public class IfNode(List<Token> tokens, ExpressionNode condition, BlockNode body, Variant<IfNode, BlockNode>? @else) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
|
public ExpressionNode Condition { get; } = condition;
|
||||||
|
public BlockNode Body { get; } = body;
|
||||||
|
public Variant<IfNode, BlockNode>? Else { get; } = @else;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Condition;
|
yield return Condition;
|
||||||
@@ -113,15 +183,19 @@ public record IfNode(List<Token> Tokens, ExpressionNode Condition, BlockNode Bod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record VariableDeclarationNode(List<Token> Tokens, string Name, ExpressionNode? Assignment, NubType Type) : StatementNode(Tokens)
|
public class VariableDeclarationNode(List<Token> tokens, IdentifierToken nameToken, ExpressionNode? assignment, NubType type) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
|
public IdentifierToken NameToken { get; } = nameToken;
|
||||||
|
public ExpressionNode? Assignment { get; } = assignment;
|
||||||
|
public NubType Type { get; } = type;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
if (Assignment != null) yield return Assignment;
|
if (Assignment != null) yield return Assignment;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record ContinueNode(List<Token> Tokens) : TerminalStatementNode(Tokens)
|
public class ContinueNode(List<Token> tokens) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
@@ -129,7 +203,7 @@ public record ContinueNode(List<Token> Tokens) : TerminalStatementNode(Tokens)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record BreakNode(List<Token> Tokens) : TerminalStatementNode(Tokens)
|
public class BreakNode(List<Token> tokens) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
@@ -137,8 +211,11 @@ public record BreakNode(List<Token> Tokens) : TerminalStatementNode(Tokens)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record WhileNode(List<Token> Tokens, ExpressionNode Condition, BlockNode Body) : StatementNode(Tokens)
|
public class WhileNode(List<Token> tokens, ExpressionNode condition, BlockNode body) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
|
public ExpressionNode Condition { get; } = condition;
|
||||||
|
public BlockNode Body { get; } = body;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Condition;
|
yield return Condition;
|
||||||
@@ -146,8 +223,13 @@ public record WhileNode(List<Token> Tokens, ExpressionNode Condition, BlockNode
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record ForSliceNode(List<Token> Tokens, string ElementName, string? IndexName, ExpressionNode Target, BlockNode Body) : StatementNode(Tokens)
|
public class ForSliceNode(List<Token> tokens, IdentifierToken elementNameToken, IdentifierToken? indexNameToken, ExpressionNode target, BlockNode body) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
|
public IdentifierToken ElementNameToken { get; } = elementNameToken;
|
||||||
|
public IdentifierToken? IndexNameToken { get; } = indexNameToken;
|
||||||
|
public ExpressionNode Target { get; } = target;
|
||||||
|
public BlockNode Body { get; } = body;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Target;
|
yield return Target;
|
||||||
@@ -155,8 +237,13 @@ public record ForSliceNode(List<Token> Tokens, string ElementName, string? Index
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record ForConstArrayNode(List<Token> Tokens, string ElementName, string? IndexName, ExpressionNode Target, BlockNode Body) : StatementNode(Tokens)
|
public class ForConstArrayNode(List<Token> tokens, IdentifierToken elementNameToken, IdentifierToken? indexNameToken, ExpressionNode target, BlockNode body) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
|
public IdentifierToken ElementNameToken { get; } = elementNameToken;
|
||||||
|
public IdentifierToken? IndexNameToken { get; } = indexNameToken;
|
||||||
|
public ExpressionNode Target { get; } = target;
|
||||||
|
public BlockNode Body { get; } = body;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Target;
|
yield return Target;
|
||||||
@@ -164,8 +251,10 @@ public record ForConstArrayNode(List<Token> Tokens, string ElementName, string?
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record DeferNode(List<Token> Tokens, StatementNode Statement) : StatementNode(Tokens)
|
public class DeferNode(List<Token> tokens, StatementNode statement) : StatementNode(tokens)
|
||||||
{
|
{
|
||||||
|
public StatementNode Statement { get; } = statement;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Statement;
|
yield return Statement;
|
||||||
@@ -204,120 +293,151 @@ public enum BinaryOperator
|
|||||||
BitwiseOr
|
BitwiseOr
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract record ExpressionNode(List<Token> Tokens, NubType Type) : Node(Tokens);
|
public abstract class ExpressionNode(List<Token> tokens, NubType type) : Node(tokens)
|
||||||
|
|
||||||
public abstract record LValueExpressionNode(List<Token> Tokens, NubType Type) : ExpressionNode(Tokens, Type);
|
|
||||||
|
|
||||||
public abstract record RValueExpressionNode(List<Token> Tokens, NubType Type) : ExpressionNode(Tokens, Type);
|
|
||||||
|
|
||||||
public abstract record IntermediateExpression(List<Token> Tokens) : ExpressionNode(Tokens, new NubVoidType());
|
|
||||||
|
|
||||||
public record StringLiteralNode(List<Token> Tokens, string Value) : RValueExpressionNode(Tokens, new NubStringType())
|
|
||||||
{
|
{
|
||||||
|
public NubType Type { get; } = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class LValue(List<Token> tokens, NubType type) : ExpressionNode(tokens, type);
|
||||||
|
|
||||||
|
public abstract class RValue(List<Token> tokens, NubType type) : ExpressionNode(tokens, type);
|
||||||
|
|
||||||
|
public class StringLiteralNode(List<Token> tokens, string value) : RValue(tokens, new NubStringType())
|
||||||
|
{
|
||||||
|
public string Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record CStringLiteralNode(List<Token> Tokens, string Value) : RValueExpressionNode(Tokens, new NubCStringType())
|
public class CStringLiteralNode(List<Token> tokens, string value) : RValue(tokens, new NubPointerType(new NubIntType(true, 8)))
|
||||||
{
|
{
|
||||||
|
public string Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record I8LiteralNode(List<Token> Tokens, sbyte Value) : RValueExpressionNode(Tokens, new NubIntType(true, 8))
|
public class I8LiteralNode(List<Token> tokens, sbyte value) : RValue(tokens, new NubIntType(true, 8))
|
||||||
{
|
{
|
||||||
|
public sbyte Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record I16LiteralNode(List<Token> Tokens, short Value) : RValueExpressionNode(Tokens, new NubIntType(true, 16))
|
public class I16LiteralNode(List<Token> tokens, short value) : RValue(tokens, new NubIntType(true, 16))
|
||||||
{
|
{
|
||||||
|
public short Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record I32LiteralNode(List<Token> Tokens, int Value) : RValueExpressionNode(Tokens, new NubIntType(true, 32))
|
public class I32LiteralNode(List<Token> tokens, int value) : RValue(tokens, new NubIntType(true, 32))
|
||||||
{
|
{
|
||||||
|
public int Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record I64LiteralNode(List<Token> Tokens, long Value) : RValueExpressionNode(Tokens, new NubIntType(true, 64))
|
public class I64LiteralNode(List<Token> tokens, long value) : RValue(tokens, new NubIntType(true, 64))
|
||||||
{
|
{
|
||||||
|
public long Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record U8LiteralNode(List<Token> Tokens, byte Value) : RValueExpressionNode(Tokens, new NubIntType(false, 8))
|
public class U8LiteralNode(List<Token> tokens, byte value) : RValue(tokens, new NubIntType(false, 8))
|
||||||
{
|
{
|
||||||
|
public byte Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record U16LiteralNode(List<Token> Tokens, ushort Value) : RValueExpressionNode(Tokens, new NubIntType(false, 16))
|
public class U16LiteralNode(List<Token> tokens, ushort value) : RValue(tokens, new NubIntType(false, 16))
|
||||||
{
|
{
|
||||||
|
public ushort Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record U32LiteralNode(List<Token> Tokens, uint Value) : RValueExpressionNode(Tokens, new NubIntType(false, 32))
|
public class U32LiteralNode(List<Token> tokens, uint value) : RValue(tokens, new NubIntType(false, 32))
|
||||||
{
|
{
|
||||||
|
public uint Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record U64LiteralNode(List<Token> Tokens, ulong Value) : RValueExpressionNode(Tokens, new NubIntType(false, 64))
|
public class U64LiteralNode(List<Token> tokens, ulong value) : RValue(tokens, new NubIntType(false, 64))
|
||||||
{
|
{
|
||||||
|
public ulong Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record Float32LiteralNode(List<Token> Tokens, float Value) : RValueExpressionNode(Tokens, new NubFloatType(32))
|
public class Float32LiteralNode(List<Token> tokens, float value) : RValue(tokens, new NubFloatType(32))
|
||||||
{
|
{
|
||||||
|
public float Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record Float64LiteralNode(List<Token> Tokens, double Value) : RValueExpressionNode(Tokens, new NubFloatType(64))
|
public class Float64LiteralNode(List<Token> tokens, double value) : RValue(tokens, new NubFloatType(64))
|
||||||
{
|
{
|
||||||
|
public double Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record BoolLiteralNode(List<Token> Tokens, NubType Type, bool Value) : RValueExpressionNode(Tokens, Type)
|
public class BoolLiteralNode(List<Token> tokens, bool value) : RValue(tokens, new NubBoolType())
|
||||||
{
|
{
|
||||||
|
public bool Value { get; } = value;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record BinaryExpressionNode(List<Token> Tokens, NubType Type, ExpressionNode Left, BinaryOperator Operator, ExpressionNode Right) : RValueExpressionNode(Tokens, Type)
|
public class BinaryExpressionNode(List<Token> tokens, NubType type, ExpressionNode left, BinaryOperator @operator, ExpressionNode right) : RValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public ExpressionNode Left { get; } = left;
|
||||||
|
public BinaryOperator Operator { get; } = @operator;
|
||||||
|
public ExpressionNode Right { get; } = right;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Left;
|
yield return Left;
|
||||||
@@ -325,16 +445,22 @@ public record BinaryExpressionNode(List<Token> Tokens, NubType Type, ExpressionN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record UnaryExpressionNode(List<Token> Tokens, NubType Type, UnaryOperator Operator, ExpressionNode Operand) : RValueExpressionNode(Tokens, Type)
|
public class UnaryExpressionNode(List<Token> tokens, NubType type, UnaryOperator @operator, ExpressionNode operand) : RValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public UnaryOperator Operator { get; } = @operator;
|
||||||
|
public ExpressionNode Operand { get; } = operand;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Operand;
|
yield return Operand;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record FuncCallNode(List<Token> Tokens, NubType Type, ExpressionNode Expression, List<ExpressionNode> Parameters) : RValueExpressionNode(Tokens, Type)
|
public class FuncCallNode(List<Token> tokens, NubType type, ExpressionNode expression, List<ExpressionNode> parameters) : RValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public ExpressionNode Expression { get; } = expression;
|
||||||
|
public List<ExpressionNode> Parameters { get; } = parameters;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Expression;
|
yield return Expression;
|
||||||
@@ -345,40 +471,44 @@ public record FuncCallNode(List<Token> Tokens, NubType Type, ExpressionNode Expr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record VariableIdentifierNode(List<Token> Tokens, NubType Type, string Name) : LValueExpressionNode(Tokens, Type)
|
public class VariableIdentifierNode(List<Token> tokens, NubType type, IdentifierToken nameToken) : LValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public IdentifierToken NameToken { get; } = nameToken;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record FuncIdentifierNode(List<Token> Tokens, NubType Type, string Module, string Name, string? ExternSymbol) : RValueExpressionNode(Tokens, Type)
|
public class LocalFuncIdentifierNode(List<Token> tokens, NubType type, IdentifierToken nameToken, StringLiteralToken? externSymbolToken) : RValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public IdentifierToken NameToken { get; } = nameToken;
|
||||||
|
public StringLiteralToken? ExternSymbolToken { get; } = externSymbolToken;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record ArrayInitializerNode(List<Token> Tokens, NubType Type, List<ExpressionNode> Values) : RValueExpressionNode(Tokens, Type)
|
public class ModuleFuncIdentifierNode(List<Token> tokens, NubType type, IdentifierToken moduleToken, IdentifierToken nameToken, StringLiteralToken? externSymbolToken) : RValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public IdentifierToken ModuleToken { get; } = moduleToken;
|
||||||
|
public IdentifierToken NameToken { get; } = nameToken;
|
||||||
|
public StringLiteralToken? ExternSymbolToken { get; } = externSymbolToken;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
return Values;
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record ConstArrayInitializerNode(List<Token> Tokens, NubType Type, List<ExpressionNode> Values) : RValueExpressionNode(Tokens, Type)
|
public class ArrayIndexAccessNode(List<Token> tokens, NubType type, ExpressionNode target, ExpressionNode index) : LValue(tokens, type)
|
||||||
{
|
{
|
||||||
public override IEnumerable<Node> Children()
|
public ExpressionNode Target { get; } = target;
|
||||||
{
|
public ExpressionNode Index { get; } = index;
|
||||||
return Values;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public record ArrayIndexAccessNode(List<Token> Tokens, NubType Type, ExpressionNode Target, ExpressionNode Index) : LValueExpressionNode(Tokens, Type)
|
|
||||||
{
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Target;
|
yield return Target;
|
||||||
@@ -386,8 +516,11 @@ public record ArrayIndexAccessNode(List<Token> Tokens, NubType Type, ExpressionN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record ConstArrayIndexAccessNode(List<Token> Tokens, NubType Type, ExpressionNode Target, ExpressionNode Index) : LValueExpressionNode(Tokens, Type)
|
public class ConstArrayIndexAccessNode(List<Token> tokens, NubType type, ExpressionNode target, ExpressionNode index) : LValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public ExpressionNode Target { get; } = target;
|
||||||
|
public ExpressionNode Index { get; } = index;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Target;
|
yield return Target;
|
||||||
@@ -395,8 +528,11 @@ public record ConstArrayIndexAccessNode(List<Token> Tokens, NubType Type, Expres
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record SliceIndexAccessNode(List<Token> Tokens, NubType Type, ExpressionNode Target, ExpressionNode Index) : LValueExpressionNode(Tokens, Type)
|
public class SliceIndexAccessNode(List<Token> tokens, NubType type, ExpressionNode target, ExpressionNode index) : LValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public ExpressionNode Target { get; } = target;
|
||||||
|
public ExpressionNode Index { get; } = index;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Target;
|
yield return Target;
|
||||||
@@ -404,24 +540,79 @@ public record SliceIndexAccessNode(List<Token> Tokens, NubType Type, ExpressionN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record AddressOfNode(List<Token> Tokens, NubType Type, LValueExpressionNode LValue) : RValueExpressionNode(Tokens, Type)
|
public class AddressOfNode(List<Token> tokens, NubType type, ExpressionNode target) : RValue(tokens, type)
|
||||||
{
|
{
|
||||||
public override IEnumerable<Node> Children()
|
public ExpressionNode Target { get; } = target;
|
||||||
{
|
|
||||||
yield return LValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public record StructFieldAccessNode(List<Token> Tokens, NubType Type, ExpressionNode Target, string Field) : LValueExpressionNode(Tokens, Type)
|
|
||||||
{
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Target;
|
yield return Target;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record StructInitializerNode(List<Token> Tokens, NubType Type, Dictionary<string, ExpressionNode> Initializers) : RValueExpressionNode(Tokens, Type)
|
public class StructFieldAccessNode(List<Token> tokens, NubType type, ExpressionNode target, IdentifierToken fieldToken) : LValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public ExpressionNode Target { get; } = target;
|
||||||
|
public IdentifierToken FieldToken { get; } = fieldToken;
|
||||||
|
|
||||||
|
public override IEnumerable<Node> Children()
|
||||||
|
{
|
||||||
|
yield return Target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DereferenceNode(List<Token> tokens, NubType type, ExpressionNode target) : LValue(tokens, type)
|
||||||
|
{
|
||||||
|
public ExpressionNode Target { get; } = target;
|
||||||
|
|
||||||
|
public override IEnumerable<Node> Children()
|
||||||
|
{
|
||||||
|
yield return Target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SizeNode(List<Token> tokens, NubType targetType) : RValue(tokens, new NubIntType(false, 64))
|
||||||
|
{
|
||||||
|
public NubType TargetType { get; } = targetType;
|
||||||
|
|
||||||
|
public override IEnumerable<Node> Children()
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CastNode(List<Token> tokens, NubType type, ExpressionNode value, CastNode.Conversion conversionType) : RValue(tokens, type)
|
||||||
|
{
|
||||||
|
public enum Conversion
|
||||||
|
{
|
||||||
|
IntToInt,
|
||||||
|
FloatToFloat,
|
||||||
|
IntToFloat,
|
||||||
|
FloatToInt,
|
||||||
|
|
||||||
|
PointerToPointer,
|
||||||
|
PointerToUInt64,
|
||||||
|
UInt64ToPointer,
|
||||||
|
|
||||||
|
ConstArrayToArray,
|
||||||
|
ConstArrayToSlice,
|
||||||
|
|
||||||
|
StringToCString
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExpressionNode Value { get; } = value;
|
||||||
|
public Conversion ConversionType { get; } = conversionType;
|
||||||
|
|
||||||
|
public override IEnumerable<Node> Children()
|
||||||
|
{
|
||||||
|
yield return Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StructInitializerNode(List<Token> tokens, NubType type, Dictionary<IdentifierToken, ExpressionNode> initializers) : LValue(tokens, type)
|
||||||
|
{
|
||||||
|
public Dictionary<IdentifierToken, ExpressionNode> Initializers { get; } = initializers;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
foreach (var initializer in Initializers)
|
foreach (var initializer in Initializers)
|
||||||
@@ -431,31 +622,17 @@ public record StructInitializerNode(List<Token> Tokens, NubType Type, Dictionary
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record DereferenceNode(List<Token> Tokens, NubType Type, ExpressionNode Target) : LValueExpressionNode(Tokens, Type)
|
public class ConstArrayInitializerNode(List<Token> tokens, NubType type, List<ExpressionNode> values) : LValue(tokens, type)
|
||||||
{
|
{
|
||||||
|
public List<ExpressionNode> Values { get; } = values;
|
||||||
|
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
yield return Target;
|
return Values;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record SizeNode(List<Token> Tokens, NubType Type, NubType TargetType) : RValueExpressionNode(Tokens, Type)
|
public class EnumTypeIntermediateNode(List<Token> tokens, NubType type) : ExpressionNode(tokens, type)
|
||||||
{
|
|
||||||
public override IEnumerable<Node> Children()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public record CastNode(List<Token> Tokens, NubType Type, ExpressionNode Value) : RValueExpressionNode(Tokens, Type)
|
|
||||||
{
|
|
||||||
public override IEnumerable<Node> Children()
|
|
||||||
{
|
|
||||||
yield return Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public record EnumReferenceIntermediateNode(List<Token> Tokens, string Module, string Name) : IntermediateExpression(Tokens)
|
|
||||||
{
|
{
|
||||||
public override IEnumerable<Node> Children()
|
public override IEnumerable<Node> Children()
|
||||||
{
|
{
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
11
compiler/NubLang/Diagnostics/CompileException.cs
Normal file
11
compiler/NubLang/Diagnostics/CompileException.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
namespace NubLang.Diagnostics;
|
||||||
|
|
||||||
|
public class CompileException : Exception
|
||||||
|
{
|
||||||
|
public Diagnostic Diagnostic { get; }
|
||||||
|
|
||||||
|
public CompileException(Diagnostic diagnostic) : base(diagnostic.Message)
|
||||||
|
{
|
||||||
|
Diagnostic = diagnostic;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ public class Diagnostic
|
|||||||
private readonly string _message;
|
private readonly string _message;
|
||||||
private SourceSpan? _span;
|
private SourceSpan? _span;
|
||||||
private string? _help;
|
private string? _help;
|
||||||
|
private List<Token>? _tokens;
|
||||||
|
|
||||||
public DiagnosticBuilder(DiagnosticSeverity severity, string message)
|
public DiagnosticBuilder(DiagnosticSeverity severity, string message)
|
||||||
{
|
{
|
||||||
@@ -18,18 +19,32 @@ public class Diagnostic
|
|||||||
_message = message;
|
_message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiagnosticBuilder At(SyntaxNode? node)
|
public DiagnosticBuilder At(SyntaxNode? node, List<Token>? tokens = null)
|
||||||
{
|
{
|
||||||
|
if (tokens != null)
|
||||||
|
{
|
||||||
|
_tokens = tokens;
|
||||||
|
}
|
||||||
|
|
||||||
if (node != null)
|
if (node != null)
|
||||||
|
{
|
||||||
|
var first = node.Tokens.FirstOrDefault();
|
||||||
|
if (first != null)
|
||||||
{
|
{
|
||||||
_span = SourceSpan.Merge(node.Tokens.Select(x => x.Span));
|
_span = SourceSpan.Merge(node.Tokens.Select(x => x.Span));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiagnosticBuilder At(Token? token)
|
public DiagnosticBuilder At(Token? token, List<Token>? tokens = null)
|
||||||
{
|
{
|
||||||
|
if (tokens != null)
|
||||||
|
{
|
||||||
|
_tokens = tokens;
|
||||||
|
}
|
||||||
|
|
||||||
if (token != null)
|
if (token != null)
|
||||||
{
|
{
|
||||||
At(token.Span);
|
At(token.Span);
|
||||||
@@ -48,11 +63,11 @@ public class Diagnostic
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiagnosticBuilder At(string filePath, int line, int column)
|
// public DiagnosticBuilder At(string filePath, int line, int column)
|
||||||
{
|
// {
|
||||||
_span = new SourceSpan(filePath, new SourceLocation(line, column), new SourceLocation(line, column));
|
// _span = new SourceSpan(filePath, new SourceLocation(line, column), new SourceLocation(line, column));
|
||||||
return this;
|
// return this;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public DiagnosticBuilder WithHelp(string help)
|
public DiagnosticBuilder WithHelp(string help)
|
||||||
{
|
{
|
||||||
@@ -60,20 +75,23 @@ public class Diagnostic
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Diagnostic Build() => new(_severity, _message, _help, _span);
|
public Diagnostic Build() => new(_severity, _message, _help, _span, _tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DiagnosticBuilder Error(string message) => new(DiagnosticSeverity.Error, message);
|
public static DiagnosticBuilder Error(string message) => new(DiagnosticSeverity.Error, message);
|
||||||
public static DiagnosticBuilder Warning(string message) => new(DiagnosticSeverity.Warning, message);
|
public static DiagnosticBuilder Warning(string message) => new(DiagnosticSeverity.Warning, message);
|
||||||
public static DiagnosticBuilder Info(string message) => new(DiagnosticSeverity.Info, message);
|
public static DiagnosticBuilder Info(string message) => new(DiagnosticSeverity.Info, message);
|
||||||
|
|
||||||
|
private readonly List<Token>? _tokens;
|
||||||
|
|
||||||
public DiagnosticSeverity Severity { get; }
|
public DiagnosticSeverity Severity { get; }
|
||||||
public string Message { get; }
|
public string Message { get; }
|
||||||
public string? Help { get; }
|
public string? Help { get; }
|
||||||
public SourceSpan? Span { get; }
|
public SourceSpan? Span { get; }
|
||||||
|
|
||||||
private Diagnostic(DiagnosticSeverity severity, string message, string? help, SourceSpan? span)
|
private Diagnostic(DiagnosticSeverity severity, string message, string? help, SourceSpan? span, List<Token>? tokens)
|
||||||
{
|
{
|
||||||
|
_tokens = tokens;
|
||||||
Severity = severity;
|
Severity = severity;
|
||||||
Message = message;
|
Message = message;
|
||||||
Help = help;
|
Help = help;
|
||||||
@@ -103,15 +121,12 @@ public class Diagnostic
|
|||||||
if (Span.HasValue)
|
if (Span.HasValue)
|
||||||
{
|
{
|
||||||
sb.AppendLine();
|
sb.AppendLine();
|
||||||
var text = File.ReadAllText(Span.Value.FilePath);
|
var text = Span.Value.Source;
|
||||||
|
|
||||||
var tokenizer = new Tokenizer(Span.Value.FilePath, text);
|
|
||||||
tokenizer.Tokenize();
|
|
||||||
|
|
||||||
var lines = text.Split('\n');
|
var lines = text.Split('\n');
|
||||||
|
|
||||||
var startLine = Span.Value.Start.Line;
|
var startLine = Span.Value.StartLine;
|
||||||
var endLine = Span.Value.End.Line;
|
var endLine = Span.Value.EndLine;
|
||||||
|
|
||||||
const int CONTEXT_LINES = 3;
|
const int CONTEXT_LINES = 3;
|
||||||
|
|
||||||
@@ -144,24 +159,31 @@ public class Diagnostic
|
|||||||
sb.Append("│ ");
|
sb.Append("│ ");
|
||||||
sb.Append(i.ToString().PadRight(numberPadding));
|
sb.Append(i.ToString().PadRight(numberPadding));
|
||||||
sb.Append(" │ ");
|
sb.Append(" │ ");
|
||||||
sb.Append(ApplySyntaxHighlighting(line.PadRight(codePadding), i, tokenizer.Tokens));
|
if (_tokens != null)
|
||||||
// sb.Append(line.PadRight(codePadding));
|
{
|
||||||
|
sb.Append(ApplySyntaxHighlighting(line.PadRight(codePadding), i, _tokens));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sb.Append(line.PadRight(codePadding));
|
||||||
|
}
|
||||||
|
|
||||||
sb.Append(" │");
|
sb.Append(" │");
|
||||||
sb.AppendLine();
|
sb.AppendLine();
|
||||||
|
|
||||||
if (i >= startLine && i <= endLine)
|
if (i >= startLine && i <= endLine)
|
||||||
{
|
{
|
||||||
var markerStartColumn = 1;
|
var markerStartColumn = 1;
|
||||||
var markerEndColumn = line.Length;
|
var markerEndColumn = line.Length + 1;
|
||||||
|
|
||||||
if (i == startLine)
|
if (i == startLine)
|
||||||
{
|
{
|
||||||
markerStartColumn = Span.Value.Start.Column;
|
markerStartColumn = Span.Value.StartColumn;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i == endLine)
|
if (i == endLine)
|
||||||
{
|
{
|
||||||
markerEndColumn = Span.Value.End.Column;
|
markerEndColumn = Span.Value.EndColumn;
|
||||||
}
|
}
|
||||||
|
|
||||||
var markerLength = markerEndColumn - markerStartColumn;
|
var markerLength = markerEndColumn - markerStartColumn;
|
||||||
@@ -206,8 +228,8 @@ public class Diagnostic
|
|||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
var lineTokens = tokens
|
var lineTokens = tokens
|
||||||
.Where(t => t.Span.Start.Line == lineNumber)
|
.Where(t => t.Span.StartLine == lineNumber)
|
||||||
.OrderBy(t => t.Span.Start.Column)
|
.OrderBy(t => t.Span.StartColumn)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
if (lineTokens.Count == 0)
|
if (lineTokens.Count == 0)
|
||||||
@@ -219,8 +241,10 @@ public class Diagnostic
|
|||||||
|
|
||||||
foreach (var token in lineTokens)
|
foreach (var token in lineTokens)
|
||||||
{
|
{
|
||||||
var tokenStart = token.Span.Start.Column;
|
if (token is WhitespaceToken) continue;
|
||||||
var tokenEnd = token.Span.End.Column;
|
|
||||||
|
var tokenStart = token.Span.StartColumn;
|
||||||
|
var tokenEnd = token.Span.EndColumn;
|
||||||
|
|
||||||
if (tokenStart > currentColumn && currentColumn - 1 < line.Length)
|
if (tokenStart > currentColumn && currentColumn - 1 < line.Length)
|
||||||
{
|
{
|
||||||
@@ -262,6 +286,10 @@ public class Diagnostic
|
|||||||
{
|
{
|
||||||
switch (token)
|
switch (token)
|
||||||
{
|
{
|
||||||
|
case CommentToken:
|
||||||
|
{
|
||||||
|
return ConsoleColors.Colorize(tokenText, ConsoleColors.Green);
|
||||||
|
}
|
||||||
case IdentifierToken:
|
case IdentifierToken:
|
||||||
{
|
{
|
||||||
return ConsoleColors.Colorize(tokenText, ConsoleColors.BrightWhite);
|
return ConsoleColors.Colorize(tokenText, ConsoleColors.BrightWhite);
|
||||||
|
|||||||
@@ -1,112 +1,56 @@
|
|||||||
namespace NubLang.Diagnostics;
|
namespace NubLang.Diagnostics;
|
||||||
|
|
||||||
public readonly struct SourceSpan : IEquatable<SourceSpan>, IComparable<SourceSpan>
|
public readonly struct SourceSpan
|
||||||
{
|
{
|
||||||
|
private readonly int _startIndex;
|
||||||
|
private readonly int _endIndex;
|
||||||
|
|
||||||
public static SourceSpan Merge(params IEnumerable<SourceSpan> spans)
|
public static SourceSpan Merge(params IEnumerable<SourceSpan> spans)
|
||||||
{
|
{
|
||||||
var spanArray = spans as SourceSpan[] ?? spans.ToArray();
|
var spanArray = spans as SourceSpan[] ?? spans.ToArray();
|
||||||
if (spanArray.Length == 0)
|
if (spanArray.Length == 0)
|
||||||
{
|
{
|
||||||
return new SourceSpan(string.Empty, new SourceLocation(0, 0), new SourceLocation(0, 0));
|
return new SourceSpan(string.Empty, string.Empty, 0, 0, 0, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var minStart = spanArray.Min(s => s.Start);
|
var first = spanArray.MinBy(x => x._startIndex);
|
||||||
var maxEnd = spanArray.Max(s => s.End);
|
var last = spanArray.MaxBy(x => x._endIndex);
|
||||||
|
|
||||||
return new SourceSpan(spanArray[0].FilePath, minStart, maxEnd);
|
return new SourceSpan(first.SourcePath, first.Source, first._startIndex, last._endIndex, first.StartLine, last.EndLine, first.StartColumn, last.EndColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SourceSpan(string filePath, SourceLocation start, SourceLocation end)
|
public SourceSpan(string sourcePath, string source, int startIndex, int endIndex, int startLine, int startColumn, int endLine, int endColumn)
|
||||||
{
|
{
|
||||||
if (start > end)
|
_startIndex = startIndex;
|
||||||
{
|
_endIndex = endIndex;
|
||||||
throw new ArgumentException("Start location cannot be after end location");
|
SourcePath = sourcePath;
|
||||||
|
Source = source;
|
||||||
|
StartLine = startLine;
|
||||||
|
StartColumn = startColumn;
|
||||||
|
EndLine = endLine;
|
||||||
|
EndColumn = endColumn;
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePath = filePath;
|
public int StartLine { get; }
|
||||||
Start = start;
|
public int StartColumn { get; }
|
||||||
End = end;
|
public int EndLine { get; }
|
||||||
}
|
public int EndColumn { get; }
|
||||||
|
|
||||||
public string FilePath { get; }
|
public string SourcePath { get; }
|
||||||
public SourceLocation Start { get; }
|
public string Source { get; }
|
||||||
public SourceLocation End { get; }
|
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
if (Start == End)
|
if (StartLine == EndLine && StartColumn == EndColumn)
|
||||||
{
|
{
|
||||||
return $"{FilePath}:{Start}";
|
return $"{SourcePath}:{StartColumn}:{StartColumn}";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Start.Line == End.Line)
|
if (StartLine == EndLine)
|
||||||
{
|
{
|
||||||
return Start.Column == End.Column ? $"{FilePath}:{Start}" : $"{FilePath}:{Start.Line}:{Start.Column}-{End.Column}";
|
return $"{SourcePath}:{StartLine}:{StartColumn}-{EndColumn}";
|
||||||
}
|
}
|
||||||
|
|
||||||
return $"{FilePath}:{Start}-{End}";
|
return $"{SourcePath}:{StartLine}:{StartColumn}-{EndLine}:{EndColumn}";
|
||||||
}
|
|
||||||
|
|
||||||
public bool Equals(SourceSpan other) => Start == other.Start && End == other.End;
|
|
||||||
public override bool Equals(object? obj) => obj is SourceSpan other && Equals(other);
|
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(SourceSpan), Start, End);
|
|
||||||
|
|
||||||
public static bool operator ==(SourceSpan left, SourceSpan right) => Equals(left, right);
|
|
||||||
public static bool operator !=(SourceSpan left, SourceSpan right) => !Equals(left, right);
|
|
||||||
|
|
||||||
public static bool operator <(SourceSpan left, SourceSpan right) => left.CompareTo(right) < 0;
|
|
||||||
public static bool operator <=(SourceSpan left, SourceSpan right) => left.CompareTo(right) <= 0;
|
|
||||||
public static bool operator >(SourceSpan left, SourceSpan right) => left.CompareTo(right) > 0;
|
|
||||||
public static bool operator >=(SourceSpan left, SourceSpan right) => left.CompareTo(right) >= 0;
|
|
||||||
|
|
||||||
public int CompareTo(SourceSpan other)
|
|
||||||
{
|
|
||||||
var startComparison = Start.CompareTo(other.Start);
|
|
||||||
return startComparison != 0 ? startComparison : End.CompareTo(other.End);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public readonly struct SourceLocation : IEquatable<SourceLocation>, IComparable<SourceLocation>
|
|
||||||
{
|
|
||||||
public SourceLocation(int line, int column)
|
|
||||||
{
|
|
||||||
Line = line;
|
|
||||||
Column = column;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Line { get; }
|
|
||||||
public int Column { get; }
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return $"{Line}:{Column}";
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool Equals(object? obj)
|
|
||||||
{
|
|
||||||
return obj is SourceLocation other && Equals(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Equals(SourceLocation other)
|
|
||||||
{
|
|
||||||
return Line == other.Line && Column == other.Column;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return HashCode.Combine(typeof(SourceLocation), Line, Column);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator ==(SourceLocation left, SourceLocation right) => Equals(left, right);
|
|
||||||
public static bool operator !=(SourceLocation left, SourceLocation right) => !Equals(left, right);
|
|
||||||
public static bool operator <(SourceLocation left, SourceLocation right) => left.Line < right.Line || (left.Line == right.Line && left.Column < right.Column);
|
|
||||||
public static bool operator >(SourceLocation left, SourceLocation right) => left.Line > right.Line || (left.Line == right.Line && left.Column > right.Column);
|
|
||||||
public static bool operator <=(SourceLocation left, SourceLocation right) => left.Line <= right.Line || (left.Line == right.Line && left.Column <= right.Column);
|
|
||||||
public static bool operator >=(SourceLocation left, SourceLocation right) => left.Line >= right.Line || (left.Line == right.Line && left.Column >= right.Column);
|
|
||||||
|
|
||||||
public int CompareTo(SourceLocation other)
|
|
||||||
{
|
|
||||||
var lineComparison = Line.CompareTo(other.Line);
|
|
||||||
return lineComparison != 0 ? lineComparison : Column.CompareTo(other.Column);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
using NubLang.Ast;
|
|
||||||
|
|
||||||
namespace NubLang.Generation;
|
|
||||||
|
|
||||||
public static class CType
|
|
||||||
{
|
|
||||||
public static string Create(NubType type, string? variableName = null, bool constArraysAsPointers = true)
|
|
||||||
{
|
|
||||||
return type switch
|
|
||||||
{
|
|
||||||
NubVoidType => "void" + (variableName != null ? $" {variableName}" : ""),
|
|
||||||
NubBoolType => "bool" + (variableName != null ? $" {variableName}" : ""),
|
|
||||||
NubIntType intType => CreateIntType(intType, variableName),
|
|
||||||
NubFloatType floatType => CreateFloatType(floatType, variableName),
|
|
||||||
NubCStringType => "char*" + (variableName != null ? $" {variableName}" : ""),
|
|
||||||
NubPointerType ptr => CreatePointerType(ptr, variableName),
|
|
||||||
NubSliceType => "slice" + (variableName != null ? $" {variableName}" : ""),
|
|
||||||
NubStringType => "string" + (variableName != null ? $" {variableName}" : ""),
|
|
||||||
NubConstArrayType arr => CreateConstArrayType(arr, variableName, constArraysAsPointers),
|
|
||||||
NubArrayType arr => CreateArrayType(arr, variableName),
|
|
||||||
NubFuncType fn => CreateFuncType(fn, variableName),
|
|
||||||
NubStructType st => $"{st.Module}_{st.Name}" + (variableName != null ? $" {variableName}" : ""),
|
|
||||||
_ => throw new NotSupportedException($"C type generation not supported for: {type}")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string CreateIntType(NubIntType intType, string? varName)
|
|
||||||
{
|
|
||||||
var cType = intType.Width switch
|
|
||||||
{
|
|
||||||
8 => intType.Signed ? "int8_t" : "uint8_t",
|
|
||||||
16 => intType.Signed ? "int16_t" : "uint16_t",
|
|
||||||
32 => intType.Signed ? "int32_t" : "uint32_t",
|
|
||||||
64 => intType.Signed ? "int64_t" : "uint64_t",
|
|
||||||
_ => throw new NotSupportedException($"Unsupported integer width: {intType.Width}")
|
|
||||||
};
|
|
||||||
return cType + (varName != null ? $" {varName}" : "");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string CreateFloatType(NubFloatType floatType, string? varName)
|
|
||||||
{
|
|
||||||
var cType = floatType.Width switch
|
|
||||||
{
|
|
||||||
32 => "float",
|
|
||||||
64 => "double",
|
|
||||||
_ => throw new NotSupportedException($"Unsupported float width: {floatType.Width}")
|
|
||||||
};
|
|
||||||
return cType + (varName != null ? $" {varName}" : "");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string CreatePointerType(NubPointerType ptr, string? varName)
|
|
||||||
{
|
|
||||||
var baseType = Create(ptr.BaseType);
|
|
||||||
return baseType + "*" + (varName != null ? $" {varName}" : "");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string CreateConstArrayType(NubConstArrayType arr, string? varName, bool inStructDef)
|
|
||||||
{
|
|
||||||
var elementType = Create(arr.ElementType);
|
|
||||||
|
|
||||||
// Treat const arrays as pointers unless in a struct definition
|
|
||||||
if (!inStructDef)
|
|
||||||
{
|
|
||||||
return elementType + "*" + (varName != null ? $" {varName}" : "");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (varName != null)
|
|
||||||
{
|
|
||||||
return $"{elementType} {varName}[{arr.Size}]";
|
|
||||||
}
|
|
||||||
|
|
||||||
return $"{elementType}[{arr.Size}]";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string CreateArrayType(NubArrayType arr, string? varName)
|
|
||||||
{
|
|
||||||
var elementType = Create(arr.ElementType);
|
|
||||||
return elementType + "*" + (varName != null ? $" {varName}" : "");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string CreateFuncType(NubFuncType fn, string? varName)
|
|
||||||
{
|
|
||||||
var returnType = Create(fn.ReturnType);
|
|
||||||
var parameters = string.Join(", ", fn.Parameters.Select(p => Create(p)));
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(parameters))
|
|
||||||
{
|
|
||||||
parameters = "void";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (varName != null)
|
|
||||||
{
|
|
||||||
return $"{returnType} (*{varName})({parameters})";
|
|
||||||
}
|
|
||||||
|
|
||||||
return $"{returnType} (*)({parameters})";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,604 +0,0 @@
|
|||||||
using System.Diagnostics;
|
|
||||||
using System.Text;
|
|
||||||
using NubLang.Ast;
|
|
||||||
using NubLang.Syntax;
|
|
||||||
|
|
||||||
namespace NubLang.Generation;
|
|
||||||
|
|
||||||
public class Generator
|
|
||||||
{
|
|
||||||
private readonly CompilationUnit _compilationUnit;
|
|
||||||
private readonly IndentedTextWriter _writer;
|
|
||||||
private readonly Stack<List<DeferNode>> _deferStack = [];
|
|
||||||
private int _tmpIndex;
|
|
||||||
|
|
||||||
public Generator(CompilationUnit compilationUnit)
|
|
||||||
{
|
|
||||||
_compilationUnit = compilationUnit;
|
|
||||||
_writer = new IndentedTextWriter();
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo(nub31): Handle name collisions
|
|
||||||
private string NewTmp()
|
|
||||||
{
|
|
||||||
return $"_t{++_tmpIndex}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string FuncName(string module, string name, string? externSymbol)
|
|
||||||
{
|
|
||||||
return externSymbol ?? $"{module}_{name}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string StructName(string module, string name)
|
|
||||||
{
|
|
||||||
return $"{module}_{name}";
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Emit()
|
|
||||||
{
|
|
||||||
_writer.WriteLine("""
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
size_t length;
|
|
||||||
char *data;
|
|
||||||
} string;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
size_t length;
|
|
||||||
void *data;
|
|
||||||
} slice;
|
|
||||||
|
|
||||||
""");
|
|
||||||
|
|
||||||
foreach (var structType in _compilationUnit.ImportedStructTypes)
|
|
||||||
{
|
|
||||||
_writer.WriteLine("typedef struct");
|
|
||||||
_writer.WriteLine("{");
|
|
||||||
using (_writer.Indent())
|
|
||||||
{
|
|
||||||
foreach (var field in structType.Fields)
|
|
||||||
{
|
|
||||||
_writer.WriteLine($"{CType.Create(field.Type, field.Name, constArraysAsPointers: false)};");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_writer.WriteLine($"}} {StructName(structType.Module, structType.Name)};");
|
|
||||||
_writer.WriteLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
// note(nub31): Forward declarations
|
|
||||||
foreach (var prototype in _compilationUnit.ImportedFunctions)
|
|
||||||
{
|
|
||||||
EmitLine(prototype.Tokens.FirstOrDefault());
|
|
||||||
var parameters = prototype.Parameters.Count != 0
|
|
||||||
? string.Join(", ", prototype.Parameters.Select(x => CType.Create(x.Type, x.Name)))
|
|
||||||
: "void";
|
|
||||||
|
|
||||||
var name = FuncName(prototype.Module, prototype.Name, prototype.ExternSymbol);
|
|
||||||
_writer.WriteLine($"{CType.Create(prototype.ReturnType, name)}({parameters});");
|
|
||||||
_writer.WriteLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
// note(nub31): Normal functions
|
|
||||||
foreach (var funcNode in _compilationUnit.Functions)
|
|
||||||
{
|
|
||||||
if (funcNode.Body == null) continue;
|
|
||||||
|
|
||||||
EmitLine(funcNode.Tokens.FirstOrDefault());
|
|
||||||
var parameters = funcNode.Prototype.Parameters.Count != 0
|
|
||||||
? string.Join(", ", funcNode.Prototype.Parameters.Select(x => CType.Create(x.Type, x.Name)))
|
|
||||||
: "void";
|
|
||||||
|
|
||||||
var name = FuncName(funcNode.Module, funcNode.Name, funcNode.Prototype.ExternSymbol);
|
|
||||||
_writer.WriteLine($"{CType.Create(funcNode.Prototype.ReturnType, name)}({parameters})");
|
|
||||||
_writer.WriteLine("{");
|
|
||||||
using (_writer.Indent())
|
|
||||||
{
|
|
||||||
EmitBlock(funcNode.Body);
|
|
||||||
}
|
|
||||||
|
|
||||||
_writer.WriteLine("}");
|
|
||||||
_writer.WriteLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
return _writer.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitStatement(StatementNode statementNode)
|
|
||||||
{
|
|
||||||
EmitLine(statementNode.Tokens.FirstOrDefault());
|
|
||||||
switch (statementNode)
|
|
||||||
{
|
|
||||||
case AssignmentNode assignmentNode:
|
|
||||||
EmitAssignment(assignmentNode);
|
|
||||||
break;
|
|
||||||
case BlockNode blockNode:
|
|
||||||
_writer.WriteLine("{");
|
|
||||||
using (_writer.Indent())
|
|
||||||
{
|
|
||||||
EmitBlock(blockNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
_writer.WriteLine("}");
|
|
||||||
break;
|
|
||||||
case BreakNode breakNode:
|
|
||||||
EmitBreak(breakNode);
|
|
||||||
break;
|
|
||||||
case ContinueNode continueNode:
|
|
||||||
EmitContinue(continueNode);
|
|
||||||
break;
|
|
||||||
case DeferNode deferNode:
|
|
||||||
EmitDefer(deferNode);
|
|
||||||
break;
|
|
||||||
case ForConstArrayNode forConstArrayNode:
|
|
||||||
EmitForConstArray(forConstArrayNode);
|
|
||||||
break;
|
|
||||||
case ForSliceNode forSliceNode:
|
|
||||||
EmitForSlice(forSliceNode);
|
|
||||||
break;
|
|
||||||
case IfNode ifNode:
|
|
||||||
EmitIf(ifNode);
|
|
||||||
break;
|
|
||||||
case ReturnNode returnNode:
|
|
||||||
EmitReturn(returnNode);
|
|
||||||
break;
|
|
||||||
case StatementFuncCallNode statementFuncCallNode:
|
|
||||||
EmitStatementFuncCall(statementFuncCallNode);
|
|
||||||
break;
|
|
||||||
case VariableDeclarationNode variableDeclarationNode:
|
|
||||||
EmitVariableDeclaration(variableDeclarationNode);
|
|
||||||
break;
|
|
||||||
case WhileNode whileNode:
|
|
||||||
EmitWhile(whileNode);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(statementNode));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitLine(Token? token)
|
|
||||||
{
|
|
||||||
if (token == null) return;
|
|
||||||
var file = token.Span.FilePath;
|
|
||||||
var line = token.Span.Start.Line;
|
|
||||||
_writer.WriteLine($"#line {line} \"{file}\"");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitAssignment(AssignmentNode assignmentNode)
|
|
||||||
{
|
|
||||||
var target = EmitExpression(assignmentNode.Target);
|
|
||||||
var value = EmitExpression(assignmentNode.Value);
|
|
||||||
_writer.WriteLine($"{target} = {value};");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitBreak(BreakNode _)
|
|
||||||
{
|
|
||||||
// todo(nub31): Emit deferred statements
|
|
||||||
_writer.WriteLine("break;");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitContinue(ContinueNode _)
|
|
||||||
{
|
|
||||||
// todo(nub31): Emit deferred statements
|
|
||||||
_writer.WriteLine("continue;");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitDefer(DeferNode deferNode)
|
|
||||||
{
|
|
||||||
_deferStack.Peek().Add(deferNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitForSlice(ForSliceNode forSliceNode)
|
|
||||||
{
|
|
||||||
var targetType = (NubSliceType)forSliceNode.Target.Type;
|
|
||||||
var target = EmitExpression(forSliceNode.Target);
|
|
||||||
var indexName = forSliceNode.IndexName ?? NewTmp();
|
|
||||||
|
|
||||||
_writer.WriteLine($"for (size_t {indexName} = 0; {indexName} < {target}.length; ++{indexName})");
|
|
||||||
_writer.WriteLine("{");
|
|
||||||
using (_writer.Indent())
|
|
||||||
{
|
|
||||||
_writer.WriteLine($"{CType.Create(targetType.ElementType, forSliceNode.ElementName)} = (({CType.Create(targetType.ElementType)}*){target}.data)[{indexName}];");
|
|
||||||
EmitBlock(forSliceNode.Body);
|
|
||||||
}
|
|
||||||
|
|
||||||
_writer.WriteLine("}");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitForConstArray(ForConstArrayNode forConstArrayNode)
|
|
||||||
{
|
|
||||||
var targetType = (NubConstArrayType)forConstArrayNode.Target.Type;
|
|
||||||
var target = EmitExpression(forConstArrayNode.Target);
|
|
||||||
var indexName = forConstArrayNode.IndexName ?? NewTmp();
|
|
||||||
|
|
||||||
_writer.WriteLine($"for (size_t {indexName} = 0; {indexName} < {targetType.Size}; ++{indexName})");
|
|
||||||
_writer.WriteLine("{");
|
|
||||||
using (_writer.Indent())
|
|
||||||
{
|
|
||||||
_writer.WriteLine($"{CType.Create(targetType.ElementType, forConstArrayNode.ElementName)} = {target}[{indexName}];");
|
|
||||||
EmitBlock(forConstArrayNode.Body);
|
|
||||||
}
|
|
||||||
|
|
||||||
_writer.WriteLine("}");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitIf(IfNode ifNode, bool elseIf = false)
|
|
||||||
{
|
|
||||||
var condition = EmitExpression(ifNode.Condition);
|
|
||||||
_writer.WriteLine($"{(elseIf ? "else " : "")}if ({condition})");
|
|
||||||
_writer.WriteLine("{");
|
|
||||||
using (_writer.Indent())
|
|
||||||
{
|
|
||||||
EmitBlock(ifNode.Body);
|
|
||||||
}
|
|
||||||
|
|
||||||
_writer.WriteLine("}");
|
|
||||||
ifNode.Else?.Match
|
|
||||||
(
|
|
||||||
elseIfNode => EmitIf(elseIfNode, true),
|
|
||||||
elseNode =>
|
|
||||||
{
|
|
||||||
_writer.WriteLine("else");
|
|
||||||
_writer.WriteLine("{");
|
|
||||||
using (_writer.Indent())
|
|
||||||
{
|
|
||||||
EmitBlock(elseNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
_writer.WriteLine("}");
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitReturn(ReturnNode returnNode)
|
|
||||||
{
|
|
||||||
if (returnNode.Value == null)
|
|
||||||
{
|
|
||||||
var blockDefers = _deferStack.Peek();
|
|
||||||
for (var i = blockDefers.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
EmitStatement(blockDefers[i].Statement);
|
|
||||||
}
|
|
||||||
|
|
||||||
_writer.WriteLine("return;");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var returnValue = EmitExpression(returnNode.Value);
|
|
||||||
|
|
||||||
if (_deferStack.Peek().Count != 0)
|
|
||||||
{
|
|
||||||
var tmp = NewTmp();
|
|
||||||
_writer.WriteLine($"{CType.Create(returnNode.Value.Type, tmp)} = {returnValue};");
|
|
||||||
|
|
||||||
var blockDefers = _deferStack.Peek();
|
|
||||||
for (var i = blockDefers.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
EmitStatement(blockDefers[i].Statement);
|
|
||||||
}
|
|
||||||
|
|
||||||
EmitLine(returnNode.Tokens.FirstOrDefault());
|
|
||||||
_writer.WriteLine($"return {tmp};");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
EmitLine(returnNode.Tokens.FirstOrDefault());
|
|
||||||
_writer.WriteLine($"return {returnValue};");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitStatementFuncCall(StatementFuncCallNode statementFuncCallNode)
|
|
||||||
{
|
|
||||||
var funcCall = EmitFuncCall(statementFuncCallNode.FuncCall);
|
|
||||||
_writer.WriteLine($"{funcCall};");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitVariableDeclaration(VariableDeclarationNode variableDeclarationNode)
|
|
||||||
{
|
|
||||||
if (variableDeclarationNode.Assignment != null)
|
|
||||||
{
|
|
||||||
var value = EmitExpression(variableDeclarationNode.Assignment);
|
|
||||||
_writer.WriteLine($"{CType.Create(variableDeclarationNode.Type, variableDeclarationNode.Name)} = {value};");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_writer.WriteLine($"{CType.Create(variableDeclarationNode.Type, variableDeclarationNode.Name)};");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitWhile(WhileNode whileNode)
|
|
||||||
{
|
|
||||||
var condition = EmitExpression(whileNode.Condition);
|
|
||||||
_writer.WriteLine($"while ({condition})");
|
|
||||||
_writer.WriteLine("{");
|
|
||||||
using (_writer.Indent())
|
|
||||||
{
|
|
||||||
EmitBlock(whileNode.Body);
|
|
||||||
}
|
|
||||||
|
|
||||||
_writer.WriteLine("}");
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitExpression(ExpressionNode expressionNode)
|
|
||||||
{
|
|
||||||
if (expressionNode is IntermediateExpression)
|
|
||||||
{
|
|
||||||
throw new UnreachableException("Type checker fucked up");
|
|
||||||
}
|
|
||||||
|
|
||||||
var expr = expressionNode switch
|
|
||||||
{
|
|
||||||
ArrayIndexAccessNode arrayIndexAccessNode => EmitArrayIndexAccess(arrayIndexAccessNode),
|
|
||||||
ArrayInitializerNode arrayInitializerNode => EmitArrayInitializer(arrayInitializerNode),
|
|
||||||
BinaryExpressionNode binaryExpressionNode => EmitBinaryExpression(binaryExpressionNode),
|
|
||||||
BoolLiteralNode boolLiteralNode => boolLiteralNode.Value ? "true" : "false",
|
|
||||||
ConstArrayIndexAccessNode constArrayIndexAccessNode => EmitConstArrayIndexAccess(constArrayIndexAccessNode),
|
|
||||||
ConstArrayInitializerNode constArrayInitializerNode => EmitConstArrayInitializer(constArrayInitializerNode),
|
|
||||||
CStringLiteralNode cStringLiteralNode => $"\"{cStringLiteralNode.Value}\"",
|
|
||||||
DereferenceNode dereferenceNode => EmitDereference(dereferenceNode),
|
|
||||||
Float32LiteralNode float32LiteralNode => EmitFloat32Literal(float32LiteralNode),
|
|
||||||
Float64LiteralNode float64LiteralNode => EmitFloat64Literal(float64LiteralNode),
|
|
||||||
CastNode castNode => EmitCast(castNode),
|
|
||||||
FuncCallNode funcCallNode => EmitFuncCall(funcCallNode),
|
|
||||||
FuncIdentifierNode funcIdentifierNode => FuncName(funcIdentifierNode.Module, funcIdentifierNode.Name, funcIdentifierNode.ExternSymbol),
|
|
||||||
AddressOfNode addressOfNode => EmitAddressOf(addressOfNode),
|
|
||||||
SizeNode sizeBuiltinNode => $"sizeof({CType.Create(sizeBuiltinNode.TargetType)})",
|
|
||||||
SliceIndexAccessNode sliceIndexAccessNode => EmitSliceArrayIndexAccess(sliceIndexAccessNode),
|
|
||||||
StringLiteralNode stringLiteralNode => EmitStringLiteral(stringLiteralNode),
|
|
||||||
StructFieldAccessNode structFieldAccessNode => EmitStructFieldAccess(structFieldAccessNode),
|
|
||||||
StructInitializerNode structInitializerNode => EmitStructInitializer(structInitializerNode),
|
|
||||||
I8LiteralNode i8LiteralNode => EmitI8Literal(i8LiteralNode),
|
|
||||||
I16LiteralNode i16LiteralNode => EmitI16Literal(i16LiteralNode),
|
|
||||||
I32LiteralNode i32LiteralNode => EmitI32Literal(i32LiteralNode),
|
|
||||||
I64LiteralNode i64LiteralNode => EmitI64Literal(i64LiteralNode),
|
|
||||||
U8LiteralNode u8LiteralNode => EmitU8Literal(u8LiteralNode),
|
|
||||||
U16LiteralNode u16LiteralNode => EmitU16Literal(u16LiteralNode),
|
|
||||||
U32LiteralNode u32LiteralNode => EmitU32Literal(u32LiteralNode),
|
|
||||||
U64LiteralNode u64LiteralNode => EmitU64Literal(u64LiteralNode),
|
|
||||||
UnaryExpressionNode unaryExpressionNode => EmitUnaryExpression(unaryExpressionNode),
|
|
||||||
VariableIdentifierNode variableIdentifierNode => variableIdentifierNode.Name,
|
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(expressionNode))
|
|
||||||
};
|
|
||||||
|
|
||||||
return $"({expr})";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitArrayIndexAccess(ArrayIndexAccessNode arrayIndexAccessNode)
|
|
||||||
{
|
|
||||||
var target = EmitExpression(arrayIndexAccessNode.Target);
|
|
||||||
var index = EmitExpression(arrayIndexAccessNode.Index);
|
|
||||||
return $"{target}[{index}]";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitArrayInitializer(ArrayInitializerNode arrayInitializerNode)
|
|
||||||
{
|
|
||||||
var values = new List<string>();
|
|
||||||
foreach (var value in arrayInitializerNode.Values)
|
|
||||||
{
|
|
||||||
values.Add(EmitExpression(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
var arrayType = (NubArrayType)arrayInitializerNode.Type;
|
|
||||||
return $"({CType.Create(arrayType.ElementType)}[]){{{string.Join(", ", values)}}}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitBinaryExpression(BinaryExpressionNode binaryExpressionNode)
|
|
||||||
{
|
|
||||||
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 EmitConstArrayIndexAccess(ConstArrayIndexAccessNode constArrayIndexAccessNode)
|
|
||||||
{
|
|
||||||
var target = EmitExpression(constArrayIndexAccessNode.Target);
|
|
||||||
var index = EmitExpression(constArrayIndexAccessNode.Index);
|
|
||||||
// todo(nub31): We can emit bounds checking here
|
|
||||||
return $"{target}[{index}]";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitConstArrayInitializer(ConstArrayInitializerNode arrayInitializerNode)
|
|
||||||
{
|
|
||||||
var values = new List<string>();
|
|
||||||
foreach (var value in arrayInitializerNode.Values)
|
|
||||||
{
|
|
||||||
values.Add(EmitExpression(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
var arrayType = (NubConstArrayType)arrayInitializerNode.Type;
|
|
||||||
return $"({CType.Create(arrayType.ElementType)}[{arrayType.Size}]){{{string.Join(", ", values)}}}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitDereference(DereferenceNode dereferenceNode)
|
|
||||||
{
|
|
||||||
var pointer = EmitExpression(dereferenceNode.Target);
|
|
||||||
return $"*{pointer}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitFloat32Literal(Float32LiteralNode float32LiteralNode)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
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 EmitCast(CastNode castNode)
|
|
||||||
{
|
|
||||||
var value = EmitExpression(castNode.Value);
|
|
||||||
|
|
||||||
if (castNode is { Type: NubSliceType, Value.Type: NubConstArrayType arrayType })
|
|
||||||
{
|
|
||||||
return $"(slice){{.length = {arrayType.Size}, .data = (void*){value}}}";
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo(nub31): Stop depending on libc
|
|
||||||
if (castNode is { Type: NubCStringType, Value.Type: NubStringType })
|
|
||||||
{
|
|
||||||
return $"(string){{.length = strlen({value}), .data = {value}}}";
|
|
||||||
}
|
|
||||||
|
|
||||||
return $"({CType.Create(castNode.Type)}){value}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitFuncCall(FuncCallNode funcCallNode)
|
|
||||||
{
|
|
||||||
var name = EmitExpression(funcCallNode.Expression);
|
|
||||||
var parameterNames = funcCallNode.Parameters.Select(EmitExpression).ToList();
|
|
||||||
return $"{name}({string.Join(", ", parameterNames)})";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitAddressOf(AddressOfNode addressOfNode)
|
|
||||||
{
|
|
||||||
var value = EmitExpression(addressOfNode.LValue);
|
|
||||||
return $"&{value}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitSliceArrayIndexAccess(SliceIndexAccessNode sliceIndexAccessNode)
|
|
||||||
{
|
|
||||||
var targetType = (NubSliceType)sliceIndexAccessNode.Target.Type;
|
|
||||||
var target = EmitExpression(sliceIndexAccessNode.Target);
|
|
||||||
var index = EmitExpression(sliceIndexAccessNode.Index);
|
|
||||||
// todo(nub31): We can emit bounds checking here
|
|
||||||
return $"(({CType.Create(targetType.ElementType)}*){target}.data)[{index}]";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitStringLiteral(StringLiteralNode stringLiteralNode)
|
|
||||||
{
|
|
||||||
var length = Encoding.UTF8.GetByteCount(stringLiteralNode.Value);
|
|
||||||
return $"(string){{.length = {length}, .data = \"{stringLiteralNode.Value}\"}}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitStructFieldAccess(StructFieldAccessNode structFieldAccessNode)
|
|
||||||
{
|
|
||||||
var structExpr = EmitExpression(structFieldAccessNode.Target);
|
|
||||||
return $"{structExpr}.{structFieldAccessNode.Field}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitStructInitializer(StructInitializerNode structInitializerNode)
|
|
||||||
{
|
|
||||||
var initValues = new List<string>();
|
|
||||||
foreach (var initializer in structInitializerNode.Initializers)
|
|
||||||
{
|
|
||||||
var value = EmitExpression(initializer.Value);
|
|
||||||
initValues.Add($".{initializer.Key} = {value}");
|
|
||||||
}
|
|
||||||
|
|
||||||
var initString = initValues.Count == 0
|
|
||||||
? "0"
|
|
||||||
: string.Join(", ", initValues);
|
|
||||||
|
|
||||||
return $"({CType.Create(structInitializerNode.Type)}){{{initString}}}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitI8Literal(I8LiteralNode i8LiteralNode)
|
|
||||||
{
|
|
||||||
return i8LiteralNode.Value.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitI16Literal(I16LiteralNode i16LiteralNode)
|
|
||||||
{
|
|
||||||
return i16LiteralNode.Value.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitI32Literal(I32LiteralNode i32LiteralNode)
|
|
||||||
{
|
|
||||||
return i32LiteralNode.Value.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitI64Literal(I64LiteralNode i64LiteralNode)
|
|
||||||
{
|
|
||||||
return i64LiteralNode.Value + "LL";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitU8Literal(U8LiteralNode u8LiteralNode)
|
|
||||||
{
|
|
||||||
return u8LiteralNode.Value.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitU16Literal(U16LiteralNode u16LiteralNode)
|
|
||||||
{
|
|
||||||
return u16LiteralNode.Value.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitU32Literal(U32LiteralNode u32LiteralNode)
|
|
||||||
{
|
|
||||||
return u32LiteralNode.Value.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitU64Literal(U64LiteralNode u64LiteralNode)
|
|
||||||
{
|
|
||||||
return u64LiteralNode.Value + "ULL";
|
|
||||||
}
|
|
||||||
|
|
||||||
private string EmitUnaryExpression(UnaryExpressionNode unaryExpressionNode)
|
|
||||||
{
|
|
||||||
var value = EmitExpression(unaryExpressionNode.Operand);
|
|
||||||
|
|
||||||
return unaryExpressionNode.Operator switch
|
|
||||||
{
|
|
||||||
UnaryOperator.Negate => $"-{value}",
|
|
||||||
UnaryOperator.Invert => $"!{value}",
|
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitBlock(BlockNode blockNode)
|
|
||||||
{
|
|
||||||
_deferStack.Push([]);
|
|
||||||
|
|
||||||
foreach (var statementNode in blockNode.Statements)
|
|
||||||
{
|
|
||||||
EmitStatement(statementNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
var blockDefers = _deferStack.Pop();
|
|
||||||
for (var i = blockDefers.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
EmitStatement(blockDefers[i].Statement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
781
compiler/NubLang/Generation/LlvmSharpGenerator.cs
Normal file
781
compiler/NubLang/Generation/LlvmSharpGenerator.cs
Normal file
@@ -0,0 +1,781 @@
|
|||||||
|
using System.Text;
|
||||||
|
using LLVMSharp.Interop;
|
||||||
|
using NubLang.Ast;
|
||||||
|
using NubLang.Modules;
|
||||||
|
using NubLang.Types;
|
||||||
|
|
||||||
|
namespace NubLang.Generation;
|
||||||
|
|
||||||
|
public class LlvmSharpGenerator
|
||||||
|
{
|
||||||
|
private string _module = string.Empty;
|
||||||
|
private LLVMContextRef _context;
|
||||||
|
private LLVMModuleRef _llvmModule;
|
||||||
|
private LLVMBuilderRef _builder;
|
||||||
|
private readonly Dictionary<string, LLVMTypeRef> _structTypes = new();
|
||||||
|
private readonly Dictionary<string, LLVMValueRef> _functions = new();
|
||||||
|
private readonly Dictionary<string, LLVMValueRef> _locals = new();
|
||||||
|
private readonly Stack<(LLVMBasicBlockRef breakBlock, LLVMBasicBlockRef continueBlock)> _loopStack = new();
|
||||||
|
private readonly Stack<Scope> _scopes = new();
|
||||||
|
|
||||||
|
private Scope CurrentScope => _scopes.Peek();
|
||||||
|
|
||||||
|
public void Emit(List<TopLevelNode> topLevelNodes, ModuleRepository repository, string sourceFileName, string outputPath)
|
||||||
|
{
|
||||||
|
_module = topLevelNodes.OfType<ModuleNode>().First().NameToken.Value;
|
||||||
|
|
||||||
|
_context = LLVMContextRef.Global;
|
||||||
|
_llvmModule = _context.CreateModuleWithName(sourceFileName);
|
||||||
|
_llvmModule.Target = "x86_64-pc-linux-gnu";
|
||||||
|
_llvmModule.DataLayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128";
|
||||||
|
|
||||||
|
_builder = _context.CreateBuilder();
|
||||||
|
|
||||||
|
_structTypes.Clear();
|
||||||
|
_functions.Clear();
|
||||||
|
_locals.Clear();
|
||||||
|
_loopStack.Clear();
|
||||||
|
_scopes.Clear();
|
||||||
|
|
||||||
|
var stringType = _context.CreateNamedStruct("nub.string");
|
||||||
|
stringType.StructSetBody([LLVMTypeRef.Int64, LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0)], false);
|
||||||
|
_structTypes["nub.string"] = stringType;
|
||||||
|
|
||||||
|
// note(nub31): Declare all structs and functions
|
||||||
|
foreach (var module in repository.GetAll())
|
||||||
|
{
|
||||||
|
foreach (var structType in module.StructTypes)
|
||||||
|
{
|
||||||
|
var llvmStructType = _context.CreateNamedStruct(StructName(structType.Module, structType.Name));
|
||||||
|
llvmStructType.StructSetBody(structType.Fields.Select(f => MapType(f.Type)).ToArray(), structType.Packed);
|
||||||
|
_structTypes[StructName(structType.Module, structType.Name)] = llvmStructType;
|
||||||
|
|
||||||
|
var constructorType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, [LLVMTypeRef.CreatePointer(llvmStructType, 0)]);
|
||||||
|
var constructor = _llvmModule.AddFunction(StructConstructorName(structType.Module, structType.Name), constructorType);
|
||||||
|
|
||||||
|
_functions[StructConstructorName(structType.Module, structType.Name)] = constructor;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var prototype in module.FunctionPrototypes)
|
||||||
|
{
|
||||||
|
var funcName = FuncName(module.Name, prototype.NameToken.Value, prototype.ExternSymbolToken?.Value);
|
||||||
|
|
||||||
|
var paramTypes = prototype.Parameters.Select(p => MapType(p.Type)).ToArray();
|
||||||
|
var funcType = LLVMTypeRef.CreateFunction(MapType(prototype.ReturnType), paramTypes);
|
||||||
|
var func = _llvmModule.AddFunction(funcName, funcType);
|
||||||
|
|
||||||
|
func.FunctionCallConv = (uint)LLVMCallConv.LLVMCCallConv;
|
||||||
|
|
||||||
|
_functions[funcName] = func;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// note(nub31): Define struct constructors
|
||||||
|
foreach (var structNode in topLevelNodes.OfType<StructNode>())
|
||||||
|
{
|
||||||
|
var structType = _structTypes[StructName(_module, structNode.NameToken.Value)];
|
||||||
|
var constructor = _functions[StructConstructorName(_module, structNode.NameToken.Value)];
|
||||||
|
|
||||||
|
var entryBlock = constructor.AppendBasicBlock("entry");
|
||||||
|
_builder.PositionAtEnd(entryBlock);
|
||||||
|
|
||||||
|
var selfParam = constructor.GetParam(0);
|
||||||
|
selfParam.Name = "self";
|
||||||
|
|
||||||
|
_locals.Clear();
|
||||||
|
|
||||||
|
foreach (var field in structNode.Fields)
|
||||||
|
{
|
||||||
|
if (field.Value == null) continue;
|
||||||
|
|
||||||
|
var index = structNode.StructType.GetFieldIndex(field.NameToken.Value);
|
||||||
|
var fieldPtr = _builder.BuildStructGEP2(structType, selfParam, (uint)index);
|
||||||
|
EmitExpressionInto(field.Value, fieldPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
_builder.BuildRetVoid();
|
||||||
|
}
|
||||||
|
|
||||||
|
// note(nub31): Define function bodies
|
||||||
|
foreach (var funcNode in topLevelNodes.OfType<FuncNode>())
|
||||||
|
{
|
||||||
|
if (funcNode.Body == null) continue;
|
||||||
|
|
||||||
|
var funcName = FuncName(_module, funcNode.Prototype.NameToken.Value, funcNode.Prototype.ExternSymbolToken?.Value);
|
||||||
|
var func = _functions[funcName];
|
||||||
|
|
||||||
|
var entryBlock = func.AppendBasicBlock("entry");
|
||||||
|
_builder.PositionAtEnd(entryBlock);
|
||||||
|
|
||||||
|
_locals.Clear();
|
||||||
|
|
||||||
|
for (uint i = 0; i < funcNode.Prototype.Parameters.Count; i++)
|
||||||
|
{
|
||||||
|
var param = func.GetParam(i);
|
||||||
|
var paramNode = funcNode.Prototype.Parameters[(int)i];
|
||||||
|
var alloca = _builder.BuildAlloca(MapType(paramNode.Type), paramNode.NameToken.Value);
|
||||||
|
_builder.BuildStore(param, alloca);
|
||||||
|
_locals[paramNode.NameToken.Value] = alloca;
|
||||||
|
}
|
||||||
|
|
||||||
|
EmitBlock(funcNode.Body);
|
||||||
|
|
||||||
|
if (funcNode.Prototype.ReturnType is NubVoidType)
|
||||||
|
{
|
||||||
|
if (_builder.InsertBlock.Terminator.Handle == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
_builder.BuildRetVoid();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_llvmModule.TryVerify(LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error))
|
||||||
|
{
|
||||||
|
// throw new Exception($"LLVM module verification failed: {error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
_llvmModule.PrintToFile(outputPath);
|
||||||
|
|
||||||
|
_builder.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EmitBlock(BlockNode blockNode)
|
||||||
|
{
|
||||||
|
_scopes.Push(new Scope());
|
||||||
|
foreach (var statement in blockNode.Statements)
|
||||||
|
{
|
||||||
|
EmitStatement(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
EmitScopeExit();
|
||||||
|
_scopes.Pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EmitStatement(StatementNode statement)
|
||||||
|
{
|
||||||
|
switch (statement)
|
||||||
|
{
|
||||||
|
case AssignmentNode assignment:
|
||||||
|
EmitAssignment(assignment);
|
||||||
|
break;
|
||||||
|
case BlockNode block:
|
||||||
|
EmitBlock(block);
|
||||||
|
break;
|
||||||
|
case BreakNode:
|
||||||
|
EmitBreak();
|
||||||
|
break;
|
||||||
|
case ContinueNode:
|
||||||
|
EmitContinue();
|
||||||
|
break;
|
||||||
|
case DeferNode deferNode:
|
||||||
|
CurrentScope.DeferredActions.Push(() => EmitStatement(deferNode.Statement));
|
||||||
|
break;
|
||||||
|
case ForConstArrayNode forConstArrayNode:
|
||||||
|
throw new NotImplementedException();
|
||||||
|
case ForSliceNode forSliceNode:
|
||||||
|
throw new NotImplementedException();
|
||||||
|
case IfNode ifNode:
|
||||||
|
EmitIf(ifNode);
|
||||||
|
break;
|
||||||
|
case ReturnNode returnNode:
|
||||||
|
EmitReturn(returnNode);
|
||||||
|
break;
|
||||||
|
case StatementFuncCallNode funcCall:
|
||||||
|
EmitExpression(funcCall.FuncCall);
|
||||||
|
break;
|
||||||
|
case VariableDeclarationNode varDecl:
|
||||||
|
EmitVariableDeclaration(varDecl);
|
||||||
|
break;
|
||||||
|
case WhileNode whileNode:
|
||||||
|
EmitWhile(whileNode);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(statement));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EmitAssignment(AssignmentNode assignment)
|
||||||
|
{
|
||||||
|
var targetPtr = EmitExpression(assignment.Target, asLValue: true);
|
||||||
|
var value = EmitExpression(assignment.Value);
|
||||||
|
_builder.BuildStore(value, targetPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo(nub31): Needs to call EmitScopeExit. However, the jump might span multiple scopes, so we must handle that
|
||||||
|
private void EmitBreak()
|
||||||
|
{
|
||||||
|
var (breakBlock, _) = _loopStack.Peek();
|
||||||
|
_builder.BuildBr(breakBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo(nub31): Needs to call EmitScopeExit. However, the jump might span multiple scopes, so we must handle that
|
||||||
|
private void EmitContinue()
|
||||||
|
{
|
||||||
|
var (_, continueBlock) = _loopStack.Peek();
|
||||||
|
_builder.BuildBr(continueBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EmitIf(IfNode ifNode)
|
||||||
|
{
|
||||||
|
var condition = EmitExpression(ifNode.Condition);
|
||||||
|
|
||||||
|
var func = _builder.InsertBlock.Parent;
|
||||||
|
var thenBlock = func.AppendBasicBlock("if.then");
|
||||||
|
var elseBlock = ifNode.Else.HasValue ? func.AppendBasicBlock("if.else") : default;
|
||||||
|
var endBlock = func.AppendBasicBlock("if.end");
|
||||||
|
|
||||||
|
_builder.BuildCondBr(condition, thenBlock, ifNode.Else.HasValue ? elseBlock : endBlock);
|
||||||
|
|
||||||
|
_builder.PositionAtEnd(thenBlock);
|
||||||
|
EmitBlock(ifNode.Body);
|
||||||
|
if (_builder.InsertBlock.Terminator.Handle == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
_builder.BuildBr(endBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ifNode.Else.HasValue)
|
||||||
|
{
|
||||||
|
_builder.PositionAtEnd(elseBlock);
|
||||||
|
ifNode.Else.Value.Match(EmitIf, EmitBlock);
|
||||||
|
if (_builder.InsertBlock.Terminator.Handle == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
_builder.BuildBr(endBlock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_builder.PositionAtEnd(endBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EmitReturn(ReturnNode returnNode)
|
||||||
|
{
|
||||||
|
if (returnNode.Value != null)
|
||||||
|
{
|
||||||
|
var value = EmitExpression(returnNode.Value);
|
||||||
|
EmitScopeExit();
|
||||||
|
_builder.BuildRet(value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
EmitScopeExit();
|
||||||
|
_builder.BuildRetVoid();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EmitVariableDeclaration(VariableDeclarationNode varDecl)
|
||||||
|
{
|
||||||
|
var alloca = _builder.BuildAlloca(MapType(varDecl.Type), varDecl.NameToken.Value);
|
||||||
|
_locals[varDecl.NameToken.Value] = alloca;
|
||||||
|
|
||||||
|
if (varDecl.Assignment != null)
|
||||||
|
{
|
||||||
|
EmitExpressionInto(varDecl.Assignment, alloca);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EmitWhile(WhileNode whileNode)
|
||||||
|
{
|
||||||
|
var func = _builder.InsertBlock.Parent;
|
||||||
|
var condBlock = func.AppendBasicBlock("while.cond");
|
||||||
|
var bodyBlock = func.AppendBasicBlock("while.body");
|
||||||
|
var endBlock = func.AppendBasicBlock("while.end");
|
||||||
|
|
||||||
|
_loopStack.Push((endBlock, condBlock));
|
||||||
|
|
||||||
|
_builder.BuildBr(condBlock);
|
||||||
|
|
||||||
|
_builder.PositionAtEnd(condBlock);
|
||||||
|
var condition = EmitExpression(whileNode.Condition);
|
||||||
|
_builder.BuildCondBr(condition, bodyBlock, endBlock);
|
||||||
|
|
||||||
|
_builder.PositionAtEnd(bodyBlock);
|
||||||
|
EmitBlock(whileNode.Body);
|
||||||
|
if (_builder.InsertBlock.Terminator.Handle == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
_builder.BuildBr(condBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
_loopStack.Pop();
|
||||||
|
|
||||||
|
_builder.PositionAtEnd(endBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitExpression(ExpressionNode expr, bool asLValue = false)
|
||||||
|
{
|
||||||
|
var result = expr switch
|
||||||
|
{
|
||||||
|
StringLiteralNode stringLiteralNode => EmitStringLiteral(stringLiteralNode),
|
||||||
|
CStringLiteralNode cStringLiteralNode => EmitCStringLiteral(cStringLiteralNode),
|
||||||
|
BoolLiteralNode b => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int1, b.Value ? 1UL : 0UL),
|
||||||
|
I8LiteralNode i => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int8, (ulong)i.Value, true),
|
||||||
|
I16LiteralNode i => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int16, (ulong)i.Value, true),
|
||||||
|
I32LiteralNode i => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, (ulong)i.Value, true),
|
||||||
|
I64LiteralNode i => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int64, (ulong)i.Value, true),
|
||||||
|
U8LiteralNode u => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int8, u.Value),
|
||||||
|
U16LiteralNode u => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int16, u.Value),
|
||||||
|
U32LiteralNode u => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, u.Value),
|
||||||
|
U64LiteralNode u => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int64, u.Value),
|
||||||
|
Float32LiteralNode f => LLVMValueRef.CreateConstReal(LLVMTypeRef.Float, f.Value),
|
||||||
|
Float64LiteralNode f => LLVMValueRef.CreateConstReal(LLVMTypeRef.Double, f.Value),
|
||||||
|
|
||||||
|
VariableIdentifierNode v => EmitVariableIdentifier(v),
|
||||||
|
LocalFuncIdentifierNode localFuncIdentifierNode => EmitLocalFuncIdentifier(localFuncIdentifierNode),
|
||||||
|
ModuleFuncIdentifierNode moduleFuncIdentifierNode => EmitModuleFuncIdentifier(moduleFuncIdentifierNode),
|
||||||
|
|
||||||
|
BinaryExpressionNode bin => EmitBinaryExpression(bin),
|
||||||
|
UnaryExpressionNode unary => EmitUnaryExpression(unary),
|
||||||
|
|
||||||
|
StructFieldAccessNode field => EmitStructFieldAccess(field),
|
||||||
|
ConstArrayIndexAccessNode arr => EmitConstArrayIndexAccess(arr),
|
||||||
|
SliceIndexAccessNode sliceIndexAccessNode => EmitSliceIndexAccess(sliceIndexAccessNode),
|
||||||
|
ArrayIndexAccessNode arrayIndexAccessNode => EmitArrayIndexAccess(arrayIndexAccessNode),
|
||||||
|
|
||||||
|
ConstArrayInitializerNode constArrayInitializerNode => EmitConstArrayInitializer(constArrayInitializerNode),
|
||||||
|
StructInitializerNode structInitializerNode => EmitStructInitializer(structInitializerNode),
|
||||||
|
|
||||||
|
AddressOfNode addr => EmitAddressOf(addr),
|
||||||
|
DereferenceNode deref => EmitDereference(deref),
|
||||||
|
|
||||||
|
FuncCallNode funcCall => EmitFuncCall(funcCall),
|
||||||
|
CastNode cast => EmitCast(cast),
|
||||||
|
SizeNode size => LLVMValueRef.CreateConstInt(LLVMTypeRef.Int64, size.TargetType.GetSize()),
|
||||||
|
|
||||||
|
_ => throw new ArgumentOutOfRangeException(nameof(expr), expr, null)
|
||||||
|
};
|
||||||
|
|
||||||
|
if (expr is LValue)
|
||||||
|
{
|
||||||
|
if (asLValue)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _builder.BuildLoad2(MapType(expr.Type), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (asLValue)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Expression of type {expr.GetType().Name} is not an lvalue and cannot be used where an address is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EmitExpressionInto(ExpressionNode expr, LLVMValueRef destPtr)
|
||||||
|
{
|
||||||
|
switch (expr)
|
||||||
|
{
|
||||||
|
case StructInitializerNode structInit:
|
||||||
|
EmitStructInitializer(structInit, destPtr);
|
||||||
|
return;
|
||||||
|
case ConstArrayInitializerNode arrayInit:
|
||||||
|
EmitConstArrayInitializer(arrayInit, destPtr);
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
var result = EmitExpression(expr);
|
||||||
|
_builder.BuildStore(result, destPtr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitStringLiteral(StringLiteralNode stringLiteralNode)
|
||||||
|
{
|
||||||
|
var strValue = stringLiteralNode.Value;
|
||||||
|
var length = (ulong)Encoding.UTF8.GetByteCount(strValue);
|
||||||
|
var globalStr = _builder.BuildGlobalStringPtr(strValue);
|
||||||
|
var llvmStringType = MapType(stringLiteralNode.Type);
|
||||||
|
|
||||||
|
var strAlloca = _builder.BuildAlloca(llvmStringType);
|
||||||
|
|
||||||
|
var lengthPtr = _builder.BuildStructGEP2(llvmStringType, strAlloca, 0);
|
||||||
|
var lengthConst = LLVMValueRef.CreateConstInt(LLVMTypeRef.Int64, length);
|
||||||
|
_builder.BuildStore(lengthConst, lengthPtr);
|
||||||
|
|
||||||
|
var dataPtr = _builder.BuildStructGEP2(llvmStringType, strAlloca, 1);
|
||||||
|
_builder.BuildStore(globalStr, dataPtr);
|
||||||
|
|
||||||
|
return _builder.BuildLoad2(llvmStringType, strAlloca);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitCStringLiteral(CStringLiteralNode cStringLiteralNode)
|
||||||
|
{
|
||||||
|
return _builder.BuildGlobalStringPtr(cStringLiteralNode.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitVariableIdentifier(VariableIdentifierNode v)
|
||||||
|
{
|
||||||
|
return _locals[v.NameToken.Value];
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitLocalFuncIdentifier(LocalFuncIdentifierNode localFuncIdentifierNode)
|
||||||
|
{
|
||||||
|
return _functions[FuncName(_module, localFuncIdentifierNode.NameToken.Value, localFuncIdentifierNode.ExternSymbolToken?.Value)];
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitModuleFuncIdentifier(ModuleFuncIdentifierNode moduleFuncIdentifierNode)
|
||||||
|
{
|
||||||
|
return _functions[FuncName(moduleFuncIdentifierNode.ModuleToken.Value, moduleFuncIdentifierNode.NameToken.Value, moduleFuncIdentifierNode.ExternSymbolToken?.Value)];
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitBinaryExpression(BinaryExpressionNode bin)
|
||||||
|
{
|
||||||
|
var left = EmitExpression(bin.Left);
|
||||||
|
var right = EmitExpression(bin.Right);
|
||||||
|
|
||||||
|
var leftType = bin.Left.Type;
|
||||||
|
|
||||||
|
var result = bin.Operator switch
|
||||||
|
{
|
||||||
|
BinaryOperator.Plus when leftType is NubIntType => _builder.BuildAdd(left, right),
|
||||||
|
BinaryOperator.Plus when leftType is NubFloatType => _builder.BuildFAdd(left, right),
|
||||||
|
|
||||||
|
BinaryOperator.Minus when leftType is NubIntType => _builder.BuildSub(left, right),
|
||||||
|
BinaryOperator.Minus when leftType is NubFloatType => _builder.BuildFSub(left, right),
|
||||||
|
|
||||||
|
BinaryOperator.Multiply when leftType is NubIntType => _builder.BuildMul(left, right),
|
||||||
|
BinaryOperator.Multiply when leftType is NubFloatType => _builder.BuildFMul(left, right),
|
||||||
|
|
||||||
|
BinaryOperator.Divide when leftType is NubIntType intType => intType.Signed ? _builder.BuildSDiv(left, right) : _builder.BuildUDiv(left, right),
|
||||||
|
BinaryOperator.Divide when leftType is NubFloatType => _builder.BuildFDiv(left, right),
|
||||||
|
|
||||||
|
BinaryOperator.Modulo when leftType is NubIntType intType => intType.Signed ? _builder.BuildSRem(left, right) : _builder.BuildURem(left, right),
|
||||||
|
BinaryOperator.Modulo when leftType is NubFloatType => _builder.BuildFRem(left, right),
|
||||||
|
|
||||||
|
BinaryOperator.LogicalAnd => _builder.BuildAnd(left, right),
|
||||||
|
BinaryOperator.LogicalOr => _builder.BuildOr(left, right),
|
||||||
|
|
||||||
|
BinaryOperator.Equal when leftType is NubIntType or NubBoolType or NubPointerType => _builder.BuildICmp(LLVMIntPredicate.LLVMIntEQ, left, right),
|
||||||
|
BinaryOperator.Equal when leftType is NubFloatType => _builder.BuildFCmp(LLVMRealPredicate.LLVMRealOEQ, left, right),
|
||||||
|
|
||||||
|
BinaryOperator.NotEqual when leftType is NubIntType or NubBoolType or NubPointerType => _builder.BuildICmp(LLVMIntPredicate.LLVMIntNE, left, right),
|
||||||
|
BinaryOperator.NotEqual when leftType is NubFloatType => _builder.BuildFCmp(LLVMRealPredicate.LLVMRealONE, left, right),
|
||||||
|
|
||||||
|
BinaryOperator.GreaterThan when leftType is NubIntType intType => _builder.BuildICmp(intType.Signed ? LLVMIntPredicate.LLVMIntSGT : LLVMIntPredicate.LLVMIntUGT, left, right),
|
||||||
|
BinaryOperator.GreaterThan when leftType is NubFloatType => _builder.BuildFCmp(LLVMRealPredicate.LLVMRealOGT, left, right),
|
||||||
|
|
||||||
|
BinaryOperator.GreaterThanOrEqual when leftType is NubIntType intType => _builder.BuildICmp(intType.Signed ? LLVMIntPredicate.LLVMIntSGE : LLVMIntPredicate.LLVMIntUGE, left, right),
|
||||||
|
BinaryOperator.GreaterThanOrEqual when leftType is NubFloatType => _builder.BuildFCmp(LLVMRealPredicate.LLVMRealOGE, left, right),
|
||||||
|
|
||||||
|
BinaryOperator.LessThan when leftType is NubIntType intType => _builder.BuildICmp(intType.Signed ? LLVMIntPredicate.LLVMIntSLT : LLVMIntPredicate.LLVMIntULT, left, right),
|
||||||
|
BinaryOperator.LessThan when leftType is NubFloatType => _builder.BuildFCmp(LLVMRealPredicate.LLVMRealOLT, left, right),
|
||||||
|
|
||||||
|
BinaryOperator.LessThanOrEqual when leftType is NubIntType intType => _builder.BuildICmp(intType.Signed ? LLVMIntPredicate.LLVMIntSLE : LLVMIntPredicate.LLVMIntULE, left, right),
|
||||||
|
BinaryOperator.LessThanOrEqual when leftType is NubFloatType => _builder.BuildFCmp(LLVMRealPredicate.LLVMRealOLE, left, right),
|
||||||
|
|
||||||
|
BinaryOperator.LeftShift => _builder.BuildShl(left, right),
|
||||||
|
BinaryOperator.RightShift when leftType is NubIntType intType => intType.Signed ? _builder.BuildAShr(left, right) : _builder.BuildLShr(left, right),
|
||||||
|
|
||||||
|
BinaryOperator.BitwiseAnd => _builder.BuildAnd(left, right),
|
||||||
|
BinaryOperator.BitwiseXor => _builder.BuildXor(left, right),
|
||||||
|
BinaryOperator.BitwiseOr => _builder.BuildOr(left, right),
|
||||||
|
|
||||||
|
_ => throw new ArgumentOutOfRangeException(nameof(bin.Operator))
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitUnaryExpression(UnaryExpressionNode unary)
|
||||||
|
{
|
||||||
|
var operand = EmitExpression(unary.Operand);
|
||||||
|
|
||||||
|
var result = unary.Operator switch
|
||||||
|
{
|
||||||
|
UnaryOperator.Negate when unary.Operand.Type is NubIntType => _builder.BuildNeg(operand),
|
||||||
|
UnaryOperator.Negate when unary.Operand.Type is NubFloatType => _builder.BuildFNeg(operand),
|
||||||
|
UnaryOperator.Invert => _builder.BuildXor(operand, LLVMValueRef.CreateConstInt(LLVMTypeRef.Int1, 1)),
|
||||||
|
_ => throw new ArgumentOutOfRangeException(nameof(unary.Operator))
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitFuncCall(FuncCallNode funcCall)
|
||||||
|
{
|
||||||
|
var funcPtr = EmitExpression(funcCall.Expression);
|
||||||
|
var args = funcCall.Parameters.Select(x => EmitExpression(x)).ToArray();
|
||||||
|
|
||||||
|
var functionType = (NubFuncType)funcCall.Expression.Type;
|
||||||
|
var llvmFunctionType = LLVMTypeRef.CreateFunction(MapType(functionType.ReturnType), functionType.Parameters.Select(MapType).ToArray());
|
||||||
|
return _builder.BuildCall2(llvmFunctionType, funcPtr, args, funcCall.Type is NubVoidType ? "" : "call");
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitStructFieldAccess(StructFieldAccessNode field)
|
||||||
|
{
|
||||||
|
var target = EmitExpression(field.Target, asLValue: true);
|
||||||
|
var structType = (NubStructType)field.Target.Type;
|
||||||
|
var index = structType.GetFieldIndex(field.FieldToken.Value);
|
||||||
|
|
||||||
|
var llvmStructType = _structTypes[StructName(structType.Module, structType.Name)];
|
||||||
|
return _builder.BuildStructGEP2(llvmStructType, target, (uint)index);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitConstArrayIndexAccess(ConstArrayIndexAccessNode constArrayIndexAccessNode)
|
||||||
|
{
|
||||||
|
var arrayPtr = EmitExpression(constArrayIndexAccessNode.Target, asLValue: true);
|
||||||
|
var index = EmitExpression(constArrayIndexAccessNode.Index);
|
||||||
|
var indices = new[] { LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 0), index };
|
||||||
|
return _builder.BuildInBoundsGEP2(MapType(constArrayIndexAccessNode.Target.Type), arrayPtr, indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitSliceIndexAccess(SliceIndexAccessNode sliceIndexAccessNode)
|
||||||
|
{
|
||||||
|
var slicePtr = EmitExpression(sliceIndexAccessNode.Target, asLValue: true);
|
||||||
|
var index = EmitExpression(sliceIndexAccessNode.Index);
|
||||||
|
|
||||||
|
var sliceType = (NubSliceType)sliceIndexAccessNode.Target.Type;
|
||||||
|
var llvmSliceType = MapType(sliceType);
|
||||||
|
var elementType = MapType(sliceType.ElementType);
|
||||||
|
|
||||||
|
var dataPtrPtr = _builder.BuildStructGEP2(llvmSliceType, slicePtr, 1);
|
||||||
|
var dataPtr = _builder.BuildLoad2(LLVMTypeRef.CreatePointer(elementType, 0), dataPtrPtr);
|
||||||
|
return _builder.BuildInBoundsGEP2(elementType, dataPtr, [index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitArrayIndexAccess(ArrayIndexAccessNode arrayIndexAccessNode)
|
||||||
|
{
|
||||||
|
var arrayPtr = EmitExpression(arrayIndexAccessNode.Target);
|
||||||
|
var index = EmitExpression(arrayIndexAccessNode.Index);
|
||||||
|
return _builder.BuildGEP2(MapType(arrayIndexAccessNode.Target.Type), arrayPtr, [index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitConstArrayInitializer(ConstArrayInitializerNode constArrayInitializerNode, LLVMValueRef? destination = null)
|
||||||
|
{
|
||||||
|
var arrayType = (NubConstArrayType)constArrayInitializerNode.Type;
|
||||||
|
var llvmType = MapType(arrayType);
|
||||||
|
|
||||||
|
destination ??= _builder.BuildAlloca(llvmType);
|
||||||
|
|
||||||
|
for (var i = 0; i < constArrayInitializerNode.Values.Count; i++)
|
||||||
|
{
|
||||||
|
var indices = new[]
|
||||||
|
{
|
||||||
|
LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 0),
|
||||||
|
LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, (ulong)i)
|
||||||
|
};
|
||||||
|
|
||||||
|
var elementPtr = _builder.BuildInBoundsGEP2(llvmType, destination.Value, indices);
|
||||||
|
|
||||||
|
EmitExpressionInto(constArrayInitializerNode.Values[i], elementPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return destination.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitStructInitializer(StructInitializerNode structInitializerNode, LLVMValueRef? destination = null)
|
||||||
|
{
|
||||||
|
var type = (NubStructType)structInitializerNode.Type;
|
||||||
|
var llvmType = MapType(type);
|
||||||
|
|
||||||
|
destination ??= _builder.BuildAlloca(llvmType);
|
||||||
|
|
||||||
|
var constructorType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, [LLVMTypeRef.CreatePointer(_structTypes[StructName(type.Module, type.Name)], 0)]);
|
||||||
|
var constructor = _functions[StructConstructorName(type.Module, type.Name)];
|
||||||
|
_builder.BuildCall2(constructorType, constructor, [destination.Value]);
|
||||||
|
|
||||||
|
foreach (var (name, value) in structInitializerNode.Initializers)
|
||||||
|
{
|
||||||
|
var fieldIndex = type.GetFieldIndex(name.Value);
|
||||||
|
var fieldPtr = _builder.BuildStructGEP2(llvmType, destination.Value, (uint)fieldIndex);
|
||||||
|
EmitExpressionInto(value, fieldPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return destination.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitAddressOf(AddressOfNode addr)
|
||||||
|
{
|
||||||
|
return EmitExpression(addr.Target, asLValue: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitDereference(DereferenceNode deref)
|
||||||
|
{
|
||||||
|
return EmitExpression(deref.Target, asLValue: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitCast(CastNode castNode)
|
||||||
|
{
|
||||||
|
return castNode.ConversionType switch
|
||||||
|
{
|
||||||
|
CastNode.Conversion.IntToInt => EmitIntToIntCast(castNode),
|
||||||
|
CastNode.Conversion.FloatToFloat => EmitFloatToFloatCast(castNode),
|
||||||
|
CastNode.Conversion.IntToFloat => EmitIntToFloatCast(castNode),
|
||||||
|
CastNode.Conversion.FloatToInt => EmitFloatToIntCast(castNode),
|
||||||
|
CastNode.Conversion.PointerToPointer or CastNode.Conversion.PointerToUInt64 or CastNode.Conversion.UInt64ToPointer => _builder.BuildIntToPtr(EmitExpression(castNode.Value), MapType(castNode.Type)),
|
||||||
|
CastNode.Conversion.ConstArrayToSlice => EmitConstArrayToSliceCast(castNode),
|
||||||
|
CastNode.Conversion.ConstArrayToArray => EmitConstArrayToArrayCast(castNode),
|
||||||
|
CastNode.Conversion.StringToCString => EmitStringToCStringCast(castNode),
|
||||||
|
_ => throw new ArgumentOutOfRangeException(nameof(castNode.ConversionType))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitIntToIntCast(CastNode castNode)
|
||||||
|
{
|
||||||
|
var sourceInt = (NubIntType)castNode.Value.Type;
|
||||||
|
var targetInt = (NubIntType)castNode.Type;
|
||||||
|
var source = EmitExpression(castNode.Value);
|
||||||
|
|
||||||
|
if (sourceInt.Width < targetInt.Width)
|
||||||
|
{
|
||||||
|
return sourceInt.Signed
|
||||||
|
? _builder.BuildSExt(source, MapType(targetInt))
|
||||||
|
: _builder.BuildZExt(source, MapType(targetInt));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sourceInt.Width > targetInt.Width)
|
||||||
|
{
|
||||||
|
return _builder.BuildTrunc(source, MapType(targetInt));
|
||||||
|
}
|
||||||
|
|
||||||
|
return _builder.BuildBitCast(source, MapType(targetInt));
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitFloatToFloatCast(CastNode castNode)
|
||||||
|
{
|
||||||
|
var sourceFloat = (NubFloatType)castNode.Value.Type;
|
||||||
|
var targetFloat = (NubFloatType)castNode.Type;
|
||||||
|
var source = EmitExpression(castNode.Value);
|
||||||
|
|
||||||
|
return sourceFloat.Width < targetFloat.Width
|
||||||
|
? _builder.BuildFPExt(source, MapType(castNode.Type))
|
||||||
|
: _builder.BuildFPTrunc(source, MapType(castNode.Type));
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitIntToFloatCast(CastNode castNode)
|
||||||
|
{
|
||||||
|
var sourceInt = (NubIntType)castNode.Value.Type;
|
||||||
|
var source = EmitExpression(castNode.Value);
|
||||||
|
|
||||||
|
return sourceInt.Signed
|
||||||
|
? _builder.BuildSIToFP(source, MapType(castNode.Type))
|
||||||
|
: _builder.BuildUIToFP(source, MapType(castNode.Type));
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitFloatToIntCast(CastNode castNode)
|
||||||
|
{
|
||||||
|
var targetInt = (NubIntType)castNode.Type;
|
||||||
|
var source = EmitExpression(castNode.Value);
|
||||||
|
|
||||||
|
return targetInt.Signed
|
||||||
|
? _builder.BuildFPToSI(source, MapType(targetInt))
|
||||||
|
: _builder.BuildFPToUI(source, MapType(targetInt));
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitConstArrayToSliceCast(CastNode castNode)
|
||||||
|
{
|
||||||
|
var sourceArrayType = (NubConstArrayType)castNode.Value.Type;
|
||||||
|
var targetSliceType = (NubSliceType)castNode.Type;
|
||||||
|
var source = EmitExpression(castNode.Value, asLValue: true);
|
||||||
|
|
||||||
|
var llvmArrayType = MapType(sourceArrayType);
|
||||||
|
var llvmSliceType = MapType(targetSliceType);
|
||||||
|
|
||||||
|
var indices = new[]
|
||||||
|
{
|
||||||
|
LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 0),
|
||||||
|
LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 0)
|
||||||
|
};
|
||||||
|
|
||||||
|
var firstElementPtr = _builder.BuildInBoundsGEP2(llvmArrayType, source, indices);
|
||||||
|
|
||||||
|
var slicePtr = _builder.BuildAlloca(llvmSliceType);
|
||||||
|
|
||||||
|
var lengthPtr = _builder.BuildStructGEP2(llvmSliceType, slicePtr, 0);
|
||||||
|
var length = LLVMValueRef.CreateConstInt(LLVMTypeRef.Int64, sourceArrayType.Size);
|
||||||
|
_builder.BuildStore(length, lengthPtr);
|
||||||
|
|
||||||
|
var dataPtrPtr = _builder.BuildStructGEP2(llvmSliceType, slicePtr, 1);
|
||||||
|
_builder.BuildStore(firstElementPtr, dataPtrPtr);
|
||||||
|
|
||||||
|
return _builder.BuildLoad2(llvmSliceType, slicePtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitConstArrayToArrayCast(CastNode castNode)
|
||||||
|
{
|
||||||
|
var sourceArrayType = (NubConstArrayType)castNode.Value.Type;
|
||||||
|
var source = EmitExpression(castNode.Value, asLValue: true);
|
||||||
|
|
||||||
|
var indices = new[]
|
||||||
|
{
|
||||||
|
LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 0),
|
||||||
|
LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 0)
|
||||||
|
};
|
||||||
|
|
||||||
|
return _builder.BuildInBoundsGEP2(MapType(sourceArrayType), source, indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMValueRef EmitStringToCStringCast(CastNode castNode)
|
||||||
|
{
|
||||||
|
var source = EmitExpression(castNode.Value, asLValue: true);
|
||||||
|
var dataPtrPtr = _builder.BuildStructGEP2(MapType(castNode.Value.Type), source, 1);
|
||||||
|
return _builder.BuildLoad2(LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0), dataPtrPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMTypeRef MapType(NubType type)
|
||||||
|
{
|
||||||
|
return type switch
|
||||||
|
{
|
||||||
|
NubBoolType => LLVMTypeRef.Int1,
|
||||||
|
NubIntType intType => LLVMTypeRef.CreateInt((uint)intType.Width),
|
||||||
|
NubFloatType floatType => floatType.Width == 32 ? LLVMTypeRef.Float : LLVMTypeRef.Double,
|
||||||
|
NubFuncType funcType => LLVMTypeRef.CreatePointer(LLVMTypeRef.CreateFunction(MapType(funcType.ReturnType), funcType.Parameters.Select(MapType).ToArray()), 0),
|
||||||
|
NubPointerType ptrType => LLVMTypeRef.CreatePointer(MapType(ptrType.BaseType), 0),
|
||||||
|
NubSliceType nubSliceType => MapSliceType(nubSliceType),
|
||||||
|
NubStringType => _structTypes["nub.string"],
|
||||||
|
NubArrayType arrType => LLVMTypeRef.CreatePointer(MapType(arrType.ElementType), 0),
|
||||||
|
NubConstArrayType constArr => LLVMTypeRef.CreateArray(MapType(constArr.ElementType), (uint)constArr.Size),
|
||||||
|
NubStructType structType => _structTypes[StructName(structType.Module, structType.Name)],
|
||||||
|
NubEnumType enumType => MapType(enumType.UnderlyingType),
|
||||||
|
NubVoidType => LLVMTypeRef.Void,
|
||||||
|
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private LLVMTypeRef MapSliceType(NubSliceType nubSliceType)
|
||||||
|
{
|
||||||
|
var mangledName = NameMangler.Mangle(nubSliceType.ElementType);
|
||||||
|
var name = $"nub.slice.{mangledName}";
|
||||||
|
if (!_structTypes.TryGetValue(name, out var type))
|
||||||
|
{
|
||||||
|
type = _context.CreateNamedStruct(name);
|
||||||
|
type.StructSetBody([LLVMTypeRef.Int64, LLVMTypeRef.CreatePointer(MapType(nubSliceType.ElementType), 0)], false);
|
||||||
|
_structTypes[name] = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string StructName(string module, string name)
|
||||||
|
{
|
||||||
|
return $"struct.{module}.{name}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string StructConstructorName(string module, string name)
|
||||||
|
{
|
||||||
|
return $"{StructName(module, name)}.new";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string FuncName(string module, string name, string? externSymbol)
|
||||||
|
{
|
||||||
|
if (externSymbol != null)
|
||||||
|
{
|
||||||
|
return externSymbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $"{module}.{name}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EmitScopeExit()
|
||||||
|
{
|
||||||
|
while (CurrentScope.DeferredActions.TryPop(out var action))
|
||||||
|
{
|
||||||
|
action.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Scope
|
||||||
|
{
|
||||||
|
public readonly Stack<Action> DeferredActions = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
241
compiler/NubLang/Modules/ModuleRepository.cs
Normal file
241
compiler/NubLang/Modules/ModuleRepository.cs
Normal file
@@ -0,0 +1,241 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using NubLang.Ast;
|
||||||
|
using NubLang.Diagnostics;
|
||||||
|
using NubLang.Syntax;
|
||||||
|
using NubLang.Types;
|
||||||
|
|
||||||
|
namespace NubLang.Modules;
|
||||||
|
|
||||||
|
public sealed class ModuleRepository
|
||||||
|
{
|
||||||
|
public static ModuleRepository Create(List<SyntaxTree> syntaxTrees)
|
||||||
|
{
|
||||||
|
var structTypes = new Dictionary<(string module, string name), NubStructType>();
|
||||||
|
var enumTypes = new Dictionary<(string module, string name), NubEnumType>();
|
||||||
|
|
||||||
|
foreach (var syntaxTree in syntaxTrees)
|
||||||
|
{
|
||||||
|
var module = syntaxTree.TopLevelSyntaxNodes.OfType<ModuleSyntax>().FirstOrDefault();
|
||||||
|
if (module == null)
|
||||||
|
{
|
||||||
|
throw new CompileException(Diagnostic.Error("Module declaration missing").WithHelp("module \"main\"").Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var structSyntax in syntaxTree.TopLevelSyntaxNodes.OfType<StructSyntax>())
|
||||||
|
{
|
||||||
|
// note(nub31): Since not all struct types are registered yet, we cannot register field types as they might reference unregistered structs
|
||||||
|
var key = (module.NameToken.Value, structSyntax.NameToken.Value);
|
||||||
|
structTypes.Add(key, new NubStructType(module.NameToken.Value, structSyntax.NameToken.Value, structSyntax.Packed, []));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var enumSyntax in syntaxTree.TopLevelSyntaxNodes.OfType<EnumSyntax>())
|
||||||
|
{
|
||||||
|
NubIntType? underlyingType = null;
|
||||||
|
if (enumSyntax.Type != null)
|
||||||
|
{
|
||||||
|
if (enumSyntax.Type is not IntTypeSyntax intType)
|
||||||
|
{
|
||||||
|
throw new CompileException(Diagnostic.Error("Underlying type of enum must be an integer type").At(enumSyntax.Type).Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
underlyingType = new NubIntType(intType.Signed, intType.Width);
|
||||||
|
}
|
||||||
|
|
||||||
|
underlyingType ??= new NubIntType(false, 64);
|
||||||
|
|
||||||
|
var key = (module.NameToken.Value, enumSyntax.NameToken.Value);
|
||||||
|
|
||||||
|
var memberValues = new Dictionary<string, ulong>();
|
||||||
|
|
||||||
|
ulong currentValue = 0;
|
||||||
|
|
||||||
|
foreach (var member in enumSyntax.Members)
|
||||||
|
{
|
||||||
|
if (member.ValueToken != null)
|
||||||
|
{
|
||||||
|
currentValue = member.ValueToken.AsU64;
|
||||||
|
}
|
||||||
|
|
||||||
|
memberValues[member.NameToken.Value] = currentValue;
|
||||||
|
currentValue++;
|
||||||
|
}
|
||||||
|
|
||||||
|
enumTypes.Add(key, new NubEnumType(module.NameToken.Value, enumSyntax.NameToken.Value, underlyingType, memberValues));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// note(nub31): Since all struct and enum types are now registered, we can safely resolve the field types
|
||||||
|
foreach (var syntaxTree in syntaxTrees)
|
||||||
|
{
|
||||||
|
var module = syntaxTree.TopLevelSyntaxNodes.OfType<ModuleSyntax>().FirstOrDefault();
|
||||||
|
if (module == null)
|
||||||
|
{
|
||||||
|
throw new CompileException(Diagnostic.Error("Module declaration missing").WithHelp("module \"main\"").Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var structSyntax in syntaxTree.TopLevelSyntaxNodes.OfType<StructSyntax>())
|
||||||
|
{
|
||||||
|
var key = (module.NameToken.Value, structSyntax.NameToken.Value);
|
||||||
|
|
||||||
|
structTypes[key].Fields = structSyntax.Fields
|
||||||
|
.Select(x => new NubStructFieldType(x.NameToken.Value, ResolveType(x.Type, module.NameToken.Value), x.Value != null))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var modules = new Dictionary<string, Module>();
|
||||||
|
|
||||||
|
foreach (var syntaxTree in syntaxTrees)
|
||||||
|
{
|
||||||
|
var moduleDecl = syntaxTree.TopLevelSyntaxNodes.OfType<ModuleSyntax>().FirstOrDefault();
|
||||||
|
if (moduleDecl == null)
|
||||||
|
{
|
||||||
|
throw new CompileException(Diagnostic.Error("Module declaration missing").WithHelp("module \"main\"").Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
var functionPrototypes = new List<FuncPrototypeNode>();
|
||||||
|
|
||||||
|
foreach (var funcSyntax in syntaxTree.TopLevelSyntaxNodes.OfType<FuncSyntax>())
|
||||||
|
{
|
||||||
|
var returnType = ResolveType(funcSyntax.Prototype.ReturnType, moduleDecl.NameToken.Value);
|
||||||
|
var parameters = funcSyntax.Prototype.Parameters.Select(x => new FuncParameterNode(x.Tokens, x.NameToken, ResolveType(x.Type, moduleDecl.NameToken.Value))).ToList();
|
||||||
|
functionPrototypes.Add(new FuncPrototypeNode(funcSyntax.Prototype.Tokens, funcSyntax.Prototype.NameToken, funcSyntax.Prototype.ExternSymbolToken, parameters, returnType));
|
||||||
|
}
|
||||||
|
|
||||||
|
var module = new Module
|
||||||
|
{
|
||||||
|
Name = moduleDecl.NameToken.Value,
|
||||||
|
StructTypes = structTypes.Where(x => x.Key.module == moduleDecl.NameToken.Value).Select(x => x.Value).ToList(),
|
||||||
|
EnumTypes = enumTypes.Where(x => x.Key.module == moduleDecl.NameToken.Value).Select(x => x.Value).ToList(),
|
||||||
|
FunctionPrototypes = functionPrototypes
|
||||||
|
};
|
||||||
|
|
||||||
|
modules.Add(moduleDecl.NameToken.Value, module);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ModuleRepository(modules);
|
||||||
|
|
||||||
|
NubType ResolveType(TypeSyntax type, string currentModule)
|
||||||
|
{
|
||||||
|
return type switch
|
||||||
|
{
|
||||||
|
ArrayTypeSyntax arr => new NubArrayType(ResolveType(arr.BaseType, currentModule)),
|
||||||
|
BoolTypeSyntax => new NubBoolType(),
|
||||||
|
IntTypeSyntax i => new NubIntType(i.Signed, i.Width),
|
||||||
|
FloatTypeSyntax f => new NubFloatType(f.Width),
|
||||||
|
FuncTypeSyntax func => new NubFuncType(func.Parameters.Select(x => ResolveType(x, currentModule)).ToList(), ResolveType(func.ReturnType, currentModule)),
|
||||||
|
SliceTypeSyntax slice => new NubSliceType(ResolveType(slice.BaseType, currentModule)),
|
||||||
|
ConstArrayTypeSyntax arr => new NubConstArrayType(ResolveType(arr.BaseType, currentModule), arr.Size),
|
||||||
|
PointerTypeSyntax ptr => new NubPointerType(ResolveType(ptr.BaseType, currentModule)),
|
||||||
|
StringTypeSyntax => new NubStringType(),
|
||||||
|
CustomTypeSyntax c => ResolveCustomType(c, currentModule),
|
||||||
|
VoidTypeSyntax => new NubVoidType(),
|
||||||
|
_ => throw new NotSupportedException($"Unknown type syntax: {type}")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
NubType ResolveCustomType(CustomTypeSyntax customType, string currentModule)
|
||||||
|
{
|
||||||
|
var customTypeKey = (customType.ModuleToken?.Value ?? currentModule, customType.NameToken.Value);
|
||||||
|
|
||||||
|
var resolvedStructType = structTypes.GetValueOrDefault(customTypeKey);
|
||||||
|
if (resolvedStructType != null)
|
||||||
|
{
|
||||||
|
return resolvedStructType;
|
||||||
|
}
|
||||||
|
|
||||||
|
var resolvedEnumType = enumTypes.GetValueOrDefault(customTypeKey);
|
||||||
|
if (resolvedEnumType != null)
|
||||||
|
{
|
||||||
|
return resolvedEnumType;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new CompileException(Diagnostic
|
||||||
|
.Error($"Type {customType.NameToken.Value} not found in module {customType.ModuleToken?.Value ?? currentModule}")
|
||||||
|
.At(customType)
|
||||||
|
.Build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModuleRepository(Dictionary<string, Module> modules)
|
||||||
|
{
|
||||||
|
_modules = modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly Dictionary<string, Module> _modules;
|
||||||
|
|
||||||
|
public Module Get(IdentifierToken ident)
|
||||||
|
{
|
||||||
|
var module = _modules.GetValueOrDefault(ident.Value);
|
||||||
|
if (module == null)
|
||||||
|
{
|
||||||
|
throw new CompileException(Diagnostic.Error($"Module {ident.Value} was not found").At(ident).Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
return module;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGet(IdentifierToken ident, [NotNullWhen(true)] out Module? module)
|
||||||
|
{
|
||||||
|
module = _modules.GetValueOrDefault(ident.Value);
|
||||||
|
return module != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Module> GetAll()
|
||||||
|
{
|
||||||
|
return _modules.Values.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class Module
|
||||||
|
{
|
||||||
|
public required string Name { get; init; }
|
||||||
|
public required List<FuncPrototypeNode> FunctionPrototypes { get; init; } = [];
|
||||||
|
public required List<NubStructType> StructTypes { get; init; } = [];
|
||||||
|
public required List<NubEnumType> EnumTypes { get; init; } = [];
|
||||||
|
|
||||||
|
public bool TryResolveFunc(IdentifierToken name, [NotNullWhen(true)] out FuncPrototypeNode? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
||||||
|
{
|
||||||
|
value = FunctionPrototypes.FirstOrDefault(x => x.NameToken.Value == name.Value);
|
||||||
|
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
value = null;
|
||||||
|
diagnostic = Diagnostic.Error($"Func {name.Value} not found in module {Name}").At(name).Build();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnostic = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryResolveStruct(IdentifierToken name, [NotNullWhen(true)] out NubStructType? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
||||||
|
{
|
||||||
|
value = StructTypes.FirstOrDefault(x => x.Name == name.Value);
|
||||||
|
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
value = null;
|
||||||
|
diagnostic = Diagnostic.Error($"Struct {name.Value} not found in module {Name}").At(name).Build();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnostic = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryResolveEnum(IdentifierToken name, [NotNullWhen(true)] out NubEnumType? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
||||||
|
{
|
||||||
|
value = EnumTypes.FirstOrDefault(x => x.Name == name.Value);
|
||||||
|
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
value = null;
|
||||||
|
diagnostic = Diagnostic.Error($"Enum {name.Value} not found in module {Name}").At(name).Build();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnostic = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,4 +7,8 @@
|
|||||||
<IsAotCompatible>true</IsAotCompatible>
|
<IsAotCompatible>true</IsAotCompatible>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="LLVMSharp" Version="20.1.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
namespace NubLang.Syntax;
|
|
||||||
|
|
||||||
public sealed class Module
|
|
||||||
{
|
|
||||||
public static Dictionary<string, Module> Collect(List<SyntaxTree> syntaxTrees)
|
|
||||||
{
|
|
||||||
var modules = new Dictionary<string, Module>();
|
|
||||||
foreach (var syntaxTree in syntaxTrees)
|
|
||||||
{
|
|
||||||
if (!modules.TryGetValue(syntaxTree.ModuleName, out var module))
|
|
||||||
{
|
|
||||||
module = new Module();
|
|
||||||
modules.Add(syntaxTree.ModuleName, module);
|
|
||||||
}
|
|
||||||
|
|
||||||
module._definitions.AddRange(syntaxTree.Definitions);
|
|
||||||
}
|
|
||||||
|
|
||||||
return modules;
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly List<DefinitionSyntax> _definitions = [];
|
|
||||||
|
|
||||||
public List<StructSyntax> Structs(bool includePrivate)
|
|
||||||
{
|
|
||||||
return _definitions
|
|
||||||
.OfType<StructSyntax>()
|
|
||||||
.Where(x => x.Exported || includePrivate)
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<FuncSyntax> Functions(bool includePrivate)
|
|
||||||
{
|
|
||||||
return _definitions
|
|
||||||
.OfType<FuncSyntax>()
|
|
||||||
.Where(x => x.Exported || includePrivate)
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<EnumSyntax> Enums(bool includePrivate)
|
|
||||||
{
|
|
||||||
return _definitions
|
|
||||||
.OfType<EnumSyntax>()
|
|
||||||
.Where(x => x.Exported || includePrivate)
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,19 +9,31 @@ public sealed class Parser
|
|||||||
private int _tokenIndex;
|
private int _tokenIndex;
|
||||||
|
|
||||||
private Token? CurrentToken => _tokenIndex < _tokens.Count ? _tokens[_tokenIndex] : null;
|
private Token? CurrentToken => _tokenIndex < _tokens.Count ? _tokens[_tokenIndex] : null;
|
||||||
|
|
||||||
|
private bool HasTrailingWhitespace(Token token)
|
||||||
|
{
|
||||||
|
var index = _tokens.IndexOf(token);
|
||||||
|
return index + 1 < _tokens.Count && _tokens[index + 1] is WhitespaceToken or CommentToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool HasLeadingWhitespace(Token token)
|
||||||
|
{
|
||||||
|
var index = _tokens.IndexOf(token);
|
||||||
|
return index - 1 < _tokens.Count && _tokens[index - 1] is WhitespaceToken or CommentToken;
|
||||||
|
}
|
||||||
|
|
||||||
private bool HasToken => CurrentToken != null;
|
private bool HasToken => CurrentToken != null;
|
||||||
|
|
||||||
public List<Diagnostic> Diagnostics { get; } = [];
|
public List<Diagnostic> Diagnostics { get; set; } = [];
|
||||||
|
|
||||||
public SyntaxTree Parse(List<Token> tokens)
|
public SyntaxTree Parse(List<Token> tokens)
|
||||||
{
|
{
|
||||||
Diagnostics.Clear();
|
|
||||||
_tokens = tokens;
|
_tokens = tokens;
|
||||||
_tokenIndex = 0;
|
_tokenIndex = 0;
|
||||||
|
|
||||||
string? moduleName = null;
|
Diagnostics = [];
|
||||||
var imports = new List<string>();
|
|
||||||
var definitions = new List<DefinitionSyntax>();
|
var topLevelSyntaxNodes = new List<TopLevelSyntaxNode>();
|
||||||
|
|
||||||
while (HasToken)
|
while (HasToken)
|
||||||
{
|
{
|
||||||
@@ -29,66 +41,34 @@ public sealed class Parser
|
|||||||
{
|
{
|
||||||
var startIndex = _tokenIndex;
|
var startIndex = _tokenIndex;
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Import))
|
|
||||||
{
|
|
||||||
var name = ExpectStringLiteral();
|
|
||||||
if (imports.Contains(name.Value))
|
|
||||||
{
|
|
||||||
Diagnostics.Add(Diagnostic
|
|
||||||
.Warning($"Module {name.Value} is imported twice")
|
|
||||||
.At(name)
|
|
||||||
.WithHelp($"Remove duplicate import \"{name.Value}\"")
|
|
||||||
.Build());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
imports.Add(name.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Module))
|
|
||||||
{
|
|
||||||
if (moduleName != null)
|
|
||||||
{
|
|
||||||
throw new ParseException(Diagnostic
|
|
||||||
.Error("Module is declared more than once")
|
|
||||||
.At(CurrentToken)
|
|
||||||
.WithHelp("Remove duplicate module declaration")
|
|
||||||
.Build());
|
|
||||||
}
|
|
||||||
|
|
||||||
moduleName = ExpectStringLiteral().Value;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var exported = TryExpectSymbol(Symbol.Export);
|
var exported = TryExpectSymbol(Symbol.Export);
|
||||||
|
var packed = TryExpectSymbol(Symbol.Packed);
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Extern))
|
if (TryExpectSymbol(Symbol.Extern))
|
||||||
{
|
{
|
||||||
var externSymbol = ExpectStringLiteral();
|
var externSymbol = ExpectStringLiteral();
|
||||||
ExpectSymbol(Symbol.Func);
|
ExpectSymbol(Symbol.Func);
|
||||||
definitions.Add(ParseFunc(startIndex, exported, externSymbol.Value));
|
topLevelSyntaxNodes.Add(ParseFunc(startIndex, exported, externSymbol));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var keyword = ExpectSymbol();
|
var keyword = ExpectSymbol();
|
||||||
DefinitionSyntax definition = keyword.Symbol switch
|
TopLevelSyntaxNode definition = keyword.Symbol switch
|
||||||
{
|
{
|
||||||
|
Symbol.Module => ParseModule(startIndex),
|
||||||
Symbol.Func => ParseFunc(startIndex, exported, null),
|
Symbol.Func => ParseFunc(startIndex, exported, null),
|
||||||
Symbol.Struct => ParseStruct(startIndex, exported),
|
Symbol.Struct => ParseStruct(startIndex, exported, packed),
|
||||||
Symbol.Enum => ParseEnum(startIndex, exported),
|
Symbol.Enum => ParseEnum(startIndex, exported),
|
||||||
_ => throw new ParseException(Diagnostic
|
_ => throw new CompileException(Diagnostic
|
||||||
.Error($"Expected 'func', 'struct', 'enum', 'import' or 'module' but found '{keyword.Symbol}'")
|
.Error($"Expected 'func', 'struct', 'enum', 'import' or 'module' but found '{keyword.Symbol}'")
|
||||||
.WithHelp("Valid top level statements are 'func', 'struct', 'enum', 'import' and 'module'")
|
.WithHelp("Valid top level statements are 'func', 'struct', 'enum', 'import' and 'module'")
|
||||||
.At(keyword)
|
.At(keyword, _tokens)
|
||||||
.Build())
|
.Build())
|
||||||
};
|
};
|
||||||
|
|
||||||
definitions.Add(definition);
|
topLevelSyntaxNodes.Add(definition);
|
||||||
}
|
}
|
||||||
catch (ParseException e)
|
catch (CompileException e)
|
||||||
{
|
{
|
||||||
Diagnostics.Add(e.Diagnostic);
|
Diagnostics.Add(e.Diagnostic);
|
||||||
while (HasToken)
|
while (HasToken)
|
||||||
@@ -103,7 +83,13 @@ public sealed class Parser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SyntaxTree(definitions, moduleName ?? "default", imports);
|
return new SyntaxTree(topLevelSyntaxNodes, _tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ModuleSyntax ParseModule(int startIndex)
|
||||||
|
{
|
||||||
|
var name = ExpectIdentifier();
|
||||||
|
return new ModuleSyntax(GetTokens(startIndex), name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private FuncParameterSyntax ParseFuncParameter()
|
private FuncParameterSyntax ParseFuncParameter()
|
||||||
@@ -113,10 +99,10 @@ public sealed class Parser
|
|||||||
ExpectSymbol(Symbol.Colon);
|
ExpectSymbol(Symbol.Colon);
|
||||||
var type = ParseType();
|
var type = ParseType();
|
||||||
|
|
||||||
return new FuncParameterSyntax(GetTokens(startIndex), name.Value, type);
|
return new FuncParameterSyntax(GetTokens(startIndex), name, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
private FuncSyntax ParseFunc(int startIndex, bool exported, string? externSymbol)
|
private FuncSyntax ParseFunc(int startIndex, bool exported, StringLiteralToken? externSymbol)
|
||||||
{
|
{
|
||||||
var name = ExpectIdentifier();
|
var name = ExpectIdentifier();
|
||||||
List<FuncParameterSyntax> parameters = [];
|
List<FuncParameterSyntax> parameters = [];
|
||||||
@@ -136,7 +122,7 @@ public sealed class Parser
|
|||||||
|
|
||||||
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new VoidTypeSyntax([]);
|
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new VoidTypeSyntax([]);
|
||||||
|
|
||||||
var prototype = new FuncPrototypeSyntax(GetTokens(startIndex), name.Value, exported, externSymbol, parameters, returnType);
|
var prototype = new FuncPrototypeSyntax(GetTokens(startIndex), name, exported, externSymbol, parameters, returnType);
|
||||||
|
|
||||||
BlockSyntax? body = null;
|
BlockSyntax? body = null;
|
||||||
var bodyStartIndex = _tokenIndex;
|
var bodyStartIndex = _tokenIndex;
|
||||||
@@ -148,7 +134,7 @@ public sealed class Parser
|
|||||||
return new FuncSyntax(GetTokens(startIndex), prototype, body);
|
return new FuncSyntax(GetTokens(startIndex), prototype, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
private StructSyntax ParseStruct(int startIndex, bool exported)
|
private StructSyntax ParseStruct(int startIndex, bool exported, bool packed)
|
||||||
{
|
{
|
||||||
var name = ExpectIdentifier();
|
var name = ExpectIdentifier();
|
||||||
|
|
||||||
@@ -160,7 +146,7 @@ public sealed class Parser
|
|||||||
{
|
{
|
||||||
var memberStartIndex = _tokenIndex;
|
var memberStartIndex = _tokenIndex;
|
||||||
|
|
||||||
var fieldName = ExpectIdentifier().Value;
|
var fieldName = ExpectIdentifier();
|
||||||
ExpectSymbol(Symbol.Colon);
|
ExpectSymbol(Symbol.Colon);
|
||||||
var fieldType = ParseType();
|
var fieldType = ParseType();
|
||||||
|
|
||||||
@@ -174,7 +160,7 @@ public sealed class Parser
|
|||||||
fields.Add(new StructFieldSyntax(GetTokens(memberStartIndex), fieldName, fieldType, fieldValue));
|
fields.Add(new StructFieldSyntax(GetTokens(memberStartIndex), fieldName, fieldType, fieldValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StructSyntax(GetTokens(startIndex), name.Value, exported, fields);
|
return new StructSyntax(GetTokens(startIndex), name, exported, packed, fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
private EnumSyntax ParseEnum(int startIndex, bool exported)
|
private EnumSyntax ParseEnum(int startIndex, bool exported)
|
||||||
@@ -188,68 +174,69 @@ public sealed class Parser
|
|||||||
type = ParseType();
|
type = ParseType();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<EnumFieldSyntax> fields = [];
|
List<EnumMemberSyntax> fields = [];
|
||||||
|
|
||||||
ExpectSymbol(Symbol.OpenBrace);
|
ExpectSymbol(Symbol.OpenBrace);
|
||||||
|
|
||||||
long value = -1;
|
|
||||||
|
|
||||||
while (!TryExpectSymbol(Symbol.CloseBrace))
|
while (!TryExpectSymbol(Symbol.CloseBrace))
|
||||||
{
|
{
|
||||||
var memberStartIndex = _tokenIndex;
|
var memberStartIndex = _tokenIndex;
|
||||||
var fieldName = ExpectIdentifier().Value;
|
var fieldName = ExpectIdentifier();
|
||||||
long fieldValue;
|
IntLiteralToken? value = null;
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Assign))
|
if (TryExpectSymbol(Symbol.Assign))
|
||||||
{
|
{
|
||||||
if (!TryExpectIntLiteral(out var intLiteralToken))
|
if (!TryExpectIntLiteral(out var intLiteralToken))
|
||||||
{
|
{
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error("Value of enum field must be an integer literal")
|
.Error("Value of enum field must be an integer literal")
|
||||||
.At(CurrentToken)
|
.At(CurrentToken, _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldValue = Convert.ToInt64(intLiteralToken.Value, intLiteralToken.Base);
|
value = intLiteralToken;
|
||||||
value = fieldValue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fieldValue = value + 1;
|
|
||||||
value = fieldValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fields.Add(new EnumFieldSyntax(GetTokens(memberStartIndex), fieldName, fieldValue));
|
fields.Add(new EnumMemberSyntax(GetTokens(memberStartIndex), fieldName, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new EnumSyntax(GetTokens(startIndex), name.Value, exported, type, fields);
|
return new EnumSyntax(GetTokens(startIndex), name, exported, type, fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
private StatementSyntax ParseStatement()
|
private StatementSyntax ParseStatement()
|
||||||
{
|
{
|
||||||
var startIndex = _tokenIndex;
|
var startIndex = _tokenIndex;
|
||||||
|
|
||||||
if (TryExpectSymbol(out var symbol))
|
if (CurrentToken is SymbolToken symbolToken)
|
||||||
{
|
{
|
||||||
switch (symbol)
|
switch (symbolToken.Symbol)
|
||||||
{
|
{
|
||||||
case Symbol.OpenBrace:
|
case Symbol.OpenBrace:
|
||||||
|
Next();
|
||||||
return ParseBlock(startIndex);
|
return ParseBlock(startIndex);
|
||||||
case Symbol.Return:
|
case Symbol.Return:
|
||||||
|
Next();
|
||||||
return ParseReturn(startIndex);
|
return ParseReturn(startIndex);
|
||||||
case Symbol.If:
|
case Symbol.If:
|
||||||
|
Next();
|
||||||
return ParseIf(startIndex);
|
return ParseIf(startIndex);
|
||||||
case Symbol.While:
|
case Symbol.While:
|
||||||
|
Next();
|
||||||
return ParseWhile(startIndex);
|
return ParseWhile(startIndex);
|
||||||
case Symbol.For:
|
case Symbol.For:
|
||||||
|
Next();
|
||||||
return ParseFor(startIndex);
|
return ParseFor(startIndex);
|
||||||
case Symbol.Let:
|
case Symbol.Let:
|
||||||
|
Next();
|
||||||
return ParseVariableDeclaration(startIndex);
|
return ParseVariableDeclaration(startIndex);
|
||||||
case Symbol.Defer:
|
case Symbol.Defer:
|
||||||
|
Next();
|
||||||
return ParseDefer(startIndex);
|
return ParseDefer(startIndex);
|
||||||
case Symbol.Break:
|
case Symbol.Break:
|
||||||
|
Next();
|
||||||
return new BreakSyntax(GetTokens(startIndex));
|
return new BreakSyntax(GetTokens(startIndex));
|
||||||
case Symbol.Continue:
|
case Symbol.Continue:
|
||||||
|
Next();
|
||||||
return new ContinueSyntax(GetTokens(startIndex));
|
return new ContinueSyntax(GetTokens(startIndex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -267,7 +254,7 @@ public sealed class Parser
|
|||||||
|
|
||||||
private VariableDeclarationSyntax ParseVariableDeclaration(int startIndex)
|
private VariableDeclarationSyntax ParseVariableDeclaration(int startIndex)
|
||||||
{
|
{
|
||||||
var name = ExpectIdentifier().Value;
|
var name = ExpectIdentifier();
|
||||||
|
|
||||||
TypeSyntax? explicitType = null;
|
TypeSyntax? explicitType = null;
|
||||||
if (TryExpectSymbol(Symbol.Colon))
|
if (TryExpectSymbol(Symbol.Colon))
|
||||||
@@ -334,12 +321,12 @@ public sealed class Parser
|
|||||||
|
|
||||||
private ForSyntax ParseFor(int startIndex)
|
private ForSyntax ParseFor(int startIndex)
|
||||||
{
|
{
|
||||||
var itemName = ExpectIdentifier().Value;
|
var itemName = ExpectIdentifier();
|
||||||
string? indexName = null;
|
IdentifierToken? indexName = null;
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Comma))
|
if (TryExpectSymbol(Symbol.Comma))
|
||||||
{
|
{
|
||||||
indexName = ExpectIdentifier().Value;
|
indexName = ExpectIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
ExpectSymbol(Symbol.In);
|
ExpectSymbol(Symbol.In);
|
||||||
@@ -354,7 +341,7 @@ public sealed class Parser
|
|||||||
var startIndex = _tokenIndex;
|
var startIndex = _tokenIndex;
|
||||||
var left = ParsePrimaryExpression();
|
var left = ParsePrimaryExpression();
|
||||||
|
|
||||||
while (CurrentToken is SymbolToken symbolToken && TryGetBinaryOperator(symbolToken.Symbol, out var op) && GetBinaryOperatorPrecedence(op.Value) >= precedence)
|
while (CurrentToken is SymbolToken symbolToken && HasLeadingWhitespace(symbolToken) && HasTrailingWhitespace(symbolToken) && TryGetBinaryOperator(symbolToken.Symbol, out var op) && GetBinaryOperatorPrecedence(op.Value) >= precedence)
|
||||||
{
|
{
|
||||||
Next();
|
Next();
|
||||||
var right = ParseExpression(GetBinaryOperatorPrecedence(op.Value) + 1);
|
var right = ParseExpression(GetBinaryOperatorPrecedence(op.Value) + 1);
|
||||||
@@ -452,7 +439,7 @@ public sealed class Parser
|
|||||||
case Symbol.Pipe:
|
case Symbol.Pipe:
|
||||||
binaryExpressionOperator = BinaryOperatorSyntax.BitwiseOr;
|
binaryExpressionOperator = BinaryOperatorSyntax.BitwiseOr;
|
||||||
return true;
|
return true;
|
||||||
case Symbol.Caret:
|
case Symbol.Tilde:
|
||||||
binaryExpressionOperator = BinaryOperatorSyntax.BitwiseXor;
|
binaryExpressionOperator = BinaryOperatorSyntax.BitwiseXor;
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
@@ -467,99 +454,47 @@ public sealed class Parser
|
|||||||
var token = ExpectToken();
|
var token = ExpectToken();
|
||||||
var expr = token switch
|
var expr = token switch
|
||||||
{
|
{
|
||||||
BoolLiteralToken boolLiteral => new BoolLiteralSyntax(GetTokens(startIndex), boolLiteral.Value),
|
BoolLiteralToken boolLiteral => new BoolLiteralSyntax(GetTokens(startIndex), boolLiteral),
|
||||||
StringLiteralToken stringLiteral => new StringLiteralSyntax(GetTokens(startIndex), stringLiteral.Value),
|
StringLiteralToken stringLiteral => new StringLiteralSyntax(GetTokens(startIndex), stringLiteral),
|
||||||
FloatLiteralToken floatLiteral => new FloatLiteralSyntax(GetTokens(startIndex), floatLiteral.Value),
|
FloatLiteralToken floatLiteral => new FloatLiteralSyntax(GetTokens(startIndex), floatLiteral),
|
||||||
IntLiteralToken intLiteral => new IntLiteralSyntax(GetTokens(startIndex), intLiteral.Value, intLiteral.Base),
|
IntLiteralToken intLiteral => new IntLiteralSyntax(GetTokens(startIndex), intLiteral),
|
||||||
IdentifierToken identifier => ParseIdentifier(startIndex, identifier),
|
IdentifierToken identifier => ParseIdentifier(startIndex, identifier),
|
||||||
SymbolToken symbolToken => symbolToken.Symbol switch
|
SymbolToken symbolToken => symbolToken.Symbol switch
|
||||||
{
|
{
|
||||||
|
Symbol.Caret => ParseAddressOf(startIndex),
|
||||||
Symbol.OpenParen => ParseParenthesizedExpression(),
|
Symbol.OpenParen => ParseParenthesizedExpression(),
|
||||||
Symbol.Minus => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, ParsePrimaryExpression()),
|
Symbol.Minus => ParseUnaryNegate(startIndex),
|
||||||
Symbol.Bang => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, ParsePrimaryExpression()),
|
Symbol.Bang => ParseUnaryInvert(startIndex),
|
||||||
Symbol.OpenBracket => ParseArrayInitializer(startIndex),
|
Symbol.OpenBracket => ParseArrayInitializer(startIndex),
|
||||||
Symbol.OpenBrace => new StructInitializerSyntax(GetTokens(startIndex), null, ParseStructInitializerBody()),
|
Symbol.OpenBrace => ParseUnnamedStructInitializer(startIndex),
|
||||||
Symbol.Struct => ParseStructInitializer(startIndex),
|
Symbol.Struct => ParseStructInitializer(startIndex),
|
||||||
Symbol.At => ParseBuiltinFunction(startIndex),
|
Symbol.At => ParseBuiltinFunction(startIndex),
|
||||||
_ => throw new ParseException(Diagnostic
|
_ => throw new CompileException(Diagnostic
|
||||||
.Error($"Unexpected symbol '{symbolToken.Symbol}' in expression")
|
.Error($"Unexpected symbol '{symbolToken.Symbol}' in expression")
|
||||||
.WithHelp("Expected '(', '-', '!', '[' or '{'")
|
.WithHelp("Expected '(', '-', '!', '[' or '{'")
|
||||||
.At(symbolToken)
|
.At(symbolToken, _tokens)
|
||||||
.Build())
|
.Build())
|
||||||
},
|
},
|
||||||
_ => throw new ParseException(Diagnostic
|
_ => throw new CompileException(Diagnostic
|
||||||
.Error($"Unexpected token '{token.GetType().Name}' in expression")
|
.Error($"Unexpected token '{token.GetType().Name}' in expression")
|
||||||
.WithHelp("Expected literal, identifier, or parenthesized expression")
|
.WithHelp("Expected literal, identifier, or parenthesized expression")
|
||||||
.At(token)
|
.At(token, _tokens)
|
||||||
.Build())
|
.Build())
|
||||||
};
|
};
|
||||||
|
|
||||||
return ParsePostfixOperators(expr);
|
return ParsePostfixOperators(expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExpressionSyntax ParseBuiltinFunction(int startIndex)
|
|
||||||
{
|
|
||||||
var name = ExpectIdentifier();
|
|
||||||
ExpectSymbol(Symbol.OpenParen);
|
|
||||||
|
|
||||||
switch (name.Value)
|
|
||||||
{
|
|
||||||
case "size":
|
|
||||||
{
|
|
||||||
var type = ParseType();
|
|
||||||
ExpectSymbol(Symbol.CloseParen);
|
|
||||||
return new SizeSyntax(GetTokens(startIndex), type);
|
|
||||||
}
|
|
||||||
case "interpret":
|
|
||||||
{
|
|
||||||
var type = ParseType();
|
|
||||||
ExpectSymbol(Symbol.Comma);
|
|
||||||
var expression = ParseExpression();
|
|
||||||
ExpectSymbol(Symbol.CloseParen);
|
|
||||||
return new InterpretSyntax(GetTokens(startIndex), type, expression);
|
|
||||||
}
|
|
||||||
case "cast":
|
|
||||||
{
|
|
||||||
var expression = ParseExpression();
|
|
||||||
ExpectSymbol(Symbol.CloseParen);
|
|
||||||
return new CastSyntax(GetTokens(startIndex), expression);
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
throw new ParseException(Diagnostic.Error($"Unknown builtin {name.Value}").At(name).Build());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ExpressionSyntax ParseIdentifier(int startIndex, IdentifierToken identifier)
|
|
||||||
{
|
|
||||||
if (TryExpectSymbol(Symbol.DoubleColon))
|
|
||||||
{
|
|
||||||
var name = ExpectIdentifier();
|
|
||||||
return new ModuleIdentifierSyntax(GetTokens(startIndex), identifier.Value, name.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new LocalIdentifierSyntax(GetTokens(startIndex), identifier.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ExpressionSyntax ParseParenthesizedExpression()
|
|
||||||
{
|
|
||||||
var expression = ParseExpression();
|
|
||||||
ExpectSymbol(Symbol.CloseParen);
|
|
||||||
return expression;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ExpressionSyntax ParsePostfixOperators(ExpressionSyntax expr)
|
private ExpressionSyntax ParsePostfixOperators(ExpressionSyntax expr)
|
||||||
{
|
{
|
||||||
|
if (CurrentToken == null || HasLeadingWhitespace(CurrentToken))
|
||||||
|
{
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
var startIndex = _tokenIndex;
|
var startIndex = _tokenIndex;
|
||||||
while (HasToken)
|
while (HasToken)
|
||||||
{
|
{
|
||||||
if (TryExpectSymbol(Symbol.Ampersand))
|
|
||||||
{
|
|
||||||
expr = new AddressOfSyntax(GetTokens(startIndex), expr);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Caret))
|
if (TryExpectSymbol(Symbol.Caret))
|
||||||
{
|
{
|
||||||
expr = new DereferenceSyntax(GetTokens(startIndex), expr);
|
expr = new DereferenceSyntax(GetTokens(startIndex), expr);
|
||||||
@@ -568,7 +503,7 @@ public sealed class Parser
|
|||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Period))
|
if (TryExpectSymbol(Symbol.Period))
|
||||||
{
|
{
|
||||||
var member = ExpectIdentifier().Value;
|
var member = ExpectIdentifier();
|
||||||
expr = new MemberAccessSyntax(GetTokens(startIndex), expr, member);
|
expr = new MemberAccessSyntax(GetTokens(startIndex), expr, member);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -605,6 +540,68 @@ public sealed class Parser
|
|||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ExpressionSyntax ParseParenthesizedExpression()
|
||||||
|
{
|
||||||
|
var expression = ParseExpression();
|
||||||
|
ExpectSymbol(Symbol.CloseParen);
|
||||||
|
return expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
private AddressOfSyntax ParseAddressOf(int startIndex)
|
||||||
|
{
|
||||||
|
var expression = ParsePrimaryExpression();
|
||||||
|
return new AddressOfSyntax(GetTokens(startIndex), expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
private UnaryExpressionSyntax ParseUnaryInvert(int startIndex)
|
||||||
|
{
|
||||||
|
var expression = ParsePrimaryExpression();
|
||||||
|
return new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
private UnaryExpressionSyntax ParseUnaryNegate(int startIndex)
|
||||||
|
{
|
||||||
|
var expression = ParsePrimaryExpression();
|
||||||
|
return new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ExpressionSyntax ParseBuiltinFunction(int startIndex)
|
||||||
|
{
|
||||||
|
var name = ExpectIdentifier();
|
||||||
|
ExpectSymbol(Symbol.OpenParen);
|
||||||
|
|
||||||
|
switch (name.Value)
|
||||||
|
{
|
||||||
|
case "size":
|
||||||
|
{
|
||||||
|
var type = ParseType();
|
||||||
|
ExpectSymbol(Symbol.CloseParen);
|
||||||
|
return new SizeSyntax(GetTokens(startIndex), type);
|
||||||
|
}
|
||||||
|
case "cast":
|
||||||
|
{
|
||||||
|
var expression = ParseExpression();
|
||||||
|
ExpectSymbol(Symbol.CloseParen);
|
||||||
|
return new CastSyntax(GetTokens(startIndex), expression);
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
throw new CompileException(Diagnostic.Error($"Unknown builtin {name.Value}").At(name).Build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ExpressionSyntax ParseIdentifier(int startIndex, IdentifierToken identifier)
|
||||||
|
{
|
||||||
|
if (TryExpectSymbol(Symbol.DoubleColon))
|
||||||
|
{
|
||||||
|
var name = ExpectIdentifier();
|
||||||
|
return new ModuleIdentifierSyntax(GetTokens(startIndex), identifier, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new LocalIdentifierSyntax(GetTokens(startIndex), identifier);
|
||||||
|
}
|
||||||
|
|
||||||
private ExpressionSyntax ParseArrayInitializer(int startIndex)
|
private ExpressionSyntax ParseArrayInitializer(int startIndex)
|
||||||
{
|
{
|
||||||
var values = new List<ExpressionSyntax>();
|
var values = new List<ExpressionSyntax>();
|
||||||
@@ -635,12 +632,18 @@ public sealed class Parser
|
|||||||
return new StructInitializerSyntax(GetTokens(startIndex), type, initializers);
|
return new StructInitializerSyntax(GetTokens(startIndex), type, initializers);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<string, ExpressionSyntax> ParseStructInitializerBody()
|
private StructInitializerSyntax ParseUnnamedStructInitializer(int startIndex)
|
||||||
{
|
{
|
||||||
Dictionary<string, ExpressionSyntax> initializers = [];
|
var body = ParseStructInitializerBody();
|
||||||
|
return new StructInitializerSyntax(GetTokens(startIndex), null, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<IdentifierToken, ExpressionSyntax> ParseStructInitializerBody()
|
||||||
|
{
|
||||||
|
Dictionary<IdentifierToken, ExpressionSyntax> initializers = [];
|
||||||
while (!TryExpectSymbol(Symbol.CloseBrace))
|
while (!TryExpectSymbol(Symbol.CloseBrace))
|
||||||
{
|
{
|
||||||
var name = ExpectIdentifier().Value;
|
var name = ExpectIdentifier();
|
||||||
ExpectSymbol(Symbol.Assign);
|
ExpectSymbol(Symbol.Assign);
|
||||||
var value = ParseExpression();
|
var value = ParseExpression();
|
||||||
initializers.Add(name, value);
|
initializers.Add(name, value);
|
||||||
@@ -666,7 +669,7 @@ public sealed class Parser
|
|||||||
{
|
{
|
||||||
statements.Add(ParseStatement());
|
statements.Add(ParseStatement());
|
||||||
}
|
}
|
||||||
catch (ParseException ex)
|
catch (CompileException ex)
|
||||||
{
|
{
|
||||||
Diagnostics.Add(ex.Diagnostic);
|
Diagnostics.Add(ex.Diagnostic);
|
||||||
if (HasToken)
|
if (HasToken)
|
||||||
@@ -688,42 +691,42 @@ public sealed class Parser
|
|||||||
var startIndex = _tokenIndex;
|
var startIndex = _tokenIndex;
|
||||||
if (TryExpectIdentifier(out var name))
|
if (TryExpectIdentifier(out var name))
|
||||||
{
|
{
|
||||||
if (name.Value[0] == 'u' && int.TryParse(name.Value[1..], out var size))
|
if (name.Value[0] == 'u' && ulong.TryParse(name.Value[1..], out var size))
|
||||||
{
|
{
|
||||||
if (size is not 8 and not 16 and not 32 and not 64)
|
if (size is not 8 and not 16 and not 32 and not 64)
|
||||||
{
|
{
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error("Arbitrary uint size is not supported")
|
.Error("Arbitrary uint size is not supported")
|
||||||
.WithHelp("Use u8, u16, u32 or u64")
|
.WithHelp("Use u8, u16, u32 or u64")
|
||||||
.At(name)
|
.At(name, _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new IntTypeSyntax(GetTokens(startIndex), false, size);
|
return new IntTypeSyntax(GetTokens(startIndex), false, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.Value[0] == 'i' && int.TryParse(name.Value[1..], out size))
|
if (name.Value[0] == 'i' && ulong.TryParse(name.Value[1..], out size))
|
||||||
{
|
{
|
||||||
if (size is not 8 and not 16 and not 32 and not 64)
|
if (size is not 8 and not 16 and not 32 and not 64)
|
||||||
{
|
{
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error("Arbitrary int size is not supported")
|
.Error("Arbitrary int size is not supported")
|
||||||
.WithHelp("Use i8, i16, i32 or i64")
|
.WithHelp("Use i8, i16, i32 or i64")
|
||||||
.At(name)
|
.At(name, _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new IntTypeSyntax(GetTokens(startIndex), true, size);
|
return new IntTypeSyntax(GetTokens(startIndex), true, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.Value[0] == 'f' && int.TryParse(name.Value[1..], out size))
|
if (name.Value[0] == 'f' && ulong.TryParse(name.Value[1..], out size))
|
||||||
{
|
{
|
||||||
if (size is not 32 and not 64)
|
if (size is not 32 and not 64)
|
||||||
{
|
{
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error("Arbitrary float size is not supported")
|
.Error("Arbitrary float size is not supported")
|
||||||
.WithHelp("Use f32 or f64")
|
.WithHelp("Use f32 or f64")
|
||||||
.At(name)
|
.At(name, _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -736,22 +739,20 @@ public sealed class Parser
|
|||||||
return new VoidTypeSyntax(GetTokens(startIndex));
|
return new VoidTypeSyntax(GetTokens(startIndex));
|
||||||
case "string":
|
case "string":
|
||||||
return new StringTypeSyntax(GetTokens(startIndex));
|
return new StringTypeSyntax(GetTokens(startIndex));
|
||||||
case "cstring":
|
|
||||||
return new CStringTypeSyntax(GetTokens(startIndex));
|
|
||||||
case "bool":
|
case "bool":
|
||||||
return new BoolTypeSyntax(GetTokens(startIndex));
|
return new BoolTypeSyntax(GetTokens(startIndex));
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
string? module = null;
|
IdentifierToken? module = null;
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.DoubleColon))
|
if (TryExpectSymbol(Symbol.DoubleColon))
|
||||||
{
|
{
|
||||||
var customTypeName = ExpectIdentifier();
|
var customTypeName = ExpectIdentifier();
|
||||||
module = name.Value;
|
module = name;
|
||||||
name = customTypeName;
|
name = customTypeName;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CustomTypeSyntax(GetTokens(startIndex), module, name.Value);
|
return new CustomTypeSyntax(GetTokens(startIndex), module, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -790,7 +791,7 @@ public sealed class Parser
|
|||||||
{
|
{
|
||||||
ExpectSymbol(Symbol.CloseBracket);
|
ExpectSymbol(Symbol.CloseBracket);
|
||||||
var baseType = ParseType();
|
var baseType = ParseType();
|
||||||
return new ConstArrayTypeSyntax(GetTokens(startIndex), baseType, Convert.ToInt64(intLiteral.Value, intLiteral.Base));
|
return new ConstArrayTypeSyntax(GetTokens(startIndex), baseType, intLiteral.AsU64);
|
||||||
}
|
}
|
||||||
else if (TryExpectSymbol(Symbol.QuestionMark))
|
else if (TryExpectSymbol(Symbol.QuestionMark))
|
||||||
{
|
{
|
||||||
@@ -806,10 +807,10 @@ public sealed class Parser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error("Invalid type syntax")
|
.Error("Invalid type syntax")
|
||||||
.WithHelp("Expected type name, '^' for pointer, or '[]' for array")
|
.WithHelp("Expected type name, '^' for pointer, or '[]' for array")
|
||||||
.At(CurrentToken)
|
.At(CurrentToken, _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -817,10 +818,10 @@ public sealed class Parser
|
|||||||
{
|
{
|
||||||
if (!HasToken)
|
if (!HasToken)
|
||||||
{
|
{
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error("Unexpected end of file")
|
.Error("Unexpected end of file")
|
||||||
.WithHelp("Expected more tokens to complete the syntax")
|
.WithHelp("Expected more tokens to complete the syntax")
|
||||||
.At(_tokens[^1])
|
.At(_tokens[^1], _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -834,10 +835,10 @@ public sealed class Parser
|
|||||||
var token = ExpectToken();
|
var token = ExpectToken();
|
||||||
if (token is not SymbolToken symbol)
|
if (token is not SymbolToken symbol)
|
||||||
{
|
{
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error($"Expected symbol, but found {token.GetType().Name}")
|
.Error($"Expected symbol, but found {token.GetType().Name}")
|
||||||
.WithHelp("This position requires a symbol like '(', ')', '{', '}', etc.")
|
.WithHelp("This position requires a symbol like '(', ')', '{', '}', etc.")
|
||||||
.At(token)
|
.At(token, _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -849,10 +850,10 @@ public sealed class Parser
|
|||||||
var token = ExpectSymbol();
|
var token = ExpectSymbol();
|
||||||
if (token.Symbol != expectedSymbol)
|
if (token.Symbol != expectedSymbol)
|
||||||
{
|
{
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error($"Expected '{expectedSymbol}', but found '{token.Symbol}'")
|
.Error($"Expected '{expectedSymbol}', but found '{token.Symbol}'")
|
||||||
.WithHelp($"Insert '{expectedSymbol}' here")
|
.WithHelp($"Insert '{expectedSymbol}' here")
|
||||||
.At(token)
|
.At(token, _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -899,10 +900,10 @@ public sealed class Parser
|
|||||||
var token = ExpectToken();
|
var token = ExpectToken();
|
||||||
if (token is not IdentifierToken identifier)
|
if (token is not IdentifierToken identifier)
|
||||||
{
|
{
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error($"Expected identifier, but found {token.GetType().Name}")
|
.Error($"Expected identifier, but found {token.GetType().Name}")
|
||||||
.WithHelp("Provide a valid identifier name here")
|
.WithHelp("Provide a valid identifier name here")
|
||||||
.At(token)
|
.At(token, _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -927,10 +928,10 @@ public sealed class Parser
|
|||||||
var token = ExpectToken();
|
var token = ExpectToken();
|
||||||
if (token is not StringLiteralToken identifier)
|
if (token is not StringLiteralToken identifier)
|
||||||
{
|
{
|
||||||
throw new ParseException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error($"Expected string literal, but found {token.GetType().Name}")
|
.Error($"Expected string literal, but found {token.GetType().Name}")
|
||||||
.WithHelp("Provide a valid string literal")
|
.WithHelp("Provide a valid string literal")
|
||||||
.At(token)
|
.At(token, _tokens)
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -940,6 +941,10 @@ public sealed class Parser
|
|||||||
private void Next()
|
private void Next()
|
||||||
{
|
{
|
||||||
_tokenIndex++;
|
_tokenIndex++;
|
||||||
|
while (_tokenIndex < _tokens.Count && _tokens[_tokenIndex] is WhitespaceToken or CommentToken)
|
||||||
|
{
|
||||||
|
_tokenIndex++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Token> GetTokens(int tokenStartIndex)
|
private List<Token> GetTokens(int tokenStartIndex)
|
||||||
@@ -948,14 +953,4 @@ public sealed class Parser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public record SyntaxTree(List<DefinitionSyntax> Definitions, string ModuleName, List<string> Imports);
|
public record SyntaxTree(List<TopLevelSyntaxNode> TopLevelSyntaxNodes, List<Token> Tokens);
|
||||||
|
|
||||||
public class ParseException : Exception
|
|
||||||
{
|
|
||||||
public Diagnostic Diagnostic { get; }
|
|
||||||
|
|
||||||
public ParseException(Diagnostic diagnostic) : base(diagnostic.Message)
|
|
||||||
{
|
|
||||||
Diagnostic = diagnostic;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,21 +4,25 @@ public abstract record SyntaxNode(List<Token> Tokens);
|
|||||||
|
|
||||||
#region Definitions
|
#region Definitions
|
||||||
|
|
||||||
public abstract record DefinitionSyntax(List<Token> Tokens, string Name, bool Exported) : SyntaxNode(Tokens);
|
public record TopLevelSyntaxNode(List<Token> Tokens) : SyntaxNode(Tokens);
|
||||||
|
|
||||||
public record FuncParameterSyntax(List<Token> Tokens, string Name, TypeSyntax Type) : SyntaxNode(Tokens);
|
public record ModuleSyntax(List<Token> Tokens, IdentifierToken NameToken) : TopLevelSyntaxNode(Tokens);
|
||||||
|
|
||||||
public record FuncPrototypeSyntax(List<Token> Tokens, string Name, bool Exported, string? ExternSymbol, List<FuncParameterSyntax> Parameters, TypeSyntax ReturnType) : SyntaxNode(Tokens);
|
public abstract record DefinitionSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported) : TopLevelSyntaxNode(Tokens);
|
||||||
|
|
||||||
public record FuncSyntax(List<Token> Tokens, FuncPrototypeSyntax Prototype, BlockSyntax? Body) : DefinitionSyntax(Tokens, Prototype.Name, Prototype.Exported);
|
public record FuncParameterSyntax(List<Token> Tokens, IdentifierToken NameToken, TypeSyntax Type) : SyntaxNode(Tokens);
|
||||||
|
|
||||||
public record StructFieldSyntax(List<Token> Tokens, string Name, TypeSyntax Type, ExpressionSyntax? Value) : SyntaxNode(Tokens);
|
public record FuncPrototypeSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported, StringLiteralToken? ExternSymbolToken, List<FuncParameterSyntax> Parameters, TypeSyntax ReturnType) : SyntaxNode(Tokens);
|
||||||
|
|
||||||
public record StructSyntax(List<Token> Tokens, string Name, bool Exported, List<StructFieldSyntax> Fields) : DefinitionSyntax(Tokens, Name, Exported);
|
public record FuncSyntax(List<Token> Tokens, FuncPrototypeSyntax Prototype, BlockSyntax? Body) : DefinitionSyntax(Tokens, Prototype.NameToken, Prototype.Exported);
|
||||||
|
|
||||||
public record EnumFieldSyntax(List<Token> Tokens, string Name, long Value) : SyntaxNode(Tokens);
|
public record StructFieldSyntax(List<Token> Tokens, IdentifierToken NameToken, TypeSyntax Type, ExpressionSyntax? Value) : SyntaxNode(Tokens);
|
||||||
|
|
||||||
public record EnumSyntax(List<Token> Tokens, string Name, bool Exported, TypeSyntax? Type, List<EnumFieldSyntax> Fields) : DefinitionSyntax(Tokens, Name, Exported);
|
public record StructSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported, bool Packed, List<StructFieldSyntax> Fields) : DefinitionSyntax(Tokens, NameToken, Exported);
|
||||||
|
|
||||||
|
public record EnumMemberSyntax(List<Token> Tokens, IdentifierToken NameToken, IntLiteralToken? ValueToken) : SyntaxNode(Tokens);
|
||||||
|
|
||||||
|
public record EnumSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported, TypeSyntax? Type, List<EnumMemberSyntax> Members) : DefinitionSyntax(Tokens, NameToken, Exported);
|
||||||
|
|
||||||
public enum UnaryOperatorSyntax
|
public enum UnaryOperatorSyntax
|
||||||
{
|
{
|
||||||
@@ -64,7 +68,7 @@ public record AssignmentSyntax(List<Token> Tokens, ExpressionSyntax Target, Expr
|
|||||||
|
|
||||||
public record IfSyntax(List<Token> Tokens, ExpressionSyntax Condition, BlockSyntax Body, Variant<IfSyntax, BlockSyntax>? Else) : StatementSyntax(Tokens);
|
public record IfSyntax(List<Token> Tokens, ExpressionSyntax Condition, BlockSyntax Body, Variant<IfSyntax, BlockSyntax>? Else) : StatementSyntax(Tokens);
|
||||||
|
|
||||||
public record VariableDeclarationSyntax(List<Token> Tokens, string Name, TypeSyntax? ExplicitType, ExpressionSyntax? Assignment) : StatementSyntax(Tokens);
|
public record VariableDeclarationSyntax(List<Token> Tokens, IdentifierToken NameToken, TypeSyntax? ExplicitType, ExpressionSyntax? Assignment) : StatementSyntax(Tokens);
|
||||||
|
|
||||||
public record ContinueSyntax(List<Token> Tokens) : StatementSyntax(Tokens);
|
public record ContinueSyntax(List<Token> Tokens) : StatementSyntax(Tokens);
|
||||||
|
|
||||||
@@ -74,7 +78,7 @@ public record DeferSyntax(List<Token> Tokens, StatementSyntax Statement) : State
|
|||||||
|
|
||||||
public record WhileSyntax(List<Token> Tokens, ExpressionSyntax Condition, BlockSyntax Body) : StatementSyntax(Tokens);
|
public record WhileSyntax(List<Token> Tokens, ExpressionSyntax Condition, BlockSyntax Body) : StatementSyntax(Tokens);
|
||||||
|
|
||||||
public record ForSyntax(List<Token> Tokens, string ElementName, string? IndexName, ExpressionSyntax Target, BlockSyntax Body) : StatementSyntax(Tokens);
|
public record ForSyntax(List<Token> Tokens, IdentifierToken ElementNameToken, IdentifierToken? IndexNameToken, ExpressionSyntax Target, BlockSyntax Body) : StatementSyntax(Tokens);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -88,9 +92,9 @@ public record UnaryExpressionSyntax(List<Token> Tokens, UnaryOperatorSyntax Oper
|
|||||||
|
|
||||||
public record FuncCallSyntax(List<Token> Tokens, ExpressionSyntax Expression, List<ExpressionSyntax> Parameters) : ExpressionSyntax(Tokens);
|
public record FuncCallSyntax(List<Token> Tokens, ExpressionSyntax Expression, List<ExpressionSyntax> Parameters) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record LocalIdentifierSyntax(List<Token> Tokens, string Name) : ExpressionSyntax(Tokens);
|
public record LocalIdentifierSyntax(List<Token> Tokens, IdentifierToken NameToken) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record ModuleIdentifierSyntax(List<Token> Tokens, string Module, string Name) : ExpressionSyntax(Tokens);
|
public record ModuleIdentifierSyntax(List<Token> Tokens, IdentifierToken ModuleToken, IdentifierToken NameToken) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record ArrayInitializerSyntax(List<Token> Tokens, List<ExpressionSyntax> Values) : ExpressionSyntax(Tokens);
|
public record ArrayInitializerSyntax(List<Token> Tokens, List<ExpressionSyntax> Values) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
@@ -98,24 +102,22 @@ public record ArrayIndexAccessSyntax(List<Token> Tokens, ExpressionSyntax Target
|
|||||||
|
|
||||||
public record AddressOfSyntax(List<Token> Tokens, ExpressionSyntax Target) : ExpressionSyntax(Tokens);
|
public record AddressOfSyntax(List<Token> Tokens, ExpressionSyntax Target) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record IntLiteralSyntax(List<Token> Tokens, string Value, int Base) : ExpressionSyntax(Tokens);
|
public record IntLiteralSyntax(List<Token> Tokens, IntLiteralToken Token) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record StringLiteralSyntax(List<Token> Tokens, string Value) : ExpressionSyntax(Tokens);
|
public record StringLiteralSyntax(List<Token> Tokens, StringLiteralToken Token) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record BoolLiteralSyntax(List<Token> Tokens, bool Value) : ExpressionSyntax(Tokens);
|
public record BoolLiteralSyntax(List<Token> Tokens, BoolLiteralToken Token) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record FloatLiteralSyntax(List<Token> Tokens, string Value) : ExpressionSyntax(Tokens);
|
public record FloatLiteralSyntax(List<Token> Tokens, FloatLiteralToken Token) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record MemberAccessSyntax(List<Token> Tokens, ExpressionSyntax Target, string Member) : ExpressionSyntax(Tokens);
|
public record MemberAccessSyntax(List<Token> Tokens, ExpressionSyntax Target, IdentifierToken MemberToken) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record StructInitializerSyntax(List<Token> Tokens, TypeSyntax? StructType, Dictionary<string, ExpressionSyntax> Initializers) : ExpressionSyntax(Tokens);
|
public record StructInitializerSyntax(List<Token> Tokens, TypeSyntax? StructType, Dictionary<IdentifierToken, ExpressionSyntax> Initializers) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record DereferenceSyntax(List<Token> Tokens, ExpressionSyntax Target) : ExpressionSyntax(Tokens);
|
public record DereferenceSyntax(List<Token> Tokens, ExpressionSyntax Target) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record SizeSyntax(List<Token> Tokens, TypeSyntax Type) : ExpressionSyntax(Tokens);
|
public record SizeSyntax(List<Token> Tokens, TypeSyntax Type) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record InterpretSyntax(List<Token> Tokens, TypeSyntax Type, ExpressionSyntax Target) : ExpressionSyntax(Tokens);
|
|
||||||
|
|
||||||
public record CastSyntax(List<Token> Tokens, ExpressionSyntax Value) : ExpressionSyntax(Tokens);
|
public record CastSyntax(List<Token> Tokens, ExpressionSyntax Value) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -130,22 +132,20 @@ public record PointerTypeSyntax(List<Token> Tokens, TypeSyntax BaseType) : TypeS
|
|||||||
|
|
||||||
public record VoidTypeSyntax(List<Token> Tokens) : TypeSyntax(Tokens);
|
public record VoidTypeSyntax(List<Token> Tokens) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
public record IntTypeSyntax(List<Token> Tokens, bool Signed, int Width) : TypeSyntax(Tokens);
|
public record IntTypeSyntax(List<Token> Tokens, bool Signed, ulong Width) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
public record FloatTypeSyntax(List<Token> Tokens, int Width) : TypeSyntax(Tokens);
|
public record FloatTypeSyntax(List<Token> Tokens, ulong Width) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
public record BoolTypeSyntax(List<Token> Tokens) : TypeSyntax(Tokens);
|
public record BoolTypeSyntax(List<Token> Tokens) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
public record StringTypeSyntax(List<Token> Tokens) : TypeSyntax(Tokens);
|
public record StringTypeSyntax(List<Token> Tokens) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
public record CStringTypeSyntax(List<Token> Tokens) : TypeSyntax(Tokens);
|
|
||||||
|
|
||||||
public record SliceTypeSyntax(List<Token> Tokens, TypeSyntax BaseType) : TypeSyntax(Tokens);
|
public record SliceTypeSyntax(List<Token> Tokens, TypeSyntax BaseType) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
public record ArrayTypeSyntax(List<Token> Tokens, TypeSyntax BaseType) : TypeSyntax(Tokens);
|
public record ArrayTypeSyntax(List<Token> Tokens, TypeSyntax BaseType) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
public record ConstArrayTypeSyntax(List<Token> Tokens, TypeSyntax BaseType, long Size) : TypeSyntax(Tokens);
|
public record ConstArrayTypeSyntax(List<Token> Tokens, TypeSyntax BaseType, ulong Size) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
public record CustomTypeSyntax(List<Token> Tokens, string? Module, string Name) : TypeSyntax(Tokens);
|
public record CustomTypeSyntax(List<Token> Tokens, IdentifierToken? ModuleToken, IdentifierToken NameToken) : TypeSyntax(Tokens);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -2,8 +2,105 @@
|
|||||||
|
|
||||||
namespace NubLang.Syntax;
|
namespace NubLang.Syntax;
|
||||||
|
|
||||||
|
public abstract class Token(SourceSpan span)
|
||||||
|
{
|
||||||
|
public SourceSpan Span { get; } = span;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WhitespaceToken(SourceSpan span) : Token(span);
|
||||||
|
|
||||||
|
public class CommentToken(SourceSpan span, string comment) : Token(span)
|
||||||
|
{
|
||||||
|
public string Comment { get; } = comment;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return "// " + Comment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class IdentifierToken(SourceSpan span, string value) : Token(span)
|
||||||
|
{
|
||||||
|
public string Value { get; } = value;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class IntLiteralToken(SourceSpan span, string value, int @base) : Token(span)
|
||||||
|
{
|
||||||
|
public string Value { get; } = value;
|
||||||
|
public int Base { get; } = @base;
|
||||||
|
|
||||||
|
private string GetNumericValue()
|
||||||
|
{
|
||||||
|
return Base switch
|
||||||
|
{
|
||||||
|
2 when Value.StartsWith("0b", StringComparison.OrdinalIgnoreCase) => Value[2..],
|
||||||
|
8 when Value.StartsWith("0o", StringComparison.OrdinalIgnoreCase) => Value[2..],
|
||||||
|
16 when Value.StartsWith("0x", StringComparison.OrdinalIgnoreCase) => Value[2..],
|
||||||
|
_ => Value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public ulong AsU64 => Convert.ToUInt64(GetNumericValue(), Base);
|
||||||
|
public long AsI64 => Convert.ToInt64(GetNumericValue(), Base);
|
||||||
|
public uint AsU32 => Convert.ToUInt32(GetNumericValue(), Base);
|
||||||
|
public int AsI32 => Convert.ToInt32(GetNumericValue(), Base);
|
||||||
|
public ushort AsU16 => Convert.ToUInt16(GetNumericValue(), Base);
|
||||||
|
public short AsI16 => Convert.ToInt16(GetNumericValue(), Base);
|
||||||
|
public byte AsU8 => Convert.ToByte(GetNumericValue(), Base);
|
||||||
|
public sbyte AsI8 => Convert.ToSByte(GetNumericValue(), Base);
|
||||||
|
|
||||||
|
public float AsF32 => Convert.ToSingle(AsI32);
|
||||||
|
public double AsF64 => Convert.ToDouble(AsI64);
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StringLiteralToken(SourceSpan span, string value) : Token(span)
|
||||||
|
{
|
||||||
|
public string Value { get; } = value;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"\"{Value}\"";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BoolLiteralToken(SourceSpan span, bool value) : Token(span)
|
||||||
|
{
|
||||||
|
public bool Value { get; } = value;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Value ? "true" : "false";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class FloatLiteralToken(SourceSpan span, string value) : Token(span)
|
||||||
|
{
|
||||||
|
public string Value { get; } = value;
|
||||||
|
|
||||||
|
public float AsF32 => Convert.ToSingle(Value);
|
||||||
|
public double AsF64 => Convert.ToDouble(Value);
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public enum Symbol
|
public enum Symbol
|
||||||
{
|
{
|
||||||
|
// Default
|
||||||
|
None,
|
||||||
|
|
||||||
// Control
|
// Control
|
||||||
If,
|
If,
|
||||||
Else,
|
Else,
|
||||||
@@ -20,11 +117,11 @@ public enum Symbol
|
|||||||
Func,
|
Func,
|
||||||
Struct,
|
Struct,
|
||||||
Enum,
|
Enum,
|
||||||
Import,
|
|
||||||
Module,
|
Module,
|
||||||
|
|
||||||
// Modifier
|
// Modifier
|
||||||
Extern,
|
Extern,
|
||||||
|
Packed,
|
||||||
Export,
|
Export,
|
||||||
|
|
||||||
Colon,
|
Colon,
|
||||||
@@ -50,6 +147,7 @@ public enum Symbol
|
|||||||
Star,
|
Star,
|
||||||
ForwardSlash,
|
ForwardSlash,
|
||||||
Caret,
|
Caret,
|
||||||
|
Tilde,
|
||||||
Ampersand,
|
Ampersand,
|
||||||
Semi,
|
Semi,
|
||||||
Percent,
|
Percent,
|
||||||
@@ -62,16 +160,64 @@ public enum Symbol
|
|||||||
QuestionMark,
|
QuestionMark,
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract record Token(SourceSpan Span);
|
public class SymbolToken(SourceSpan span, Symbol symbol) : Token(span)
|
||||||
|
{
|
||||||
|
public Symbol Symbol { get; } = symbol;
|
||||||
|
|
||||||
public record IdentifierToken(SourceSpan Span, string Value) : Token(Span);
|
public override string ToString()
|
||||||
|
{
|
||||||
public record IntLiteralToken(SourceSpan Span, string Value, int Base) : Token(Span);
|
return Symbol switch
|
||||||
|
{
|
||||||
public record StringLiteralToken(SourceSpan Span, string Value) : Token(Span);
|
Symbol.Func => "func",
|
||||||
|
Symbol.If => "if",
|
||||||
public record BoolLiteralToken(SourceSpan Span, bool Value) : Token(Span);
|
Symbol.Else => "else",
|
||||||
|
Symbol.While => "while",
|
||||||
public record FloatLiteralToken(SourceSpan Span, string Value) : Token(Span);
|
Symbol.For => "for",
|
||||||
|
Symbol.In => "in",
|
||||||
public record SymbolToken(SourceSpan Span, Symbol Symbol) : Token(Span);
|
Symbol.Break => "break",
|
||||||
|
Symbol.Continue => "continue",
|
||||||
|
Symbol.Return => "return",
|
||||||
|
Symbol.Struct => "struct",
|
||||||
|
Symbol.Let => "let",
|
||||||
|
Symbol.Extern => "extern",
|
||||||
|
Symbol.Module => "module",
|
||||||
|
Symbol.Export => "export",
|
||||||
|
Symbol.Defer => "defer",
|
||||||
|
Symbol.Enum => "enum",
|
||||||
|
Symbol.Equal => "==",
|
||||||
|
Symbol.NotEqual => "!=",
|
||||||
|
Symbol.LessThanOrEqual => "<=",
|
||||||
|
Symbol.GreaterThanOrEqual => ">=",
|
||||||
|
Symbol.LeftShift => "<<",
|
||||||
|
Symbol.RightShift => ">>",
|
||||||
|
Symbol.And => "&&",
|
||||||
|
Symbol.Or => "||",
|
||||||
|
Symbol.DoubleColon => "::",
|
||||||
|
Symbol.Colon => ":",
|
||||||
|
Symbol.OpenParen => "(",
|
||||||
|
Symbol.CloseParen => ")",
|
||||||
|
Symbol.OpenBrace => "{",
|
||||||
|
Symbol.CloseBrace => "}",
|
||||||
|
Symbol.OpenBracket => "[",
|
||||||
|
Symbol.CloseBracket => "]",
|
||||||
|
Symbol.Comma => ",",
|
||||||
|
Symbol.Period => ".",
|
||||||
|
Symbol.Assign => "=",
|
||||||
|
Symbol.LessThan => "<",
|
||||||
|
Symbol.GreaterThan => ">",
|
||||||
|
Symbol.Plus => "+",
|
||||||
|
Symbol.Minus => "-",
|
||||||
|
Symbol.Star => "*",
|
||||||
|
Symbol.ForwardSlash => "/",
|
||||||
|
Symbol.Bang => "!",
|
||||||
|
Symbol.Caret => "^",
|
||||||
|
Symbol.Ampersand => "&",
|
||||||
|
Symbol.Semi => ";",
|
||||||
|
Symbol.Percent => "%",
|
||||||
|
Symbol.Pipe => "|",
|
||||||
|
Symbol.At => "@",
|
||||||
|
Symbol.QuestionMark => "?",
|
||||||
|
_ => "none",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,225 +4,175 @@ namespace NubLang.Syntax;
|
|||||||
|
|
||||||
public sealed class Tokenizer
|
public sealed class Tokenizer
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<string, Symbol> Keywords = new()
|
private string _fileName = null!;
|
||||||
{
|
private string _content = null!;
|
||||||
["func"] = Symbol.Func,
|
private int _index;
|
||||||
["if"] = Symbol.If,
|
|
||||||
["else"] = Symbol.Else,
|
|
||||||
["while"] = Symbol.While,
|
|
||||||
["for"] = Symbol.For,
|
|
||||||
["in"] = Symbol.In,
|
|
||||||
["break"] = Symbol.Break,
|
|
||||||
["continue"] = Symbol.Continue,
|
|
||||||
["return"] = Symbol.Return,
|
|
||||||
["struct"] = Symbol.Struct,
|
|
||||||
["let"] = Symbol.Let,
|
|
||||||
["extern"] = Symbol.Extern,
|
|
||||||
["module"] = Symbol.Module,
|
|
||||||
["export"] = Symbol.Export,
|
|
||||||
["import"] = Symbol.Import,
|
|
||||||
["defer"] = Symbol.Defer,
|
|
||||||
["enum"] = Symbol.Enum,
|
|
||||||
};
|
|
||||||
|
|
||||||
private static readonly Dictionary<char[], Symbol> Symbols = new()
|
|
||||||
{
|
|
||||||
[['=', '=']] = Symbol.Equal,
|
|
||||||
[['!', '=']] = Symbol.NotEqual,
|
|
||||||
[['<', '=']] = Symbol.LessThanOrEqual,
|
|
||||||
[['>', '=']] = Symbol.GreaterThanOrEqual,
|
|
||||||
[['<', '<']] = Symbol.LeftShift,
|
|
||||||
[['>', '>']] = Symbol.RightShift,
|
|
||||||
[['&', '&']] = Symbol.And,
|
|
||||||
[['|', '|']] = Symbol.Or,
|
|
||||||
[[':', ':']] = Symbol.DoubleColon,
|
|
||||||
[[':']] = Symbol.Colon,
|
|
||||||
[['(']] = Symbol.OpenParen,
|
|
||||||
[[')']] = Symbol.CloseParen,
|
|
||||||
[['{']] = Symbol.OpenBrace,
|
|
||||||
[['}']] = Symbol.CloseBrace,
|
|
||||||
[['[']] = Symbol.OpenBracket,
|
|
||||||
[[']']] = Symbol.CloseBracket,
|
|
||||||
[[',']] = Symbol.Comma,
|
|
||||||
[['.']] = Symbol.Period,
|
|
||||||
[['=']] = Symbol.Assign,
|
|
||||||
[['<']] = Symbol.LessThan,
|
|
||||||
[['>']] = Symbol.GreaterThan,
|
|
||||||
[['+']] = Symbol.Plus,
|
|
||||||
[['-']] = Symbol.Minus,
|
|
||||||
[['*']] = Symbol.Star,
|
|
||||||
[['/']] = Symbol.ForwardSlash,
|
|
||||||
[['!']] = Symbol.Bang,
|
|
||||||
[['^']] = Symbol.Caret,
|
|
||||||
[['&']] = Symbol.Ampersand,
|
|
||||||
[[';']] = Symbol.Semi,
|
|
||||||
[['%']] = Symbol.Percent,
|
|
||||||
[['|']] = Symbol.Pipe,
|
|
||||||
[['@']] = Symbol.At,
|
|
||||||
[['?']] = Symbol.QuestionMark,
|
|
||||||
};
|
|
||||||
|
|
||||||
private static readonly (char[] Pattern, Symbol Symbol)[] OrderedSymbols = Symbols
|
|
||||||
.OrderByDescending(kvp => kvp.Key.Length)
|
|
||||||
.Select(kvp => (kvp.Key, kvp.Value))
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
private readonly string _fileName;
|
|
||||||
private readonly string _content;
|
|
||||||
private int _index = 0;
|
|
||||||
private int _line = 1;
|
private int _line = 1;
|
||||||
private int _column = 1;
|
private int _column = 1;
|
||||||
|
|
||||||
public Tokenizer(string fileName, string content)
|
public List<Diagnostic> Diagnostics { get; set; } = new(16);
|
||||||
|
|
||||||
|
public List<Token> Tokenize(string fileName, string content)
|
||||||
{
|
{
|
||||||
_fileName = fileName;
|
_fileName = fileName;
|
||||||
_content = content;
|
_content = content;
|
||||||
}
|
|
||||||
|
|
||||||
public List<Diagnostic> Diagnostics { get; } = [];
|
Diagnostics = [];
|
||||||
public List<Token> Tokens { get; } = [];
|
|
||||||
|
|
||||||
public void Tokenize()
|
|
||||||
{
|
|
||||||
Diagnostics.Clear();
|
|
||||||
Tokens.Clear();
|
|
||||||
_index = 0;
|
_index = 0;
|
||||||
_line = 1;
|
_line = 1;
|
||||||
_column = 1;
|
_column = 1;
|
||||||
|
|
||||||
while (Peek().HasValue)
|
var tokens = new List<Token>();
|
||||||
|
|
||||||
|
while (_index < _content.Length)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var current = Peek()!.Value;
|
tokens.Add(ParseToken());
|
||||||
if (char.IsWhiteSpace(current))
|
|
||||||
{
|
|
||||||
if (current is '\n')
|
|
||||||
{
|
|
||||||
_line += 1;
|
|
||||||
// note(nub31): Next increments the column, so 0 is correct here
|
|
||||||
_column = 0;
|
|
||||||
}
|
}
|
||||||
|
catch (CompileException e)
|
||||||
Next();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == '/' && Peek(1) == '/')
|
|
||||||
{
|
|
||||||
// note(nub31): Keep newline so next iteration increments the line counter
|
|
||||||
while (Peek() is not '\n')
|
|
||||||
{
|
|
||||||
Next();
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Tokens.Add(ParseToken(current, _line, _column));
|
|
||||||
}
|
|
||||||
catch (TokenizerException e)
|
|
||||||
{
|
{
|
||||||
Diagnostics.Add(e.Diagnostic);
|
Diagnostics.Add(e.Diagnostic);
|
||||||
Next();
|
Next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Token ParseToken(char current, int lineStart, int columnStart)
|
private Token ParseToken()
|
||||||
{
|
{
|
||||||
if (char.IsLetter(current) || current == '_')
|
var indexStart = _index;
|
||||||
{
|
var lineStart = _line;
|
||||||
var buffer = string.Empty;
|
var columnStart = _column;
|
||||||
|
|
||||||
while (Peek() != null && (char.IsLetterOrDigit(Peek()!.Value) || Peek() == '_'))
|
if (char.IsWhiteSpace(_content[_index]))
|
||||||
|
{
|
||||||
|
while (_index < _content.Length && char.IsWhiteSpace(_content[_index]))
|
||||||
{
|
{
|
||||||
buffer += Peek();
|
|
||||||
Next();
|
Next();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Keywords.TryGetValue(buffer, out var keywordSymbol))
|
return new WhitespaceToken(CreateSpan(indexStart, lineStart, columnStart));
|
||||||
{
|
|
||||||
return new SymbolToken(CreateSpan(lineStart, columnStart), keywordSymbol);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer is "true" or "false")
|
if (_content[_index] == '/' && _index + 1 < _content.Length && _content[_index + 1] == '/')
|
||||||
{
|
{
|
||||||
return new BoolLiteralToken(CreateSpan(lineStart, columnStart), Convert.ToBoolean(buffer));
|
var startIndex = _index;
|
||||||
}
|
|
||||||
|
|
||||||
return new IdentifierToken(CreateSpan(lineStart, columnStart), buffer);
|
Next(2);
|
||||||
}
|
while (_index < _content.Length && _content[_index] != '\n')
|
||||||
|
|
||||||
if (char.IsDigit(current))
|
|
||||||
{
|
{
|
||||||
var buffer = string.Empty;
|
|
||||||
|
|
||||||
if (current == '0' && Peek(1) is 'x')
|
|
||||||
{
|
|
||||||
buffer += "0x";
|
|
||||||
Next();
|
|
||||||
Next();
|
|
||||||
while (Peek() != null && Uri.IsHexDigit(Peek()!.Value))
|
|
||||||
{
|
|
||||||
buffer += Peek()!.Value;
|
|
||||||
Next();
|
Next();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer.Length <= 2)
|
return new CommentToken(CreateSpan(indexStart, lineStart, columnStart), _content.AsSpan(startIndex, _index - startIndex).ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (char.IsDigit(_content[_index]))
|
||||||
{
|
{
|
||||||
throw new TokenizerException(Diagnostic
|
return ParseNumber(indexStart, lineStart, columnStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_content[_index] == '"')
|
||||||
|
{
|
||||||
|
return ParseString(indexStart, lineStart, columnStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
// note(nub31): Look for keywords (longest first in case a keyword fits partially in a larger keyword)
|
||||||
|
for (var i = 8; i >= 1; i--)
|
||||||
|
{
|
||||||
|
if (TryMatchSymbol(i, indexStart, lineStart, columnStart, out var token))
|
||||||
|
{
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (char.IsLetter(_content[_index]) || _content[_index] == '_')
|
||||||
|
{
|
||||||
|
return ParseIdentifier(indexStart, lineStart, columnStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new CompileException(Diagnostic.Error($"Unknown token '{_content[_index]}'").Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Token ParseNumber(int indexStart, int lineStart, int columnStart)
|
||||||
|
{
|
||||||
|
var start = _index;
|
||||||
|
var current = _content[_index];
|
||||||
|
|
||||||
|
// note(nub31): 0xFFFFFF
|
||||||
|
if (current == '0' && _index + 1 < _content.Length && _content[_index + 1] == 'x')
|
||||||
|
{
|
||||||
|
Next(2);
|
||||||
|
var digitStart = _index;
|
||||||
|
|
||||||
|
while (_index < _content.Length && Uri.IsHexDigit(_content[_index]))
|
||||||
|
{
|
||||||
|
Next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_index == digitStart)
|
||||||
|
{
|
||||||
|
throw new CompileException(Diagnostic
|
||||||
.Error("Invalid hex literal, no digits found")
|
.Error("Invalid hex literal, no digits found")
|
||||||
.At(_fileName, _line, _column)
|
.At(CreateSpan(_index, _line, _column))
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new IntLiteralToken(CreateSpan(lineStart, columnStart), buffer, 16);
|
return new IntLiteralToken(
|
||||||
|
CreateSpan(indexStart, lineStart, columnStart),
|
||||||
|
_content.Substring(start, _index - start),
|
||||||
|
16);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (current == '0' && Peek(1) is 'b')
|
// note(nub31): 0b11001100
|
||||||
|
if (current == '0' && _index + 1 < _content.Length && _content[_index + 1] == 'b')
|
||||||
{
|
{
|
||||||
buffer += "0b";
|
Next(2);
|
||||||
Next();
|
var digitStart = _index;
|
||||||
Next();
|
|
||||||
while (Peek() != null && (Peek() == '0' || Peek() == '1'))
|
while (_index < _content.Length && (_content[_index] == '0' || _content[_index] == '1'))
|
||||||
{
|
{
|
||||||
buffer += Peek()!.Value;
|
|
||||||
Next();
|
Next();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer.Length <= 2)
|
if (_index == digitStart)
|
||||||
{
|
{
|
||||||
throw new TokenizerException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error("Invalid binary literal, no digits found")
|
.Error("Invalid binary literal, no digits found")
|
||||||
.At(_fileName, _line, _column)
|
.At(CreateSpan(_index, _line, _column))
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new IntLiteralToken(CreateSpan(lineStart, columnStart), buffer, 2);
|
return new IntLiteralToken(
|
||||||
|
CreateSpan(indexStart, lineStart, columnStart),
|
||||||
|
_content.Substring(start, _index - start),
|
||||||
|
2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// note(nub31): 23/23.5
|
||||||
var isFloat = false;
|
var isFloat = false;
|
||||||
while (Peek() != null)
|
while (_index < _content.Length)
|
||||||
{
|
{
|
||||||
var next = Peek()!.Value;
|
var next = _content[_index];
|
||||||
|
|
||||||
if (next == '.')
|
if (next == '.')
|
||||||
{
|
{
|
||||||
if (isFloat)
|
if (isFloat)
|
||||||
{
|
{
|
||||||
throw new TokenizerException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error("More than one period found in float literal")
|
.Error("More than one period found in float literal")
|
||||||
.At(_fileName, _line, _column)
|
.At(CreateSpan(_index, _line, _column))
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
isFloat = true;
|
isFloat = true;
|
||||||
buffer += next;
|
|
||||||
Next();
|
Next();
|
||||||
}
|
}
|
||||||
else if (char.IsDigit(next))
|
else if (char.IsDigit(next))
|
||||||
{
|
{
|
||||||
buffer += next;
|
|
||||||
Next();
|
Next();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -231,101 +181,229 @@ public sealed class Tokenizer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFloat)
|
var buffer = _content.Substring(start, _index - start);
|
||||||
{
|
|
||||||
return new FloatLiteralToken(CreateSpan(lineStart, columnStart), buffer);
|
return isFloat
|
||||||
}
|
? new FloatLiteralToken(CreateSpan(indexStart, lineStart, columnStart), buffer)
|
||||||
else
|
: new IntLiteralToken(CreateSpan(indexStart, lineStart, columnStart), buffer, 10);
|
||||||
{
|
|
||||||
return new IntLiteralToken(CreateSpan(lineStart, columnStart), buffer, 10);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (current == '"')
|
private StringLiteralToken ParseString(int indexStart, int lineStart, int columnStart)
|
||||||
{
|
{
|
||||||
Next();
|
Next();
|
||||||
var buffer = string.Empty;
|
var start = _index;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var next = Peek();
|
if (_index >= _content.Length)
|
||||||
if (!next.HasValue)
|
|
||||||
{
|
{
|
||||||
throw new TokenizerException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error("Unclosed string literal")
|
.Error("Unclosed string literal")
|
||||||
.At(_fileName, _line, _column)
|
.At(CreateSpan(_index, _line, _column))
|
||||||
.Build());
|
.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (next is '\n')
|
var next = _content[_index];
|
||||||
|
|
||||||
|
if (next == '\n')
|
||||||
|
{
|
||||||
|
throw new CompileException(Diagnostic
|
||||||
|
.Error("Unclosed string literal (newline found)")
|
||||||
|
.At(CreateSpan(_index, _line, _column))
|
||||||
|
.Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (next == '"')
|
||||||
|
{
|
||||||
|
var buffer = _content.Substring(start, _index - start);
|
||||||
|
Next();
|
||||||
|
return new StringLiteralToken(CreateSpan(indexStart, lineStart, columnStart), buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
Next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool TryMatchSymbol(int length, int indexStart, int lineStart, int columnStart, out Token token)
|
||||||
|
{
|
||||||
|
token = null!;
|
||||||
|
|
||||||
|
if (_index + length > _content.Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var span = _content.AsSpan(_index, length);
|
||||||
|
|
||||||
|
if (span is "true")
|
||||||
|
{
|
||||||
|
Next(4);
|
||||||
|
token = new BoolLiteralToken(CreateSpan(indexStart, lineStart, columnStart), true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (span is "false")
|
||||||
|
{
|
||||||
|
Next(5);
|
||||||
|
token = new BoolLiteralToken(CreateSpan(indexStart, lineStart, columnStart), false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var symbol = length switch
|
||||||
|
{
|
||||||
|
8 => span switch
|
||||||
|
{
|
||||||
|
"continue" => Symbol.Continue,
|
||||||
|
_ => Symbol.None
|
||||||
|
},
|
||||||
|
6 => span switch
|
||||||
|
{
|
||||||
|
"return" => Symbol.Return,
|
||||||
|
"struct" => Symbol.Struct,
|
||||||
|
"extern" => Symbol.Extern,
|
||||||
|
"packed" => Symbol.Packed,
|
||||||
|
"module" => Symbol.Module,
|
||||||
|
"export" => Symbol.Export,
|
||||||
|
_ => Symbol.None
|
||||||
|
},
|
||||||
|
5 => span switch
|
||||||
|
{
|
||||||
|
"break" => Symbol.Break,
|
||||||
|
"while" => Symbol.While,
|
||||||
|
"defer" => Symbol.Defer,
|
||||||
|
_ => Symbol.None
|
||||||
|
},
|
||||||
|
4 => span switch
|
||||||
|
{
|
||||||
|
"func" => Symbol.Func,
|
||||||
|
"else" => Symbol.Else,
|
||||||
|
"enum" => Symbol.Enum,
|
||||||
|
_ => Symbol.None
|
||||||
|
},
|
||||||
|
3 => span switch
|
||||||
|
{
|
||||||
|
"for" => Symbol.For,
|
||||||
|
"let" => Symbol.Let,
|
||||||
|
_ => Symbol.None
|
||||||
|
},
|
||||||
|
2 => span switch
|
||||||
|
{
|
||||||
|
"if" => Symbol.If,
|
||||||
|
"in" => Symbol.In,
|
||||||
|
"==" => Symbol.Equal,
|
||||||
|
"!=" => Symbol.NotEqual,
|
||||||
|
"<=" => Symbol.LessThanOrEqual,
|
||||||
|
">=" => Symbol.GreaterThanOrEqual,
|
||||||
|
"<<" => Symbol.LeftShift,
|
||||||
|
">>" => Symbol.RightShift,
|
||||||
|
"&&" => Symbol.And,
|
||||||
|
"||" => Symbol.Or,
|
||||||
|
"::" => Symbol.DoubleColon,
|
||||||
|
_ => Symbol.None
|
||||||
|
},
|
||||||
|
1 => span[0] switch
|
||||||
|
{
|
||||||
|
':' => Symbol.Colon,
|
||||||
|
'(' => Symbol.OpenParen,
|
||||||
|
')' => Symbol.CloseParen,
|
||||||
|
'{' => Symbol.OpenBrace,
|
||||||
|
'}' => Symbol.CloseBrace,
|
||||||
|
'[' => Symbol.OpenBracket,
|
||||||
|
']' => Symbol.CloseBracket,
|
||||||
|
',' => Symbol.Comma,
|
||||||
|
'.' => Symbol.Period,
|
||||||
|
'=' => Symbol.Assign,
|
||||||
|
'<' => Symbol.LessThan,
|
||||||
|
'>' => Symbol.GreaterThan,
|
||||||
|
'+' => Symbol.Plus,
|
||||||
|
'-' => Symbol.Minus,
|
||||||
|
'*' => Symbol.Star,
|
||||||
|
'/' => Symbol.ForwardSlash,
|
||||||
|
'!' => Symbol.Bang,
|
||||||
|
'^' => Symbol.Caret,
|
||||||
|
'&' => Symbol.Ampersand,
|
||||||
|
';' => Symbol.Semi,
|
||||||
|
'%' => Symbol.Percent,
|
||||||
|
'|' => Symbol.Pipe,
|
||||||
|
'@' => Symbol.At,
|
||||||
|
'?' => Symbol.QuestionMark,
|
||||||
|
'~' => Symbol.Tilde,
|
||||||
|
_ => Symbol.None
|
||||||
|
},
|
||||||
|
_ => Symbol.None
|
||||||
|
};
|
||||||
|
|
||||||
|
if (symbol != Symbol.None)
|
||||||
|
{
|
||||||
|
var isAlphaKeyword = char.IsLetter(span[0]);
|
||||||
|
if (isAlphaKeyword)
|
||||||
|
{
|
||||||
|
var nextIdx = _index + length;
|
||||||
|
if (nextIdx < _content.Length)
|
||||||
|
{
|
||||||
|
var nextChar = _content[nextIdx];
|
||||||
|
if (char.IsLetterOrDigit(nextChar) || nextChar == '_')
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Next(length);
|
||||||
|
token = new SymbolToken(CreateSpan(indexStart, lineStart, columnStart), symbol);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IdentifierToken ParseIdentifier(int indexStart, int lineStart, int columnStart)
|
||||||
|
{
|
||||||
|
var start = _index;
|
||||||
|
|
||||||
|
while (_index < _content.Length)
|
||||||
|
{
|
||||||
|
var ch = _content[_index];
|
||||||
|
if (char.IsLetterOrDigit(ch) || ch == '_')
|
||||||
|
{
|
||||||
|
Next();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new IdentifierToken(CreateSpan(indexStart, lineStart, columnStart), _content.Substring(start, _index - start));
|
||||||
|
}
|
||||||
|
|
||||||
|
private SourceSpan CreateSpan(int indexStart, int lineStart, int columnStart)
|
||||||
|
{
|
||||||
|
return new SourceSpan(_fileName, _content, indexStart, Math.Min(_index, _content.Length), lineStart, columnStart, _line, _column);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Next(int count = 1)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
if (_index < _content.Length)
|
||||||
|
{
|
||||||
|
if (_content[_index] == '\n')
|
||||||
{
|
{
|
||||||
_line += 1;
|
_line += 1;
|
||||||
break;
|
_column = 1;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
if (next is '"')
|
|
||||||
{
|
{
|
||||||
Next();
|
_column++;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer += next;
|
|
||||||
Next();
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
return new StringLiteralToken(CreateSpan(lineStart, columnStart), buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var (pattern, symbol) in OrderedSymbols)
|
|
||||||
{
|
{
|
||||||
for (var i = 0; i < pattern.Length; i++)
|
_column++;
|
||||||
{
|
|
||||||
var c = Peek(i);
|
|
||||||
if (!c.HasValue || c.Value != pattern[i]) break;
|
|
||||||
|
|
||||||
if (i == pattern.Length - 1)
|
|
||||||
{
|
|
||||||
for (var j = 0; j <= i; j++)
|
|
||||||
{
|
|
||||||
Next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SymbolToken(CreateSpan(lineStart, columnStart), symbol);
|
_index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new TokenizerException(Diagnostic.Error($"Unknown token '{current}'").Build());
|
|
||||||
}
|
|
||||||
|
|
||||||
private SourceSpan CreateSpan(int lineStart, int columnStart)
|
|
||||||
{
|
|
||||||
return new SourceSpan(_fileName, new SourceLocation(lineStart, columnStart), new SourceLocation(_line, _column));
|
|
||||||
}
|
|
||||||
|
|
||||||
private char? Peek(int offset = 0)
|
|
||||||
{
|
|
||||||
if (_index + offset < _content.Length)
|
|
||||||
{
|
|
||||||
return _content[_index + offset];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Next()
|
|
||||||
{
|
|
||||||
_index += 1;
|
|
||||||
_column += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class TokenizerException : Exception
|
|
||||||
{
|
|
||||||
public Diagnostic Diagnostic { get; }
|
|
||||||
|
|
||||||
public TokenizerException(Diagnostic diagnostic) : base(diagnostic.Message)
|
|
||||||
{
|
|
||||||
Diagnostic = diagnostic;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,14 @@
|
|||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace NubLang.Ast;
|
namespace NubLang.Types;
|
||||||
|
|
||||||
public abstract class NubType : IEquatable<NubType>
|
public abstract class NubType : IEquatable<NubType>
|
||||||
{
|
{
|
||||||
|
public abstract ulong GetSize();
|
||||||
|
public abstract ulong GetAlignment();
|
||||||
|
public abstract bool IsAggregate();
|
||||||
|
|
||||||
public override bool Equals(object? obj) => obj is NubType other && Equals(other);
|
public override bool Equals(object? obj) => obj is NubType other && Equals(other);
|
||||||
public abstract bool Equals(NubType? other);
|
public abstract bool Equals(NubType? other);
|
||||||
|
|
||||||
@@ -17,24 +21,36 @@ public abstract class NubType : IEquatable<NubType>
|
|||||||
|
|
||||||
public class NubVoidType : NubType
|
public class NubVoidType : NubType
|
||||||
{
|
{
|
||||||
|
public override ulong GetSize() => 8;
|
||||||
|
public override ulong GetAlignment() => 8;
|
||||||
|
public override bool IsAggregate() => false;
|
||||||
|
|
||||||
public override string ToString() => "void";
|
public override string ToString() => "void";
|
||||||
public override bool Equals(NubType? other) => other is NubVoidType;
|
public override bool Equals(NubType? other) => other is NubVoidType;
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubVoidType));
|
public override int GetHashCode() => HashCode.Combine(typeof(NubVoidType));
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubIntType(bool signed, int width) : NubType
|
public sealed class NubIntType(bool signed, ulong width) : NubType
|
||||||
{
|
{
|
||||||
public bool Signed { get; } = signed;
|
public bool Signed { get; } = signed;
|
||||||
public int Width { get; } = width;
|
public ulong Width { get; } = width;
|
||||||
|
|
||||||
|
public override ulong GetSize() => Width / 8;
|
||||||
|
public override ulong GetAlignment() => Width / 8;
|
||||||
|
public override bool IsAggregate() => false;
|
||||||
|
|
||||||
public override string ToString() => $"{(Signed ? "i" : "u")}{Width}";
|
public override string ToString() => $"{(Signed ? "i" : "u")}{Width}";
|
||||||
public override bool Equals(NubType? other) => other is NubIntType @int && @int.Width == Width && @int.Signed == Signed;
|
public override bool Equals(NubType? other) => other is NubIntType @int && @int.Width == Width && @int.Signed == Signed;
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubIntType), Signed, Width);
|
public override int GetHashCode() => HashCode.Combine(typeof(NubIntType), Signed, Width);
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubFloatType(int width) : NubType
|
public sealed class NubFloatType(ulong width) : NubType
|
||||||
{
|
{
|
||||||
public int Width { get; } = width;
|
public ulong Width { get; } = width;
|
||||||
|
|
||||||
|
public override ulong GetSize() => Width / 8;
|
||||||
|
public override ulong GetAlignment() => Width / 8;
|
||||||
|
public override bool IsAggregate() => false;
|
||||||
|
|
||||||
public override string ToString() => $"f{Width}";
|
public override string ToString() => $"f{Width}";
|
||||||
public override bool Equals(NubType? other) => other is NubFloatType @float && @float.Width == Width;
|
public override bool Equals(NubType? other) => other is NubFloatType @float && @float.Width == Width;
|
||||||
@@ -43,6 +59,10 @@ public sealed class NubFloatType(int width) : NubType
|
|||||||
|
|
||||||
public class NubBoolType : NubType
|
public class NubBoolType : NubType
|
||||||
{
|
{
|
||||||
|
public override ulong GetSize() => 1;
|
||||||
|
public override ulong GetAlignment() => 1;
|
||||||
|
public override bool IsAggregate() => false;
|
||||||
|
|
||||||
public override string ToString() => "bool";
|
public override string ToString() => "bool";
|
||||||
public override bool Equals(NubType? other) => other is NubBoolType;
|
public override bool Equals(NubType? other) => other is NubBoolType;
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubBoolType));
|
public override int GetHashCode() => HashCode.Combine(typeof(NubBoolType));
|
||||||
@@ -52,6 +72,10 @@ public sealed class NubPointerType(NubType baseType) : NubType
|
|||||||
{
|
{
|
||||||
public NubType BaseType { get; } = baseType;
|
public NubType BaseType { get; } = baseType;
|
||||||
|
|
||||||
|
public override ulong GetSize() => 8;
|
||||||
|
public override ulong GetAlignment() => 8;
|
||||||
|
public override bool IsAggregate() => false;
|
||||||
|
|
||||||
public override string ToString() => "^" + BaseType;
|
public override string ToString() => "^" + BaseType;
|
||||||
public override bool Equals(NubType? other) => other is NubPointerType pointer && BaseType.Equals(pointer.BaseType);
|
public override bool Equals(NubType? other) => other is NubPointerType pointer && BaseType.Equals(pointer.BaseType);
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubPointerType), BaseType);
|
public override int GetHashCode() => HashCode.Combine(typeof(NubPointerType), BaseType);
|
||||||
@@ -62,6 +86,10 @@ public class NubFuncType(List<NubType> parameters, NubType returnType) : NubType
|
|||||||
public List<NubType> Parameters { get; } = parameters;
|
public List<NubType> Parameters { get; } = parameters;
|
||||||
public NubType ReturnType { get; } = returnType;
|
public NubType ReturnType { get; } = returnType;
|
||||||
|
|
||||||
|
public override ulong GetSize() => 8;
|
||||||
|
public override ulong GetAlignment() => 8;
|
||||||
|
public override bool IsAggregate() => false;
|
||||||
|
|
||||||
public override string ToString() => $"func({string.Join(", ", Parameters)}): {ReturnType}";
|
public override string ToString() => $"func({string.Join(", ", Parameters)}): {ReturnType}";
|
||||||
public override bool Equals(NubType? other) => other is NubFuncType func && ReturnType.Equals(func.ReturnType) && Parameters.SequenceEqual(func.Parameters);
|
public override bool Equals(NubType? other) => other is NubFuncType func && ReturnType.Equals(func.ReturnType) && Parameters.SequenceEqual(func.Parameters);
|
||||||
|
|
||||||
@@ -79,12 +107,70 @@ public class NubFuncType(List<NubType> parameters, NubType returnType) : NubType
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubStructType(string module, string name, List<NubStructFieldType> fields) : NubType
|
public class NubStructType(string module, string name, bool packed, List<NubStructFieldType> fields) : NubType
|
||||||
{
|
{
|
||||||
public string Module { get; } = module;
|
public string Module { get; } = module;
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
|
public bool Packed { get; } = packed;
|
||||||
public List<NubStructFieldType> Fields { get; set; } = fields;
|
public List<NubStructFieldType> Fields { get; set; } = fields;
|
||||||
|
|
||||||
|
public int GetFieldIndex(string name)
|
||||||
|
{
|
||||||
|
return Fields.FindIndex(x => x.Name == name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dictionary<string, ulong> GetFieldOffsets()
|
||||||
|
{
|
||||||
|
var offsets = new Dictionary<string, ulong>();
|
||||||
|
ulong offset = 0;
|
||||||
|
|
||||||
|
foreach (var field in Fields)
|
||||||
|
{
|
||||||
|
var alignment = Packed ? 1 : field.Type.GetAlignment();
|
||||||
|
if (!Packed)
|
||||||
|
{
|
||||||
|
var padding = (alignment - offset % alignment) % alignment;
|
||||||
|
offset += padding;
|
||||||
|
}
|
||||||
|
|
||||||
|
offsets[field.Name] = offset;
|
||||||
|
offset += field.Type.GetSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
return offsets;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ulong GetSize()
|
||||||
|
{
|
||||||
|
var offsets = GetFieldOffsets();
|
||||||
|
if (Fields.Count == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var lastField = Fields.Last();
|
||||||
|
var size = offsets[lastField.Name] + lastField.Type.GetSize();
|
||||||
|
|
||||||
|
if (!Packed)
|
||||||
|
{
|
||||||
|
var structAlignment = GetAlignment();
|
||||||
|
var padding = (structAlignment - size % structAlignment) % structAlignment;
|
||||||
|
size += padding;
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ulong GetAlignment()
|
||||||
|
{
|
||||||
|
if (Fields.Count == 0)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return Packed ? 1 : Fields.Max(f => f.Type.GetAlignment());
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool IsAggregate() => true;
|
||||||
|
|
||||||
public override string ToString() => $"{Module}::{Name}";
|
public override string ToString() => $"{Module}::{Name}";
|
||||||
public override bool Equals(NubType? other) => other is NubStructType structType && Name == structType.Name && Module == structType.Module;
|
public override bool Equals(NubType? other) => other is NubStructType structType && Name == structType.Name && Module == structType.Module;
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubStructType), Module, Name);
|
public override int GetHashCode() => HashCode.Combine(typeof(NubStructType), Module, Name);
|
||||||
@@ -97,19 +183,43 @@ public class NubStructFieldType(string name, NubType type, bool hasDefaultValue)
|
|||||||
public bool HasDefaultValue { get; } = hasDefaultValue;
|
public bool HasDefaultValue { get; } = hasDefaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class NubEnumType(string module, string name, NubIntType underlyingType, Dictionary<string, ulong> members) : NubType
|
||||||
|
{
|
||||||
|
public string Module { get; } = module;
|
||||||
|
public string Name { get; } = name;
|
||||||
|
public NubIntType UnderlyingType { get; } = underlyingType;
|
||||||
|
public Dictionary<string, ulong> Members { get; } = members;
|
||||||
|
|
||||||
|
public override ulong GetSize() => UnderlyingType.GetSize();
|
||||||
|
public override ulong GetAlignment() => UnderlyingType.GetSize();
|
||||||
|
public override bool IsAggregate() => false;
|
||||||
|
|
||||||
|
public override bool Equals(NubType? other) => other is NubEnumType enumType && Name == enumType.Name && Module == enumType.Module;
|
||||||
|
public override int GetHashCode() => HashCode.Combine(typeof(NubEnumType), Module, Name);
|
||||||
|
public override string ToString() => $"{Module}::{Name}";
|
||||||
|
}
|
||||||
|
|
||||||
public class NubSliceType(NubType elementType) : NubType
|
public class NubSliceType(NubType elementType) : NubType
|
||||||
{
|
{
|
||||||
public NubType ElementType { get; } = elementType;
|
public NubType ElementType { get; } = elementType;
|
||||||
|
|
||||||
|
public override ulong GetSize() => 16; // note(nub31): Fat pointer
|
||||||
|
public override ulong GetAlignment() => 8;
|
||||||
|
public override bool IsAggregate() => true;
|
||||||
|
|
||||||
public override string ToString() => "[]" + ElementType;
|
public override string ToString() => "[]" + ElementType;
|
||||||
public override bool Equals(NubType? other) => other is NubSliceType slice && ElementType.Equals(slice.ElementType);
|
public override bool Equals(NubType? other) => other is NubSliceType slice && ElementType.Equals(slice.ElementType);
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubSliceType), ElementType);
|
public override int GetHashCode() => HashCode.Combine(typeof(NubSliceType), ElementType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubConstArrayType(NubType elementType, long size) : NubType
|
public class NubConstArrayType(NubType elementType, ulong size) : NubType
|
||||||
{
|
{
|
||||||
public NubType ElementType { get; } = elementType;
|
public NubType ElementType { get; } = elementType;
|
||||||
public long Size { get; } = size;
|
public ulong Size { get; } = size;
|
||||||
|
|
||||||
|
public override ulong GetSize() => ElementType.GetSize() * Size;
|
||||||
|
public override ulong GetAlignment() => ElementType.GetAlignment();
|
||||||
|
public override bool IsAggregate() => true;
|
||||||
|
|
||||||
public override string ToString() => $"[{Size}]{ElementType}";
|
public override string ToString() => $"[{Size}]{ElementType}";
|
||||||
public override bool Equals(NubType? other) => other is NubConstArrayType array && ElementType.Equals(array.ElementType) && Size == array.Size;
|
public override bool Equals(NubType? other) => other is NubConstArrayType array && ElementType.Equals(array.ElementType) && Size == array.Size;
|
||||||
@@ -120,20 +230,21 @@ public class NubArrayType(NubType elementType) : NubType
|
|||||||
{
|
{
|
||||||
public NubType ElementType { get; } = elementType;
|
public NubType ElementType { get; } = elementType;
|
||||||
|
|
||||||
|
public override ulong GetSize() => 8;
|
||||||
|
public override ulong GetAlignment() => 8;
|
||||||
|
public override bool IsAggregate() => false; // note(nub31): Just a pointer
|
||||||
|
|
||||||
public override string ToString() => $"[?]{ElementType}";
|
public override string ToString() => $"[?]{ElementType}";
|
||||||
public override bool Equals(NubType? other) => other is NubArrayType array && ElementType.Equals(array.ElementType);
|
public override bool Equals(NubType? other) => other is NubArrayType array && ElementType.Equals(array.ElementType);
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubArrayType), ElementType);
|
public override int GetHashCode() => HashCode.Combine(typeof(NubArrayType), ElementType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubCStringType : NubType
|
|
||||||
{
|
|
||||||
public override string ToString() => "cstring";
|
|
||||||
public override bool Equals(NubType? other) => other is NubCStringType;
|
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubCStringType));
|
|
||||||
}
|
|
||||||
|
|
||||||
public class NubStringType : NubType
|
public class NubStringType : NubType
|
||||||
{
|
{
|
||||||
|
public override ulong GetSize() => 16; // note(nub31): Fat pointer
|
||||||
|
public override ulong GetAlignment() => 8;
|
||||||
|
public override bool IsAggregate() => true;
|
||||||
|
|
||||||
public override string ToString() => "string";
|
public override string ToString() => "string";
|
||||||
public override bool Equals(NubType? other) => other is NubStringType;
|
public override bool Equals(NubType? other) => other is NubStringType;
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubStringType));
|
public override int GetHashCode() => HashCode.Combine(typeof(NubStringType));
|
||||||
@@ -143,7 +254,7 @@ public static class NameMangler
|
|||||||
{
|
{
|
||||||
public static string Mangle(params IEnumerable<NubType> types)
|
public static string Mangle(params IEnumerable<NubType> types)
|
||||||
{
|
{
|
||||||
var readable = string.Join("_", types.Select(EncodeType));
|
var readable = string.Join(":", types.Select(EncodeType));
|
||||||
return ComputeShortHash(readable);
|
return ComputeShortHash(readable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,12 +264,13 @@ public static class NameMangler
|
|||||||
NubBoolType => "B",
|
NubBoolType => "B",
|
||||||
NubIntType i => (i.Signed ? "I" : "U") + i.Width,
|
NubIntType i => (i.Signed ? "I" : "U") + i.Width,
|
||||||
NubFloatType f => "F" + f.Width,
|
NubFloatType f => "F" + f.Width,
|
||||||
NubCStringType => "CS",
|
|
||||||
NubStringType => "S",
|
NubStringType => "S",
|
||||||
NubPointerType p => "P" + EncodeType(p.BaseType),
|
NubArrayType a => $"A({EncodeType(a.ElementType)})",
|
||||||
NubSliceType a => "A" + EncodeType(a.ElementType),
|
NubConstArrayType ca => $"CA({EncodeType(ca.ElementType)})",
|
||||||
NubFuncType fn => "FN(" + string.Join(",", fn.Parameters.Select(EncodeType)) + ")" + EncodeType(fn.ReturnType),
|
NubSliceType a => $"SL{EncodeType(a.ElementType)}()",
|
||||||
NubStructType st => "ST(" + st.Module + "." + st.Name + ")",
|
NubPointerType p => $"P({EncodeType(p.BaseType)})",
|
||||||
|
NubFuncType fn => $"FN({string.Join(":", fn.Parameters.Select(EncodeType))}:{EncodeType(fn.ReturnType)})",
|
||||||
|
NubStructType st => $"ST({st.Module}:{st.Name})",
|
||||||
_ => throw new NotSupportedException($"Cannot encode type: {node}")
|
_ => throw new NotSupportedException($"Cannot encode type: {node}")
|
||||||
};
|
};
|
||||||
|
|
||||||
31
compiler/NubLang/Utils.cs
Normal file
31
compiler/NubLang/Utils.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
namespace NubLang;
|
||||||
|
|
||||||
|
public static class Utils
|
||||||
|
{
|
||||||
|
public static int LevenshteinDistance(string a, string b)
|
||||||
|
{
|
||||||
|
if (a == b) return 0;
|
||||||
|
if (a.Length == 0) return b.Length;
|
||||||
|
if (b.Length == 0) return a.Length;
|
||||||
|
|
||||||
|
var costs = new int[b.Length + 1];
|
||||||
|
for (int j = 0; j <= b.Length; j++) costs[j] = j;
|
||||||
|
|
||||||
|
for (int i = 1; i <= a.Length; i++)
|
||||||
|
{
|
||||||
|
costs[0] = i;
|
||||||
|
int prevCost = i - 1;
|
||||||
|
for (int j = 1; j <= b.Length; j++)
|
||||||
|
{
|
||||||
|
int currentCost = costs[j];
|
||||||
|
costs[j] = Math.Min(
|
||||||
|
Math.Min(costs[j - 1] + 1, costs[j] + 1),
|
||||||
|
prevCost + (a[i - 1] == b[j - 1] ? 0 : 1)
|
||||||
|
);
|
||||||
|
prevCost = currentCost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return costs[b.Length];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +1,9 @@
|
|||||||
module "main"
|
module main
|
||||||
|
|
||||||
extern "puts" func puts(text: cstring)
|
extern "puts" func puts(text: ^i8)
|
||||||
|
|
||||||
struct X
|
extern "main" func main(argc: i64, argv: [?]^i8): i64
|
||||||
{
|
{
|
||||||
points: []i32
|
puts("Hello, World!")
|
||||||
}
|
|
||||||
|
|
||||||
extern "main" func main(argc: i64, argv: [?]cstring): i64
|
|
||||||
{
|
|
||||||
let f: []cstring = ["1", "2", "3"]
|
|
||||||
|
|
||||||
puts(f[0])
|
|
||||||
puts(f[1])
|
|
||||||
puts(f[2])
|
|
||||||
|
|
||||||
for num in f
|
|
||||||
{
|
|
||||||
puts(num)
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
6
examples/playgroud/build.sh
Executable file
6
examples/playgroud/build.sh
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
nubc main.nub
|
||||||
|
clang .build/main.ll -o .build/out
|
||||||
13
examples/playgroud/main.nub
Normal file
13
examples/playgroud/main.nub
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
extern "puts" func puts(text: ^i8)
|
||||||
|
|
||||||
|
struct Test {
|
||||||
|
test: ^i8 = "test1"
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "main" func main(argc: i64, argv: [?]^i8)
|
||||||
|
{
|
||||||
|
let x: Test = {}
|
||||||
|
puts(x.test)
|
||||||
|
}
|
||||||
@@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
obj=$(nubc main.nub generated/raylib.nub)
|
nubc main.nub generated/raylib.nub
|
||||||
clang $obj raylib-5.5_linux_amd64/lib/libraylib.a -lm -o .build/out
|
clang .build/main.ll .build/generated/raylib.ll raylib-5.5_linux_amd64/lib/libraylib.a -lm -o .build/out
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
module "raylib"
|
module raylib
|
||||||
|
|
||||||
export struct Vector2
|
export struct Vector2
|
||||||
{
|
{
|
||||||
@@ -255,7 +255,7 @@ export struct FilePathList
|
|||||||
{
|
{
|
||||||
capacity: u32
|
capacity: u32
|
||||||
count: u32
|
count: u32
|
||||||
paths: ^cstring
|
paths: ^^i8
|
||||||
}
|
}
|
||||||
export struct AutomationEvent
|
export struct AutomationEvent
|
||||||
{
|
{
|
||||||
@@ -288,36 +288,6 @@ export enum ConfigFlags : u32
|
|||||||
FLAG_MSAA_4X_HINT = 32
|
FLAG_MSAA_4X_HINT = 32
|
||||||
FLAG_INTERLACED_HINT = 65536
|
FLAG_INTERLACED_HINT = 65536
|
||||||
}
|
}
|
||||||
export enum ConfigFlags : u32
|
|
||||||
{
|
|
||||||
FLAG_VSYNC_HINT = 64
|
|
||||||
FLAG_FULLSCREEN_MODE = 2
|
|
||||||
FLAG_WINDOW_RESIZABLE = 4
|
|
||||||
FLAG_WINDOW_UNDECORATED = 8
|
|
||||||
FLAG_WINDOW_HIDDEN = 128
|
|
||||||
FLAG_WINDOW_MINIMIZED = 512
|
|
||||||
FLAG_WINDOW_MAXIMIZED = 1024
|
|
||||||
FLAG_WINDOW_UNFOCUSED = 2048
|
|
||||||
FLAG_WINDOW_TOPMOST = 4096
|
|
||||||
FLAG_WINDOW_ALWAYS_RUN = 256
|
|
||||||
FLAG_WINDOW_TRANSPARENT = 16
|
|
||||||
FLAG_WINDOW_HIGHDPI = 8192
|
|
||||||
FLAG_WINDOW_MOUSE_PASSTHROUGH = 16384
|
|
||||||
FLAG_BORDERLESS_WINDOWED_MODE = 32768
|
|
||||||
FLAG_MSAA_4X_HINT = 32
|
|
||||||
FLAG_INTERLACED_HINT = 65536
|
|
||||||
}
|
|
||||||
export enum TraceLogLevel : u32
|
|
||||||
{
|
|
||||||
LOG_ALL = 0
|
|
||||||
LOG_TRACE = 1
|
|
||||||
LOG_DEBUG = 2
|
|
||||||
LOG_INFO = 3
|
|
||||||
LOG_WARNING = 4
|
|
||||||
LOG_ERROR = 5
|
|
||||||
LOG_FATAL = 6
|
|
||||||
LOG_NONE = 7
|
|
||||||
}
|
|
||||||
export enum TraceLogLevel : u32
|
export enum TraceLogLevel : u32
|
||||||
{
|
{
|
||||||
LOG_ALL = 0
|
LOG_ALL = 0
|
||||||
@@ -442,119 +412,6 @@ export enum KeyboardKey : u32
|
|||||||
KEY_VOLUME_UP = 24
|
KEY_VOLUME_UP = 24
|
||||||
KEY_VOLUME_DOWN = 25
|
KEY_VOLUME_DOWN = 25
|
||||||
}
|
}
|
||||||
export enum KeyboardKey : u32
|
|
||||||
{
|
|
||||||
KEY_NULL = 0
|
|
||||||
KEY_APOSTROPHE = 39
|
|
||||||
KEY_COMMA = 44
|
|
||||||
KEY_MINUS = 45
|
|
||||||
KEY_PERIOD = 46
|
|
||||||
KEY_SLASH = 47
|
|
||||||
KEY_ZERO = 48
|
|
||||||
KEY_ONE = 49
|
|
||||||
KEY_TWO = 50
|
|
||||||
KEY_THREE = 51
|
|
||||||
KEY_FOUR = 52
|
|
||||||
KEY_FIVE = 53
|
|
||||||
KEY_SIX = 54
|
|
||||||
KEY_SEVEN = 55
|
|
||||||
KEY_EIGHT = 56
|
|
||||||
KEY_NINE = 57
|
|
||||||
KEY_SEMICOLON = 59
|
|
||||||
KEY_EQUAL = 61
|
|
||||||
KEY_A = 65
|
|
||||||
KEY_B = 66
|
|
||||||
KEY_C = 67
|
|
||||||
KEY_D = 68
|
|
||||||
KEY_E = 69
|
|
||||||
KEY_F = 70
|
|
||||||
KEY_G = 71
|
|
||||||
KEY_H = 72
|
|
||||||
KEY_I = 73
|
|
||||||
KEY_J = 74
|
|
||||||
KEY_K = 75
|
|
||||||
KEY_L = 76
|
|
||||||
KEY_M = 77
|
|
||||||
KEY_N = 78
|
|
||||||
KEY_O = 79
|
|
||||||
KEY_P = 80
|
|
||||||
KEY_Q = 81
|
|
||||||
KEY_R = 82
|
|
||||||
KEY_S = 83
|
|
||||||
KEY_T = 84
|
|
||||||
KEY_U = 85
|
|
||||||
KEY_V = 86
|
|
||||||
KEY_W = 87
|
|
||||||
KEY_X = 88
|
|
||||||
KEY_Y = 89
|
|
||||||
KEY_Z = 90
|
|
||||||
KEY_LEFT_BRACKET = 91
|
|
||||||
KEY_BACKSLASH = 92
|
|
||||||
KEY_RIGHT_BRACKET = 93
|
|
||||||
KEY_GRAVE = 96
|
|
||||||
KEY_SPACE = 32
|
|
||||||
KEY_ESCAPE = 256
|
|
||||||
KEY_ENTER = 257
|
|
||||||
KEY_TAB = 258
|
|
||||||
KEY_BACKSPACE = 259
|
|
||||||
KEY_INSERT = 260
|
|
||||||
KEY_DELETE = 261
|
|
||||||
KEY_RIGHT = 262
|
|
||||||
KEY_LEFT = 263
|
|
||||||
KEY_DOWN = 264
|
|
||||||
KEY_UP = 265
|
|
||||||
KEY_PAGE_UP = 266
|
|
||||||
KEY_PAGE_DOWN = 267
|
|
||||||
KEY_HOME = 268
|
|
||||||
KEY_END = 269
|
|
||||||
KEY_CAPS_LOCK = 280
|
|
||||||
KEY_SCROLL_LOCK = 281
|
|
||||||
KEY_NUM_LOCK = 282
|
|
||||||
KEY_PRINT_SCREEN = 283
|
|
||||||
KEY_PAUSE = 284
|
|
||||||
KEY_F1 = 290
|
|
||||||
KEY_F2 = 291
|
|
||||||
KEY_F3 = 292
|
|
||||||
KEY_F4 = 293
|
|
||||||
KEY_F5 = 294
|
|
||||||
KEY_F6 = 295
|
|
||||||
KEY_F7 = 296
|
|
||||||
KEY_F8 = 297
|
|
||||||
KEY_F9 = 298
|
|
||||||
KEY_F10 = 299
|
|
||||||
KEY_F11 = 300
|
|
||||||
KEY_F12 = 301
|
|
||||||
KEY_LEFT_SHIFT = 340
|
|
||||||
KEY_LEFT_CONTROL = 341
|
|
||||||
KEY_LEFT_ALT = 342
|
|
||||||
KEY_LEFT_SUPER = 343
|
|
||||||
KEY_RIGHT_SHIFT = 344
|
|
||||||
KEY_RIGHT_CONTROL = 345
|
|
||||||
KEY_RIGHT_ALT = 346
|
|
||||||
KEY_RIGHT_SUPER = 347
|
|
||||||
KEY_KB_MENU = 348
|
|
||||||
KEY_KP_0 = 320
|
|
||||||
KEY_KP_1 = 321
|
|
||||||
KEY_KP_2 = 322
|
|
||||||
KEY_KP_3 = 323
|
|
||||||
KEY_KP_4 = 324
|
|
||||||
KEY_KP_5 = 325
|
|
||||||
KEY_KP_6 = 326
|
|
||||||
KEY_KP_7 = 327
|
|
||||||
KEY_KP_8 = 328
|
|
||||||
KEY_KP_9 = 329
|
|
||||||
KEY_KP_DECIMAL = 330
|
|
||||||
KEY_KP_DIVIDE = 331
|
|
||||||
KEY_KP_MULTIPLY = 332
|
|
||||||
KEY_KP_SUBTRACT = 333
|
|
||||||
KEY_KP_ADD = 334
|
|
||||||
KEY_KP_ENTER = 335
|
|
||||||
KEY_KP_EQUAL = 336
|
|
||||||
KEY_BACK = 4
|
|
||||||
KEY_MENU = 5
|
|
||||||
KEY_VOLUME_UP = 24
|
|
||||||
KEY_VOLUME_DOWN = 25
|
|
||||||
}
|
|
||||||
export enum MouseButton : u32
|
export enum MouseButton : u32
|
||||||
{
|
{
|
||||||
MOUSE_BUTTON_LEFT = 0
|
MOUSE_BUTTON_LEFT = 0
|
||||||
@@ -565,30 +422,6 @@ export enum MouseButton : u32
|
|||||||
MOUSE_BUTTON_FORWARD = 5
|
MOUSE_BUTTON_FORWARD = 5
|
||||||
MOUSE_BUTTON_BACK = 6
|
MOUSE_BUTTON_BACK = 6
|
||||||
}
|
}
|
||||||
export enum MouseButton : u32
|
|
||||||
{
|
|
||||||
MOUSE_BUTTON_LEFT = 0
|
|
||||||
MOUSE_BUTTON_RIGHT = 1
|
|
||||||
MOUSE_BUTTON_MIDDLE = 2
|
|
||||||
MOUSE_BUTTON_SIDE = 3
|
|
||||||
MOUSE_BUTTON_EXTRA = 4
|
|
||||||
MOUSE_BUTTON_FORWARD = 5
|
|
||||||
MOUSE_BUTTON_BACK = 6
|
|
||||||
}
|
|
||||||
export enum MouseCursor : u32
|
|
||||||
{
|
|
||||||
MOUSE_CURSOR_DEFAULT = 0
|
|
||||||
MOUSE_CURSOR_ARROW = 1
|
|
||||||
MOUSE_CURSOR_IBEAM = 2
|
|
||||||
MOUSE_CURSOR_CROSSHAIR = 3
|
|
||||||
MOUSE_CURSOR_POINTING_HAND = 4
|
|
||||||
MOUSE_CURSOR_RESIZE_EW = 5
|
|
||||||
MOUSE_CURSOR_RESIZE_NS = 6
|
|
||||||
MOUSE_CURSOR_RESIZE_NWSE = 7
|
|
||||||
MOUSE_CURSOR_RESIZE_NESW = 8
|
|
||||||
MOUSE_CURSOR_RESIZE_ALL = 9
|
|
||||||
MOUSE_CURSOR_NOT_ALLOWED = 10
|
|
||||||
}
|
|
||||||
export enum MouseCursor : u32
|
export enum MouseCursor : u32
|
||||||
{
|
{
|
||||||
MOUSE_CURSOR_DEFAULT = 0
|
MOUSE_CURSOR_DEFAULT = 0
|
||||||
@@ -624,27 +457,6 @@ export enum GamepadButton : u32
|
|||||||
GAMEPAD_BUTTON_LEFT_THUMB = 16
|
GAMEPAD_BUTTON_LEFT_THUMB = 16
|
||||||
GAMEPAD_BUTTON_RIGHT_THUMB = 17
|
GAMEPAD_BUTTON_RIGHT_THUMB = 17
|
||||||
}
|
}
|
||||||
export enum GamepadButton : u32
|
|
||||||
{
|
|
||||||
GAMEPAD_BUTTON_UNKNOWN = 0
|
|
||||||
GAMEPAD_BUTTON_LEFT_FACE_UP = 1
|
|
||||||
GAMEPAD_BUTTON_LEFT_FACE_RIGHT = 2
|
|
||||||
GAMEPAD_BUTTON_LEFT_FACE_DOWN = 3
|
|
||||||
GAMEPAD_BUTTON_LEFT_FACE_LEFT = 4
|
|
||||||
GAMEPAD_BUTTON_RIGHT_FACE_UP = 5
|
|
||||||
GAMEPAD_BUTTON_RIGHT_FACE_RIGHT = 6
|
|
||||||
GAMEPAD_BUTTON_RIGHT_FACE_DOWN = 7
|
|
||||||
GAMEPAD_BUTTON_RIGHT_FACE_LEFT = 8
|
|
||||||
GAMEPAD_BUTTON_LEFT_TRIGGER_1 = 9
|
|
||||||
GAMEPAD_BUTTON_LEFT_TRIGGER_2 = 10
|
|
||||||
GAMEPAD_BUTTON_RIGHT_TRIGGER_1 = 11
|
|
||||||
GAMEPAD_BUTTON_RIGHT_TRIGGER_2 = 12
|
|
||||||
GAMEPAD_BUTTON_MIDDLE_LEFT = 13
|
|
||||||
GAMEPAD_BUTTON_MIDDLE = 14
|
|
||||||
GAMEPAD_BUTTON_MIDDLE_RIGHT = 15
|
|
||||||
GAMEPAD_BUTTON_LEFT_THUMB = 16
|
|
||||||
GAMEPAD_BUTTON_RIGHT_THUMB = 17
|
|
||||||
}
|
|
||||||
export enum GamepadAxis : u32
|
export enum GamepadAxis : u32
|
||||||
{
|
{
|
||||||
GAMEPAD_AXIS_LEFT_X = 0
|
GAMEPAD_AXIS_LEFT_X = 0
|
||||||
@@ -654,29 +466,6 @@ export enum GamepadAxis : u32
|
|||||||
GAMEPAD_AXIS_LEFT_TRIGGER = 4
|
GAMEPAD_AXIS_LEFT_TRIGGER = 4
|
||||||
GAMEPAD_AXIS_RIGHT_TRIGGER = 5
|
GAMEPAD_AXIS_RIGHT_TRIGGER = 5
|
||||||
}
|
}
|
||||||
export enum GamepadAxis : u32
|
|
||||||
{
|
|
||||||
GAMEPAD_AXIS_LEFT_X = 0
|
|
||||||
GAMEPAD_AXIS_LEFT_Y = 1
|
|
||||||
GAMEPAD_AXIS_RIGHT_X = 2
|
|
||||||
GAMEPAD_AXIS_RIGHT_Y = 3
|
|
||||||
GAMEPAD_AXIS_LEFT_TRIGGER = 4
|
|
||||||
GAMEPAD_AXIS_RIGHT_TRIGGER = 5
|
|
||||||
}
|
|
||||||
export enum MaterialMapIndex : u32
|
|
||||||
{
|
|
||||||
MATERIAL_MAP_ALBEDO = 0
|
|
||||||
MATERIAL_MAP_METALNESS = 1
|
|
||||||
MATERIAL_MAP_NORMAL = 2
|
|
||||||
MATERIAL_MAP_ROUGHNESS = 3
|
|
||||||
MATERIAL_MAP_OCCLUSION = 4
|
|
||||||
MATERIAL_MAP_EMISSION = 5
|
|
||||||
MATERIAL_MAP_HEIGHT = 6
|
|
||||||
MATERIAL_MAP_CUBEMAP = 7
|
|
||||||
MATERIAL_MAP_IRRADIANCE = 8
|
|
||||||
MATERIAL_MAP_PREFILTER = 9
|
|
||||||
MATERIAL_MAP_BRDF = 10
|
|
||||||
}
|
|
||||||
export enum MaterialMapIndex : u32
|
export enum MaterialMapIndex : u32
|
||||||
{
|
{
|
||||||
MATERIAL_MAP_ALBEDO = 0
|
MATERIAL_MAP_ALBEDO = 0
|
||||||
@@ -723,50 +512,6 @@ export enum ShaderLocationIndex : u32
|
|||||||
SHADER_LOC_VERTEX_BONEWEIGHTS = 27
|
SHADER_LOC_VERTEX_BONEWEIGHTS = 27
|
||||||
SHADER_LOC_BONE_MATRICES = 28
|
SHADER_LOC_BONE_MATRICES = 28
|
||||||
}
|
}
|
||||||
export enum ShaderLocationIndex : u32
|
|
||||||
{
|
|
||||||
SHADER_LOC_VERTEX_POSITION = 0
|
|
||||||
SHADER_LOC_VERTEX_TEXCOORD01 = 1
|
|
||||||
SHADER_LOC_VERTEX_TEXCOORD02 = 2
|
|
||||||
SHADER_LOC_VERTEX_NORMAL = 3
|
|
||||||
SHADER_LOC_VERTEX_TANGENT = 4
|
|
||||||
SHADER_LOC_VERTEX_COLOR = 5
|
|
||||||
SHADER_LOC_MATRIX_MVP = 6
|
|
||||||
SHADER_LOC_MATRIX_VIEW = 7
|
|
||||||
SHADER_LOC_MATRIX_PROJECTION = 8
|
|
||||||
SHADER_LOC_MATRIX_MODEL = 9
|
|
||||||
SHADER_LOC_MATRIX_NORMAL = 10
|
|
||||||
SHADER_LOC_VECTOR_VIEW = 11
|
|
||||||
SHADER_LOC_COLOR_DIFFUSE = 12
|
|
||||||
SHADER_LOC_COLOR_SPECULAR = 13
|
|
||||||
SHADER_LOC_COLOR_AMBIENT = 14
|
|
||||||
SHADER_LOC_MAP_ALBEDO = 15
|
|
||||||
SHADER_LOC_MAP_METALNESS = 16
|
|
||||||
SHADER_LOC_MAP_NORMAL = 17
|
|
||||||
SHADER_LOC_MAP_ROUGHNESS = 18
|
|
||||||
SHADER_LOC_MAP_OCCLUSION = 19
|
|
||||||
SHADER_LOC_MAP_EMISSION = 20
|
|
||||||
SHADER_LOC_MAP_HEIGHT = 21
|
|
||||||
SHADER_LOC_MAP_CUBEMAP = 22
|
|
||||||
SHADER_LOC_MAP_IRRADIANCE = 23
|
|
||||||
SHADER_LOC_MAP_PREFILTER = 24
|
|
||||||
SHADER_LOC_MAP_BRDF = 25
|
|
||||||
SHADER_LOC_VERTEX_BONEIDS = 26
|
|
||||||
SHADER_LOC_VERTEX_BONEWEIGHTS = 27
|
|
||||||
SHADER_LOC_BONE_MATRICES = 28
|
|
||||||
}
|
|
||||||
export enum ShaderUniformDataType : u32
|
|
||||||
{
|
|
||||||
SHADER_UNIFORM_FLOAT = 0
|
|
||||||
SHADER_UNIFORM_VEC2 = 1
|
|
||||||
SHADER_UNIFORM_VEC3 = 2
|
|
||||||
SHADER_UNIFORM_VEC4 = 3
|
|
||||||
SHADER_UNIFORM_INT = 4
|
|
||||||
SHADER_UNIFORM_IVEC2 = 5
|
|
||||||
SHADER_UNIFORM_IVEC3 = 6
|
|
||||||
SHADER_UNIFORM_IVEC4 = 7
|
|
||||||
SHADER_UNIFORM_SAMPLER2D = 8
|
|
||||||
}
|
|
||||||
export enum ShaderUniformDataType : u32
|
export enum ShaderUniformDataType : u32
|
||||||
{
|
{
|
||||||
SHADER_UNIFORM_FLOAT = 0
|
SHADER_UNIFORM_FLOAT = 0
|
||||||
@@ -786,40 +531,6 @@ export enum ShaderAttributeDataType : u32
|
|||||||
SHADER_ATTRIB_VEC3 = 2
|
SHADER_ATTRIB_VEC3 = 2
|
||||||
SHADER_ATTRIB_VEC4 = 3
|
SHADER_ATTRIB_VEC4 = 3
|
||||||
}
|
}
|
||||||
export enum ShaderAttributeDataType : u32
|
|
||||||
{
|
|
||||||
SHADER_ATTRIB_FLOAT = 0
|
|
||||||
SHADER_ATTRIB_VEC2 = 1
|
|
||||||
SHADER_ATTRIB_VEC3 = 2
|
|
||||||
SHADER_ATTRIB_VEC4 = 3
|
|
||||||
}
|
|
||||||
export enum PixelFormat : u32
|
|
||||||
{
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_GRAYSCALE = 1
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA = 2
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R5G6B5 = 3
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R8G8B8 = 4
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R5G5B5A1 = 5
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R4G4B4A4 = 6
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 = 7
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R32 = 8
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R32G32B32 = 9
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R32G32B32A32 = 10
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R16 = 11
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R16G16B16 = 12
|
|
||||||
PIXELFORMAT_UNCOMPRESSED_R16G16B16A16 = 13
|
|
||||||
PIXELFORMAT_COMPRESSED_DXT1_RGB = 14
|
|
||||||
PIXELFORMAT_COMPRESSED_DXT1_RGBA = 15
|
|
||||||
PIXELFORMAT_COMPRESSED_DXT3_RGBA = 16
|
|
||||||
PIXELFORMAT_COMPRESSED_DXT5_RGBA = 17
|
|
||||||
PIXELFORMAT_COMPRESSED_ETC1_RGB = 18
|
|
||||||
PIXELFORMAT_COMPRESSED_ETC2_RGB = 19
|
|
||||||
PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA = 20
|
|
||||||
PIXELFORMAT_COMPRESSED_PVRT_RGB = 21
|
|
||||||
PIXELFORMAT_COMPRESSED_PVRT_RGBA = 22
|
|
||||||
PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA = 23
|
|
||||||
PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA = 24
|
|
||||||
}
|
|
||||||
export enum PixelFormat : u32
|
export enum PixelFormat : u32
|
||||||
{
|
{
|
||||||
PIXELFORMAT_UNCOMPRESSED_GRAYSCALE = 1
|
PIXELFORMAT_UNCOMPRESSED_GRAYSCALE = 1
|
||||||
@@ -856,22 +567,6 @@ export enum TextureFilter : u32
|
|||||||
TEXTURE_FILTER_ANISOTROPIC_8X = 4
|
TEXTURE_FILTER_ANISOTROPIC_8X = 4
|
||||||
TEXTURE_FILTER_ANISOTROPIC_16X = 5
|
TEXTURE_FILTER_ANISOTROPIC_16X = 5
|
||||||
}
|
}
|
||||||
export enum TextureFilter : u32
|
|
||||||
{
|
|
||||||
TEXTURE_FILTER_POINT = 0
|
|
||||||
TEXTURE_FILTER_BILINEAR = 1
|
|
||||||
TEXTURE_FILTER_TRILINEAR = 2
|
|
||||||
TEXTURE_FILTER_ANISOTROPIC_4X = 3
|
|
||||||
TEXTURE_FILTER_ANISOTROPIC_8X = 4
|
|
||||||
TEXTURE_FILTER_ANISOTROPIC_16X = 5
|
|
||||||
}
|
|
||||||
export enum TextureWrap : u32
|
|
||||||
{
|
|
||||||
TEXTURE_WRAP_REPEAT = 0
|
|
||||||
TEXTURE_WRAP_CLAMP = 1
|
|
||||||
TEXTURE_WRAP_MIRROR_REPEAT = 2
|
|
||||||
TEXTURE_WRAP_MIRROR_CLAMP = 3
|
|
||||||
}
|
|
||||||
export enum TextureWrap : u32
|
export enum TextureWrap : u32
|
||||||
{
|
{
|
||||||
TEXTURE_WRAP_REPEAT = 0
|
TEXTURE_WRAP_REPEAT = 0
|
||||||
@@ -887,37 +582,12 @@ export enum CubemapLayout : u32
|
|||||||
CUBEMAP_LAYOUT_CROSS_THREE_BY_FOUR = 3
|
CUBEMAP_LAYOUT_CROSS_THREE_BY_FOUR = 3
|
||||||
CUBEMAP_LAYOUT_CROSS_FOUR_BY_THREE = 4
|
CUBEMAP_LAYOUT_CROSS_FOUR_BY_THREE = 4
|
||||||
}
|
}
|
||||||
export enum CubemapLayout : u32
|
|
||||||
{
|
|
||||||
CUBEMAP_LAYOUT_AUTO_DETECT = 0
|
|
||||||
CUBEMAP_LAYOUT_LINE_VERTICAL = 1
|
|
||||||
CUBEMAP_LAYOUT_LINE_HORIZONTAL = 2
|
|
||||||
CUBEMAP_LAYOUT_CROSS_THREE_BY_FOUR = 3
|
|
||||||
CUBEMAP_LAYOUT_CROSS_FOUR_BY_THREE = 4
|
|
||||||
}
|
|
||||||
export enum FontType : u32
|
export enum FontType : u32
|
||||||
{
|
{
|
||||||
FONT_DEFAULT = 0
|
FONT_DEFAULT = 0
|
||||||
FONT_BITMAP = 1
|
FONT_BITMAP = 1
|
||||||
FONT_SDF = 2
|
FONT_SDF = 2
|
||||||
}
|
}
|
||||||
export enum FontType : u32
|
|
||||||
{
|
|
||||||
FONT_DEFAULT = 0
|
|
||||||
FONT_BITMAP = 1
|
|
||||||
FONT_SDF = 2
|
|
||||||
}
|
|
||||||
export enum BlendMode : u32
|
|
||||||
{
|
|
||||||
BLEND_ALPHA = 0
|
|
||||||
BLEND_ADDITIVE = 1
|
|
||||||
BLEND_MULTIPLIED = 2
|
|
||||||
BLEND_ADD_COLORS = 3
|
|
||||||
BLEND_SUBTRACT_COLORS = 4
|
|
||||||
BLEND_ALPHA_PREMULTIPLY = 5
|
|
||||||
BLEND_CUSTOM = 6
|
|
||||||
BLEND_CUSTOM_SEPARATE = 7
|
|
||||||
}
|
|
||||||
export enum BlendMode : u32
|
export enum BlendMode : u32
|
||||||
{
|
{
|
||||||
BLEND_ALPHA = 0
|
BLEND_ALPHA = 0
|
||||||
@@ -943,28 +613,6 @@ export enum Gesture : u32
|
|||||||
GESTURE_PINCH_IN = 256
|
GESTURE_PINCH_IN = 256
|
||||||
GESTURE_PINCH_OUT = 512
|
GESTURE_PINCH_OUT = 512
|
||||||
}
|
}
|
||||||
export enum Gesture : u32
|
|
||||||
{
|
|
||||||
GESTURE_NONE = 0
|
|
||||||
GESTURE_TAP = 1
|
|
||||||
GESTURE_DOUBLETAP = 2
|
|
||||||
GESTURE_HOLD = 4
|
|
||||||
GESTURE_DRAG = 8
|
|
||||||
GESTURE_SWIPE_RIGHT = 16
|
|
||||||
GESTURE_SWIPE_LEFT = 32
|
|
||||||
GESTURE_SWIPE_UP = 64
|
|
||||||
GESTURE_SWIPE_DOWN = 128
|
|
||||||
GESTURE_PINCH_IN = 256
|
|
||||||
GESTURE_PINCH_OUT = 512
|
|
||||||
}
|
|
||||||
export enum CameraMode : u32
|
|
||||||
{
|
|
||||||
CAMERA_CUSTOM = 0
|
|
||||||
CAMERA_FREE = 1
|
|
||||||
CAMERA_ORBITAL = 2
|
|
||||||
CAMERA_FIRST_PERSON = 3
|
|
||||||
CAMERA_THIRD_PERSON = 4
|
|
||||||
}
|
|
||||||
export enum CameraMode : u32
|
export enum CameraMode : u32
|
||||||
{
|
{
|
||||||
CAMERA_CUSTOM = 0
|
CAMERA_CUSTOM = 0
|
||||||
@@ -978,24 +626,13 @@ export enum CameraProjection : u32
|
|||||||
CAMERA_PERSPECTIVE = 0
|
CAMERA_PERSPECTIVE = 0
|
||||||
CAMERA_ORTHOGRAPHIC = 1
|
CAMERA_ORTHOGRAPHIC = 1
|
||||||
}
|
}
|
||||||
export enum CameraProjection : u32
|
|
||||||
{
|
|
||||||
CAMERA_PERSPECTIVE = 0
|
|
||||||
CAMERA_ORTHOGRAPHIC = 1
|
|
||||||
}
|
|
||||||
export enum NPatchLayout : u32
|
export enum NPatchLayout : u32
|
||||||
{
|
{
|
||||||
NPATCH_NINE_PATCH = 0
|
NPATCH_NINE_PATCH = 0
|
||||||
NPATCH_THREE_PATCH_VERTICAL = 1
|
NPATCH_THREE_PATCH_VERTICAL = 1
|
||||||
NPATCH_THREE_PATCH_HORIZONTAL = 2
|
NPATCH_THREE_PATCH_HORIZONTAL = 2
|
||||||
}
|
}
|
||||||
export enum NPatchLayout : u32
|
export extern "InitWindow" func InitWindow(width: i32, height: i32, title: ^i8): void
|
||||||
{
|
|
||||||
NPATCH_NINE_PATCH = 0
|
|
||||||
NPATCH_THREE_PATCH_VERTICAL = 1
|
|
||||||
NPATCH_THREE_PATCH_HORIZONTAL = 2
|
|
||||||
}
|
|
||||||
export extern "InitWindow" func InitWindow(width: i32, height: i32, title: cstring): void
|
|
||||||
export extern "CloseWindow" func CloseWindow(): void
|
export extern "CloseWindow" func CloseWindow(): void
|
||||||
export extern "WindowShouldClose" func WindowShouldClose(): bool
|
export extern "WindowShouldClose" func WindowShouldClose(): bool
|
||||||
export extern "IsWindowReady" func IsWindowReady(): bool
|
export extern "IsWindowReady" func IsWindowReady(): bool
|
||||||
@@ -1015,7 +652,7 @@ export extern "MinimizeWindow" func MinimizeWindow(): void
|
|||||||
export extern "RestoreWindow" func RestoreWindow(): void
|
export extern "RestoreWindow" func RestoreWindow(): void
|
||||||
export extern "SetWindowIcon" func SetWindowIcon(image: Image): void
|
export extern "SetWindowIcon" func SetWindowIcon(image: Image): void
|
||||||
export extern "SetWindowIcons" func SetWindowIcons(images: ^Image, count: i32): void
|
export extern "SetWindowIcons" func SetWindowIcons(images: ^Image, count: i32): void
|
||||||
export extern "SetWindowTitle" func SetWindowTitle(title: cstring): void
|
export extern "SetWindowTitle" func SetWindowTitle(title: ^i8): void
|
||||||
export extern "SetWindowPosition" func SetWindowPosition(x: i32, y: i32): void
|
export extern "SetWindowPosition" func SetWindowPosition(x: i32, y: i32): void
|
||||||
export extern "SetWindowMonitor" func SetWindowMonitor(monitor: i32): void
|
export extern "SetWindowMonitor" func SetWindowMonitor(monitor: i32): void
|
||||||
export extern "SetWindowMinSize" func SetWindowMinSize(width: i32, height: i32): void
|
export extern "SetWindowMinSize" func SetWindowMinSize(width: i32, height: i32): void
|
||||||
@@ -1038,9 +675,9 @@ export extern "GetMonitorPhysicalHeight" func GetMonitorPhysicalHeight(monitor:
|
|||||||
export extern "GetMonitorRefreshRate" func GetMonitorRefreshRate(monitor: i32): i32
|
export extern "GetMonitorRefreshRate" func GetMonitorRefreshRate(monitor: i32): i32
|
||||||
export extern "GetWindowPosition" func GetWindowPosition(): Vector2
|
export extern "GetWindowPosition" func GetWindowPosition(): Vector2
|
||||||
export extern "GetWindowScaleDPI" func GetWindowScaleDPI(): Vector2
|
export extern "GetWindowScaleDPI" func GetWindowScaleDPI(): Vector2
|
||||||
export extern "GetMonitorName" func GetMonitorName(monitor: i32): cstring
|
export extern "GetMonitorName" func GetMonitorName(monitor: i32): ^i8
|
||||||
export extern "SetClipboardText" func SetClipboardText(text: cstring): void
|
export extern "SetClipboardText" func SetClipboardText(text: ^i8): void
|
||||||
export extern "GetClipboardText" func GetClipboardText(): cstring
|
export extern "GetClipboardText" func GetClipboardText(): ^i8
|
||||||
export extern "GetClipboardImage" func GetClipboardImage(): Image
|
export extern "GetClipboardImage" func GetClipboardImage(): Image
|
||||||
export extern "EnableEventWaiting" func EnableEventWaiting(): void
|
export extern "EnableEventWaiting" func EnableEventWaiting(): void
|
||||||
export extern "DisableEventWaiting" func DisableEventWaiting(): void
|
export extern "DisableEventWaiting" func DisableEventWaiting(): void
|
||||||
@@ -1069,11 +706,11 @@ export extern "BeginVrStereoMode" func BeginVrStereoMode(config: VrStereoConfig)
|
|||||||
export extern "EndVrStereoMode" func EndVrStereoMode(): void
|
export extern "EndVrStereoMode" func EndVrStereoMode(): void
|
||||||
export extern "LoadVrStereoConfig" func LoadVrStereoConfig(device: VrDeviceInfo): VrStereoConfig
|
export extern "LoadVrStereoConfig" func LoadVrStereoConfig(device: VrDeviceInfo): VrStereoConfig
|
||||||
export extern "UnloadVrStereoConfig" func UnloadVrStereoConfig(config: VrStereoConfig): void
|
export extern "UnloadVrStereoConfig" func UnloadVrStereoConfig(config: VrStereoConfig): void
|
||||||
export extern "LoadShader" func LoadShader(vsFileName: cstring, fsFileName: cstring): Shader
|
export extern "LoadShader" func LoadShader(vsFileName: ^i8, fsFileName: ^i8): Shader
|
||||||
export extern "LoadShaderFromMemory" func LoadShaderFromMemory(vsCode: cstring, fsCode: cstring): Shader
|
export extern "LoadShaderFromMemory" func LoadShaderFromMemory(vsCode: ^i8, fsCode: ^i8): Shader
|
||||||
export extern "IsShaderValid" func IsShaderValid(shader: Shader): bool
|
export extern "IsShaderValid" func IsShaderValid(shader: Shader): bool
|
||||||
export extern "GetShaderLocation" func GetShaderLocation(shader: Shader, uniformName: cstring): i32
|
export extern "GetShaderLocation" func GetShaderLocation(shader: Shader, uniformName: ^i8): i32
|
||||||
export extern "GetShaderLocationAttrib" func GetShaderLocationAttrib(shader: Shader, attribName: cstring): i32
|
export extern "GetShaderLocationAttrib" func GetShaderLocationAttrib(shader: Shader, attribName: ^i8): i32
|
||||||
export extern "SetShaderValue" func SetShaderValue(shader: Shader, locIndex: i32, value: ^void, uniformType: i32): void
|
export extern "SetShaderValue" func SetShaderValue(shader: Shader, locIndex: i32, value: ^void, uniformType: i32): void
|
||||||
export extern "SetShaderValueV" func SetShaderValueV(shader: Shader, locIndex: i32, value: ^void, uniformType: i32, count: i32): void
|
export extern "SetShaderValueV" func SetShaderValueV(shader: Shader, locIndex: i32, value: ^void, uniformType: i32, count: i32): void
|
||||||
export extern "SetShaderValueMatrix" func SetShaderValueMatrix(shader: Shader, locIndex: i32, mat: Matrix): void
|
export extern "SetShaderValueMatrix" func SetShaderValueMatrix(shader: Shader, locIndex: i32, mat: Matrix): void
|
||||||
@@ -1098,58 +735,58 @@ export extern "SetRandomSeed" func SetRandomSeed(seed: u32): void
|
|||||||
export extern "GetRandomValue" func GetRandomValue(min: i32, max: i32): i32
|
export extern "GetRandomValue" func GetRandomValue(min: i32, max: i32): i32
|
||||||
export extern "LoadRandomSequence" func LoadRandomSequence(count: u32, min: i32, max: i32): ^i32
|
export extern "LoadRandomSequence" func LoadRandomSequence(count: u32, min: i32, max: i32): ^i32
|
||||||
export extern "UnloadRandomSequence" func UnloadRandomSequence(sequence: ^i32): void
|
export extern "UnloadRandomSequence" func UnloadRandomSequence(sequence: ^i32): void
|
||||||
export extern "TakeScreenshot" func TakeScreenshot(fileName: cstring): void
|
export extern "TakeScreenshot" func TakeScreenshot(fileName: ^i8): void
|
||||||
export extern "SetConfigFlags" func SetConfigFlags(flags: u32): void
|
export extern "SetConfigFlags" func SetConfigFlags(flags: u32): void
|
||||||
export extern "OpenURL" func OpenURL(url: cstring): void
|
export extern "OpenURL" func OpenURL(url: ^i8): void
|
||||||
export extern "TraceLog" func TraceLog(logLevel: i32, text: cstring): void
|
export extern "TraceLog" func TraceLog(logLevel: i32, text: ^i8): void
|
||||||
export extern "SetTraceLogLevel" func SetTraceLogLevel(logLevel: i32): void
|
export extern "SetTraceLogLevel" func SetTraceLogLevel(logLevel: i32): void
|
||||||
export extern "MemAlloc" func MemAlloc(size: u32): ^void
|
export extern "MemAlloc" func MemAlloc(size: u32): ^void
|
||||||
export extern "MemRealloc" func MemRealloc(ptr: ^void, size: u32): ^void
|
export extern "MemRealloc" func MemRealloc(ptr: ^void, size: u32): ^void
|
||||||
export extern "MemFree" func MemFree(ptr: ^void): void
|
export extern "MemFree" func MemFree(ptr: ^void): void
|
||||||
export extern "SetTraceLogCallback" func SetTraceLogCallback(callback: func(i32, cstring, i32): void): void
|
export extern "SetTraceLogCallback" func SetTraceLogCallback(callback: func(i32, ^i8, i32): void): void
|
||||||
export extern "SetLoadFileDataCallback" func SetLoadFileDataCallback(callback: func(cstring, ^i32): ^u8): void
|
export extern "SetLoadFileDataCallback" func SetLoadFileDataCallback(callback: func(^i8, ^i32): ^u8): void
|
||||||
export extern "SetSaveFileDataCallback" func SetSaveFileDataCallback(callback: func(cstring, ^void, i32): bool): void
|
export extern "SetSaveFileDataCallback" func SetSaveFileDataCallback(callback: func(^i8, ^void, i32): bool): void
|
||||||
export extern "SetLoadFileTextCallback" func SetLoadFileTextCallback(callback: func(cstring): cstring): void
|
export extern "SetLoadFileTextCallback" func SetLoadFileTextCallback(callback: func(^i8): ^i8): void
|
||||||
export extern "SetSaveFileTextCallback" func SetSaveFileTextCallback(callback: func(cstring, cstring): bool): void
|
export extern "SetSaveFileTextCallback" func SetSaveFileTextCallback(callback: func(^i8, ^i8): bool): void
|
||||||
export extern "LoadFileData" func LoadFileData(fileName: cstring, dataSize: ^i32): ^u8
|
export extern "LoadFileData" func LoadFileData(fileName: ^i8, dataSize: ^i32): ^u8
|
||||||
export extern "UnloadFileData" func UnloadFileData(data: ^u8): void
|
export extern "UnloadFileData" func UnloadFileData(data: ^u8): void
|
||||||
export extern "SaveFileData" func SaveFileData(fileName: cstring, data: ^void, dataSize: i32): bool
|
export extern "SaveFileData" func SaveFileData(fileName: ^i8, data: ^void, dataSize: i32): bool
|
||||||
export extern "ExportDataAsCode" func ExportDataAsCode(data: ^u8, dataSize: i32, fileName: cstring): bool
|
export extern "ExportDataAsCode" func ExportDataAsCode(data: ^u8, dataSize: i32, fileName: ^i8): bool
|
||||||
export extern "LoadFileText" func LoadFileText(fileName: cstring): cstring
|
export extern "LoadFileText" func LoadFileText(fileName: ^i8): ^i8
|
||||||
export extern "UnloadFileText" func UnloadFileText(text: cstring): void
|
export extern "UnloadFileText" func UnloadFileText(text: ^i8): void
|
||||||
export extern "SaveFileText" func SaveFileText(fileName: cstring, text: cstring): bool
|
export extern "SaveFileText" func SaveFileText(fileName: ^i8, text: ^i8): bool
|
||||||
export extern "FileExists" func FileExists(fileName: cstring): bool
|
export extern "FileExists" func FileExists(fileName: ^i8): bool
|
||||||
export extern "DirectoryExists" func DirectoryExists(dirPath: cstring): bool
|
export extern "DirectoryExists" func DirectoryExists(dirPath: ^i8): bool
|
||||||
export extern "IsFileExtension" func IsFileExtension(fileName: cstring, ext: cstring): bool
|
export extern "IsFileExtension" func IsFileExtension(fileName: ^i8, ext: ^i8): bool
|
||||||
export extern "GetFileLength" func GetFileLength(fileName: cstring): i32
|
export extern "GetFileLength" func GetFileLength(fileName: ^i8): i32
|
||||||
export extern "GetFileExtension" func GetFileExtension(fileName: cstring): cstring
|
export extern "GetFileExtension" func GetFileExtension(fileName: ^i8): ^i8
|
||||||
export extern "GetFileName" func GetFileName(filePath: cstring): cstring
|
export extern "GetFileName" func GetFileName(filePath: ^i8): ^i8
|
||||||
export extern "GetFileNameWithoutExt" func GetFileNameWithoutExt(filePath: cstring): cstring
|
export extern "GetFileNameWithoutExt" func GetFileNameWithoutExt(filePath: ^i8): ^i8
|
||||||
export extern "GetDirectoryPath" func GetDirectoryPath(filePath: cstring): cstring
|
export extern "GetDirectoryPath" func GetDirectoryPath(filePath: ^i8): ^i8
|
||||||
export extern "GetPrevDirectoryPath" func GetPrevDirectoryPath(dirPath: cstring): cstring
|
export extern "GetPrevDirectoryPath" func GetPrevDirectoryPath(dirPath: ^i8): ^i8
|
||||||
export extern "GetWorkingDirectory" func GetWorkingDirectory(): cstring
|
export extern "GetWorkingDirectory" func GetWorkingDirectory(): ^i8
|
||||||
export extern "GetApplicationDirectory" func GetApplicationDirectory(): cstring
|
export extern "GetApplicationDirectory" func GetApplicationDirectory(): ^i8
|
||||||
export extern "MakeDirectory" func MakeDirectory(dirPath: cstring): i32
|
export extern "MakeDirectory" func MakeDirectory(dirPath: ^i8): i32
|
||||||
export extern "ChangeDirectory" func ChangeDirectory(dir: cstring): bool
|
export extern "ChangeDirectory" func ChangeDirectory(dir: ^i8): bool
|
||||||
export extern "IsPathFile" func IsPathFile(path: cstring): bool
|
export extern "IsPathFile" func IsPathFile(path: ^i8): bool
|
||||||
export extern "IsFileNameValid" func IsFileNameValid(fileName: cstring): bool
|
export extern "IsFileNameValid" func IsFileNameValid(fileName: ^i8): bool
|
||||||
export extern "LoadDirectoryFiles" func LoadDirectoryFiles(dirPath: cstring): FilePathList
|
export extern "LoadDirectoryFiles" func LoadDirectoryFiles(dirPath: ^i8): FilePathList
|
||||||
export extern "LoadDirectoryFilesEx" func LoadDirectoryFilesEx(basePath: cstring, filter: cstring, scanSubdirs: bool): FilePathList
|
export extern "LoadDirectoryFilesEx" func LoadDirectoryFilesEx(basePath: ^i8, filter: ^i8, scanSubdirs: bool): FilePathList
|
||||||
export extern "UnloadDirectoryFiles" func UnloadDirectoryFiles(files: FilePathList): void
|
export extern "UnloadDirectoryFiles" func UnloadDirectoryFiles(files: FilePathList): void
|
||||||
export extern "IsFileDropped" func IsFileDropped(): bool
|
export extern "IsFileDropped" func IsFileDropped(): bool
|
||||||
export extern "LoadDroppedFiles" func LoadDroppedFiles(): FilePathList
|
export extern "LoadDroppedFiles" func LoadDroppedFiles(): FilePathList
|
||||||
export extern "UnloadDroppedFiles" func UnloadDroppedFiles(files: FilePathList): void
|
export extern "UnloadDroppedFiles" func UnloadDroppedFiles(files: FilePathList): void
|
||||||
export extern "GetFileModTime" func GetFileModTime(fileName: cstring): i64
|
export extern "GetFileModTime" func GetFileModTime(fileName: ^i8): i64
|
||||||
export extern "CompressData" func CompressData(data: ^u8, dataSize: i32, compDataSize: ^i32): ^u8
|
export extern "CompressData" func CompressData(data: ^u8, dataSize: i32, compDataSize: ^i32): ^u8
|
||||||
export extern "DecompressData" func DecompressData(compData: ^u8, compDataSize: i32, dataSize: ^i32): ^u8
|
export extern "DecompressData" func DecompressData(compData: ^u8, compDataSize: i32, dataSize: ^i32): ^u8
|
||||||
export extern "EncodeDataBase64" func EncodeDataBase64(data: ^u8, dataSize: i32, outputSize: ^i32): cstring
|
export extern "EncodeDataBase64" func EncodeDataBase64(data: ^u8, dataSize: i32, outputSize: ^i32): ^i8
|
||||||
export extern "DecodeDataBase64" func DecodeDataBase64(data: ^u8, outputSize: ^i32): ^u8
|
export extern "DecodeDataBase64" func DecodeDataBase64(data: ^u8, outputSize: ^i32): ^u8
|
||||||
export extern "ComputeCRC32" func ComputeCRC32(data: ^u8, dataSize: i32): u32
|
export extern "ComputeCRC32" func ComputeCRC32(data: ^u8, dataSize: i32): u32
|
||||||
export extern "ComputeMD5" func ComputeMD5(data: ^u8, dataSize: i32): ^u32
|
export extern "ComputeMD5" func ComputeMD5(data: ^u8, dataSize: i32): ^u32
|
||||||
export extern "ComputeSHA1" func ComputeSHA1(data: ^u8, dataSize: i32): ^u32
|
export extern "ComputeSHA1" func ComputeSHA1(data: ^u8, dataSize: i32): ^u32
|
||||||
export extern "LoadAutomationEventList" func LoadAutomationEventList(fileName: cstring): AutomationEventList
|
export extern "LoadAutomationEventList" func LoadAutomationEventList(fileName: ^i8): AutomationEventList
|
||||||
export extern "UnloadAutomationEventList" func UnloadAutomationEventList(list: AutomationEventList): void
|
export extern "UnloadAutomationEventList" func UnloadAutomationEventList(list: AutomationEventList): void
|
||||||
export extern "ExportAutomationEventList" func ExportAutomationEventList(list: AutomationEventList, fileName: cstring): bool
|
export extern "ExportAutomationEventList" func ExportAutomationEventList(list: AutomationEventList, fileName: ^i8): bool
|
||||||
export extern "SetAutomationEventList" func SetAutomationEventList(list: ^AutomationEventList): void
|
export extern "SetAutomationEventList" func SetAutomationEventList(list: ^AutomationEventList): void
|
||||||
export extern "SetAutomationEventBaseFrame" func SetAutomationEventBaseFrame(frame: i32): void
|
export extern "SetAutomationEventBaseFrame" func SetAutomationEventBaseFrame(frame: i32): void
|
||||||
export extern "StartAutomationEventRecording" func StartAutomationEventRecording(): void
|
export extern "StartAutomationEventRecording" func StartAutomationEventRecording(): void
|
||||||
@@ -1164,7 +801,7 @@ export extern "GetKeyPressed" func GetKeyPressed(): i32
|
|||||||
export extern "GetCharPressed" func GetCharPressed(): i32
|
export extern "GetCharPressed" func GetCharPressed(): i32
|
||||||
export extern "SetExitKey" func SetExitKey(key: i32): void
|
export extern "SetExitKey" func SetExitKey(key: i32): void
|
||||||
export extern "IsGamepadAvailable" func IsGamepadAvailable(gamepad: i32): bool
|
export extern "IsGamepadAvailable" func IsGamepadAvailable(gamepad: i32): bool
|
||||||
export extern "GetGamepadName" func GetGamepadName(gamepad: i32): cstring
|
export extern "GetGamepadName" func GetGamepadName(gamepad: i32): ^i8
|
||||||
export extern "IsGamepadButtonPressed" func IsGamepadButtonPressed(gamepad: i32, button: i32): bool
|
export extern "IsGamepadButtonPressed" func IsGamepadButtonPressed(gamepad: i32, button: i32): bool
|
||||||
export extern "IsGamepadButtonDown" func IsGamepadButtonDown(gamepad: i32, button: i32): bool
|
export extern "IsGamepadButtonDown" func IsGamepadButtonDown(gamepad: i32, button: i32): bool
|
||||||
export extern "IsGamepadButtonReleased" func IsGamepadButtonReleased(gamepad: i32, button: i32): bool
|
export extern "IsGamepadButtonReleased" func IsGamepadButtonReleased(gamepad: i32, button: i32): bool
|
||||||
@@ -1172,7 +809,7 @@ export extern "IsGamepadButtonUp" func IsGamepadButtonUp(gamepad: i32, button: i
|
|||||||
export extern "GetGamepadButtonPressed" func GetGamepadButtonPressed(): i32
|
export extern "GetGamepadButtonPressed" func GetGamepadButtonPressed(): i32
|
||||||
export extern "GetGamepadAxisCount" func GetGamepadAxisCount(gamepad: i32): i32
|
export extern "GetGamepadAxisCount" func GetGamepadAxisCount(gamepad: i32): i32
|
||||||
export extern "GetGamepadAxisMovement" func GetGamepadAxisMovement(gamepad: i32, axis: i32): f32
|
export extern "GetGamepadAxisMovement" func GetGamepadAxisMovement(gamepad: i32, axis: i32): f32
|
||||||
export extern "SetGamepadMappings" func SetGamepadMappings(mappings: cstring): i32
|
export extern "SetGamepadMappings" func SetGamepadMappings(mappings: ^i8): i32
|
||||||
export extern "SetGamepadVibration" func SetGamepadVibration(gamepad: i32, leftMotor: f32, rightMotor: f32, duration: f32): void
|
export extern "SetGamepadVibration" func SetGamepadVibration(gamepad: i32, leftMotor: f32, rightMotor: f32, duration: f32): void
|
||||||
export extern "IsMouseButtonPressed" func IsMouseButtonPressed(button: i32): bool
|
export extern "IsMouseButtonPressed" func IsMouseButtonPressed(button: i32): bool
|
||||||
export extern "IsMouseButtonDown" func IsMouseButtonDown(button: i32): bool
|
export extern "IsMouseButtonDown" func IsMouseButtonDown(button: i32): bool
|
||||||
@@ -1269,18 +906,18 @@ export extern "CheckCollisionPointLine" func CheckCollisionPointLine(point: Vect
|
|||||||
export extern "CheckCollisionPointPoly" func CheckCollisionPointPoly(point: Vector2, points: ^Vector2, pointCount: i32): bool
|
export extern "CheckCollisionPointPoly" func CheckCollisionPointPoly(point: Vector2, points: ^Vector2, pointCount: i32): bool
|
||||||
export extern "CheckCollisionLines" func CheckCollisionLines(startPos1: Vector2, endPos1: Vector2, startPos2: Vector2, endPos2: Vector2, collisionPoint: ^Vector2): bool
|
export extern "CheckCollisionLines" func CheckCollisionLines(startPos1: Vector2, endPos1: Vector2, startPos2: Vector2, endPos2: Vector2, collisionPoint: ^Vector2): bool
|
||||||
export extern "GetCollisionRec" func GetCollisionRec(rec1: Rectangle, rec2: Rectangle): Rectangle
|
export extern "GetCollisionRec" func GetCollisionRec(rec1: Rectangle, rec2: Rectangle): Rectangle
|
||||||
export extern "LoadImage" func LoadImage(fileName: cstring): Image
|
export extern "LoadImage" func LoadImage(fileName: ^i8): Image
|
||||||
export extern "LoadImageRaw" func LoadImageRaw(fileName: cstring, width: i32, height: i32, format: i32, headerSize: i32): Image
|
export extern "LoadImageRaw" func LoadImageRaw(fileName: ^i8, width: i32, height: i32, format: i32, headerSize: i32): Image
|
||||||
export extern "LoadImageAnim" func LoadImageAnim(fileName: cstring, frames: ^i32): Image
|
export extern "LoadImageAnim" func LoadImageAnim(fileName: ^i8, frames: ^i32): Image
|
||||||
export extern "LoadImageAnimFromMemory" func LoadImageAnimFromMemory(fileType: cstring, fileData: ^u8, dataSize: i32, frames: ^i32): Image
|
export extern "LoadImageAnimFromMemory" func LoadImageAnimFromMemory(fileType: ^i8, fileData: ^u8, dataSize: i32, frames: ^i32): Image
|
||||||
export extern "LoadImageFromMemory" func LoadImageFromMemory(fileType: cstring, fileData: ^u8, dataSize: i32): Image
|
export extern "LoadImageFromMemory" func LoadImageFromMemory(fileType: ^i8, fileData: ^u8, dataSize: i32): Image
|
||||||
export extern "LoadImageFromTexture" func LoadImageFromTexture(texture: Texture): Image
|
export extern "LoadImageFromTexture" func LoadImageFromTexture(texture: Texture): Image
|
||||||
export extern "LoadImageFromScreen" func LoadImageFromScreen(): Image
|
export extern "LoadImageFromScreen" func LoadImageFromScreen(): Image
|
||||||
export extern "IsImageValid" func IsImageValid(image: Image): bool
|
export extern "IsImageValid" func IsImageValid(image: Image): bool
|
||||||
export extern "UnloadImage" func UnloadImage(image: Image): void
|
export extern "UnloadImage" func UnloadImage(image: Image): void
|
||||||
export extern "ExportImage" func ExportImage(image: Image, fileName: cstring): bool
|
export extern "ExportImage" func ExportImage(image: Image, fileName: ^i8): bool
|
||||||
export extern "ExportImageToMemory" func ExportImageToMemory(image: Image, fileType: cstring, fileSize: ^i32): ^u8
|
export extern "ExportImageToMemory" func ExportImageToMemory(image: Image, fileType: ^i8, fileSize: ^i32): ^u8
|
||||||
export extern "ExportImageAsCode" func ExportImageAsCode(image: Image, fileName: cstring): bool
|
export extern "ExportImageAsCode" func ExportImageAsCode(image: Image, fileName: ^i8): bool
|
||||||
export extern "GenImageColor" func GenImageColor(width: i32, height: i32, color: Color): Image
|
export extern "GenImageColor" func GenImageColor(width: i32, height: i32, color: Color): Image
|
||||||
export extern "GenImageGradientLinear" func GenImageGradientLinear(width: i32, height: i32, direction: i32, start: Color, end: Color): Image
|
export extern "GenImageGradientLinear" func GenImageGradientLinear(width: i32, height: i32, direction: i32, start: Color, end: Color): Image
|
||||||
export extern "GenImageGradientRadial" func GenImageGradientRadial(width: i32, height: i32, density: f32, inner: Color, outer: Color): Image
|
export extern "GenImageGradientRadial" func GenImageGradientRadial(width: i32, height: i32, density: f32, inner: Color, outer: Color): Image
|
||||||
@@ -1289,12 +926,12 @@ export extern "GenImageChecked" func GenImageChecked(width: i32, height: i32, ch
|
|||||||
export extern "GenImageWhiteNoise" func GenImageWhiteNoise(width: i32, height: i32, factor: f32): Image
|
export extern "GenImageWhiteNoise" func GenImageWhiteNoise(width: i32, height: i32, factor: f32): Image
|
||||||
export extern "GenImagePerlinNoise" func GenImagePerlinNoise(width: i32, height: i32, offsetX: i32, offsetY: i32, scale: f32): Image
|
export extern "GenImagePerlinNoise" func GenImagePerlinNoise(width: i32, height: i32, offsetX: i32, offsetY: i32, scale: f32): Image
|
||||||
export extern "GenImageCellular" func GenImageCellular(width: i32, height: i32, tileSize: i32): Image
|
export extern "GenImageCellular" func GenImageCellular(width: i32, height: i32, tileSize: i32): Image
|
||||||
export extern "GenImageText" func GenImageText(width: i32, height: i32, text: cstring): Image
|
export extern "GenImageText" func GenImageText(width: i32, height: i32, text: ^i8): Image
|
||||||
export extern "ImageCopy" func ImageCopy(image: Image): Image
|
export extern "ImageCopy" func ImageCopy(image: Image): Image
|
||||||
export extern "ImageFromImage" func ImageFromImage(image: Image, rec: Rectangle): Image
|
export extern "ImageFromImage" func ImageFromImage(image: Image, rec: Rectangle): Image
|
||||||
export extern "ImageFromChannel" func ImageFromChannel(image: Image, selectedChannel: i32): Image
|
export extern "ImageFromChannel" func ImageFromChannel(image: Image, selectedChannel: i32): Image
|
||||||
export extern "ImageText" func ImageText(text: cstring, fontSize: i32, color: Color): Image
|
export extern "ImageText" func ImageText(text: ^i8, fontSize: i32, color: Color): Image
|
||||||
export extern "ImageTextEx" func ImageTextEx(font: Font, text: cstring, fontSize: f32, spacing: f32, tint: Color): Image
|
export extern "ImageTextEx" func ImageTextEx(font: Font, text: ^i8, fontSize: f32, spacing: f32, tint: Color): Image
|
||||||
export extern "ImageFormat" func ImageFormat(image: ^Image, newFormat: i32): void
|
export extern "ImageFormat" func ImageFormat(image: ^Image, newFormat: i32): void
|
||||||
export extern "ImageToPOT" func ImageToPOT(image: ^Image, fill: Color): void
|
export extern "ImageToPOT" func ImageToPOT(image: ^Image, fill: Color): void
|
||||||
export extern "ImageCrop" func ImageCrop(image: ^Image, crop: Rectangle): void
|
export extern "ImageCrop" func ImageCrop(image: ^Image, crop: Rectangle): void
|
||||||
@@ -1346,9 +983,9 @@ export extern "ImageDrawTriangleLines" func ImageDrawTriangleLines(dst: ^Image,
|
|||||||
export extern "ImageDrawTriangleFan" func ImageDrawTriangleFan(dst: ^Image, points: ^Vector2, pointCount: i32, color: Color): void
|
export extern "ImageDrawTriangleFan" func ImageDrawTriangleFan(dst: ^Image, points: ^Vector2, pointCount: i32, color: Color): void
|
||||||
export extern "ImageDrawTriangleStrip" func ImageDrawTriangleStrip(dst: ^Image, points: ^Vector2, pointCount: i32, color: Color): void
|
export extern "ImageDrawTriangleStrip" func ImageDrawTriangleStrip(dst: ^Image, points: ^Vector2, pointCount: i32, color: Color): void
|
||||||
export extern "ImageDraw" func ImageDraw(dst: ^Image, src: Image, srcRec: Rectangle, dstRec: Rectangle, tint: Color): void
|
export extern "ImageDraw" func ImageDraw(dst: ^Image, src: Image, srcRec: Rectangle, dstRec: Rectangle, tint: Color): void
|
||||||
export extern "ImageDrawText" func ImageDrawText(dst: ^Image, text: cstring, posX: i32, posY: i32, fontSize: i32, color: Color): void
|
export extern "ImageDrawText" func ImageDrawText(dst: ^Image, text: ^i8, posX: i32, posY: i32, fontSize: i32, color: Color): void
|
||||||
export extern "ImageDrawTextEx" func ImageDrawTextEx(dst: ^Image, font: Font, text: cstring, position: Vector2, fontSize: f32, spacing: f32, tint: Color): void
|
export extern "ImageDrawTextEx" func ImageDrawTextEx(dst: ^Image, font: Font, text: ^i8, position: Vector2, fontSize: f32, spacing: f32, tint: Color): void
|
||||||
export extern "LoadTexture" func LoadTexture(fileName: cstring): Texture
|
export extern "LoadTexture" func LoadTexture(fileName: ^i8): Texture
|
||||||
export extern "LoadTextureFromImage" func LoadTextureFromImage(image: Image): Texture
|
export extern "LoadTextureFromImage" func LoadTextureFromImage(image: Image): Texture
|
||||||
export extern "LoadTextureCubemap" func LoadTextureCubemap(image: Image, layout: i32): Texture
|
export extern "LoadTextureCubemap" func LoadTextureCubemap(image: Image, layout: i32): Texture
|
||||||
export extern "LoadRenderTexture" func LoadRenderTexture(width: i32, height: i32): RenderTexture
|
export extern "LoadRenderTexture" func LoadRenderTexture(width: i32, height: i32): RenderTexture
|
||||||
@@ -1385,55 +1022,55 @@ export extern "GetPixelColor" func GetPixelColor(srcPtr: ^void, format: i32): Co
|
|||||||
export extern "SetPixelColor" func SetPixelColor(dstPtr: ^void, color: Color, format: i32): void
|
export extern "SetPixelColor" func SetPixelColor(dstPtr: ^void, color: Color, format: i32): void
|
||||||
export extern "GetPixelDataSize" func GetPixelDataSize(width: i32, height: i32, format: i32): i32
|
export extern "GetPixelDataSize" func GetPixelDataSize(width: i32, height: i32, format: i32): i32
|
||||||
export extern "GetFontDefault" func GetFontDefault(): Font
|
export extern "GetFontDefault" func GetFontDefault(): Font
|
||||||
export extern "LoadFont" func LoadFont(fileName: cstring): Font
|
export extern "LoadFont" func LoadFont(fileName: ^i8): Font
|
||||||
export extern "LoadFontEx" func LoadFontEx(fileName: cstring, fontSize: i32, codepoints: ^i32, codepointCount: i32): Font
|
export extern "LoadFontEx" func LoadFontEx(fileName: ^i8, fontSize: i32, codepoints: ^i32, codepointCount: i32): Font
|
||||||
export extern "LoadFontFromImage" func LoadFontFromImage(image: Image, key: Color, firstChar: i32): Font
|
export extern "LoadFontFromImage" func LoadFontFromImage(image: Image, key: Color, firstChar: i32): Font
|
||||||
export extern "LoadFontFromMemory" func LoadFontFromMemory(fileType: cstring, fileData: ^u8, dataSize: i32, fontSize: i32, codepoints: ^i32, codepointCount: i32): Font
|
export extern "LoadFontFromMemory" func LoadFontFromMemory(fileType: ^i8, fileData: ^u8, dataSize: i32, fontSize: i32, codepoints: ^i32, codepointCount: i32): Font
|
||||||
export extern "IsFontValid" func IsFontValid(font: Font): bool
|
export extern "IsFontValid" func IsFontValid(font: Font): bool
|
||||||
export extern "LoadFontData" func LoadFontData(fileData: ^u8, dataSize: i32, fontSize: i32, codepoints: ^i32, codepointCount: i32, type: i32): ^GlyphInfo
|
export extern "LoadFontData" func LoadFontData(fileData: ^u8, dataSize: i32, fontSize: i32, codepoints: ^i32, codepointCount: i32, type: i32): ^GlyphInfo
|
||||||
export extern "GenImageFontAtlas" func GenImageFontAtlas(glyphs: ^GlyphInfo, glyphRecs: ^^Rectangle, glyphCount: i32, fontSize: i32, padding: i32, packMethod: i32): Image
|
export extern "GenImageFontAtlas" func GenImageFontAtlas(glyphs: ^GlyphInfo, glyphRecs: ^^Rectangle, glyphCount: i32, fontSize: i32, padding: i32, packMethod: i32): Image
|
||||||
export extern "UnloadFontData" func UnloadFontData(glyphs: ^GlyphInfo, glyphCount: i32): void
|
export extern "UnloadFontData" func UnloadFontData(glyphs: ^GlyphInfo, glyphCount: i32): void
|
||||||
export extern "UnloadFont" func UnloadFont(font: Font): void
|
export extern "UnloadFont" func UnloadFont(font: Font): void
|
||||||
export extern "ExportFontAsCode" func ExportFontAsCode(font: Font, fileName: cstring): bool
|
export extern "ExportFontAsCode" func ExportFontAsCode(font: Font, fileName: ^i8): bool
|
||||||
export extern "DrawFPS" func DrawFPS(posX: i32, posY: i32): void
|
export extern "DrawFPS" func DrawFPS(posX: i32, posY: i32): void
|
||||||
export extern "DrawText" func DrawText(text: cstring, posX: i32, posY: i32, fontSize: i32, color: Color): void
|
export extern "DrawText" func DrawText(text: ^i8, posX: i32, posY: i32, fontSize: i32, color: Color): void
|
||||||
export extern "DrawTextEx" func DrawTextEx(font: Font, text: cstring, position: Vector2, fontSize: f32, spacing: f32, tint: Color): void
|
export extern "DrawTextEx" func DrawTextEx(font: Font, text: ^i8, position: Vector2, fontSize: f32, spacing: f32, tint: Color): void
|
||||||
export extern "DrawTextPro" func DrawTextPro(font: Font, text: cstring, position: Vector2, origin: Vector2, rotation: f32, fontSize: f32, spacing: f32, tint: Color): void
|
export extern "DrawTextPro" func DrawTextPro(font: Font, text: ^i8, position: Vector2, origin: Vector2, rotation: f32, fontSize: f32, spacing: f32, tint: Color): void
|
||||||
export extern "DrawTextCodepoint" func DrawTextCodepoint(font: Font, codepoint: i32, position: Vector2, fontSize: f32, tint: Color): void
|
export extern "DrawTextCodepoint" func DrawTextCodepoint(font: Font, codepoint: i32, position: Vector2, fontSize: f32, tint: Color): void
|
||||||
export extern "DrawTextCodepoints" func DrawTextCodepoints(font: Font, codepoints: ^i32, codepointCount: i32, position: Vector2, fontSize: f32, spacing: f32, tint: Color): void
|
export extern "DrawTextCodepoints" func DrawTextCodepoints(font: Font, codepoints: ^i32, codepointCount: i32, position: Vector2, fontSize: f32, spacing: f32, tint: Color): void
|
||||||
export extern "SetTextLineSpacing" func SetTextLineSpacing(spacing: i32): void
|
export extern "SetTextLineSpacing" func SetTextLineSpacing(spacing: i32): void
|
||||||
export extern "MeasureText" func MeasureText(text: cstring, fontSize: i32): i32
|
export extern "MeasureText" func MeasureText(text: ^i8, fontSize: i32): i32
|
||||||
export extern "MeasureTextEx" func MeasureTextEx(font: Font, text: cstring, fontSize: f32, spacing: f32): Vector2
|
export extern "MeasureTextEx" func MeasureTextEx(font: Font, text: ^i8, fontSize: f32, spacing: f32): Vector2
|
||||||
export extern "GetGlyphIndex" func GetGlyphIndex(font: Font, codepoint: i32): i32
|
export extern "GetGlyphIndex" func GetGlyphIndex(font: Font, codepoint: i32): i32
|
||||||
export extern "GetGlyphInfo" func GetGlyphInfo(font: Font, codepoint: i32): GlyphInfo
|
export extern "GetGlyphInfo" func GetGlyphInfo(font: Font, codepoint: i32): GlyphInfo
|
||||||
export extern "GetGlyphAtlasRec" func GetGlyphAtlasRec(font: Font, codepoint: i32): Rectangle
|
export extern "GetGlyphAtlasRec" func GetGlyphAtlasRec(font: Font, codepoint: i32): Rectangle
|
||||||
export extern "LoadUTF8" func LoadUTF8(codepoints: ^i32, length: i32): cstring
|
export extern "LoadUTF8" func LoadUTF8(codepoints: ^i32, length: i32): ^i8
|
||||||
export extern "UnloadUTF8" func UnloadUTF8(text: cstring): void
|
export extern "UnloadUTF8" func UnloadUTF8(text: ^i8): void
|
||||||
export extern "LoadCodepoints" func LoadCodepoints(text: cstring, count: ^i32): ^i32
|
export extern "LoadCodepoints" func LoadCodepoints(text: ^i8, count: ^i32): ^i32
|
||||||
export extern "UnloadCodepoints" func UnloadCodepoints(codepoints: ^i32): void
|
export extern "UnloadCodepoints" func UnloadCodepoints(codepoints: ^i32): void
|
||||||
export extern "GetCodepointCount" func GetCodepointCount(text: cstring): i32
|
export extern "GetCodepointCount" func GetCodepointCount(text: ^i8): i32
|
||||||
export extern "GetCodepoint" func GetCodepoint(text: cstring, codepointSize: ^i32): i32
|
export extern "GetCodepoint" func GetCodepoint(text: ^i8, codepointSize: ^i32): i32
|
||||||
export extern "GetCodepointNext" func GetCodepointNext(text: cstring, codepointSize: ^i32): i32
|
export extern "GetCodepointNext" func GetCodepointNext(text: ^i8, codepointSize: ^i32): i32
|
||||||
export extern "GetCodepointPrevious" func GetCodepointPrevious(text: cstring, codepointSize: ^i32): i32
|
export extern "GetCodepointPrevious" func GetCodepointPrevious(text: ^i8, codepointSize: ^i32): i32
|
||||||
export extern "CodepointToUTF8" func CodepointToUTF8(codepoint: i32, utf8Size: ^i32): cstring
|
export extern "CodepointToUTF8" func CodepointToUTF8(codepoint: i32, utf8Size: ^i32): ^i8
|
||||||
export extern "TextCopy" func TextCopy(dst: cstring, src: cstring): i32
|
export extern "TextCopy" func TextCopy(dst: ^i8, src: ^i8): i32
|
||||||
export extern "TextIsEqual" func TextIsEqual(text1: cstring, text2: cstring): bool
|
export extern "TextIsEqual" func TextIsEqual(text1: ^i8, text2: ^i8): bool
|
||||||
export extern "TextLength" func TextLength(text: cstring): u32
|
export extern "TextLength" func TextLength(text: ^i8): u32
|
||||||
export extern "TextFormat" func TextFormat(text: cstring): cstring
|
export extern "TextFormat" func TextFormat(text: ^i8): ^i8
|
||||||
export extern "TextSubtext" func TextSubtext(text: cstring, position: i32, length: i32): cstring
|
export extern "TextSubtext" func TextSubtext(text: ^i8, position: i32, length: i32): ^i8
|
||||||
export extern "TextReplace" func TextReplace(text: cstring, replace: cstring, by: cstring): cstring
|
export extern "TextReplace" func TextReplace(text: ^i8, replace: ^i8, by: ^i8): ^i8
|
||||||
export extern "TextInsert" func TextInsert(text: cstring, insert: cstring, position: i32): cstring
|
export extern "TextInsert" func TextInsert(text: ^i8, insert: ^i8, position: i32): ^i8
|
||||||
export extern "TextJoin" func TextJoin(textList: ^cstring, count: i32, delimiter: cstring): cstring
|
export extern "TextJoin" func TextJoin(textList: ^^i8, count: i32, delimiter: ^i8): ^i8
|
||||||
export extern "TextSplit" func TextSplit(text: cstring, delimiter: i8, count: ^i32): ^cstring
|
export extern "TextSplit" func TextSplit(text: ^i8, delimiter: i8, count: ^i32): ^^i8
|
||||||
export extern "TextAppend" func TextAppend(text: cstring, append: cstring, position: ^i32): void
|
export extern "TextAppend" func TextAppend(text: ^i8, append: ^i8, position: ^i32): void
|
||||||
export extern "TextFindIndex" func TextFindIndex(text: cstring, find: cstring): i32
|
export extern "TextFindIndex" func TextFindIndex(text: ^i8, find: ^i8): i32
|
||||||
export extern "TextToUpper" func TextToUpper(text: cstring): cstring
|
export extern "TextToUpper" func TextToUpper(text: ^i8): ^i8
|
||||||
export extern "TextToLower" func TextToLower(text: cstring): cstring
|
export extern "TextToLower" func TextToLower(text: ^i8): ^i8
|
||||||
export extern "TextToPascal" func TextToPascal(text: cstring): cstring
|
export extern "TextToPascal" func TextToPascal(text: ^i8): ^i8
|
||||||
export extern "TextToSnake" func TextToSnake(text: cstring): cstring
|
export extern "TextToSnake" func TextToSnake(text: ^i8): ^i8
|
||||||
export extern "TextToCamel" func TextToCamel(text: cstring): cstring
|
export extern "TextToCamel" func TextToCamel(text: ^i8): ^i8
|
||||||
export extern "TextToInteger" func TextToInteger(text: cstring): i32
|
export extern "TextToInteger" func TextToInteger(text: ^i8): i32
|
||||||
export extern "TextToFloat" func TextToFloat(text: cstring): f32
|
export extern "TextToFloat" func TextToFloat(text: ^i8): f32
|
||||||
export extern "DrawLine3D" func DrawLine3D(startPos: Vector3, endPos: Vector3, color: Color): void
|
export extern "DrawLine3D" func DrawLine3D(startPos: Vector3, endPos: Vector3, color: Color): void
|
||||||
export extern "DrawPoint3D" func DrawPoint3D(position: Vector3, color: Color): void
|
export extern "DrawPoint3D" func DrawPoint3D(position: Vector3, color: Color): void
|
||||||
export extern "DrawCircle3D" func DrawCircle3D(center: Vector3, radius: f32, rotationAxis: Vector3, rotationAngle: f32, color: Color): void
|
export extern "DrawCircle3D" func DrawCircle3D(center: Vector3, radius: f32, rotationAxis: Vector3, rotationAngle: f32, color: Color): void
|
||||||
@@ -1455,7 +1092,7 @@ export extern "DrawCapsuleWires" func DrawCapsuleWires(startPos: Vector3, endPos
|
|||||||
export extern "DrawPlane" func DrawPlane(centerPos: Vector3, size: Vector2, color: Color): void
|
export extern "DrawPlane" func DrawPlane(centerPos: Vector3, size: Vector2, color: Color): void
|
||||||
export extern "DrawRay" func DrawRay(ray: Ray, color: Color): void
|
export extern "DrawRay" func DrawRay(ray: Ray, color: Color): void
|
||||||
export extern "DrawGrid" func DrawGrid(slices: i32, spacing: f32): void
|
export extern "DrawGrid" func DrawGrid(slices: i32, spacing: f32): void
|
||||||
export extern "LoadModel" func LoadModel(fileName: cstring): Model
|
export extern "LoadModel" func LoadModel(fileName: ^i8): Model
|
||||||
export extern "LoadModelFromMesh" func LoadModelFromMesh(mesh: Mesh): Model
|
export extern "LoadModelFromMesh" func LoadModelFromMesh(mesh: Mesh): Model
|
||||||
export extern "IsModelValid" func IsModelValid(model: Model): bool
|
export extern "IsModelValid" func IsModelValid(model: Model): bool
|
||||||
export extern "UnloadModel" func UnloadModel(model: Model): void
|
export extern "UnloadModel" func UnloadModel(model: Model): void
|
||||||
@@ -1477,8 +1114,8 @@ export extern "DrawMesh" func DrawMesh(mesh: Mesh, material: Material, transform
|
|||||||
export extern "DrawMeshInstanced" func DrawMeshInstanced(mesh: Mesh, material: Material, transforms: ^Matrix, instances: i32): void
|
export extern "DrawMeshInstanced" func DrawMeshInstanced(mesh: Mesh, material: Material, transforms: ^Matrix, instances: i32): void
|
||||||
export extern "GetMeshBoundingBox" func GetMeshBoundingBox(mesh: Mesh): BoundingBox
|
export extern "GetMeshBoundingBox" func GetMeshBoundingBox(mesh: Mesh): BoundingBox
|
||||||
export extern "GenMeshTangents" func GenMeshTangents(mesh: ^Mesh): void
|
export extern "GenMeshTangents" func GenMeshTangents(mesh: ^Mesh): void
|
||||||
export extern "ExportMesh" func ExportMesh(mesh: Mesh, fileName: cstring): bool
|
export extern "ExportMesh" func ExportMesh(mesh: Mesh, fileName: ^i8): bool
|
||||||
export extern "ExportMeshAsCode" func ExportMeshAsCode(mesh: Mesh, fileName: cstring): bool
|
export extern "ExportMeshAsCode" func ExportMeshAsCode(mesh: Mesh, fileName: ^i8): bool
|
||||||
export extern "GenMeshPoly" func GenMeshPoly(sides: i32, radius: f32): Mesh
|
export extern "GenMeshPoly" func GenMeshPoly(sides: i32, radius: f32): Mesh
|
||||||
export extern "GenMeshPlane" func GenMeshPlane(width: f32, length: f32, resX: i32, resZ: i32): Mesh
|
export extern "GenMeshPlane" func GenMeshPlane(width: f32, length: f32, resX: i32, resZ: i32): Mesh
|
||||||
export extern "GenMeshCube" func GenMeshCube(width: f32, height: f32, length: f32): Mesh
|
export extern "GenMeshCube" func GenMeshCube(width: f32, height: f32, length: f32): Mesh
|
||||||
@@ -1490,13 +1127,13 @@ export extern "GenMeshTorus" func GenMeshTorus(radius: f32, size: f32, radSeg: i
|
|||||||
export extern "GenMeshKnot" func GenMeshKnot(radius: f32, size: f32, radSeg: i32, sides: i32): Mesh
|
export extern "GenMeshKnot" func GenMeshKnot(radius: f32, size: f32, radSeg: i32, sides: i32): Mesh
|
||||||
export extern "GenMeshHeightmap" func GenMeshHeightmap(heightmap: Image, size: Vector3): Mesh
|
export extern "GenMeshHeightmap" func GenMeshHeightmap(heightmap: Image, size: Vector3): Mesh
|
||||||
export extern "GenMeshCubicmap" func GenMeshCubicmap(cubicmap: Image, cubeSize: Vector3): Mesh
|
export extern "GenMeshCubicmap" func GenMeshCubicmap(cubicmap: Image, cubeSize: Vector3): Mesh
|
||||||
export extern "LoadMaterials" func LoadMaterials(fileName: cstring, materialCount: ^i32): ^Material
|
export extern "LoadMaterials" func LoadMaterials(fileName: ^i8, materialCount: ^i32): ^Material
|
||||||
export extern "LoadMaterialDefault" func LoadMaterialDefault(): Material
|
export extern "LoadMaterialDefault" func LoadMaterialDefault(): Material
|
||||||
export extern "IsMaterialValid" func IsMaterialValid(material: Material): bool
|
export extern "IsMaterialValid" func IsMaterialValid(material: Material): bool
|
||||||
export extern "UnloadMaterial" func UnloadMaterial(material: Material): void
|
export extern "UnloadMaterial" func UnloadMaterial(material: Material): void
|
||||||
export extern "SetMaterialTexture" func SetMaterialTexture(material: ^Material, mapType: i32, texture: Texture): void
|
export extern "SetMaterialTexture" func SetMaterialTexture(material: ^Material, mapType: i32, texture: Texture): void
|
||||||
export extern "SetModelMeshMaterial" func SetModelMeshMaterial(model: ^Model, meshId: i32, materialId: i32): void
|
export extern "SetModelMeshMaterial" func SetModelMeshMaterial(model: ^Model, meshId: i32, materialId: i32): void
|
||||||
export extern "LoadModelAnimations" func LoadModelAnimations(fileName: cstring, animCount: ^i32): ^ModelAnimation
|
export extern "LoadModelAnimations" func LoadModelAnimations(fileName: ^i8, animCount: ^i32): ^ModelAnimation
|
||||||
export extern "UpdateModelAnimation" func UpdateModelAnimation(model: Model, anim: ModelAnimation, frame: i32): void
|
export extern "UpdateModelAnimation" func UpdateModelAnimation(model: Model, anim: ModelAnimation, frame: i32): void
|
||||||
export extern "UpdateModelAnimationBones" func UpdateModelAnimationBones(model: Model, anim: ModelAnimation, frame: i32): void
|
export extern "UpdateModelAnimationBones" func UpdateModelAnimationBones(model: Model, anim: ModelAnimation, frame: i32): void
|
||||||
export extern "UnloadModelAnimation" func UnloadModelAnimation(anim: ModelAnimation): void
|
export extern "UnloadModelAnimation" func UnloadModelAnimation(anim: ModelAnimation): void
|
||||||
@@ -1515,10 +1152,10 @@ export extern "CloseAudioDevice" func CloseAudioDevice(): void
|
|||||||
export extern "IsAudioDeviceReady" func IsAudioDeviceReady(): bool
|
export extern "IsAudioDeviceReady" func IsAudioDeviceReady(): bool
|
||||||
export extern "SetMasterVolume" func SetMasterVolume(volume: f32): void
|
export extern "SetMasterVolume" func SetMasterVolume(volume: f32): void
|
||||||
export extern "GetMasterVolume" func GetMasterVolume(): f32
|
export extern "GetMasterVolume" func GetMasterVolume(): f32
|
||||||
export extern "LoadWave" func LoadWave(fileName: cstring): Wave
|
export extern "LoadWave" func LoadWave(fileName: ^i8): Wave
|
||||||
export extern "LoadWaveFromMemory" func LoadWaveFromMemory(fileType: cstring, fileData: ^u8, dataSize: i32): Wave
|
export extern "LoadWaveFromMemory" func LoadWaveFromMemory(fileType: ^i8, fileData: ^u8, dataSize: i32): Wave
|
||||||
export extern "IsWaveValid" func IsWaveValid(wave: Wave): bool
|
export extern "IsWaveValid" func IsWaveValid(wave: Wave): bool
|
||||||
export extern "LoadSound" func LoadSound(fileName: cstring): Sound
|
export extern "LoadSound" func LoadSound(fileName: ^i8): Sound
|
||||||
export extern "LoadSoundFromWave" func LoadSoundFromWave(wave: Wave): Sound
|
export extern "LoadSoundFromWave" func LoadSoundFromWave(wave: Wave): Sound
|
||||||
export extern "LoadSoundAlias" func LoadSoundAlias(source: Sound): Sound
|
export extern "LoadSoundAlias" func LoadSoundAlias(source: Sound): Sound
|
||||||
export extern "IsSoundValid" func IsSoundValid(sound: Sound): bool
|
export extern "IsSoundValid" func IsSoundValid(sound: Sound): bool
|
||||||
@@ -1526,8 +1163,8 @@ export extern "UpdateSound" func UpdateSound(sound: Sound, data: ^void, sampleCo
|
|||||||
export extern "UnloadWave" func UnloadWave(wave: Wave): void
|
export extern "UnloadWave" func UnloadWave(wave: Wave): void
|
||||||
export extern "UnloadSound" func UnloadSound(sound: Sound): void
|
export extern "UnloadSound" func UnloadSound(sound: Sound): void
|
||||||
export extern "UnloadSoundAlias" func UnloadSoundAlias(alias: Sound): void
|
export extern "UnloadSoundAlias" func UnloadSoundAlias(alias: Sound): void
|
||||||
export extern "ExportWave" func ExportWave(wave: Wave, fileName: cstring): bool
|
export extern "ExportWave" func ExportWave(wave: Wave, fileName: ^i8): bool
|
||||||
export extern "ExportWaveAsCode" func ExportWaveAsCode(wave: Wave, fileName: cstring): bool
|
export extern "ExportWaveAsCode" func ExportWaveAsCode(wave: Wave, fileName: ^i8): bool
|
||||||
export extern "PlaySound" func PlaySound(sound: Sound): void
|
export extern "PlaySound" func PlaySound(sound: Sound): void
|
||||||
export extern "StopSound" func StopSound(sound: Sound): void
|
export extern "StopSound" func StopSound(sound: Sound): void
|
||||||
export extern "PauseSound" func PauseSound(sound: Sound): void
|
export extern "PauseSound" func PauseSound(sound: Sound): void
|
||||||
@@ -1541,8 +1178,8 @@ export extern "WaveCrop" func WaveCrop(wave: ^Wave, initFrame: i32, finalFrame:
|
|||||||
export extern "WaveFormat" func WaveFormat(wave: ^Wave, sampleRate: i32, sampleSize: i32, channels: i32): void
|
export extern "WaveFormat" func WaveFormat(wave: ^Wave, sampleRate: i32, sampleSize: i32, channels: i32): void
|
||||||
export extern "LoadWaveSamples" func LoadWaveSamples(wave: Wave): ^f32
|
export extern "LoadWaveSamples" func LoadWaveSamples(wave: Wave): ^f32
|
||||||
export extern "UnloadWaveSamples" func UnloadWaveSamples(samples: ^f32): void
|
export extern "UnloadWaveSamples" func UnloadWaveSamples(samples: ^f32): void
|
||||||
export extern "LoadMusicStream" func LoadMusicStream(fileName: cstring): Music
|
export extern "LoadMusicStream" func LoadMusicStream(fileName: ^i8): Music
|
||||||
export extern "LoadMusicStreamFromMemory" func LoadMusicStreamFromMemory(fileType: cstring, data: ^u8, dataSize: i32): Music
|
export extern "LoadMusicStreamFromMemory" func LoadMusicStreamFromMemory(fileType: ^i8, data: ^u8, dataSize: i32): Music
|
||||||
export extern "IsMusicValid" func IsMusicValid(music: Music): bool
|
export extern "IsMusicValid" func IsMusicValid(music: Music): bool
|
||||||
export extern "UnloadMusicStream" func UnloadMusicStream(music: Music): void
|
export extern "UnloadMusicStream" func UnloadMusicStream(music: Music): void
|
||||||
export extern "PlayMusicStream" func PlayMusicStream(music: Music): void
|
export extern "PlayMusicStream" func PlayMusicStream(music: Music): void
|
||||||
|
|||||||
@@ -1,190 +0,0 @@
|
|||||||
module "raymath"
|
|
||||||
|
|
||||||
export struct Vector2
|
|
||||||
{
|
|
||||||
x: f32
|
|
||||||
y: f32
|
|
||||||
}
|
|
||||||
export struct Vector3
|
|
||||||
{
|
|
||||||
x: f32
|
|
||||||
y: f32
|
|
||||||
z: f32
|
|
||||||
}
|
|
||||||
export struct Vector4
|
|
||||||
{
|
|
||||||
x: f32
|
|
||||||
y: f32
|
|
||||||
z: f32
|
|
||||||
w: f32
|
|
||||||
}
|
|
||||||
export struct Matrix
|
|
||||||
{
|
|
||||||
m0: f32
|
|
||||||
m4: f32
|
|
||||||
m8: f32
|
|
||||||
m12: f32
|
|
||||||
m1: f32
|
|
||||||
m5: f32
|
|
||||||
m9: f32
|
|
||||||
m13: f32
|
|
||||||
m2: f32
|
|
||||||
m6: f32
|
|
||||||
m10: f32
|
|
||||||
m14: f32
|
|
||||||
m3: f32
|
|
||||||
m7: f32
|
|
||||||
m11: f32
|
|
||||||
m15: f32
|
|
||||||
}
|
|
||||||
export struct float3
|
|
||||||
{
|
|
||||||
v: [3]f32
|
|
||||||
}
|
|
||||||
export struct float16
|
|
||||||
{
|
|
||||||
v: [16]f32
|
|
||||||
}
|
|
||||||
export extern "Clamp" func Clamp(value: f32, min: f32, max: f32): f32
|
|
||||||
export extern "Lerp" func Lerp(start: f32, end: f32, amount: f32): f32
|
|
||||||
export extern "Normalize" func Normalize(value: f32, start: f32, end: f32): f32
|
|
||||||
export extern "Remap" func Remap(value: f32, inputStart: f32, inputEnd: f32, outputStart: f32, outputEnd: f32): f32
|
|
||||||
export extern "Wrap" func Wrap(value: f32, min: f32, max: f32): f32
|
|
||||||
export extern "FloatEquals" func FloatEquals(x: f32, y: f32): i32
|
|
||||||
export extern "Vector2Zero" func Vector2Zero(): Vector2
|
|
||||||
export extern "Vector2One" func Vector2One(): Vector2
|
|
||||||
export extern "Vector2Add" func Vector2Add(v1: Vector2, v2: Vector2): Vector2
|
|
||||||
export extern "Vector2AddValue" func Vector2AddValue(v: Vector2, add: f32): Vector2
|
|
||||||
export extern "Vector2Subtract" func Vector2Subtract(v1: Vector2, v2: Vector2): Vector2
|
|
||||||
export extern "Vector2SubtractValue" func Vector2SubtractValue(v: Vector2, sub: f32): Vector2
|
|
||||||
export extern "Vector2Length" func Vector2Length(v: Vector2): f32
|
|
||||||
export extern "Vector2LengthSqr" func Vector2LengthSqr(v: Vector2): f32
|
|
||||||
export extern "Vector2DotProduct" func Vector2DotProduct(v1: Vector2, v2: Vector2): f32
|
|
||||||
export extern "Vector2Distance" func Vector2Distance(v1: Vector2, v2: Vector2): f32
|
|
||||||
export extern "Vector2DistanceSqr" func Vector2DistanceSqr(v1: Vector2, v2: Vector2): f32
|
|
||||||
export extern "Vector2Angle" func Vector2Angle(v1: Vector2, v2: Vector2): f32
|
|
||||||
export extern "Vector2LineAngle" func Vector2LineAngle(start: Vector2, end: Vector2): f32
|
|
||||||
export extern "Vector2Scale" func Vector2Scale(v: Vector2, scale: f32): Vector2
|
|
||||||
export extern "Vector2Multiply" func Vector2Multiply(v1: Vector2, v2: Vector2): Vector2
|
|
||||||
export extern "Vector2Negate" func Vector2Negate(v: Vector2): Vector2
|
|
||||||
export extern "Vector2Divide" func Vector2Divide(v1: Vector2, v2: Vector2): Vector2
|
|
||||||
export extern "Vector2Normalize" func Vector2Normalize(v: Vector2): Vector2
|
|
||||||
export extern "Vector2Transform" func Vector2Transform(v: Vector2, mat: Matrix): Vector2
|
|
||||||
export extern "Vector2Lerp" func Vector2Lerp(v1: Vector2, v2: Vector2, amount: f32): Vector2
|
|
||||||
export extern "Vector2Reflect" func Vector2Reflect(v: Vector2, normal: Vector2): Vector2
|
|
||||||
export extern "Vector2Min" func Vector2Min(v1: Vector2, v2: Vector2): Vector2
|
|
||||||
export extern "Vector2Max" func Vector2Max(v1: Vector2, v2: Vector2): Vector2
|
|
||||||
export extern "Vector2Rotate" func Vector2Rotate(v: Vector2, angle: f32): Vector2
|
|
||||||
export extern "Vector2MoveTowards" func Vector2MoveTowards(v: Vector2, target: Vector2, maxDistance: f32): Vector2
|
|
||||||
export extern "Vector2Invert" func Vector2Invert(v: Vector2): Vector2
|
|
||||||
export extern "Vector2Clamp" func Vector2Clamp(v: Vector2, min: Vector2, max: Vector2): Vector2
|
|
||||||
export extern "Vector2ClampValue" func Vector2ClampValue(v: Vector2, min: f32, max: f32): Vector2
|
|
||||||
export extern "Vector2Equals" func Vector2Equals(p: Vector2, q: Vector2): i32
|
|
||||||
export extern "Vector2Refract" func Vector2Refract(v: Vector2, n: Vector2, r: f32): Vector2
|
|
||||||
export extern "Vector3Zero" func Vector3Zero(): Vector3
|
|
||||||
export extern "Vector3One" func Vector3One(): Vector3
|
|
||||||
export extern "Vector3Add" func Vector3Add(v1: Vector3, v2: Vector3): Vector3
|
|
||||||
export extern "Vector3AddValue" func Vector3AddValue(v: Vector3, add: f32): Vector3
|
|
||||||
export extern "Vector3Subtract" func Vector3Subtract(v1: Vector3, v2: Vector3): Vector3
|
|
||||||
export extern "Vector3SubtractValue" func Vector3SubtractValue(v: Vector3, sub: f32): Vector3
|
|
||||||
export extern "Vector3Scale" func Vector3Scale(v: Vector3, scalar: f32): Vector3
|
|
||||||
export extern "Vector3Multiply" func Vector3Multiply(v1: Vector3, v2: Vector3): Vector3
|
|
||||||
export extern "Vector3CrossProduct" func Vector3CrossProduct(v1: Vector3, v2: Vector3): Vector3
|
|
||||||
export extern "Vector3Perpendicular" func Vector3Perpendicular(v: Vector3): Vector3
|
|
||||||
export extern "Vector3Length" func Vector3Length(v: Vector3): f32
|
|
||||||
export extern "Vector3LengthSqr" func Vector3LengthSqr(v: Vector3): f32
|
|
||||||
export extern "Vector3DotProduct" func Vector3DotProduct(v1: Vector3, v2: Vector3): f32
|
|
||||||
export extern "Vector3Distance" func Vector3Distance(v1: Vector3, v2: Vector3): f32
|
|
||||||
export extern "Vector3DistanceSqr" func Vector3DistanceSqr(v1: Vector3, v2: Vector3): f32
|
|
||||||
export extern "Vector3Angle" func Vector3Angle(v1: Vector3, v2: Vector3): f32
|
|
||||||
export extern "Vector3Negate" func Vector3Negate(v: Vector3): Vector3
|
|
||||||
export extern "Vector3Divide" func Vector3Divide(v1: Vector3, v2: Vector3): Vector3
|
|
||||||
export extern "Vector3Normalize" func Vector3Normalize(v: Vector3): Vector3
|
|
||||||
export extern "Vector3Project" func Vector3Project(v1: Vector3, v2: Vector3): Vector3
|
|
||||||
export extern "Vector3Reject" func Vector3Reject(v1: Vector3, v2: Vector3): Vector3
|
|
||||||
export extern "Vector3OrthoNormalize" func Vector3OrthoNormalize(v1: ^Vector3, v2: ^Vector3): void
|
|
||||||
export extern "Vector3Transform" func Vector3Transform(v: Vector3, mat: Matrix): Vector3
|
|
||||||
export extern "Vector3RotateByQuaternion" func Vector3RotateByQuaternion(v: Vector3, q: Vector4): Vector3
|
|
||||||
export extern "Vector3RotateByAxisAngle" func Vector3RotateByAxisAngle(v: Vector3, axis: Vector3, angle: f32): Vector3
|
|
||||||
export extern "Vector3MoveTowards" func Vector3MoveTowards(v: Vector3, target: Vector3, maxDistance: f32): Vector3
|
|
||||||
export extern "Vector3Lerp" func Vector3Lerp(v1: Vector3, v2: Vector3, amount: f32): Vector3
|
|
||||||
export extern "Vector3CubicHermite" func Vector3CubicHermite(v1: Vector3, tangent1: Vector3, v2: Vector3, tangent2: Vector3, amount: f32): Vector3
|
|
||||||
export extern "Vector3Reflect" func Vector3Reflect(v: Vector3, normal: Vector3): Vector3
|
|
||||||
export extern "Vector3Min" func Vector3Min(v1: Vector3, v2: Vector3): Vector3
|
|
||||||
export extern "Vector3Max" func Vector3Max(v1: Vector3, v2: Vector3): Vector3
|
|
||||||
export extern "Vector3Barycenter" func Vector3Barycenter(p: Vector3, a: Vector3, b: Vector3, c: Vector3): Vector3
|
|
||||||
export extern "Vector3Unproject" func Vector3Unproject(source: Vector3, projection: Matrix, view: Matrix): Vector3
|
|
||||||
export extern "Vector3ToFloatV" func Vector3ToFloatV(v: Vector3): float3
|
|
||||||
export extern "Vector3Invert" func Vector3Invert(v: Vector3): Vector3
|
|
||||||
export extern "Vector3Clamp" func Vector3Clamp(v: Vector3, min: Vector3, max: Vector3): Vector3
|
|
||||||
export extern "Vector3ClampValue" func Vector3ClampValue(v: Vector3, min: f32, max: f32): Vector3
|
|
||||||
export extern "Vector3Equals" func Vector3Equals(p: Vector3, q: Vector3): i32
|
|
||||||
export extern "Vector3Refract" func Vector3Refract(v: Vector3, n: Vector3, r: f32): Vector3
|
|
||||||
export extern "Vector4Zero" func Vector4Zero(): Vector4
|
|
||||||
export extern "Vector4One" func Vector4One(): Vector4
|
|
||||||
export extern "Vector4Add" func Vector4Add(v1: Vector4, v2: Vector4): Vector4
|
|
||||||
export extern "Vector4AddValue" func Vector4AddValue(v: Vector4, add: f32): Vector4
|
|
||||||
export extern "Vector4Subtract" func Vector4Subtract(v1: Vector4, v2: Vector4): Vector4
|
|
||||||
export extern "Vector4SubtractValue" func Vector4SubtractValue(v: Vector4, add: f32): Vector4
|
|
||||||
export extern "Vector4Length" func Vector4Length(v: Vector4): f32
|
|
||||||
export extern "Vector4LengthSqr" func Vector4LengthSqr(v: Vector4): f32
|
|
||||||
export extern "Vector4DotProduct" func Vector4DotProduct(v1: Vector4, v2: Vector4): f32
|
|
||||||
export extern "Vector4Distance" func Vector4Distance(v1: Vector4, v2: Vector4): f32
|
|
||||||
export extern "Vector4DistanceSqr" func Vector4DistanceSqr(v1: Vector4, v2: Vector4): f32
|
|
||||||
export extern "Vector4Scale" func Vector4Scale(v: Vector4, scale: f32): Vector4
|
|
||||||
export extern "Vector4Multiply" func Vector4Multiply(v1: Vector4, v2: Vector4): Vector4
|
|
||||||
export extern "Vector4Negate" func Vector4Negate(v: Vector4): Vector4
|
|
||||||
export extern "Vector4Divide" func Vector4Divide(v1: Vector4, v2: Vector4): Vector4
|
|
||||||
export extern "Vector4Normalize" func Vector4Normalize(v: Vector4): Vector4
|
|
||||||
export extern "Vector4Min" func Vector4Min(v1: Vector4, v2: Vector4): Vector4
|
|
||||||
export extern "Vector4Max" func Vector4Max(v1: Vector4, v2: Vector4): Vector4
|
|
||||||
export extern "Vector4Lerp" func Vector4Lerp(v1: Vector4, v2: Vector4, amount: f32): Vector4
|
|
||||||
export extern "Vector4MoveTowards" func Vector4MoveTowards(v: Vector4, target: Vector4, maxDistance: f32): Vector4
|
|
||||||
export extern "Vector4Invert" func Vector4Invert(v: Vector4): Vector4
|
|
||||||
export extern "Vector4Equals" func Vector4Equals(p: Vector4, q: Vector4): i32
|
|
||||||
export extern "MatrixDeterminant" func MatrixDeterminant(mat: Matrix): f32
|
|
||||||
export extern "MatrixTrace" func MatrixTrace(mat: Matrix): f32
|
|
||||||
export extern "MatrixTranspose" func MatrixTranspose(mat: Matrix): Matrix
|
|
||||||
export extern "MatrixInvert" func MatrixInvert(mat: Matrix): Matrix
|
|
||||||
export extern "MatrixIdentity" func MatrixIdentity(): Matrix
|
|
||||||
export extern "MatrixAdd" func MatrixAdd(left: Matrix, right: Matrix): Matrix
|
|
||||||
export extern "MatrixSubtract" func MatrixSubtract(left: Matrix, right: Matrix): Matrix
|
|
||||||
export extern "MatrixMultiply" func MatrixMultiply(left: Matrix, right: Matrix): Matrix
|
|
||||||
export extern "MatrixTranslate" func MatrixTranslate(x: f32, y: f32, z: f32): Matrix
|
|
||||||
export extern "MatrixRotate" func MatrixRotate(axis: Vector3, angle: f32): Matrix
|
|
||||||
export extern "MatrixRotateX" func MatrixRotateX(angle: f32): Matrix
|
|
||||||
export extern "MatrixRotateY" func MatrixRotateY(angle: f32): Matrix
|
|
||||||
export extern "MatrixRotateZ" func MatrixRotateZ(angle: f32): Matrix
|
|
||||||
export extern "MatrixRotateXYZ" func MatrixRotateXYZ(angle: Vector3): Matrix
|
|
||||||
export extern "MatrixRotateZYX" func MatrixRotateZYX(angle: Vector3): Matrix
|
|
||||||
export extern "MatrixScale" func MatrixScale(x: f32, y: f32, z: f32): Matrix
|
|
||||||
export extern "MatrixFrustum" func MatrixFrustum(left: f64, right: f64, bottom: f64, top: f64, nearPlane: f64, farPlane: f64): Matrix
|
|
||||||
export extern "MatrixPerspective" func MatrixPerspective(fovY: f64, aspect: f64, nearPlane: f64, farPlane: f64): Matrix
|
|
||||||
export extern "MatrixOrtho" func MatrixOrtho(left: f64, right: f64, bottom: f64, top: f64, nearPlane: f64, farPlane: f64): Matrix
|
|
||||||
export extern "MatrixLookAt" func MatrixLookAt(eye: Vector3, target: Vector3, up: Vector3): Matrix
|
|
||||||
export extern "MatrixToFloatV" func MatrixToFloatV(mat: Matrix): float16
|
|
||||||
export extern "QuaternionAdd" func QuaternionAdd(q1: Vector4, q2: Vector4): Vector4
|
|
||||||
export extern "QuaternionAddValue" func QuaternionAddValue(q: Vector4, add: f32): Vector4
|
|
||||||
export extern "QuaternionSubtract" func QuaternionSubtract(q1: Vector4, q2: Vector4): Vector4
|
|
||||||
export extern "QuaternionSubtractValue" func QuaternionSubtractValue(q: Vector4, sub: f32): Vector4
|
|
||||||
export extern "QuaternionIdentity" func QuaternionIdentity(): Vector4
|
|
||||||
export extern "QuaternionLength" func QuaternionLength(q: Vector4): f32
|
|
||||||
export extern "QuaternionNormalize" func QuaternionNormalize(q: Vector4): Vector4
|
|
||||||
export extern "QuaternionInvert" func QuaternionInvert(q: Vector4): Vector4
|
|
||||||
export extern "QuaternionMultiply" func QuaternionMultiply(q1: Vector4, q2: Vector4): Vector4
|
|
||||||
export extern "QuaternionScale" func QuaternionScale(q: Vector4, mul: f32): Vector4
|
|
||||||
export extern "QuaternionDivide" func QuaternionDivide(q1: Vector4, q2: Vector4): Vector4
|
|
||||||
export extern "QuaternionLerp" func QuaternionLerp(q1: Vector4, q2: Vector4, amount: f32): Vector4
|
|
||||||
export extern "QuaternionNlerp" func QuaternionNlerp(q1: Vector4, q2: Vector4, amount: f32): Vector4
|
|
||||||
export extern "QuaternionSlerp" func QuaternionSlerp(q1: Vector4, q2: Vector4, amount: f32): Vector4
|
|
||||||
export extern "QuaternionCubicHermiteSpline" func QuaternionCubicHermiteSpline(q1: Vector4, outTangent1: Vector4, q2: Vector4, inTangent2: Vector4, t: f32): Vector4
|
|
||||||
export extern "QuaternionFromVector3ToVector3" func QuaternionFromVector3ToVector3(from: Vector3, to: Vector3): Vector4
|
|
||||||
export extern "QuaternionFromMatrix" func QuaternionFromMatrix(mat: Matrix): Vector4
|
|
||||||
export extern "QuaternionToMatrix" func QuaternionToMatrix(q: Vector4): Matrix
|
|
||||||
export extern "QuaternionFromAxisAngle" func QuaternionFromAxisAngle(axis: Vector3, angle: f32): Vector4
|
|
||||||
export extern "QuaternionToAxisAngle" func QuaternionToAxisAngle(q: Vector4, outAxis: ^Vector3, outAngle: ^f32): void
|
|
||||||
export extern "QuaternionFromEuler" func QuaternionFromEuler(pitch: f32, yaw: f32, roll: f32): Vector4
|
|
||||||
export extern "QuaternionToEuler" func QuaternionToEuler(q: Vector4): Vector3
|
|
||||||
export extern "QuaternionTransform" func QuaternionTransform(q: Vector4, mat: Matrix): Vector4
|
|
||||||
export extern "QuaternionEquals" func QuaternionEquals(p: Vector4, q: Vector4): i32
|
|
||||||
export extern "MatrixDecompose" func MatrixDecompose(mat: Matrix, translation: ^Vector3, rotation: ^Vector4, scale: ^Vector3): void
|
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
module "rlgl"
|
|
||||||
|
|
||||||
export struct Matrix
|
|
||||||
{
|
|
||||||
m0: f32
|
|
||||||
m4: f32
|
|
||||||
m8: f32
|
|
||||||
m12: f32
|
|
||||||
m1: f32
|
|
||||||
m5: f32
|
|
||||||
m9: f32
|
|
||||||
m13: f32
|
|
||||||
m2: f32
|
|
||||||
m6: f32
|
|
||||||
m10: f32
|
|
||||||
m14: f32
|
|
||||||
m3: f32
|
|
||||||
m7: f32
|
|
||||||
m11: f32
|
|
||||||
m15: f32
|
|
||||||
}
|
|
||||||
export struct rlVertexBuffer
|
|
||||||
{
|
|
||||||
elementCount: i32
|
|
||||||
vertices: ^f32
|
|
||||||
texcoords: ^f32
|
|
||||||
normals: ^f32
|
|
||||||
colors: ^u8
|
|
||||||
indices: ^u32
|
|
||||||
vaoId: u32
|
|
||||||
vboId: [5]u32
|
|
||||||
}
|
|
||||||
export struct rlDrawCall
|
|
||||||
{
|
|
||||||
mode: i32
|
|
||||||
vertexCount: i32
|
|
||||||
vertexAlignment: i32
|
|
||||||
textureId: u32
|
|
||||||
}
|
|
||||||
export struct rlRenderBatch
|
|
||||||
{
|
|
||||||
bufferCount: i32
|
|
||||||
currentBuffer: i32
|
|
||||||
vertexBuffer: ^rlVertexBuffer
|
|
||||||
draws: ^rlDrawCall
|
|
||||||
drawCounter: i32
|
|
||||||
currentDepth: f32
|
|
||||||
}
|
|
||||||
export extern "rlMatrixMode" func rlMatrixMode(mode: i32): void
|
|
||||||
export extern "rlPushMatrix" func rlPushMatrix(): void
|
|
||||||
export extern "rlPopMatrix" func rlPopMatrix(): void
|
|
||||||
export extern "rlLoadIdentity" func rlLoadIdentity(): void
|
|
||||||
export extern "rlTranslatef" func rlTranslatef(x: f32, y: f32, z: f32): void
|
|
||||||
export extern "rlRotatef" func rlRotatef(angle: f32, x: f32, y: f32, z: f32): void
|
|
||||||
export extern "rlScalef" func rlScalef(x: f32, y: f32, z: f32): void
|
|
||||||
export extern "rlMultMatrixf" func rlMultMatrixf(matf: ^f32): void
|
|
||||||
export extern "rlFrustum" func rlFrustum(left: f64, right: f64, bottom: f64, top: f64, znear: f64, zfar: f64): void
|
|
||||||
export extern "rlOrtho" func rlOrtho(left: f64, right: f64, bottom: f64, top: f64, znear: f64, zfar: f64): void
|
|
||||||
export extern "rlViewport" func rlViewport(x: i32, y: i32, width: i32, height: i32): void
|
|
||||||
export extern "rlSetClipPlanes" func rlSetClipPlanes(nearPlane: f64, farPlane: f64): void
|
|
||||||
export extern "rlGetCullDistanceNear" func rlGetCullDistanceNear(): f64
|
|
||||||
export extern "rlGetCullDistanceFar" func rlGetCullDistanceFar(): f64
|
|
||||||
export extern "rlBegin" func rlBegin(mode: i32): void
|
|
||||||
export extern "rlEnd" func rlEnd(): void
|
|
||||||
export extern "rlVertex2i" func rlVertex2i(x: i32, y: i32): void
|
|
||||||
export extern "rlVertex2f" func rlVertex2f(x: f32, y: f32): void
|
|
||||||
export extern "rlVertex3f" func rlVertex3f(x: f32, y: f32, z: f32): void
|
|
||||||
export extern "rlTexCoord2f" func rlTexCoord2f(x: f32, y: f32): void
|
|
||||||
export extern "rlNormal3f" func rlNormal3f(x: f32, y: f32, z: f32): void
|
|
||||||
export extern "rlColor4ub" func rlColor4ub(r: u8, g: u8, b: u8, a: u8): void
|
|
||||||
export extern "rlColor3f" func rlColor3f(x: f32, y: f32, z: f32): void
|
|
||||||
export extern "rlColor4f" func rlColor4f(x: f32, y: f32, z: f32, w: f32): void
|
|
||||||
export extern "rlEnableVertexArray" func rlEnableVertexArray(vaoId: u32): bool
|
|
||||||
export extern "rlDisableVertexArray" func rlDisableVertexArray(): void
|
|
||||||
export extern "rlEnableVertexBuffer" func rlEnableVertexBuffer(id: u32): void
|
|
||||||
export extern "rlDisableVertexBuffer" func rlDisableVertexBuffer(): void
|
|
||||||
export extern "rlEnableVertexBufferElement" func rlEnableVertexBufferElement(id: u32): void
|
|
||||||
export extern "rlDisableVertexBufferElement" func rlDisableVertexBufferElement(): void
|
|
||||||
export extern "rlEnableVertexAttribute" func rlEnableVertexAttribute(index: u32): void
|
|
||||||
export extern "rlDisableVertexAttribute" func rlDisableVertexAttribute(index: u32): void
|
|
||||||
export extern "rlActiveTextureSlot" func rlActiveTextureSlot(slot: i32): void
|
|
||||||
export extern "rlEnableTexture" func rlEnableTexture(id: u32): void
|
|
||||||
export extern "rlDisableTexture" func rlDisableTexture(): void
|
|
||||||
export extern "rlEnableTextureCubemap" func rlEnableTextureCubemap(id: u32): void
|
|
||||||
export extern "rlDisableTextureCubemap" func rlDisableTextureCubemap(): void
|
|
||||||
export extern "rlTextureParameters" func rlTextureParameters(id: u32, param: i32, value: i32): void
|
|
||||||
export extern "rlCubemapParameters" func rlCubemapParameters(id: u32, param: i32, value: i32): void
|
|
||||||
export extern "rlEnableShader" func rlEnableShader(id: u32): void
|
|
||||||
export extern "rlDisableShader" func rlDisableShader(): void
|
|
||||||
export extern "rlEnableFramebuffer" func rlEnableFramebuffer(id: u32): void
|
|
||||||
export extern "rlDisableFramebuffer" func rlDisableFramebuffer(): void
|
|
||||||
export extern "rlGetActiveFramebuffer" func rlGetActiveFramebuffer(): u32
|
|
||||||
export extern "rlActiveDrawBuffers" func rlActiveDrawBuffers(count: i32): void
|
|
||||||
export extern "rlBlitFramebuffer" func rlBlitFramebuffer(srcX: i32, srcY: i32, srcWidth: i32, srcHeight: i32, dstX: i32, dstY: i32, dstWidth: i32, dstHeight: i32, bufferMask: i32): void
|
|
||||||
export extern "rlBindFramebuffer" func rlBindFramebuffer(target: u32, framebuffer: u32): void
|
|
||||||
export extern "rlEnableColorBlend" func rlEnableColorBlend(): void
|
|
||||||
export extern "rlDisableColorBlend" func rlDisableColorBlend(): void
|
|
||||||
export extern "rlEnableDepthTest" func rlEnableDepthTest(): void
|
|
||||||
export extern "rlDisableDepthTest" func rlDisableDepthTest(): void
|
|
||||||
export extern "rlEnableDepthMask" func rlEnableDepthMask(): void
|
|
||||||
export extern "rlDisableDepthMask" func rlDisableDepthMask(): void
|
|
||||||
export extern "rlEnableBackfaceCulling" func rlEnableBackfaceCulling(): void
|
|
||||||
export extern "rlDisableBackfaceCulling" func rlDisableBackfaceCulling(): void
|
|
||||||
export extern "rlColorMask" func rlColorMask(r: bool, g: bool, b: bool, a: bool): void
|
|
||||||
export extern "rlSetCullFace" func rlSetCullFace(mode: i32): void
|
|
||||||
export extern "rlEnableScissorTest" func rlEnableScissorTest(): void
|
|
||||||
export extern "rlDisableScissorTest" func rlDisableScissorTest(): void
|
|
||||||
export extern "rlScissor" func rlScissor(x: i32, y: i32, width: i32, height: i32): void
|
|
||||||
export extern "rlEnableWireMode" func rlEnableWireMode(): void
|
|
||||||
export extern "rlEnablePointMode" func rlEnablePointMode(): void
|
|
||||||
export extern "rlDisableWireMode" func rlDisableWireMode(): void
|
|
||||||
export extern "rlSetLineWidth" func rlSetLineWidth(width: f32): void
|
|
||||||
export extern "rlGetLineWidth" func rlGetLineWidth(): f32
|
|
||||||
export extern "rlEnableSmoothLines" func rlEnableSmoothLines(): void
|
|
||||||
export extern "rlDisableSmoothLines" func rlDisableSmoothLines(): void
|
|
||||||
export extern "rlEnableStereoRender" func rlEnableStereoRender(): void
|
|
||||||
export extern "rlDisableStereoRender" func rlDisableStereoRender(): void
|
|
||||||
export extern "rlIsStereoRenderEnabled" func rlIsStereoRenderEnabled(): bool
|
|
||||||
export extern "rlClearColor" func rlClearColor(r: u8, g: u8, b: u8, a: u8): void
|
|
||||||
export extern "rlClearScreenBuffers" func rlClearScreenBuffers(): void
|
|
||||||
export extern "rlCheckErrors" func rlCheckErrors(): void
|
|
||||||
export extern "rlSetBlendMode" func rlSetBlendMode(mode: i32): void
|
|
||||||
export extern "rlSetBlendFactors" func rlSetBlendFactors(glSrcFactor: i32, glDstFactor: i32, glEquation: i32): void
|
|
||||||
export extern "rlSetBlendFactorsSeparate" func rlSetBlendFactorsSeparate(glSrcRGB: i32, glDstRGB: i32, glSrcAlpha: i32, glDstAlpha: i32, glEqRGB: i32, glEqAlpha: i32): void
|
|
||||||
export extern "rlglInit" func rlglInit(width: i32, height: i32): void
|
|
||||||
export extern "rlglClose" func rlglClose(): void
|
|
||||||
export extern "rlLoadExtensions" func rlLoadExtensions(loader: ^void): void
|
|
||||||
export extern "rlGetVersion" func rlGetVersion(): i32
|
|
||||||
export extern "rlSetFramebufferWidth" func rlSetFramebufferWidth(width: i32): void
|
|
||||||
export extern "rlGetFramebufferWidth" func rlGetFramebufferWidth(): i32
|
|
||||||
export extern "rlSetFramebufferHeight" func rlSetFramebufferHeight(height: i32): void
|
|
||||||
export extern "rlGetFramebufferHeight" func rlGetFramebufferHeight(): i32
|
|
||||||
export extern "rlGetTextureIdDefault" func rlGetTextureIdDefault(): u32
|
|
||||||
export extern "rlGetShaderIdDefault" func rlGetShaderIdDefault(): u32
|
|
||||||
export extern "rlGetShaderLocsDefault" func rlGetShaderLocsDefault(): ^i32
|
|
||||||
export extern "rlLoadRenderBatch" func rlLoadRenderBatch(numBuffers: i32, bufferElements: i32): rlRenderBatch
|
|
||||||
export extern "rlUnloadRenderBatch" func rlUnloadRenderBatch(batch: rlRenderBatch): void
|
|
||||||
export extern "rlDrawRenderBatch" func rlDrawRenderBatch(batch: ^rlRenderBatch): void
|
|
||||||
export extern "rlSetRenderBatchActive" func rlSetRenderBatchActive(batch: ^rlRenderBatch): void
|
|
||||||
export extern "rlDrawRenderBatchActive" func rlDrawRenderBatchActive(): void
|
|
||||||
export extern "rlCheckRenderBatchLimit" func rlCheckRenderBatchLimit(vCount: i32): bool
|
|
||||||
export extern "rlSetTexture" func rlSetTexture(id: u32): void
|
|
||||||
export extern "rlLoadVertexArray" func rlLoadVertexArray(): u32
|
|
||||||
export extern "rlLoadVertexBuffer" func rlLoadVertexBuffer(buffer: ^void, size: i32, dynamic: bool): u32
|
|
||||||
export extern "rlLoadVertexBufferElement" func rlLoadVertexBufferElement(buffer: ^void, size: i32, dynamic: bool): u32
|
|
||||||
export extern "rlUpdateVertexBuffer" func rlUpdateVertexBuffer(bufferId: u32, data: ^void, dataSize: i32, offset: i32): void
|
|
||||||
export extern "rlUpdateVertexBufferElements" func rlUpdateVertexBufferElements(id: u32, data: ^void, dataSize: i32, offset: i32): void
|
|
||||||
export extern "rlUnloadVertexArray" func rlUnloadVertexArray(vaoId: u32): void
|
|
||||||
export extern "rlUnloadVertexBuffer" func rlUnloadVertexBuffer(vboId: u32): void
|
|
||||||
export extern "rlSetVertexAttribute" func rlSetVertexAttribute(index: u32, compSize: i32, type: i32, normalized: bool, stride: i32, offset: i32): void
|
|
||||||
export extern "rlSetVertexAttributeDivisor" func rlSetVertexAttributeDivisor(index: u32, divisor: i32): void
|
|
||||||
export extern "rlSetVertexAttributeDefault" func rlSetVertexAttributeDefault(locIndex: i32, value: ^void, attribType: i32, count: i32): void
|
|
||||||
export extern "rlDrawVertexArray" func rlDrawVertexArray(offset: i32, count: i32): void
|
|
||||||
export extern "rlDrawVertexArrayElements" func rlDrawVertexArrayElements(offset: i32, count: i32, buffer: ^void): void
|
|
||||||
export extern "rlDrawVertexArrayInstanced" func rlDrawVertexArrayInstanced(offset: i32, count: i32, instances: i32): void
|
|
||||||
export extern "rlDrawVertexArrayElementsInstanced" func rlDrawVertexArrayElementsInstanced(offset: i32, count: i32, buffer: ^void, instances: i32): void
|
|
||||||
export extern "rlLoadTexture" func rlLoadTexture(data: ^void, width: i32, height: i32, format: i32, mipmapCount: i32): u32
|
|
||||||
export extern "rlLoadTextureDepth" func rlLoadTextureDepth(width: i32, height: i32, useRenderBuffer: bool): u32
|
|
||||||
export extern "rlLoadTextureCubemap" func rlLoadTextureCubemap(data: ^void, size: i32, format: i32, mipmapCount: i32): u32
|
|
||||||
export extern "rlUpdateTexture" func rlUpdateTexture(id: u32, offsetX: i32, offsetY: i32, width: i32, height: i32, format: i32, data: ^void): void
|
|
||||||
export extern "rlGetGlTextureFormats" func rlGetGlTextureFormats(format: i32, glInternalFormat: ^u32, glFormat: ^u32, glType: ^u32): void
|
|
||||||
export extern "rlGetPixelFormatName" func rlGetPixelFormatName(format: u32): cstring
|
|
||||||
export extern "rlUnloadTexture" func rlUnloadTexture(id: u32): void
|
|
||||||
export extern "rlGenTextureMipmaps" func rlGenTextureMipmaps(id: u32, width: i32, height: i32, format: i32, mipmaps: ^i32): void
|
|
||||||
export extern "rlReadTexturePixels" func rlReadTexturePixels(id: u32, width: i32, height: i32, format: i32): ^void
|
|
||||||
export extern "rlReadScreenPixels" func rlReadScreenPixels(width: i32, height: i32): ^u8
|
|
||||||
export extern "rlLoadFramebuffer" func rlLoadFramebuffer(): u32
|
|
||||||
export extern "rlFramebufferAttach" func rlFramebufferAttach(fboId: u32, texId: u32, attachType: i32, texType: i32, mipLevel: i32): void
|
|
||||||
export extern "rlFramebufferComplete" func rlFramebufferComplete(id: u32): bool
|
|
||||||
export extern "rlUnloadFramebuffer" func rlUnloadFramebuffer(id: u32): void
|
|
||||||
export extern "rlLoadShaderCode" func rlLoadShaderCode(vsCode: cstring, fsCode: cstring): u32
|
|
||||||
export extern "rlCompileShader" func rlCompileShader(shaderCode: cstring, type: i32): u32
|
|
||||||
export extern "rlLoadShaderProgram" func rlLoadShaderProgram(vShaderId: u32, fShaderId: u32): u32
|
|
||||||
export extern "rlUnloadShaderProgram" func rlUnloadShaderProgram(id: u32): void
|
|
||||||
export extern "rlGetLocationUniform" func rlGetLocationUniform(shaderId: u32, uniformName: cstring): i32
|
|
||||||
export extern "rlGetLocationAttrib" func rlGetLocationAttrib(shaderId: u32, attribName: cstring): i32
|
|
||||||
export extern "rlSetUniform" func rlSetUniform(locIndex: i32, value: ^void, uniformType: i32, count: i32): void
|
|
||||||
export extern "rlSetUniformMatrix" func rlSetUniformMatrix(locIndex: i32, mat: Matrix): void
|
|
||||||
export extern "rlSetUniformMatrices" func rlSetUniformMatrices(locIndex: i32, mat: ^Matrix, count: i32): void
|
|
||||||
export extern "rlSetUniformSampler" func rlSetUniformSampler(locIndex: i32, textureId: u32): void
|
|
||||||
export extern "rlSetShader" func rlSetShader(id: u32, locs: ^i32): void
|
|
||||||
export extern "rlLoadComputeShaderProgram" func rlLoadComputeShaderProgram(shaderId: u32): u32
|
|
||||||
export extern "rlComputeShaderDispatch" func rlComputeShaderDispatch(groupX: u32, groupY: u32, groupZ: u32): void
|
|
||||||
export extern "rlLoadShaderBuffer" func rlLoadShaderBuffer(size: u32, data: ^void, usageHint: i32): u32
|
|
||||||
export extern "rlUnloadShaderBuffer" func rlUnloadShaderBuffer(ssboId: u32): void
|
|
||||||
export extern "rlUpdateShaderBuffer" func rlUpdateShaderBuffer(id: u32, data: ^void, dataSize: u32, offset: u32): void
|
|
||||||
export extern "rlBindShaderBuffer" func rlBindShaderBuffer(id: u32, index: u32): void
|
|
||||||
export extern "rlReadShaderBuffer" func rlReadShaderBuffer(id: u32, dest: ^void, count: u32, offset: u32): void
|
|
||||||
export extern "rlCopyShaderBuffer" func rlCopyShaderBuffer(destId: u32, srcId: u32, destOffset: u32, srcOffset: u32, count: u32): void
|
|
||||||
export extern "rlGetShaderBufferSize" func rlGetShaderBufferSize(id: u32): u32
|
|
||||||
export extern "rlBindImageTexture" func rlBindImageTexture(id: u32, index: u32, format: i32, readonly: bool): void
|
|
||||||
export extern "rlGetMatrixModelview" func rlGetMatrixModelview(): Matrix
|
|
||||||
export extern "rlGetMatrixProjection" func rlGetMatrixProjection(): Matrix
|
|
||||||
export extern "rlGetMatrixTransform" func rlGetMatrixTransform(): Matrix
|
|
||||||
export extern "rlGetMatrixProjectionStereo" func rlGetMatrixProjectionStereo(eye: i32): Matrix
|
|
||||||
export extern "rlGetMatrixViewOffsetStereo" func rlGetMatrixViewOffsetStereo(eye: i32): Matrix
|
|
||||||
export extern "rlSetMatrixProjection" func rlSetMatrixProjection(proj: Matrix): void
|
|
||||||
export extern "rlSetMatrixModelview" func rlSetMatrixModelview(view: Matrix): void
|
|
||||||
export extern "rlSetMatrixProjectionStereo" func rlSetMatrixProjectionStereo(right: Matrix, left: Matrix): void
|
|
||||||
export extern "rlSetMatrixViewOffsetStereo" func rlSetMatrixViewOffsetStereo(right: Matrix, left: Matrix): void
|
|
||||||
export extern "rlLoadDrawCube" func rlLoadDrawCube(): void
|
|
||||||
export extern "rlLoadDrawQuad" func rlLoadDrawQuad(): void
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
import "raylib"
|
module main
|
||||||
|
|
||||||
module "main"
|
extern "main" func main(argc: i64, argv: [?]^i8): i64
|
||||||
|
|
||||||
extern "main" func main(argc: i64, argv: [?]cstring): i64
|
|
||||||
{
|
{
|
||||||
raylib::SetConfigFlags(raylib::ConfigFlags.FLAG_VSYNC_HINT | raylib::ConfigFlags.FLAG_WINDOW_RESIZABLE)
|
raylib::SetConfigFlags(raylib::ConfigFlags.FLAG_VSYNC_HINT | raylib::ConfigFlags.FLAG_WINDOW_RESIZABLE)
|
||||||
|
|
||||||
|
|||||||
1
runtime/.gitignore
vendored
Normal file
1
runtime/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.build
|
||||||
6
runtime/build.sh
Executable file
6
runtime/build.sh
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
mkdir -p .build
|
||||||
|
clang -c runtime.c -o .build/runtime.o
|
||||||
49
runtime/ref.c
Normal file
49
runtime/ref.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include "ref.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void *rc_alloc(size_t size, void (*destructor)(void *self))
|
||||||
|
{
|
||||||
|
printf("rc_alloc %zu bytes\n", size);
|
||||||
|
ref_header *header = malloc(sizeof(ref_header) + size);
|
||||||
|
memset(header, 0, size);
|
||||||
|
if (!header)
|
||||||
|
{
|
||||||
|
exit(69);
|
||||||
|
}
|
||||||
|
|
||||||
|
header->ref_count = 1;
|
||||||
|
header->destructor = destructor;
|
||||||
|
|
||||||
|
return (void *)(header + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rc_retain(void *obj)
|
||||||
|
{
|
||||||
|
if (!obj)
|
||||||
|
return;
|
||||||
|
|
||||||
|
printf("rc_retain\n");
|
||||||
|
ref_header *header = ((ref_header *)obj) - 1;
|
||||||
|
header->ref_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rc_release(void *obj)
|
||||||
|
{
|
||||||
|
if (!obj)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ref_header *header = ((ref_header *)obj) - 1;
|
||||||
|
printf("rc_release\n");
|
||||||
|
if (--header->ref_count == 0)
|
||||||
|
{
|
||||||
|
if (header->destructor)
|
||||||
|
{
|
||||||
|
header->destructor(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(header);
|
||||||
|
printf("rc_free\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
13
runtime/ref.h
Normal file
13
runtime/ref.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int ref_count;
|
||||||
|
void (*destructor)(void *self);
|
||||||
|
} ref_header;
|
||||||
|
|
||||||
|
void *rc_alloc(size_t size, void (*destructor)(void *self));
|
||||||
|
void rc_retain(void *obj);
|
||||||
|
void rc_release(void *obj);
|
||||||
1
runtime/runtime.c
Normal file
1
runtime/runtime.c
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include "ref.c"
|
||||||
@@ -31,6 +31,12 @@
|
|||||||
"configuration": "./language-configuration.json"
|
"configuration": "./language-configuration.json"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"commands": [
|
||||||
|
{
|
||||||
|
"command": "nub.setRootPath",
|
||||||
|
"title": "Set root path"
|
||||||
|
}
|
||||||
|
],
|
||||||
"grammars": [
|
"grammars": [
|
||||||
{
|
{
|
||||||
"language": "nub",
|
"language": "nub",
|
||||||
|
|||||||
@@ -32,7 +32,19 @@ export async function activate(context: vscode.ExtensionContext) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
vscode.commands.registerCommand('nub.setRootPath', setRootPath);
|
||||||
|
|
||||||
client.start();
|
client.start();
|
||||||
|
|
||||||
|
const choice = await vscode.window.showInformationMessage(
|
||||||
|
'Do you want to set the root directory for the project',
|
||||||
|
'Yes',
|
||||||
|
'No'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (choice === 'Yes') {
|
||||||
|
await setRootPath();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deactivate(): Thenable<void> | undefined {
|
export function deactivate(): Thenable<void> | undefined {
|
||||||
@@ -42,3 +54,25 @@ export function deactivate(): Thenable<void> | undefined {
|
|||||||
|
|
||||||
return client.stop();
|
return client.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function setRootPath() {
|
||||||
|
if (!client) return;
|
||||||
|
|
||||||
|
const folder = await vscode.window.showOpenDialog({
|
||||||
|
canSelectFolders: true,
|
||||||
|
canSelectFiles: false,
|
||||||
|
canSelectMany: false,
|
||||||
|
openLabel: 'Select root location'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (folder && folder.length > 0) {
|
||||||
|
const newRoot = folder[0].fsPath;
|
||||||
|
|
||||||
|
await client.sendRequest('workspace/executeCommand', {
|
||||||
|
command: 'nub.setRootPath',
|
||||||
|
arguments: [newRoot]
|
||||||
|
});
|
||||||
|
|
||||||
|
vscode.window.showInformationMessage(`Root path set to: ${newRoot}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user